Using Clang to Expand Macros


How do I use clang to expand macros?

I’ve been able to find the CXCursor enums to do with it, but I’ve not been able to find anything more.

Ideally this would be some sort of operation on a Translation Unit.


You can expand macros in whole translation unit by calling clang -E on source file.

Ah, I’m sorry, I was thinking a bit too in-depth to give all the details I should have - I had meant from in C++ (so using ).

Thanks, Peter

For C++, I think you should look into clang::Preprocessor [1] and related classes.


Colin is using libclang. The question is whether there is an API exposed in libclang that allows you to get the expanded text for a macro instantiation.