Extracting preprocessor symbols from source

為{幸葍}努か 提交于 2021-02-06 15:23:02

问题


I'm looking for a way to extract all preprocessor symbols used in my code.
As an example, if my code looks like this:

#ifdef FOO
#endif

#if ( BAR == 1 && \
      defined (Z) )
#endif

I'd like to get the list [FOO,BAR,Z] as the output.

I found some posts suggesting gcc -E -dM, but this displays all symbols that the preprocessor would apply to the code.
What I want, in contrast, is a list of all symbols actually used in the code.

Any suggestions?


回答1:


That's quite simple. You have just to parse the source code exactly the way a conformant pre-processor would, and with the correct C or C++ version support. Ok, I'm joking, if you support only the later version, your code is likely to produce correct results on older versions - but even this should be thoroughly controlled.

More seriously now. As you can ask the pre-processor to give you the list of all defined symbols, you can simply tokenize the source, and identify all tokens from that list that are not immediately following an initial #define or #undef. This part should be reasonably feasable with lex+yacc.

The only alternative I can imagine would be to use the code of a real compiler (Clang should be easier than gcc but unsure) discard all code generation and consistently store every macro usage.

TL/DR: however you take it, it will be a hard work: if you can do without, keep away from that...




回答2:


You can get half way there by using a preprocessor library such as Boost.Wave. It can act as a lexer so you wouldn't have to write that part yourself. You would have to supply a grammar for the bit you cared about (define, ifdef, ifndef, if, elif) though.



来源:https://stackoverflow.com/questions/36180427/extracting-preprocessor-symbols-from-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!