Correct way to generate IR code with Frontend?

Hi,

I would like to use the clang Frontend api to build a file and output the IR code. I have found that I can set the TargetInfo on the CompilerInstance but I cant seem to set the LangStandard to c++11. Instead I have to provide an argument and call CompilerInvocation::CreateFromArgs to get the LangStandard setting. What am I doing wrong in my code (see llvm-env/main.cxx at master · peteasa/llvm-env · GitHub for the full listing)?

In my code the following does not work:

     clang::CompilerInvocation &compInvocation = compInst.getInvocation();

     clang::TargetOptions &targetOptions = compInvocation.getTargetOpts();
     targetOptions.Triple = llvm::sys::getDefaultTargetTriple();
     llvm::Triple llvmTriple(compInvocation.getTargetOpts().Triple);

     std::shared_ptr<clang::TargetOptions> pTargetOptions =
std::make_shared<clang::TargetOptions>(targetOptions);
     clang::TargetInfo *pTargetInfo =
         clang::TargetInfo::CreateTargetInfo(
             compInst.getDiagnostics(),
             pTargetOptions);
     compInst.setTarget(pTargetInfo); // this works ok setting the TargetInfo

     clang::LangOptions langOpts;
     langOpts.GNUMode = 1;
     langOpts.CXXExceptions = 1;
     langOpts.RTTI = 1;
     langOpts.Bool = 1;
     langOpts.CPlusPlus11 = 1;
     clang::PreprocessorOptions &PreprocOpts = compInst.getPreprocessorOpts();

     compInvocation.setLangDefaults(
         langOpts,
         clang::IK_CXX,
         llvmTriple,
         PreprocOpts,
         clang::LangStandard::lang_cxx11); // this does not work, instead I have to set -std=c++11 and CreateFromArgs()

I have been looking at the tutorials and other document but so far have not found a good description of how to do this.

Thanks in advance for any assistance you can provide.

Peter.