[cfe-dev] "Unexpected diagnostic outside source file processing" ?

Mark Dickinson dickinsm at gmail.com
Sat Jun 5 02:52:20 PDT 2010


Hello all.

I'm just getting started with Clang, with the aim of using it to do
static analysis on some C99 code.  I've got as far as successfully
parsing to AST, but I'm getting the following failure when my test
file "mytestfile.c" contains any sort of syntax error:

    Assertion failed: (LangOpts && "Unexpected diagnostic outside
source file processing"),
    function EmitCaretDiagnostic, file TextDiagnosticPrinter.cpp, line 293.

Clearly I've failed to initialize the language options correctly
somewhere, but I can't figure out where.  Can anyone give me any
pointers?

Here's the code I'm using, minus the necessary #includes..


using namespace clang;

int main(int argc, const char** argv) {
    DiagnosticOptions DiagOpts;
    llvm::IntrusiveRefCntPtr<Diagnostic> Diags =
        CompilerInstance::createDiagnostics(DiagOpts, 0, NULL);

    CompilerInvocation *cv = new CompilerInvocation;
    CompilerInvocation::CreateFromArgs(*cv, argv+1, argv+argc, *Diags);

    CompilerInstance ci;
    ci.setInvocation(cv);
    ci.setDiagnostics(Diags.getPtr());
    ci.setDiagnosticClient(Diags->getClient());

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

    ci.getTarget().setForcedLangOptions(cv->getLangOpts());

    ci.createFileManager();
    ci.createSourceManager();
    ci.InitializeSourceManager("mytestfile.c");

    ci.createPreprocessor();
    ci.createASTContext();

    ASTConsumer consumer;

    ParseAST(ci.getPreprocessor(),
             &consumer,
             ci.getASTContext(),
             true,
             true,
             0);

    return 0;
}


The file "mytestfile.c" just contains:

int main(void) {
  syntax error;
}


Any hints appreciated.

Thanks,

Mark



More information about the cfe-dev mailing list