使用clang api的ast发生器

时间:2014-02-23 12:09:29

标签: qt clang qt5 llvm-clang

我是初学者。我在Qt5.2中使用了clang api 3.4版 我没有使用api进行ast转储。 告诉我我做错了什么

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();
}

我为我的代码和我的英语道歉

1 个答案:

答案 0 :(得分:0)

这里的问题是您使用错误的功能createDiagnostics createDiagnostics有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);

你应该为它提供一个DiagnosticConsumer。但更简单的解决方法是:

   CompilerInstance compiler;
   compiler.createDiagnostics();