diff --git a/clang/include/clang/Frontend/TextDiagnosticPrinter.h b/clang/include/clang/Frontend/TextDiagnosticPrinter.h index 3c5c6267c632..29a9c305b114 100644 --- a/clang/include/clang/Frontend/TextDiagnosticPrinter.h +++ b/clang/include/clang/Frontend/TextDiagnosticPrinter.h @@ -34,10 +34,13 @@ class TextDiagnosticPrinter : public DiagnosticClient { SourceLocation LastWarningLoc; FullSourceLoc LastLoc; - bool LastCaretDiagnosticWasNote; + unsigned LastCaretDiagnosticWasNote : 1; + unsigned OwnsOutputStream : 1; public: - TextDiagnosticPrinter(llvm::raw_ostream &os, const DiagnosticOptions &diags); + TextDiagnosticPrinter(llvm::raw_ostream &os, const DiagnosticOptions &diags, + bool OwnsOutputStream = false); + virtual ~TextDiagnosticPrinter(); void BeginSourceFile(const LangOptions &LO) { LangOpts = &LO; diff --git a/clang/lib/Frontend/TextDiagnosticPrinter.cpp b/clang/lib/Frontend/TextDiagnosticPrinter.cpp index 4f8c804844b0..6ab0e1605276 100644 --- a/clang/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/clang/lib/Frontend/TextDiagnosticPrinter.cpp @@ -39,9 +39,16 @@ static const enum llvm::raw_ostream::Colors savedColor = const unsigned WordWrapIndentation = 6; TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream &os, - const DiagnosticOptions &diags) + const DiagnosticOptions &diags, + bool _OwnsOutputStream) : OS(os), LangOpts(0), DiagOpts(&diags), - LastCaretDiagnosticWasNote(false) { + LastCaretDiagnosticWasNote(0), + OwnsOutputStream(_OwnsOutputStream) { +} + +TextDiagnosticPrinter::~TextDiagnosticPrinter() { + if (OwnsOutputStream) + delete &OS; } void TextDiagnosticPrinter:: diff --git a/clang/tools/clang-cc/clang-cc.cpp b/clang/tools/clang-cc/clang-cc.cpp index e31f5a628506..e0ca9cf29045 100644 --- a/clang/tools/clang-cc/clang-cc.cpp +++ b/clang/tools/clang-cc/clang-cc.cpp @@ -517,31 +517,27 @@ DumpBuildInformation("dump-build-information", llvm::cl::value_desc("filename"), llvm::cl::desc("output a dump of some build information to a file")); -static llvm::raw_ostream *BuildLogFile = 0; - static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, unsigned argc, char **argv, llvm::OwningPtr &DiagClient) { std::string ErrorInfo; - BuildLogFile = new llvm::raw_fd_ostream(DumpBuildInformation.c_str(), - ErrorInfo); - + llvm::raw_ostream *OS = new llvm::raw_fd_ostream(DumpBuildInformation.c_str(), + ErrorInfo); if (!ErrorInfo.empty()) { llvm::errs() << "error opening -dump-build-information file '" << DumpBuildInformation << "', option ignored!\n"; - delete BuildLogFile; - BuildLogFile = 0; - DumpBuildInformation = ""; + delete OS; return; } - (*BuildLogFile) << "clang-cc command line arguments: "; + (*OS) << "clang-cc command line arguments: "; for (unsigned i = 0; i != argc; ++i) - (*BuildLogFile) << argv[i] << ' '; - (*BuildLogFile) << '\n'; + (*OS) << argv[i] << ' '; + (*OS) << '\n'; // Chain in a diagnostic client which will log the diagnostics. - DiagnosticClient *Logger = new TextDiagnosticPrinter(*BuildLogFile, DiagOpts); + DiagnosticClient *Logger = + new TextDiagnosticPrinter(*OS, DiagOpts, /*OwnsOutputStream=*/true); DiagClient.reset(new ChainedDiagnosticClient(DiagClient.take(), Logger)); } @@ -1316,7 +1312,6 @@ int main(int argc, char **argv) { } delete ClangFrontendTimer; - delete BuildLogFile; // If verifying diagnostics and we reached here, all is well. if (VerifyDiagnostics)