Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
Hi all, is there any automated way to insert macro into start of function body?
 
Let's say I have macro:
 
#define WRITE_LINE printf("%s - %d\n", __FUNCTION__, __LINE__);
 
And a function:
 
void foo()
{
  DoSomething();
}
 
What I want is somehow modify all functions in VisualStudio 2005 project to have it like follows:
 
void foo()
{
  WRITE_LINE
  DoSomething();
}
 
Is it possible?
 
Thank you
Posted 16-Dec-11 2:21am
Edited 16-Dec-11 2:38am
v2
Comments
Pranit Kothari at 16-Dec-11 7:35am
   
My 5!
Albert Holguin at 16-Dec-11 9:44am
   
Side note... if you did this to every function/method... you would have a HUGE number of lines printed.
Pranit Kothari at 16-Dec-11 9:54am
   
Albert, why I cannot vote 5 to comment! (;Disappointed;)
Albert Holguin at 16-Dec-11 14:33pm
   
Thanks, just pointing out the obvious... :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
_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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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).
  Permalink  
Comments
Pranit Kothari at 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 at 16-Dec-11 14:33pm
   
Well, you can't add a macro at run-time... that doesn't make any sense.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 Wink | ;)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v3

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

  Print Answers RSS
0 DamithSL 320
1 OriginalGriff 235
2 Afzaal Ahmad Zeeshan 154
3 Sergey Alexandrovich Kryukov 147
4 BillWoodruff 135
0 OriginalGriff 7,510
1 DamithSL 5,494
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 17 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100