1

I would like to add a single line of code at the beginning of each function in my c++ visual studio 2010 project.

It would take months to manually add a line into each function. Are there any quick way or tool to solve this problem?

Edit: I would like to add a checkpoint for debugging purposes in every function in my project. I have a macro to handle adding checkpoints so the problem is now adding one single line of code. it could be anything, a macro, a console output, etc.

For example, have hundreds of functions:

void func1() 
{
    //code
}

int func2() 
{
    //code
}

char* func3() 
{
    //code
}

/* more functions */

bool func100()
{
    //code
}


//I want them to become:

void func1() 
{
    myMacro;
    //code
}

int func2() 
{
    myMacro;
    //code
}

char* func3() 
{
    myMacro;
    //code
}

/* more functions */

bool func100() 
{
    myMacro;
    //code
}
  • 2
    Can you show us an example of what the functions look like, and what the line you want to add is? This could be helpful in choosing the best solution. – Gray Oct 04 '13 at 13:28
  • 3
    This screams X-Y problem - what is it you actually want to do? – The Forest And The Trees Oct 04 '13 at 13:35
  • 3
    For those who don't know what the "X-Y problem" is: http://www.perlmonks.org/?node_id=542341 – Gray Oct 04 '13 at 13:39
  • You could always write a program to parse the .map file to get the names of all the functions and let it update the source code. – Jim Rhodes Oct 04 '13 at 13:47
  • Thank you for all your quick responses. I have added more description in my question above. – user2846700 Oct 04 '13 at 13:51
  • 1
    You can use the /Gh compile option to ask the compiler to inject a call to _penter() at function entry. /GH is available to do the same at the function exit, it calls _pexit(). It is up to you to write these functions, you cannot change their names. The intention of these options are tooling, like a profiler. – Hans Passant Oct 04 '13 at 14:34

2 Answers2

4

You don't need to hack up your code to get function instrumentation! See here for example: http://www.drdobbs.com/automatic-code-instrumentation/184403601

The short story is that MSVC has _penter, a facility for doing pretty much what you're trying to accomplish here, but without modifying most of the source code.

As an aside, a standard term for what you asked about (adding code before function calls) is Aspect Oriented Programming.

John Zwinck
  • 207,363
  • 31
  • 261
  • 371
  • Thanks a lot. I tried and it worked. However that method does not support 64 bit applications. Do you know how to solve that problem? – user2846700 Oct 07 '13 at 12:34
  • This page: http://blogs.msdn.com/b/joshpoley/archive/2008/03/12/poor-man-s-profiler.aspx says "Here is the basic outline of what needs to happen to pull this together. Also note that the discussion here assumes a 32bit Intel architecture. If you are on a 64bit or a non-Intel compatible platform, you will have to forgo the inline assembly, and supply the _penter/_pexit functions in a stand-alone assembly module." I suppose that means you need to implement your hooks separately somehow, since there's no inline assembly for 64-bit targets using MSVC. How precisely to do that, I'm not certain. – John Zwinck Oct 07 '13 at 12:50
0

MSVC supports keyboard macro recording (for c++ keyboard layout it is Ctrl+Shift+R - to start and Ctrl+Shift+P to stop). Define regular expression to find signature of function after that store keyboard macro something like following sequence:

  • F3 (for next function entry)

  • key down - to seek '{'

  • Ctrl + '}' to seek closing bracket

    .... add extra line there ....

When keyboard-macro ready press Ctrl+R - to play this macro. The thousands of line handled very quickly

Community
  • 1
  • 1
Dewfy
  • 21,895
  • 12
  • 66
  • 114
  • Thanks for your help. This also worked. Are there anyway to make the changes to all of the files in project, or the keyboard macro only applies to the current open document? – user2846700 Oct 07 '13 at 14:35
  • @user2846700 yes, 'play macro' runs in the current open document, but switching to another document allows you play it again. There you have 2 options: (1) Recorded macro is available for editing by "Tools\Macros\Macros IDE" - so you can take it as a base and manually extend to run across multiple documents. (2) Pressing Ctrl+Shift+F opens "find in file" and result are placed to "Output" panel, then instead my proposed 'F3' use 'F8' - that seeks you to the next found result – Dewfy Oct 07 '13 at 15:10
  • thanks! One last question, what is the correct regular expression for a function call? I've tried various combination but they always include "if" and "for" statement as well. Also, apparently there is no shortcut key to traverse to next function for c++ in visual studio 10 – user2846700 Oct 07 '13 at 17:23
  • try use lookahead that excludes keywords `((?!if)|(?!for)|(?!while).)` - see this (http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word) for dteails. *shortcut that traverse* is not exists, instead I've proposed use 'F8' - that seeks to the next issue of output result or cross-document search results – Dewfy Oct 08 '13 at 15:08