A Clang plugin which iterates over macro declarations


I know how to write a function pass in LLVM to iterate over function decls. I’d like to do something similiar using Clang for C/C++/ObjC macros (#define foo).

I haven’t been able to find anything in the docs which describes how to hook into Clang’s preprocessing phase. Did I just miss it? Are there any samples which demonstrate this?

Thank you in advance!

Hi, Reed.
You may try to use libclang[1] for this purpose.

[1] http://clang.llvm.org/doxygen/group__CINDEX.html

Have a look at PPCallbacks class, sorry I can’t be of more help, but searching the list for “preprocessor callbacks” might give you something.


and onwards (until about 1087 or so) is the code in DXR that handles preprocessor callbacks. There’s a bit of nastiness to account for trying to support multiple versions of Clang via preprocessor declarations, and I use a thunk class to take into account the fact that I’m trying to use the same class as both an ASTConsumer and a PPCallbacks, which causes issue with double deletes if I don’t use a thunk (it’s also useful for glossing over API differences). The thunk is defined here: and used here: .