r275313 - [PCH/preamble] Make sure that if the preamble/PCH was serialized with errors that we set diagnostic engine state appropriately.

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 13 13:35:26 PDT 2016


Author: akirtzidis
Date: Wed Jul 13 15:35:26 2016
New Revision: 275313

URL: http://llvm.org/viewvc/llvm-project?rev=275313&view=rev
Log:
[PCH/preamble] Make sure that if the preamble/PCH was serialized with errors that we set diagnostic engine state appropriately.

Otherwise there can be a crash with CFG analysis warnings doing work on invalid AST.

Fixes crash of rdar://26224134

Added:
    cfe/trunk/test/Index/pch-warn-as-error-code-split.cpp
    cfe/trunk/test/Index/pch-warn-as-error-code-split.h
    cfe/trunk/test/Index/pch-warn-as-error-code.cpp
    cfe/trunk/test/PCH/chain-invalid-code.cpp
Modified:
    cfe/trunk/lib/Basic/DiagnosticIDs.cpp
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Frontend/ChainedIncludesSource.cpp
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/GeneratePCH.cpp

Modified: cfe/trunk/lib/Basic/DiagnosticIDs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/DiagnosticIDs.cpp?rev=275313&r1=275312&r2=275313&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/DiagnosticIDs.cpp (original)
+++ cfe/trunk/lib/Basic/DiagnosticIDs.cpp Wed Jul 13 15:35:26 2016
@@ -351,7 +351,7 @@ bool DiagnosticIDs::isDefaultMappingAsEr
   if (DiagID >= diag::DIAG_UPPER_LIMIT)
     return false;
 
