Evaluate all macros in a C++ header file

后端 未结 3 1738
陌清茗
陌清茗 2021-01-23 11:36

I have a requirement to build an automated system to parse a C++ .h file with a lot of #define statements in it and do something with the value that each #def

3条回答
  •  我在风中等你
    2021-01-23 12:05

    Here is a concept, based on assumptions from a clarification comment.

    • only one header
    • no includes
    • no dependency on the including code file
    • no dependency on previously included headers
    • no dependency on include order

    Main requirements otherwise:

    • do not risk influence on binary build process (being the part which makes the actual software product)
    • do not try to emulate the binary build compiler/parser

    How to:

    • make a copy
    • include it from a dedicated code file,
      which only contains "#include "copy.h";
      or directly preprocess the header
      (this just feels weirdly against my habits)
    • delete everything except preprocessor and pragmas, paying attention to line-continuation
    • replace all "#define"s by "HaPoDefine", except one (e.g. the first)
    • repeat
      • preprocess the including code file (most compiler have a switch to do this)
      • save the output
      • turn another "HaPoDefine" back into "#define"
    • until no "HaPoDefine" is left
    • harvest all macro expansions from the deltas of intermediate saves
    • discard everything which is not of relevance
    • since the final actual numerical value is most likely a result of the compiler (not the preprocessor), use a tool like bashs "expr" to calculate values for human-eye readability,
      be careful not to risk differences to binary build process
    • use some regex magic to achieve any desired format

提交回复
热议问题