How do I put Clang into C++ mode?
<p>I am working on an AST transformer using Clang. It is supposed to take a file-name as input, perform some transformations to the code in that file, and return the transformed code. It is based on <a href="https://github.com/eliben/llvm-clang-samples/blob/master/src_clang/rewritersample.cpp" rel="nofollow">a helpful example from Eli Bendersky</a>. </p>

<p>Here is the code: </p>

<pre><code>std::string transform(std::string fileName) {

  // CompilerInstance will hold the instance of the Clang compiler for us,
  // managing the various objects needed to run the compiler.
  CompilerInstance compilerInstance;

  compilerInstance.createDiagnostics();

  auto&amp; langOpts = compilerInstance.getLangOpts();
  langOpts.CPlusPlus = true;

  // Initialize target info with the default triple for our platform.
  auto TO = std::make_shared&lt;TargetOptions&gt;();
  TO-&gt;Triple = llvm::sys::getDefaultTargetTriple();
  TargetInfo* targetInfo =
      TargetInfo::CreateTargetInfo(compilerInstance.getDiagnostics(), TO);
  compilerInstance.setTarget(targetInfo);

  compilerInstance.createFileManager();
  auto&amp; fileManager = compilerInstance.getFileManager();

  compilerInstance.createSourceManager(fileManager);
  auto&amp; sourceManager = compilerInstance.getSourceManager();

  compilerInstance.createPreprocessor(TU_Module);
  compilerInstance.createASTContext();

  // A Rewriter helps us manage the code rewriting task.
  auto rewriter = clang::Rewriter(sourceManager, compilerInstance.getLangOpts());

  // Set the main file handled by the source manager to the input file.
  const FileEntry* inputFile = fileManager.getFile(fileName);
  sourceManager.setMainFileID(
      sourceManager.createFileID(inputFile, SourceLocation(), SrcMgr::C_User));
  compilerInstance.getDiagnosticClient().BeginSourceFile(
      compilerInstance.getLangOpts(), &amp;compilerInstance.getPreprocessor());

  // Create an AST consumer instance which is going to get called by
  // ParseAST.
  MyASTConsumer consumer(rewriter);

  // Parse the file to AST, registering our consumer as the AST consumer.
  clang::ParseAST(
    compilerInstance.getPreprocessor(), 
    &amp;consumer, 
    compilerInstance.getASTContext());

  // At this point the rewriter's buffer should be full with the rewritten
  // file contents.
  const RewriteBuffer* buffer = rewriter.getRewriteBufferFor(sourceManager.getMainFileID());

  return std::string(buffer-&gt;begin(), buffer-&gt;end());
}
</code></pre>

<p>Here is my input program, <code>negate.cpp</code>: </p>

<pre><code>bool negate(bool b) {
    if (b) {
        return false;
    } else {
        return true;
    }
}
</code></pre>

<p>When I run <code>transform</code> on this code I get the following error: </p>

<pre><code>negate.cpp:1:1: error: unknown type name 'bool'
bool negate(bool b) {
^
</code></pre>

<p>This suggests to me that it is working in C mode, not C++ mode. To confirm, I replaced <code>bool</code> with <code>int</code>, <code>true</code> with <code>1</code> and <code>false</code> with <code>0</code>: </p>

<pre><code>int negate(int b) {
    if (b) {
        return 0;
    } else {
        return 1;
    }
}
</code></pre>

<p>This worked, so my question is: </p>

<p>How do I put a Clang <code>CompilerInstance</code> into C++ mode? </p>

<hr>

<p>Update: </p>

<p>I tried changing the invocation, but no luck: </p>

<pre><code>  auto&amp; langOpts = compilerInstance.getLangOpts();
  langOpts.CPlusPlus = true;
  langOpts.CPlusPlus11 = true;

  auto* compilerInvocation = new CompilerInvocation();

  compilerInvocation-&gt;setLangDefaults(
    langOpts, 
    clang::InputKind::IK_CXX, 
    LangStandard::lang_gnu11);
</code></pre>