From 878271b2945e89b868fb8daefea3dfaffce32710 Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Thu, 7 Mar 2019 10:13:50 +0000 Subject: [PATCH] [libclang] Fix CXTranslationUnit_KeepGoing Since commit 56f548bbbb7e4387a69708f70724d00e9e076153 [modules] Round-trip -Werror flag through explicit module build. the behavior of CXTranslationUnit_KeepGoing changed: Unresolved #includes are fatal errors again. As a consequence, some templates are not instantiated and lead to confusing errors. Revert to the old behavior: With CXTranslationUnit_KeepGoing fatal errors are mapped to errors. Patch by Nikolai Kosjar. Differential Revision: https://reviews.llvm.org/D58501 llvm-svn: 355586 --- clang/include/clang/Basic/Diagnostic.h | 12 +++++++----- clang/lib/Basic/DiagnosticIDs.cpp | 7 ++++++- .../Inputs/keep-going-template-instantiations.h | 3 +++ .../Index/keep-going-template-instantiations.cpp | 5 +++++ clang/test/Index/keep-going.cpp | 4 ++-- clang/tools/libclang/CIndex.cpp | 2 +- clang/unittests/Basic/DiagnosticTest.cpp | 13 ++++++------- 7 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 clang/test/Index/Inputs/keep-going-template-instantiations.h create mode 100644 clang/test/Index/keep-going-template-instantiations.cpp diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h index 88947b7ed688..2cd68ea1524b 100644 --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -209,8 +209,8 @@ private: // Used by __extension__ unsigned char AllExtensionsSilenced = 0; - // Suppress diagnostics after a fatal error? - bool SuppressAfterFatalError = true; + // Treat fatal errors like errors. + bool FatalsAsError = false; // Suppress all diagnostics. bool SuppressAllDiagnostics = false; @@ -614,9 +614,11 @@ public: void setErrorsAsFatal(bool Val) { GetCurDiagState()->ErrorsAsFatal = Val; } bool getErrorsAsFatal() const { return GetCurDiagState()->ErrorsAsFatal; } - /// When set to true (the default), suppress further diagnostics after - /// a fatal error. - void setSuppressAfterFatalError(bool Val) { SuppressAfterFatalError = Val; } + /// \brief When set to true, any fatal error reported is made an error. + /// + /// This setting takes precedence over the setErrorsAsFatal setting above. + void setFatalsAsError(bool Val) { FatalsAsError = Val; } + bool getFatalsAsError() const { return FatalsAsError; } /// When set to true mask warnings that come from system headers. void setSuppressSystemWarnings(bool Val) { diff --git a/clang/lib/Basic/DiagnosticIDs.cpp b/clang/lib/Basic/DiagnosticIDs.cpp index a7473d8bb5d4..e8a99d08a913 100644 --- a/clang/lib/Basic/DiagnosticIDs.cpp +++ b/clang/lib/Basic/DiagnosticIDs.cpp @@ -481,6 +481,11 @@ DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc, Result = diag::Severity::Fatal; } + // If explicitly requested, map fatal errors to errors. + if (Result == diag::Severity::Fatal && + Diag.CurDiagID != diag::fatal_too_many_errors && Diag.FatalsAsError) + Result = diag::Severity::Error; + // Custom diagnostics always are emitted in system headers. bool ShowInSystemHeader = !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader; @@ -660,7 +665,7 @@ bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const { // If a fatal error has already been emitted, silence all subsequent // diagnostics. - if (Diag.FatalErrorOccurred && Diag.SuppressAfterFatalError) { + if (Diag.FatalErrorOccurred) { if (DiagLevel >= DiagnosticIDs::Error && Diag.Client->IncludeInDiagnosticCounts()) { ++Diag.NumErrors; diff --git a/clang/test/Index/Inputs/keep-going-template-instantiations.h b/clang/test/Index/Inputs/keep-going-template-instantiations.h new file mode 100644 index 000000000000..042918b1f8e6 --- /dev/null +++ b/clang/test/Index/Inputs/keep-going-template-instantiations.h @@ -0,0 +1,3 @@ +template struct c {}; +using d = c; +struct foo : public d {}; diff --git a/clang/test/Index/keep-going-template-instantiations.cpp b/clang/test/Index/keep-going-template-instantiations.cpp new file mode 100644 index 000000000000..7deef2120ed0 --- /dev/null +++ b/clang/test/Index/keep-going-template-instantiations.cpp @@ -0,0 +1,5 @@ +#include "missing.h" +#include + +// RUN: env CINDEXTEST_KEEP_GOING=1 c-index-test -test-load-source none -I%S/Inputs %s 2>&1 | FileCheck %s +// CHECK-NOT: error: expected class name diff --git a/clang/test/Index/keep-going.cpp b/clang/test/Index/keep-going.cpp index b3f29c5d3dae..0b2df725a5a7 100644 --- a/clang/test/Index/keep-going.cpp +++ b/clang/test/Index/keep-going.cpp @@ -34,5 +34,5 @@ class C : public A { }; // CHECK-KEEP-GOING-ONLY: VarDecl=global_var:1:12 [type=int] [typekind=Int] [isPOD=1] -// CHECK-DIAG: keep-going.cpp:1:10: fatal error: 'missing1.h' file not found -// CHECK-DIAG: keep-going.cpp:8:10: fatal error: 'missing2.h' file not found +// CHECK-DIAG: keep-going.cpp:1:10: error: 'missing1.h' file not found +// CHECK-DIAG: keep-going.cpp:8:10: error: 'missing2.h' file not found diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index cdcdfc167b2e..07e052bf0c13 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -3408,7 +3408,7 @@ clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); if (options & CXTranslationUnit_KeepGoing) - Diags->setSuppressAfterFatalError(false); + Diags->setFatalsAsError(true); // Recover resources if we crash before exiting this function. llvm::CrashRecoveryContextCleanupRegistrar