LLVM断言错误 [英] LLVM assertion error

查看:669
本文介绍了LLVM断言错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用LLVM / Clang API将源代码编译为LLVM IR。

I'm trying to use LLVM/Clang API to compile the source code to LLVM IR.

clang_ir.cpp:

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 $

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$

运行时出现断言错误:

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$ 

原因是什么?

推荐答案

它很清楚地告诉你问题是什么。未设置 DiagnosticClient DiagnosticEngine 需要一个 DiagnosticClient - 实际上是一个回调来说明诊断发生时该怎么做。

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.

坦率地说,Clang C ++ API中充满了讨厌的狗屎。它是太多的要求他们只是引用或指针到引擎构造函数中的客户端。你只需要习惯于修复每个断言失败,因为它出现。

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.

这篇关于LLVM断言错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