Skip to content

Commit 2f72147

Browse files
committed
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
1 parent f36007e commit 2f72147

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ class CompilerInstance : public ModuleLoader {
172172
}
173173
};
174174

175-
/// If the output doesn't support seeking (terminal, pipe). we switch
176-
/// the stream to a buffer_ostream. These are the buffer and the original
177-
/// stream.
178-
std::unique_ptr<llvm::raw_fd_ostream> NonSeekStream;
179-
180175
/// The list of active output files.
181176
std::list<OutputFile> OutputFiles;
182177

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ void CompilerInstance::clearOutputFiles(bool EraseFiles) {
678678
llvm::sys::fs::remove(Module.second);
679679
BuiltModules.clear();
680680
}
681-
NonSeekStream.reset();
682681
}
683682

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

819-
auto B = std::make_unique<llvm::buffer_ostream>(*OS);
820-
assert(!NonSeekStream);
821-
NonSeekStream = std::move(OS);
822-
return std::move(B);
818+
return std::make_unique<llvm::buffer_unique_ostream>(std::move(OS));
823819
}
824820

825821
// Initialization Utilities

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,18 @@ class buffer_ostream : public raw_svector_ostream {
687687
~buffer_ostream() override { OS << str(); }
688688
};
689689

690+
class buffer_unique_ostream : public raw_svector_ostream {
691+
std::unique_ptr<raw_ostream> OS;
692+
SmallVector<char, 0> Buffer;
693+
694+
virtual void anchor() override;
695+
696+
public:
697+
buffer_unique_ostream(std::unique_ptr<raw_ostream> OS)
698+
: raw_svector_ostream(Buffer), OS(std::move(OS)) {}
699+
~buffer_unique_ostream() override { *OS << str(); }
700+
};
701+
690702
} // end namespace llvm
691703

692704
#endif // LLVM_SUPPORT_RAW_OSTREAM_H

llvm/lib/Support/raw_ostream.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,5 @@ void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
987987
void raw_pwrite_stream::anchor() {}
988988

989989
void buffer_ostream::anchor() {}
990+
991+
void buffer_unique_ostream::anchor() {}

0 commit comments

Comments
 (0)