views:

100

answers:

3

I have a lot of "stupid" #define in a project and i want to remove them. Unfortunately, i can't do a simple search&replace, since the #define is parametrized. For example:

#define FHEADGRP( x ) bool _process_grp##x( grp_id_t , unsigned char )

This is used to generate headers of a couple of functions. I would like to somehow do the same thing as the preprocessor does - replace each call of the macro by it's result (with correct parameters inserted. I hope you guys understand what i want to do.

EDIT: I found out that with Visual Studio, one can get the preprocessed intermediate files with the /P option. Unfortunately, this does not help me, since the file is "polluted" with thousands of other lines and with all #defines expanded. I do not want to do this, i just want to expand some of the macros and preferably do it in my IDE (which is Visual Studio). Is there any way how to achieve this?

+5  A: 

You can normally get the output of the preprocessor with gcc -E (assuming you're using gcc of course, though other compiler tend to have the same feature).

Of course, processing that file to automatically expand the #define's into other text is not a trivial task. I'd probably write a shell script (or Perl since it's a lot better at massaging text in my opinion) to automate the task.


In Visual Studio, you can use /P to perform the same operation. This can be set in the IDE according to this page.

paxdiablo
Thank you for your answer. Since i'm using visual studio (stated in the tags, but i forgot to mention that in the question) this does not help much. Do you know of an easy way how to do this with this IDE? Or i will have to go with Perl? (which is a nice idea but involves more coding...)
PeterK
See http://stackoverflow.com/questions/277258/c-c-source-file-after-preprocessing for how to do it with visual studio
nos
Already found that, but was looking for a cleaner way (expanding just one macro...). Anyway, thanks for the answers, i will probably go with perl ;)
PeterK
A: 

Uh I would advise you to use sed, http://www.gnu.org/software/sed/, or another regex tool.

wash
+2  A: 

Yes, there is - since you're using Visual Studio.

The Visual Studio IDE has a powerful search & replace mechanism. You seem to assume it can only handle literal strings. It can do more. Hit Ctrl-Shift-H for a global search and replace. In the "Find options", select "Use: Wildcards".

Now replace FHEADGRP(*) by bool _process_grp\1( grp_id_t , unsigned char )

The wildcard is *, and \1 is the backreference.

[edit] Macros work on the tokenized source, but Search&Replace works on characters. This can cause a slight problem. Consider the cases FHEADGRP(Foo) and FHEADGRP( Foo ). For a C macro, they're equivalent, but in the second case the backreference will expand to Foo - with spaces.

The workaround is to use regexes, in particular replace FHEADGRP\(:b*(.*):b*\) with bool _process_grp\0( grp_id_t , unsigned char ). I find that the VS2005 implementation is a bit buggy; for instance the simple ? expression fails to match a single space. But the example above should work.

MSalters
Thanks! I really didn't explore this functionality enough it seems. Thank you, this is much better than writing my own perl script for it!
PeterK