Commit 2f721476 authored by Duncan P. N. Exon Smith's avatar Duncan P. N. Exon Smith
Browse files

Frontend: Simplify handling of non-seeking streams in CompilerInstance, NFC

Add a new `raw_pwrite_ostream` variant, `buffer_unique_ostream`, which
is like `buffer_ostream` but with unique ownership of the stream it's
wrapping. Use this in CompilerInstance to simplify the ownership of
non-seeking output streams, avoiding logic sprawled around to deal with
them specially.

This also simplifies future work to encapsulate output files in a
different class.

Differential Revision: https://reviews.llvm.org/D93260
parent f36007e8
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -172,11 +172,6 @@ class CompilerInstance : public ModuleLoader {
    }
  };

  /// If the output doesn't support seeking (terminal, pipe). we switch
  /// the stream to a buffer_ostream. These are the buffer and the original
  /// stream.
  std::unique_ptr<llvm::raw_fd_ostream> NonSeekStream;

  /// The list of active output files.
  std::list<OutputFile> OutputFiles;

+1 −5
Original line number Diff line number Diff line
@@ -678,7 +678,6 @@ void CompilerInstance::clearOutputFiles(bool EraseFiles) {
      llvm::sys::fs::remove(Module.second);
    BuiltModules.clear();
  }
  NonSeekStream.reset();
}

std::unique_ptr<raw_pwrite_stream>
@@ -816,10 +815,7 @@ std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile(
  if (!Binary || OS->supportsSeeking())
    return std::move(OS);

  auto B = std::make_unique<llvm::buffer_ostream>(*OS);
  assert(!NonSeekStream);
  NonSeekStream = std::move(OS);
  return std::move(B);
  return std::make_unique<llvm::buffer_unique_ostream>(std::move(OS));
}

// Initialization Utilities
+12 −0
Original line number Diff line number Diff line
@@ -687,6 +687,18 @@ public:
  ~buffer_ostream() override { OS << str(); }
};

class buffer_unique_ostream : public raw_svector_ostream {
  std::unique_ptr<raw_ostream> OS;
  SmallVector<char, 0> Buffer;

  virtual void anchor() override;

public:
  buffer_unique_ostream(std::unique_ptr<raw_ostream> OS)
      : raw_svector_ostream(Buffer), OS(std::move(OS)) {}
  ~buffer_unique_ostream() override { *OS << str(); }
};

} // end namespace llvm

#endif // LLVM_SUPPORT_RAW_OSTREAM_H
+2 −0
Original line number Diff line number Diff line
@@ -987,3 +987,5 @@ void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
void raw_pwrite_stream::anchor() {}

void buffer_ostream::anchor() {}

void buffer_unique_ostream::anchor() {}