编写一个将 LLVM IR 文件作为命令行参数的程序

Writing a program taking LLVM IR file as a Command line argument

本文关键字:文件 命令行 参数 程序 IR LLVM 一个      更新时间:2023-10-16

我正在编写一个用于测试 LLVM IR 命令行参数的主.cpp文件。

我正在使用llvm版本:6.0.1

#include<iostream>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include <llvm/Support/Error.h>
#include <llvm/IRReader/IRReader.h>
using namespace llvm;
using namespace std;
static cl::opt<string> input(cl::Positional, cl::desc("Bitcode file"), cl::Required);
int main(int argc, char** argv)
{
cl::ParseCommandLineOptions(argc, argv, "LLVM IR to Bytecode n");
LLVMContext context;
ErrorOr<std::unique_ptr<MemoryBuffer>> mb = MemoryBuffer::getFile(input);
if (error_code ec = mb.getError()) {
errs() << ec.message();
return -1;
}
ErrorOr<std::unique_ptr<Module>> m=parseBitcodeFile(mb->get()->getMemBufferRef(), context);
if (error_code ec = m.getError())
{
errs() << "Error reading bitcode: " << ec.message() << "n";
return -1;
}
return 0;
}

我收到此错误:

错误:"类 llvm::预期>"没有名为"getError"的成员 if (error_code ec = m.getError(((

我谷歌了很多次,但我在任何地方都没有找到答案。请给我一些解决方案。

上述错误是由于一段时间内LLVM API的变化造成的。更改模块读取器函数以返回llvm::Expected <T>[]https://reviews.llvm.org/D26562 .此类与 ErrorOr 类似,但将 error_code 替换为Error。由于无法复制错误,因此此类将 getError(( 替换为takeError().

所以上面的代码更改为:

Expected<std::unique_ptr<Module>> m = parseBitcodeFile(mb->get()->getMemBufferRef(), context);
if (std::error_code ec = errorToErrorCode(m.takeError())) {
errs() << "Error reading bitcode: " << ec.message() << "n";
return -1;
}