6

If I is of type llvm::Instruction, we can print out the same in human-readable form (textual representation) by errs() << I;

I want the assign the exact same representation to a std::string to C type string. How can I do that?

sherlock
  • 2,397
  • 3
  • 27
  • 44

2 Answers2

6

Well, LLVM provides a string stream as well:

#include <llvm/Support/raw_ostream.h>

Use it like this:

std::string str;
llvm::raw_string_ostream(str) << I;

// use str
eush77
  • 3,870
  • 1
  • 23
  • 30
2

I ran into some problems using @eush77's answer. Here is my fix.

std::string str;
llvm::raw_string_ostream ss(str);
ss << I;
errs() << ss.str() << "\n";
Rabeez Riaz
  • 442
  • 5
  • 15