Index: cfe/trunk/lib/Frontend/CompilerInstance.cpp =================================================================== --- cfe/trunk/lib/Frontend/CompilerInstance.cpp +++ cfe/trunk/lib/Frontend/CompilerInstance.cpp @@ -232,9 +232,13 @@ std::move(StreamOwner)); if (CodeGenOpts) Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); - assert(Diags.ownsClient()); - Diags.setClient( - new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger))); + if (Diags.ownsClient()) { + Diags.setClient( + new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger))); + } else { + Diags.setClient( + new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger))); + } } static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, Index: cfe/trunk/unittests/Frontend/CompilerInstanceTest.cpp =================================================================== --- cfe/trunk/unittests/Frontend/CompilerInstanceTest.cpp +++ cfe/trunk/unittests/Frontend/CompilerInstanceTest.cpp @@ -8,6 +8,7 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/CompilerInvocation.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" #include "llvm/Support/ToolOutputFile.h" @@ -70,4 +71,21 @@ ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file")); } +TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) { + auto DiagOpts = new DiagnosticOptions(); + DiagOpts->DiagnosticLogFile = "log.diags"; + + // Create the diagnostic engine with unowned consumer. + std::string DiagnosticOutput; + llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); + auto DiagPrinter = llvm::make_unique( + DiagnosticsOS, new DiagnosticOptions()); + CompilerInstance Instance; + IntrusiveRefCntPtr Diags = Instance.createDiagnostics( + DiagOpts, DiagPrinter.get(), /*ShouldOwnClient=*/false); + + Diags->Report(diag::err_expected) << "no crash"; + ASSERT_EQ(DiagnosticsOS.str(), "error: expected no crash\n"); +} + } // anonymous namespace