使用CLANG API的AST生成器



我是一个初学者。我在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();
}

我为我的代码和英语道歉

这里的问题是功能创建诊断你一直使用错误。
创建诊断有两种形式:

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

您应该为其提供诊断消费者。但更简单的解决方法是:

   CompilerInstance compiler;
   compiler.createDiagnostics();

最新更新