I have a LibTooling based utility and I would like to output a list of macro definitions for debug purposes. One can print compiler macro definitions with clang/gcc -dM -E -
, but it does not seem to work if I pass -dM -E
or -dD
to ClangTool. Is it possible to do so with LibTooling API or CLI options in any way? It does not matter if it will include macros defined in a parsed source code or not.
I've looked at other similar questions, and as far as I can tell they all are about macros expanded in a parsed source code. That's not exactly what I need.
The answer is blindingly obvious—in retrospect. clang::Preprocessor::getPredefines()
returns just that—a list of compiler predefines. The preprocessor object can be obtained e.g. from clang::CompilerInstance
, as an argument in clang::DiagnosticConsumer::BeginSourceFile()
, etc.
Just for the sake of completeness, this functionality is not available via libclang API, but I could do that using the fact that all predefines are present in the beginning of a translation unit. So after parsing I just listed all top-level cursors of CursorKind.MACRO_DEFINITION
that are not in any real location (location.file is None
using python bindings notation)