raw_pwrite_stream to string or stdout?

TargetMachine::addPassesToEmitFile(..)
requires as its 2nd argument an raw_pwrite_stream.

Is it possible to create such a thing which either writes into a standard string or streams to outs() ?

Thanks,

raw_string_ostream is an adaptor for std::string.
raw_svector_ostream is more efficient adaptor for llvm::SmallString.

outs() will not work as it can’t seek.

I would suggest doing what llc does:

  if ((FileType != TargetMachine::CGFT_AssemblyFile &&
         !Out->os().supportsSeeking()) ||
        CompileTwice) {
      BOS = make_unique<raw_svector_ostream>(Buffer);
      OS = BOS.get();
}

That will work even with redirects.

Cheers,
Rafael

TargetMachine::CGFT_AssemblyFile is exactly what I am trying to write out.

Frank

I see.

For that I think we should be able to just change outs() to return a
raw_fd_ostream. It is already implemented with one anyway.

Cheers,
Rafael

Note that raw_fd_ostream is not seekable, and hence will not be suitable as addPassesToEmitFile output stream.

It is:

class raw_fd_ostream : public raw_pwrite_stream {

...
  bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out...

Cheers,
Rafael

Yes, you are correct. I had in mind the usual use of outs() for console or pipe, where raw_fd_ostream::SupportsSeeking will be false and actually not seekable.

Looking at the code, shouldn’t raw_fd_ostream::seek assert(SupportsSeeking)?

Good question. Originally it just flags the error, but I think that
predates the SupportsSeeking flag.

Cheers,
Rafael