[PATCH] D98426: [llvm-objcopy][Support] move writeToStream helper function to Support.

Alexey Lapshin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 19 03:36:27 PDT 2021


avl added a comment.

> There is of course a completely different approach, which would avoid this issue, namely to change the function to just create and return the stream and then leave the clients to pass the stream to their write functions. If you decided to switch to this approach, you'd probably want a new stream class derived from raw_fd_ostream which does the tempfile stuff itself, in the constructor/destructor, so that clients don't need t worry about that logic.

I thought about similar approach:

  std::unique_ptr<raw_ostream> Stream = createStream(FileName);
  
  Stream << "data to write";

But it makes error handling to be inconvenient. The subclass of raw_fd_ostream needs to return Error from it`s destructor.

  raw_fd_ostream_sub::~raw_fd_ostream_sub () {
      Error Err = Temp->keep(OutputFileName);
  }

So it is not clear how to get that error from the destructor of std::unique_ptr(which calls destructor of subclass of raw_fd_ostream).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98426/new/

https://reviews.llvm.org/D98426



More information about the llvm-commits mailing list