-4

I have a project with lots of files which I want to add a simple "printf" command to each function found in each file. Please share your ideas on how to detect a function programmatically.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Jack Mc Lauren
  • 275
  • 2
  • 4
  • 17

2 Answers2

2

While I hate to be that guy and immediately tell you that you're doing it wrong, and I have no idea what you actually want to accomplish with this (care to further explain?), you are going to get a lot more insight into your code using a debugger or profiler. If you want see all the function calls leading up to some event in your program (like a crash), try gdb. If you want stats on how often functions are being called, look into something like Valgrind or gprof. The following question goes on at length about profiling C++:

What can I use to profile C++ code in Linux?

But, if you're really determined to get a printf into every single function, I would use a third party tool that does C++ parsing. Trying to write regexes to parse C++ yourself is going to be an exercise in frustration.

CScope is probably the easiest "off the shelf" solution: http://cscope.sourceforge.net/ Note that it might have trouble with C++ code that is heavy on template programming.

If you want to invest some more serious time into this, developing a solution with the open-source clang compiler would allow you to make something really comprehensive. Check out http://clang.llvm.org/

example of something in the ballpark of what you're doing: Get list of methods in class using clang

Hope that helps...

Oversearch
  • 494
  • 5
  • 6
1

use __FUNCTION__ and __LINE__ macro. Hope this will be helpfull

#define PRINT_FUNC_NAME() do{printf("Entered the function %s in line no %d",__FUNCTION__,__LINE__)}while(0)

Sample program

#include<iostream>
using namespace std;
#define PRINT_FUNC_NAME() do {printf("Entered the function %s in line no %d\n",__FUNCTION__,__LINE__) } while(0)


void callme()
{
   PRINT_FUNC_NAME();
}
int main()
{
   PRINT_FUNC_NAME();
      callme();
   return 0;
}
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38
  • 1
    Never hide a `;` inside a macro. Have a `;` after each invocation, instead. This helps a lot making the code readable. Also for such a macro that clearly performs a function, it is much easier to read when defined as functional macro and called as `PRINT_FUNC_NAME()`. – Jens Gustedt Feb 26 '14 at 07:40
  • do{ printf("blahblah")l } while(0) is often a recommended method to use Macros. – Stolas Feb 26 '14 at 08:29
  • @JensGustedt Edited as per your suggestion. Thanks – VINOTH ENERGETIC Feb 26 '14 at 08:35
  • 1
    @Stolas I was not clear about your statment. Kindly clarify – VINOTH ENERGETIC Feb 26 '14 at 08:42
  • Well like Jens said don't hide a ; inside a macro. Something that I do often (and seen other people do a lot is: #define PRINT_FUNC_NAME() do{ printf("Entered the function %s in line no %d\n",__FUNCTION__,__LINE__); }while(0) As it has kinda best of both worlds and if you have a Typo in your Macro it won't spill over the rest of the code while debugging ;) (note I am not saying you must, I am saying you should/could) – Stolas Feb 26 '14 at 11:23
  • @Stolas Really I cannot understand why we have to use do{ printf("Entered the function %s in line no %d\n",FUNCTION,LINE); }while(0) . What makes this special from usual. Can you please some samples – VINOTH ENERGETIC Feb 26 '14 at 12:47
  • @vinothcse2000 http://stackoverflow.com/questions/154136/do-while-and-if-else-statements-in-c-c-macros I think this will explain it :) – Stolas Feb 26 '14 at 13:28
  • 1
    @Stolas Really helped me out. Note my edits – VINOTH ENERGETIC Feb 27 '14 at 04:43