Clang LLVM断言错误

Clang LLVM断言错误,clang,llvm,Clang,Llvm,我正在尝试使用LLVM/ClangAPI将源代码编译为LLVM IR clang_ir.cpp: #include <iostream> #include <clang/Driver/Compilation.h> #include <clang/Driver/Driver.h> #include <clang/Frontend/TextDiagnosticPrinter.h> #include <llvm/Support/Host.h>

我正在尝试使用LLVM/ClangAPI将源代码编译为LLVM IR

clang_ir.cpp:

#include <iostream>

#include <clang/Driver/Compilation.h>
#include <clang/Driver/Driver.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/Program.h>
#include <llvm/Support/raw_ostream.h>

using namespace std;
using namespace clang;
using namespace clang::driver;

int main(int argc, char** argv)
{
    cout << "Starting ----" << std::endl;

    // [clang -S -emit-llvm ./test/hello_world.cpp]

    // Arguments to pass to the clang driver:
    string clangPath = "clang";

    string inputPath = "./test/hello_world.cpp";
    string outputPath = "hello_world.ll";

    vector<const char *> args;
    args.push_back(clangPath.c_str());
    args.push_back("-S");
    args.push_back("-emit-llvm");
    args.push_back(inputPath.c_str());

    // The clang driver needs a DiagnosticsEngine so it can report problems
    clang::DiagnosticOptions *Options = new clang::DiagnosticOptions();
    //clang::TextDiagnosticPrinter *DiagClient = new clang::TextDiagnosticPrinter(llvm::errs(), Options);
    clang::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new clang::DiagnosticIDs());
    clang::DiagnosticsEngine Diags(DiagID, Options);

    cout << "making driver" << endl;

    // Create the clang driver
    clang::driver::Driver TheDriver(args[0], llvm::sys::getDefaultTargetTriple(), outputPath, Diags);

    // C++ code
    //TheDriver.CCCIsCXX = true;

    cout << "making compilation" << endl;

    // Create the set of actions to perform
    clang::OwningPtr<clang::driver::Compilation> Compilation(TheDriver.BuildCompilation(args));

    cout << "printing actions:" << endl;

    // Print the set of actions
    TheDriver.PrintActions(*Compilation);

    std::cout << "Done ----" << std::endl;

    // Carry out the actions
    int Res = 0;
    SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
    if (Compilation)
        Res = TheDriver.ExecuteCompilation(*Compilation, FailingCommands);

    // Report problems
        /*
    if (Res < 0)
        TheDriver.generateCompilationDiagnostics(*Compilation, FailingCommands);
        */
    for (SmallVectorImpl< std::pair<int,
        const Command *> >::iterator it = FailingCommands.begin(),
        ie = FailingCommands.end(); it != ie; ++it) {
            int CommandRes = it->first;
            const Command *FailingCommand = it->second;
            if (!Res)
              Res = CommandRes;

            // If result status is < 0, then the driver command signalled an error.
            // If result status is 70, then the driver command reported a fatal error.
            // In these cases, generate additional diagnostic information if possible.
            if (CommandRes < 0 || CommandRes == 70) {
              TheDriver.generateCompilationDiagnostics(*Compilation, FailingCommand);
              break;
            }
      }

    return Res;
}

原因是什么?

它非常清楚地告诉你问题出在哪里。未设置
DiagnosticClient
DiagnosticEngine
需要一个
DiagnosticClient
——本质上是一个回调,用于说明发生诊断时要做什么

坦率地说,CLANC++的API中充满了恼人的狗屎。要求他们只在引擎构造函数中引用或指向客户机的指针,这太过分了。您只需习惯于在出现每个断言失败时修复它

MBA-Anton:build asmirnov$ ./clang_ir
Starting ----
making driver
making compilation
Assertion failed: (getClient() && "DiagnosticClient not set!"), function EmitCurrentDiagnostic, file /Users/asmirnov/Documents/dev/src/llvm_34/tools/clang/lib/Basic/Diagnostic.cpp, line 391.
Abort trap: 6
MBA-Anton:build asmirnov$