Reputation: 6317
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?
Upvotes: 3
Views: 1299
Reputation: 179819
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.
Upvotes: 3
Reputation: 881423
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.
Upvotes: 7
Reputation: 497
Uh I would advise you to use sed, http://www.gnu.org/software/sed/, or another regex tool.
Upvotes: 0