Skip to content

Commit

Permalink
Frontend: Simplify ownership model for clang's output streams.
Browse files Browse the repository at this point in the history
This changes the CompilerInstance::createOutputFile function to return
a std::unique_ptr<llvm::raw_ostream>, rather than an llvm::raw_ostream
implicitly owned by the CompilerInstance. This in most cases required that
I move ownership of the output stream to the relevant ASTConsumer.

The motivation for this change is to allow BackendConsumer to be a client
of interfaces such as D20268 which take ownership of the output stream.

Differential Revision: http://reviews.llvm.org/D21537

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@275507 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
pcc committed Jul 15, 2016
1 parent 543a430 commit f1bc238
Show file tree
Hide file tree
Showing 20 changed files with 236 additions and 252 deletions.
3 changes: 2 additions & 1 deletion include/clang/CodeGen/BackendUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ namespace clang {
void EmitBackendOutput(DiagnosticsEngine &Diags, const CodeGenOptions &CGOpts,
const TargetOptions &TOpts, const LangOptions &LOpts,
const llvm::DataLayout &TDesc, llvm::Module *M,
BackendAction Action, raw_pwrite_stream *OS);
BackendAction Action,
std::unique_ptr<raw_pwrite_stream> OS);

void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
llvm::MemoryBufferRef Buf);
Expand Down
10 changes: 6 additions & 4 deletions include/clang/CodeGen/ObjectFilePCHContainerOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class ObjectFilePCHContainerWriter : public PCHContainerWriter {
/// Return an ASTConsumer that can be chained with a
/// PCHGenerator that produces a wrapper file format
/// that also contains full debug info for the module.
std::unique_ptr<ASTConsumer> CreatePCHContainerGenerator(
CompilerInstance &CI, const std::string &MainFileName,
const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
std::shared_ptr<PCHBuffer> Buffer) const override;
std::unique_ptr<ASTConsumer>
CreatePCHContainerGenerator(CompilerInstance &CI,
const std::string &MainFileName,
const std::string &OutputFileName,
std::unique_ptr<llvm::raw_pwrite_stream> OS,
std::shared_ptr<PCHBuffer> Buffer) const override;
};

