Click here to Skip to main content
14,099,174 members
Rate this:
Please Sign up or sign in to vote.
See more:
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()

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

void foo()

Is it possible?

Thank you
Updated 16-Dec-11 1:38am
Pranit Kothari 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.
Pranit Kothari 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... :)
Rate this: bad
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:



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
Rate this: bad
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 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.
Rate this: bad
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 ;)
Rate this: bad
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).
Pranit Kothari 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190518.1 | Last Updated 17 Dec 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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