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 and replace, since the #define is parameterized. 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 its result (with correct parameters inserted. I hope you understand what I want to do.

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PeterK
  • 6,287
  • 5
  • 50
  • 86

3 Answers3

7

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
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 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 Jun 21 '10 at 07:50
  • See http://stackoverflow.com/questions/277258/c-c-source-file-after-preprocessing for how to do it with visual studio – nos Jun 21 '10 at 07:58
  • 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 Jun 21 '10 at 08:04
  • You can also use a diff program to compare the preprocessor output to the source file. Each #define expansion is quite obvious and makes editing easier. – doug Jun 14 '16 at 19:01
3

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
  • 173,980
  • 10
  • 155
  • 350
  • 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 Jun 21 '10 at 12:15
0

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

wash
  • 497
  • 4
  • 7