CompilerInstance as CPP compiler?

using this dummy source file

struct AA {
int a, b, c;
void ff(int z){ }
void funcAZ() {}
int main(){
struct AA aa;
int a=1;

I ran the code at the end of this email. there are no cmd line arguments and the file is a.cpp.

When it runs my ASTConsumer i get the assert error

Unexpected diagnostic outside source file processing

however if i remove the ff function in AA, it runs fine. I assume the problem is the compiler is running in c mode and i am using c++ source.
How do i tell it to compile in C++ mode?

CompilerInstance compiler;

compiler.createDiagnostics(argc, argv);
CompilerInvocation::CreateFromArgs( compiler.getInvocation(),
argv + 1, argv + argc, compiler.getDiagnostics() );

compiler.setTarget( TargetInfo::CreateTargetInfo(
compiler.getDiagnostics(), compiler.getTargetOpts() ) );

compiler.setASTConsumer( new PrintFunctionsConsumer);

compiler.createSema(false, NULL);

compiler.InitializeSourceManager( main_input_file );
ParseAST( compiler.getPreprocessor(), &compiler.getASTConsumer(), compiler.getASTContext() );

Either your adding the “-x c++”-option to your compilercreateion, or, by code, you can take the language-options of the compiler and set the appropriate flags by yourself
LangOptions lopts= compiler.getLangOptions(); (

2011/6/22 Adam Smalin <>