How to programatically define a symbol?

I want to programatically define a symbol, e.g. FOO, such that the code “#if defined(FOO)” will evaluate to true during pre-processing.
How do I that?

I tried addMacroDef(“FOO”), and other methods, but to no avail.


#define FOO

in the code or


on the command line during the compilation (for gcc and clang at least)

– Matthieu

I’m not using the command line, but the lib through code. How do I achieve the desired effect programatically?

Found a way. For anyone interested:

MacroInfo* RegisterMacro(Preprocessor &PP, const char *Name){
// Get the identifier.
IdentifierInfo *Id = PP.getIdentifierInfo(Name);

// Mark it as being a macro that is builtin.
MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
PP.setMacroInfo(Id, MI);

return MI;

RegisterMacro(preprocessor, “FOO”);

If you need replacement strings, manipulate the returned MacroInfo as follows:

Token t;
auto& i = preprocessor.getIdentifierTable().get(“Replacement_String”, tok::TokenKind::identifier);