Question

I am a beginner developer. I use clang api version 3.4 in Qt5.2 I did not get to use the api for ast dump. tell me what I'm doing wrong

clang-check --version
LLVM (http://llvm.org/):
LLVM version 3.4

using namespace clang ;
using namespace llvm ;

int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
DiagnosticOptions* diagOpts = new DiagnosticOptions();
    CodeGenOptions* codeGenOpts = new CodeGenOptions();
    CompilerInstance compiler;
    compiler.createDiagnostics(
        diagOpts,
        false,
        false,
        codeGenOpts
    ); // to stdout
    assert(compiler.hasDiagnostics());
    const char *args[] =
    {
        "-cc1", // вызов LLVM Clang
        "a.cpp" // входной файл
    };

    clang::CompilerInvocation::CreateFromArgs(
    compiler.getInvocation(),
        args,
        args + 2,
        compiler.getDiagnostics());
        assert(0 == compiler.getDiagnostics().getErrorsAsFatal());
        FrontendAction *action = new ASTDumpAction;
        if(compiler.ExecuteAction(*action)){
            std::cout << "ok:";
        }else{
           std::cout << "error:";
        }
    std::cout << "8" << std::endl;
        assert(0 == compiler.getDiagnostics().getNumWarnings());
        assert(actionSuccessful);
return a.exec();
}

I apologize for my code and my English

Was it helpful?

Solution

The problem here is funciton createDiagnostics you've been using wrong.
createDiagnostics has two forms:

void createDiagnostics(DiagnosticConsumer *Client = 0,
                       bool ShouldOwnClient = true);
static IntrusiveRefCntPtr<DiagnosticsEngine>
createDiagnostics(DiagnosticOptions *Opts,
                  DiagnosticConsumer *Client = 0,
                  bool ShouldOwnClient = true,
                  const CodeGenOptions *CodeGenOpts = 0);

You should provide a DiagnosticConsumer to it. But a simpler fix is:

   CompilerInstance compiler;
   compiler.createDiagnostics();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top