Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi all, is there any automated way to insert macro into start of function body?

Let's say I have macro:

C++
#define WRITE_LINE printf("%s - %d\n", __FUNCTION__, __LINE__);


And a function:

C++
void foo()
{
  DoSomething();
}


What I want is somehow modify all functions in VisualStudio 2005 project to have it like follows:

C++
void foo()
{
  WRITE_LINE
  DoSomething();
}


Is it possible?

Thank you
Posted
Updated 16-Dec-11 1:38am
v2
Comments
[no name] 16-Dec-11 7:35am    
My 5!
Albert Holguin 16-Dec-11 9:44am    
Side note... if you did this to every function/method... you would have a HUGE number of lines printed.
[no name] 16-Dec-11 9:54am    
Albert, why I cannot vote 5 to comment! (;Disappointed;)
Albert Holguin 16-Dec-11 14:33pm    
Thanks, just pointing out the obvious... :)

Well what OS are you using? You cannot exactly enter a macro but you can use a special function that gets called when ever a function gets called.
In windows this function is called _penter.
Suppose you have a function called void foo();

At the call site it is foo();
With a special compiler setting you can turn the call to

C++
_penter();
foo();
_pexit();


_penter and _pexit are special functions that the VC++ compiler inserts, if you enable the setting. If you want you can do anything in this _penter(). But be aware of the performance impact.

A Simple Profiler using the Visual Studio C/C++ Compiler and DIA SDK[^]

This article describes it.

There is a similar functionality for GCC also.
 
Share this answer
 
v3
Don't think I've ever seen a way to do this within Studio, but it would probably be easy enough to accomplish using a script. You would have to parse out and identify the function/method definitions first then add your macro line. Most of the work, as you might be able to guess, would be in correctly identifying a new function/method (but it still wouldn't be too hard).
 
Share this answer
 
Comments
[no name] 16-Dec-11 9:59am    
Correct, if he doesn't need it at run-time this will work..
4! (-1 --> will not be a solution for runtime)
Albert Holguin 16-Dec-11 14:33pm    
Well, you can't add a macro at run-time... that doesn't make any sense.
While you can surely instrument your code as described in Solution 1, if your program already has so many functions that you don't want to do this manually, then it is definitely a bad idea to print out every call of every function like that - you'll be surprised to see just how many calls that will be, and the printing might significantly slow your program as well, since printf is a rather slow function.

For all practical purposes you will always have to manually enable or disable (comment out) these commands, or you will be swamped with so much text that it is simply not of any use for you. Doing this unchecked is on the same lines as trying to download the internet ;)
 
Share this answer
 
Hi (Dusan*2),

If you are developing with one of the more recent versions of Microsoft Visual Studio then you could use Regular Expressions[^]

A basic regular expression that would potentially match the function you provided in your code sample... might look something like:

Find:
:a[(]*[)]\n[{]


Replace:
\1\n{\n\tWRITE_LINE


The regular expression could probably be improved... my sample above would probably only match indent styles that put the bracket on the second line. The '\n' line break should probably be optional. Feel free to improve it.

Don't forget to backup your project before performing a large 'Find and Replace'. The Visual Studio editor is unable to track/undo some large multi-text replacements. You should get a warning prompt before attempting to update multiple files.

Best Wishes,
-David Delaune
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900