C++ 从std::istream读取LLVM IR
以前我问过如何写然后从文件中读回IR。读取的代码如下所示:C++ 从std::istream读取LLVM IR,c++,llvm,llvm-ir,C++,Llvm,Llvm 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来编写模块,但是没有明显的方法来改编代码进行读取
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
来编写模块,但是没有明显的方法来改编代码进行读取,例如,没有MemoryBuffer
来改编std::istream
(除非我错过了它)。你应该使用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
构造,所以您需要做的就是。是的,这就是我最后要做的。虽然这显然是唯一可行的答案,但如果您可以直接将istream
传递给ParseIR()
,这样就不必不断增加字符串,那就更好了。