LLVM assertion error

浪子不回头ぞ 提交于 2020-01-07 08:32:00

问题


I'm trying to use LLVM/Clang API to compile the source code to 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;
}

successfully built with command-line:

MBA-Anton:build asmirnov$ clang++ llvm-config --cxxflags llvm-config --ldflags llvm-config --libs all -lclang -lclangAST -lclangASTMatchers -lclangAnalysis -lclangApplyReplacements -lclangBasic -lclangCodeGen -lclangDriver -lclangDynamicASTMatchers -lclangEdit -lclangFormat -lclangFrontend -lclangFrontendTool -lclangIndex -lclangLex -lclangParse -lclangQuery -lclangRewriteCore -lclangRewriteFrontend -lclangSema -lclangSerialization -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangStaticAnalyzerFrontend -lclangTidy -lclangTidyLLVMModule -lclangTooling ../clang_ir.cpp -o clang_ir MBA-Anton:build asmirnov$

while running i'm getting assertion error:

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$ 

What can be the reason?


回答1:


It pretty clearly tells you what the problem is. The DiagnosticClient wasn't set. The DiagnosticEngine requires a DiagnosticClient- essentially a callback to say what to do when a diagnostic occurs.

Frankly, the Clang C++ API is full of annoying shit like this. It's just too much to ask that they simply take a reference or pointer to a client in the engine constructor. You will simply have to get used to fixing each assertion failure as it comes up.



来源:https://stackoverflow.com/questions/24455630/llvm-assertion-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!