summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2019-06-10 23:32:42 +0000
committerAlex Lorenz <arphaman@gmail.com>2019-06-10 23:32:42 +0000
commit88377d8db79c46658f41dbf2503225265036444e (patch)
treec5e5c814e78bd99884ff15c6370e1c2597ec0e24
parent60e52cab86bee81d17a3279b83b8f645dfd8d1a4 (diff)
downloadbcm5719-llvm-88377d8db79c46658f41dbf2503225265036444e.tar.gz
bcm5719-llvm-88377d8db79c46658f41dbf2503225265036444e.zip
[Frontend] SetUpDiagnosticLog should handle unowned diagnostic consumer
in the compiler The function SetUpDiagnosticLog that was called from createDiagnostics didn't handle the case where the diagnostics engine didn't own the diagnostics consumer. This is a potential problem for a clang tool, in particular some of the follow-up patches for clang-scan-deps will need this fix. Differential Revision: https://reviews.llvm.org/D63101 llvm-svn: 363009
-rw-r--r--clang/lib/Frontend/CompilerInstance.cpp10
-rw-r--r--clang/unittests/Frontend/CompilerInstanceTest.cpp18
2 files changed, 25 insertions, 3 deletions
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 9c70a70ed7e..fd33b85b31d 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -232,9 +232,13 @@ static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
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,
diff --git a/clang/unittests/Frontend/CompilerInstanceTest.cpp b/clang/unittests/Frontend/CompilerInstanceTest.cpp
index 1c3803289b9..3ef13d32ca4 100644
--- a/clang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/clang/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 @@ TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) {
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<TextDiagnosticPrinter>(
+ DiagnosticsOS, new DiagnosticOptions());
+ CompilerInstance Instance;
+ IntrusiveRefCntPtr<DiagnosticsEngine> 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
OpenPOWER on IntegriCloud