[cfe-dev] How to pass '#define's and '#undef's to a clang::Preprocessor object?

Peeter Joot peeter.joot at gmail.com
Wed Dec 5 09:24:08 PST 2012


> 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 <built-in> (2 is LC_LEAVE).
  if (!PP.getLangOpts().AsmPreprocessor)
    Builder.append("# 1 \"<built-in>\" 2");

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

-- 
Peeter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121205/77f04a44/attachment.html>


More information about the cfe-dev mailing list