user2846700
user2846700

Reputation: 23

add a single line of code in each function in visual studio

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
}

Upvotes: 1

Views: 1950

Answers (2)

Dewfy
Dewfy

Reputation: 23644

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

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249502

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.

Upvotes: 4

Related Questions