-  return GetDefaultDiagMapping(DiagID).getSeverity() == diag::Severity::Error;
+  return GetDefaultDiagMapping(DiagID).getSeverity() >= diag::Severity::Error;
 }
 
 /// getDescription - Given a diagnostic ID, return a description of the

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=275313&r1=275312&r2=275313&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Wed Jul 13 15:35:26 2016
@@ -2509,7 +2509,8 @@ static bool serializeUnit(ASTWriter &Wri
 }
 
 bool ASTUnit::serialize(raw_ostream &OS) {
-  bool hasErrors = getDiagnostics().hasErrorOccurred();
+  // For serialization we are lenient if the errors were only warn-as-error kind.
+  bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred();
 
   if (WriterData)
     return serializeUnit(WriterData->Writer, WriterData->Buffer,

Modified: cfe/trunk/lib/Frontend/ChainedIncludesSource.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ChainedIncludesSource.cpp?rev=275313&r1=275312&r2=275313&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ChainedIncludesSource.cpp (original)
+++ cfe/trunk/lib/Frontend/ChainedIncludesSource.cpp Wed Jul 13 15:35:26 2016
@@ -164,7 +164,7 @@ IntrusiveRefCntPtr<ExternalSemaSource> c
     ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions;
     auto consumer = llvm::make_unique<PCHGenerator>(
         Clang->getPreprocessor(), "-", nullptr, /*isysroot=*/"", Buffer,
-        Extensions);
+        Extensions, /*AllowASTWithErrors=*/true);
     Clang->getASTContext().setASTMutationListener(
                                             consumer->GetASTMutationListener());
     Clang->setASTConsumer(std::move(consumer));

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=275313&r1=275312&r2=275313&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Wed Jul 13 15:35:26 2016
@@ -1897,7 +1897,7 @@ AnalysisBasedWarnings::IssueWarnings(sem
   if (cast<DeclContext>(D)->isDependentContext())
     return;
 
-  if (Diags.hasUncompilableErrorOccurred() || Diags.hasFatalErrorOccurred()) {
+  if (Diags.hasUncompilableErrorOccurred()) {
     // Flush out any possibly unreachable diagnostics.
     flushDiagnostics(S, fscope);
     return;

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=275313&r1=275312&r2=275313&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Wed Jul 13 15:35:26 2016
@@ -2309,6 +2309,11 @@ ASTReader::ReadControlBlock(ModuleFile &
         Diag(diag::err_pch_with_compiler_errors);
         return HadErrors;
       }
+      if (hasErrors) {
+        Diags.ErrorOccurred = true;
+        Diags.UncompilableErrorOccurred = true;
+        Diags.UnrecoverableErrorOccurred = true;
+      }
 
       F.RelocatablePCH = Record[4];
       // Relative paths in a relocatable PCH are relative to our sysroot.

Modified: cfe/trunk/lib/Serialization/GeneratePCH.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/GeneratePCH.cpp?rev=275313&r1=275312&r2=275313&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/GeneratePCH.cpp (original)
+++ cfe/trunk/lib/Serialization/GeneratePCH.cpp Wed Jul 13 15:35:26 2016
@@ -51,7 +51,10 @@ void PCHGenerator::HandleTranslationUnit
   // Emit the PCH file to the Buffer.
   assert(SemaPtr && "No Sema?");
   Buffer->Signature =
-      Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot, hasErrors);
+      Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
+                      // For serialization we are lenient if the errors were
+                      // only warn-as-error kind.
+                      PP.getDiagnostics().hasUncompilableErrorOccurred());
 
   Buffer->IsComplete = true;
 }

Added: cfe/trunk/test/Index/pch-warn-as-error-code-split.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/pch-warn-as-error-code-split.cpp?rev=275313&view=auto
==============================================================================
--- cfe/trunk/test/Index/pch-warn-as-error-code-split.cpp (added)
+++ cfe/trunk/test/Index/pch-warn-as-error-code-split.cpp Wed Jul 13 15:35:26 2016
@@ -0,0 +1,17 @@
+// RUN: CINDEXTEST_EDITING=1 c-index-test -test-load-source local %s -Wuninitialized -Werror=unused 2>&1 | FileCheck -check-prefix=DIAGS %s
+
+// Make sure -Wuninitialized works even though the header had a warn-as-error occurrence.
+
+// DIAGS: error: unused variable 'x'
+// DIAGS: warning: variable 'x1' is uninitialized
+// DIAGS-NOT: error: use of undeclared identifier
+// DIAGS: warning: variable 'x1' is uninitialized
+
+#include "pch-warn-as-error-code-split.h"
+
+void test() {
+  int x1; // expected-note {{initialize}}
+  int x2 = x1; // expected-warning {{uninitialized}}
+  (void)x2;
+  foo_head();
+}

Added: cfe/trunk/test/Index/pch-warn-as-error-code-split.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/pch-warn-as-error-code-split.h?rev=275313&view=auto
==============================================================================
--- cfe/trunk/test/Index/pch-warn-as-error-code-split.h (added)
+++ cfe/trunk/test/Index/pch-warn-as-error-code-split.h Wed Jul 13 15:35:26 2016
@@ -0,0 +1,4 @@
+
+static void foo_head() {
+  int x;
+}

Added: cfe/trunk/test/Index/pch-warn-as-error-code.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/pch-warn-as-error-code.cpp?rev=275313&view=auto
==============================================================================
--- cfe/trunk/test/Index/pch-warn-as-error-code.cpp (added)
+++ cfe/trunk/test/Index/pch-warn-as-error-code.cpp Wed Jul 13 15:35:26 2016
@@ -0,0 +1,27 @@
+// RUN: rm -f %t.head.h.pch
+// RUN: c-index-test -write-pch %t.head.h.pch %s -Wuninitialized -Werror=unused 2>&1 | FileCheck -check-prefix=HEAD_DIAGS %s
+// RUN: c-index-test -test-load-source local %s -include %t.head.h -Wuninitialized -Werror=unused 2>&1 | FileCheck -check-prefix=MAIN_DIAGS %s
+
+// Make sure -Wuninitialized works even though the header had a warn-as-error occurrence.
+
+// HEAD_DIAGS: error: unused variable 'x'
+// MAIN_DIAGS: warning: variable 'x1' is uninitialized
+// MAIN_DIAGS-NOT: error: use of undeclared identifier
+
+#ifndef HEADER
+#define HEADER
+
+static void foo_head() {
+  int x;
+}
+
+#else
+
+void test() {
+  int x1; // expected-note {{initialize}}
+  int x2 = x1; // expected-warning {{uninitialized}}
+  (void)x2;
+  foo_head();
+}
+
+#endif

Added: cfe/trunk/test/PCH/chain-invalid-code.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/chain-invalid-code.cpp?rev=275313&view=auto
==============================================================================
--- cfe/trunk/test/PCH/chain-invalid-code.cpp (added)
+++ cfe/trunk/test/PCH/chain-invalid-code.cpp Wed Jul 13 15:35:26 2016
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -fsyntax-only %s -chain-include %s -Wuninitialized -Wunused -verify
+
+// Make sure there is no crash.
+
+#ifndef HEADER
+#define HEADER
+
+#include "non-existent-header.h"
+
+class A {
+public:
+  ~A();
+};
+
+class ForwardCls;
+struct B {
+  ForwardCls f;
+  A a;
+};
+
+#else
+
+static void test() {
+  int x; // expected-warning {{unused}}
+  B b;
+}
+
+#endif




More information about the cfe-commits mailing list