简体   繁体   English

预处理器保留宏

[英]Preprocessor keeping macros

I want to preprocess C++ header files keeping all macros verbatim in the output text. 我想预处理C ++头文件,在输出文本中逐字保留所有宏。

For that, I need a C preprocessor-like program that performs these tasks: 为此,我需要一个类似C预处理器的程序来执行这些任务:

  • store in memory macros from #define directives; 存储在#define指令的内存宏中;
  • recursively follow #include directives; 递归地遵循#include指令;
  • evaluate conditions in #if and #ifdef directives; 评估#if#ifdef指令中的条件;
  • suppress the code in inactive portions of #if .. #else .. #endif blocks; 抑制#if .. #else .. #endif blocks的非活动部分中的代码;
  • (optionally) remove /* .. */ and // comments; (可选)删除/* .. *///注释;
  • remove all remaining directives lines. 删除所有剩余的指令行。

But the macros must not be replaced in the output. 但是不能在输出中替换宏。 Or alternatively, the preprocessor may take in argument a list of macro names that shall not be replaced. 或者,预处理器可以参数化一个不应被替换的宏名称列表。

This may sound weird, but I have a good reason for that. 这可能听起来很奇怪,但我有充分的理由。 I have a series of Perl scripts able to analyze preprocessed C++ class headers. 我有一系列Perl脚本能够分析预处理的C ++类头。 And I use some macros to tell them for example which methods to export. 我使用一些宏来告诉他们例如导出哪些方法。

I haven't found a preprocessor program able to perform what I need, so I wrote a Perl script. 我还没有找到能够执行我需要的预处理程序,所以我编写了一个Perl脚本。 The latter actually works, but is slow and non standard. 后者确实有效,但速度慢且不标准。 I am looking for a better alternative. 我正在寻找更好的选择。

Use gcc -E to run the preprocessor manually. 使用gcc -E手动运行预处理器。 This will expand all the macros but that's not a problem. 这将扩展所有宏,但这不是问题。

What you want is special macros for the time when you need the output for your Perl scripts. 当你需要Perl脚本的输出时,你想要的是特殊的宏。 Try this: 尝试这个:

#ifdef PERL_PREPROCESSING
# define EXPORT(...) PERL_EXPORT
#else
# define EXPORT(...) ...normal C code...
#endif

So the idea is that you call gcc -E -DPERL_PREPROCESSING to switch some of the macros to produce output that your perl scripts can locate. 所以我的想法是你调用gcc -E -DPERL_PREPROCESSING来切换一些宏来产生你的perl脚本可以找到的输出。 The macros will be expanded as usual. 宏将像往常一样扩展。

[EDIT] If you don't want to pollute your sources with Perl-specific macros, use this trick: Create a folder which contains the header file with the Perl versions of the macros and include this folder before every other folder with -I . [编辑]如果您不想使用Perl特定的宏污染您的源,请使用此技巧:创建一个文件夹,其中包含带有Perl版本宏的头文件,并在每个其他文件夹之前包含此文件夹-I gcc will then ignore the standard header file. 然后gcc将忽略标准头文件。

If you are using *nix you can use the grep command to find all the #defines in the directory 如果您使用的是* nix,则可以使用grep命令查找目录中的所有#defines

grep -R . '#define'

For the preprocessing required, use gcc -E. 对于所需的预处理,请使用gcc -E。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM