How to pass '#define's and '#undef's to a clang::Preprocessor object?

I’d like to handle command line -D/-U options, so that I can use my RecursiveASTVisitor code in place of my compiler command line (having done the -I parts of this based on the https://github.com/loarabia/Clang-tutorial/blob/master/tutorial6.cpp sample).

How are a preprocessor defines inserted (or removed) from a clang::Preprocessor object?

How are a preprocessor defines inserted (or removed) from a clang::Preprocessor object?

Answering my own question, by hunting down the compiler driver handling of these:

tools/clang/lib/Frontend/InitPreprocessor.cpp:

void clang::InitializePreprocessor(Preprocessor &PP,

) {
const LangOptions &LangOpts = PP.getLangOpts();
std::string PredefineBuffer;
PredefineBuffer.reserve(4080);
llvm::raw_string_ostream Predefines(PredefineBuffer);
MacroBuilder Builder(Predefines);

// Process #define’s and #undef’s in the order they are given.
for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
if (InitOpts.Macros[i].second) // isUndef
Builder.undefineMacro(InitOpts.Macros[i].first);
else
DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
PP.getDiagnostics());
}

// If -imacros are specified, include them now. These are processed before
// any -include directives.
for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i],
PP.getFileManager());


// Exit the command line and go back to (2 is LC_LEAVE).
if (!PP.getLangOpts().AsmPreprocessor)
Builder.append("# 1 “” 2");

// Copy PredefinedBuffer into the Preprocessor.
PP.setPredefines(Predefines.str());

}