Running CLang as a preprocessor

Is it possible to use libclang to preprocess source code, i.e. skip the actual parsing ?

I have tried clang_parseTranslationUnit(), passing "-E" in the flags argument, but that didn't have the desired effect. (It gave me a "warning: argument unused during compilation: '-fsyntax-only'" warning and then did the parse anyhow.)

Is there another libclang function I should use instead ? (I'm interested in all the normal preprocessor information, such as header inclusions, macro definitions, and macro expansions.


There is no way to invoke (only) the preprocessor through the libclang interface, although you can get at the artifacts of preprocessing (inclusions, macro definitions, macro instantiations, etc.) through the cursor interface.

You can use clang_tokenize() to re-lex parts of the buffer.

  - Doug


For context: With Synopsis I'm providing preprocessing as a separate step. That is useful as I'm not only targeting C and C++, but also e.g. IDL, which requires a preprocessor. Also, in some cases I want to get the preprocessor info (such as header inclusion graph), even though the actual C++ code can't be parsed without errors.

Can CLang be used to provide that info even if the parse step would yield compilation errors ? If not, would that be a useful feature to add to the wishlist ?