Reading LLVM IR from a std::istream
Frage
Previously I had asked how to write then read back IR to/from a file. The read code looked like:
LLVMContext ctx;
SMDiagnostic diag;
Module *m = ParseIRFile( "my_file", diag, ctx );
However, the code I'm trying to retrofit LLVM IR into passes me just a std::istream&
. How can I read IR from a std::istream
?
I figured out how to use raw_os_ostream
to adapt a std::ostream
to a raw_ostream
for writing a module, but there's no obvious way to adapt the code for reading, e.g., no MemoryBuffer
that adapts a std::istream
(unless I missed it).
Lösung
You should use ParseIR()
instead of ParseIRFile()
. It gets a MemoryBuffer
as parameter, instead of a file name. You can create a MemoryBuffer
from a StringRef
via its getMemBuffer()
factory method:
/// 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);
And since a StringRef
can be (even implicitly) constructed from an std::string
, all you need to do is convert your std::istream
to std::string
.