Skip to content

Commit

Permalink
Frontend: Simplify handling of non-seeking streams in CompilerInstanc…
Browse files Browse the repository at this point in the history
…e, 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
  • Loading branch information
dexonsmith committed Jan 26, 2021
1 parent f36007e commit 2f72147
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
5 changes: 0 additions & 5 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 1 addition & 5 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm/Support/raw_ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,18 @@ class buffer_ostream : public raw_svector_ostream {
~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 changes: 2 additions & 0 deletions llvm/lib/Support/raw_ostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

0 comments on commit 2f72147

Please sign in to comment.