Search code examples
c++cmacrosclanglibclang

set -D macro compiler flag globally for libclang


I am trying to generate mutants for a code (with an AST based mutant generator) which in order to compile uses -D macros. I am able to use clang to generate an AST dump using this command

clang -Xclang -ast-dump -fsyntax-only -DFLAG=0 -DOTHER_FLAG file.c

But, the mutant generator tool (Milu) uses libclang to parse the c code and generate the AST. Without these macros, libclang is not able to parse the code properly, and it's not possible to add the #defines in the code. I want to set these -D macros globally.

For example, I can set an environment variable C_INCLUDE_PATH which is read by libclang. I am wondering if I can do something similar for -D macros.


Solution

  • Just pass them when creating your CXTranslationUnit. The relevant function clang_createTranslationUnit takes arguments similar to argv and argc.