有没有办法“扩展” #define 指令?
我的项目中有很多“愚蠢的”#define
,我想删除它们。不幸的是,我无法进行简单的搜索和替换,因为 #define
已参数化。例如:
#define FHEADGRP( x ) bool _process_grp##x( grp_id_t , unsigned char )
这用于生成几个函数的标头。我想以某种方式做与预处理器相同的事情 - 用它的结果替换宏的每个调用(插入正确的参数。我希望你明白我想要做什么。
我发现使用 Visual Studio,可以得到不幸的是,这对我没有帮助,因为该文件被数千个其他行“污染”,并且所有 #defines 都已扩展。一些宏,最好在我的 IDE(即 Visual Studio)中执行,有什么办法可以实现这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
呃,我建议你使用 sed, http://www.gnu.org/software/sed/ ,或其他正则表达式工具。
Uh I would advise you to use sed, http://www.gnu.org/software/sed/, or another regex tool.
通常,您可以使用 gcc -E 获取预处理器的输出(当然假设您使用的是 gcc,尽管其他编译器往往具有相同的功能)。
当然,处理该文件以自动将
#define
扩展为其他文本并不是一项简单的任务。我可能会编写一个 shell 脚本(或 Perl,因为我认为它在处理文本方面要好得多)来自动执行任务。在 Visual Studio 中,您可以使用
/P
执行相同的操作。可以根据此页面< /a>.You can normally get the output of the preprocessor with
gcc -E
(assuming you're usinggcc
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.是的,有 - 因为您使用的是 Visual Studio。
Visual Studio IDE 具有强大的搜索和搜索功能。更换机制。您似乎认为它只能处理文字字符串。它可以做得更多。按 Ctrl-Shift-H 进行全局搜索和替换。在“查找选项”中,选择“使用:通配符”。
现在将
FHEADGRP(*)
替换为bool _process_grp\1( grp_id_t , unsigned char )
通配符为
*
和\1< /code> 是反向引用。
[编辑]
宏适用于标记化源,但搜索和替换适用于字符。这可能会导致一个小问题。考虑
FHEADGRP(Foo)
和FHEADGRP( Foo )
的情况。对于 C 宏,它们是等效的,但在第二种情况下,反向引用将扩展为Foo
- 带空格。解决方法是使用正则表达式,特别是将
FHEADGRP\(:b*(.*):b*\)
替换为bool _process_grp\0( grp_id_t , unsigned char )
。我发现VS2005的实现有点bug;例如,简单的?
表达式无法匹配单个空格。但上面的例子应该可以工作。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(*)
bybool _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)
andFHEADGRP( Foo )
. For a C macro, they're equivalent, but in the second case the backreference will expand toFoo
- with spaces.The workaround is to use regexes, in particular replace
FHEADGRP\(:b*(.*):b*\)
withbool _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.