之前我曾询问过如何将 IR 写入文件或从文件中读回 IR。读取的代码如下所示:
LLVMContext ctx;
SMDiagnostic diag;
Module *m = ParseIRFile( "my_file", diag, ctx );
但是,我尝试改造 LLVM IR 的代码只传递给我一个 std::istream&
。如何从 std::istream
读取 IR?
我想出了如何使用raw_os_ostream
将std::ostream
改编为raw_ostream
来编写模块,但没有明显的适应读取代码的方法,例如,没有适应 std::istream
的 MemoryBuffer
(除非我错过了)。
最佳答案
您应该使用 ParseIR()
而不是 ParseIRFile()
。它获取一个 MemoryBuffer 作为参数,而不是文件名。您可以通过 getMemBuffer()
工厂方法从 StringRef
创建一个 MemoryBuffer
:
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be null terminated if RequiresNullTerminator is true.
static MemoryBuffer *getMemBuffer(StringRef InputData,
StringRef BufferName = "",
bool RequiresNullTerminator = true);
由于 StringRef
可以(甚至隐式)从 std::string
构造,因此您需要做的就是 convert your std::istream
to std::string
.
关于c++ - 从 std::istream 读取 LLVM IR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13013270/