Clang AST dump doesn't show #defines

2019-07-24 13:08发布

问题:

I'm dumping the AST of some headers like this:

clang -cc1 -ast-dump -fblocks header.h

However, any #defines on the header are not showing on the dump. Is there a way of adding them?

回答1:

It's true, #defines are handled by the preprocessor, not the compiler. So you need a preprocessor parser stage. I know of two:

  1. Boost Wave can preprocess the input for you, and/or give you hooks to trigger on macro definitions or uses.

  2. The Clang tool pp-trace uses a Clang library that can do callbacks on many preprocessor events, including macro definitions.