/// A PCHContainerReader implementation that uses LLVM to
Expand Down
2 changes: 1 addition & 1 deletion include/clang/Frontend/ASTConsumers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TargetOptions;
// original C code. The output is intended to be in a format such that
// clang could re-parse the output back into the same AST, but the
// implementation is still incomplete.
std::unique_ptr<ASTConsumer> CreateASTPrinter(raw_ostream *OS,
std::unique_ptr<ASTConsumer> CreateASTPrinter(std::unique_ptr<raw_ostream> OS,
StringRef FilterString);

// AST dumper: dumps the raw AST in human-readable form to stderr; this is
Expand Down
34 changes: 14 additions & 20 deletions include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,10 @@ class CompilerInstance : public ModuleLoader {
struct OutputFile {
std::string Filename;
std::string TempFilename;
std::unique_ptr<raw_ostream> OS;

OutputFile(std::string filename, std::string tempFilename,
std::unique_ptr<raw_ostream> OS)
: Filename(std::move(filename)), TempFilename(std::move(tempFilename)),
OS(std::move(OS)) {}
OutputFile(OutputFile &&O)
: Filename(std::move(O.Filename)),
TempFilename(std::move(O.TempFilename)), OS(std::move(O.OS)) {}

OutputFile(std::string filename, std::string tempFilename)
: Filename(std::move(filename)), TempFilename(std::move(tempFilename)) {
}
};

/// If the output doesn't support seeking (terminal, pipe). we switch
Expand Down Expand Up @@ -577,8 +572,8 @@ class CompilerInstance : public ModuleLoader {
/// \param OutFile - The output file info.
void addOutputFile(OutputFile &&OutFile);

/// clearOutputFiles - Clear the output file list, destroying the contained
/// output streams.
/// clearOutputFiles - Clear the output file list. The underlying output
/// streams must have been closed beforehand.
///
/// \param EraseFiles - If true, attempt to erase the files from disk.
void clearOutputFiles(bool EraseFiles);
Expand Down Expand Up @@ -685,19 +680,18 @@ class CompilerInstance : public ModuleLoader {
/// atomically replace the target output on success).
///
/// \return - Null on error.
raw_pwrite_stream *createDefaultOutputFile(bool Binary = true,
StringRef BaseInput = "",
StringRef Extension = "");
std::unique_ptr<raw_pwrite_stream>
createDefaultOutputFile(bool Binary = true, StringRef BaseInput = "",
StringRef Extension = "");

/// Create a new output file and add it to the list of tracked output files,
/// optionally deriving the output path name.
///
/// \return - Null on error.
raw_pwrite_stream *createOutputFile(StringRef OutputPath, bool Binary,
bool RemoveFileOnSignal,
StringRef BaseInput, StringRef Extension,
bool UseTemporary,
bool CreateMissingDirectories = false);
std::unique_ptr<raw_pwrite_stream>
createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
StringRef BaseInput, StringRef Extension, bool UseTemporary,
bool CreateMissingDirectories = false);

/// Create a new output file, optionally deriving the output path name.
///
Expand Down Expand Up @@ -731,7 +725,7 @@ class CompilerInstance : public ModuleLoader {
bool CreateMissingDirectories, std::string *ResultPathName,
std::string *TempPathName);

llvm::raw_null_ostream *createNullOutputFile();
std::unique_ptr<raw_pwrite_stream> createNullOutputFile();

/// }
/// @name Initialization Utility Methods
Expand Down
9 changes: 4 additions & 5 deletions include/clang/Frontend/FrontendActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class GeneratePCHAction : public ASTFrontendAction {
/// create the PCHGenerator instance returned by CreateASTConsumer.
///
/// \returns true if an error occurred, false otherwise.
static raw_pwrite_stream *
static std::unique_ptr<raw_pwrite_stream>
ComputeASTConsumerArguments(CompilerInstance &CI, StringRef InFile,
std::string &Sysroot, std::string &OutputFile);
};
Expand Down Expand Up @@ -117,10 +117,9 @@ class GenerateModuleAction : public ASTFrontendAction {
/// create the PCHGenerator instance returned by CreateASTConsumer.
///
/// \returns true if an error occurred, false otherwise.
raw_pwrite_stream *ComputeASTConsumerArguments(CompilerInstance &CI,
StringRef InFile,
std::string &Sysroot,
std::string &OutputFile);
std::unique_ptr<raw_pwrite_stream>
ComputeASTConsumerArguments(CompilerInstance &CI, StringRef InFile,
std::string &Sysroot, std::string &OutputFile);
};

class SyntaxOnlyAction : public ASTFrontendAction {
Expand Down
20 changes: 12 additions & 8 deletions include/clang/Frontend/PCHContainerOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ class PCHContainerWriter {
/// Return an ASTConsumer that can be chained with a
/// PCHGenerator that produces a wrapper file format containing a
/// serialized AST bitstream.
virtual std::unique_ptr<ASTConsumer> CreatePCHContainerGenerator(
CompilerInstance &CI, const std::string &MainFileName,
const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
std::shared_ptr<PCHBuffer> Buffer) const = 0;
virtual std::unique_ptr<ASTConsumer>
CreatePCHContainerGenerator(CompilerInstance &CI,
const std::string &MainFileName,
const std::string &OutputFileName,
std::unique_ptr<llvm::raw_pwrite_stream> OS,
std::shared_ptr<PCHBuffer> Buffer) const = 0;
};

/// This abstract interface provides operations for unwrapping
Expand All @@ -73,10 +75,12 @@ class RawPCHContainerWriter : public PCHContainerWriter {

/// Return an ASTConsumer that can be chained with a
/// PCHGenerator that writes the module to a flat file.
std::unique_ptr<ASTConsumer> CreatePCHContainerGenerator(
CompilerInstance &CI, const std::string &MainFileName,
const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
std::shared_ptr<PCHBuffer> Buffer) const override;
std::unique_ptr<ASTConsumer>
CreatePCHContainerGenerator(CompilerInstance &CI,
const std::string &MainFileName,
const std::string &OutputFileName,
std::unique_ptr<llvm::raw_pwrite_stream> OS,
std::shared_ptr<PCHBuffer> Buffer) const override;
};

/// Implements read operations for a raw pass-through PCH container.
Expand Down
7 changes: 4 additions & 3 deletions include/clang/Rewrite/Frontend/ASTConsumers.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ class Preprocessor;
// ObjC rewriter: attempts to rewrite ObjC constructs into pure C code.
// This is considered experimental, and only works with Apple's ObjC runtime.
std::unique_ptr<ASTConsumer>
CreateObjCRewriter(const std::string &InFile, raw_ostream *OS,
CreateObjCRewriter(const std::string &InFile, std::unique_ptr<raw_ostream> OS,
DiagnosticsEngine &Diags, const LangOptions &LOpts,
bool SilenceRewriteMacroWarning);
std::unique_ptr<ASTConsumer>
CreateModernObjCRewriter(const std::string &InFile, raw_ostream *OS,
CreateModernObjCRewriter(const std::string &InFile,
std::unique_ptr<raw_ostream> OS,
DiagnosticsEngine &Diags, const LangOptions &LOpts,
bool SilenceRewriteMacroWarning, bool LineInfo);

/// CreateHTMLPrinter - Create an AST consumer which rewrites source code to
/// HTML with syntax highlighting suitable for viewing in a web-browser.
std::unique_ptr<ASTConsumer> CreateHTMLPrinter(raw_ostream *OS,
std::unique_ptr<ASTConsumer> CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS,
Preprocessor &PP,
bool SyntaxHighlight = true,
bool HighlightMacros = true);
Expand Down
Loading

0 comments on commit f1bc238

Please sign in to comment.