From: Argyrios Kyrtzidis Date: Wed, 13 Jul 2016 20:35:26 +0000 (+0000) Subject: [PCH/preamble] Make sure that if the preamble/PCH was serialized with errors that... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dc0545b300c137ece52da033095c277b6cd81467;p=clang [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@275313 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Basic/DiagnosticIDs.cpp b/lib/Basic/DiagnosticIDs.cpp index efc3f94c95..3c370f67fa 100644 --- a/lib/Basic/DiagnosticIDs.cpp +++ b/lib/Basic/DiagnosticIDs.cpp @@ -351,7 +351,7 @@ bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) { 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 diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 635d566159..e0bf1c51c8 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -2509,7 +2509,8 @@ static bool serializeUnit(ASTWriter &Writer, } 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, diff --git a/lib/Frontend/ChainedIncludesSource.cpp b/lib/Frontend/ChainedIncludesSource.cpp index 1c1081fbe0..51771bfb54 100644 --- a/lib/Frontend/ChainedIncludesSource.cpp +++ b/lib/Frontend/ChainedIncludesSource.cpp @@ -164,7 +164,7 @@ IntrusiveRefCntPtr clang::createChainedIncludesSource( ArrayRef> Extensions; auto consumer = llvm::make_unique( Clang->getPreprocessor(), "-", nullptr, /*isysroot=*/"", Buffer, - Extensions); + Extensions, /*AllowASTWithErrors=*/true); Clang->getASTContext().setASTMutationListener( consumer->GetASTMutationListener()); Clang->setASTConsumer(std::move(consumer)); diff --git a/lib/Sema/AnalysisBasedWarnings.cpp b/lib/Sema/AnalysisBasedWarnings.cpp index a43513530e..67762bde34 100644 --- a/lib/Sema/AnalysisBasedWarnings.cpp +++ b/lib/Sema/AnalysisBasedWarnings.cpp @@ -1897,7 +1897,7 @@ AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, if (cast(D)->isDependentContext()) return; - if (Diags.hasUncompilableErrorOccurred() || Diags.hasFatalErrorOccurred()) { + if (Diags.hasUncompilableErrorOccurred()) { // Flush out any possibly unreachable diagnostics. flushDiagnostics(S, fscope); return; diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 820f339a75..b35bd7bd32 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -2309,6 +2309,11 @@ ASTReader::ReadControlBlock(ModuleFile &F, 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. diff --git a/lib/Serialization/GeneratePCH.cpp b/lib/Serialization/GeneratePCH.cpp index 4a2255ab6d..308fde8b1d 100644 --- a/lib/Serialization/GeneratePCH.cpp +++ b/lib/Serialization/GeneratePCH.cpp @@ -51,7 +51,10 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { // 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; } diff --git a/test/Index/pch-warn-as-error-code-split.cpp b/test/Index/pch-warn-as-error-code-split.cpp new file mode 100644 index 0000000000..115c9e3d31 --- /dev/null +++ b/test/Index/pch-warn-as-error-code-split.cpp @@ -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(); +} diff --git a/test/Index/pch-warn-as-error-code-split.h b/test/Index/pch-warn-as-error-code-split.h new file mode 100644 index 0000000000..5893ee2a3f --- /dev/null +++ b/test/Index/pch-warn-as-error-code-split.h @@ -0,0 +1,4 @@ + +static void foo_head() { + int x; +} diff --git a/test/Index/pch-warn-as-error-code.cpp b/test/Index/pch-warn-as-error-code.cpp new file mode 100644 index 0000000000..6a7924a09e --- /dev/null +++ b/test/Index/pch-warn-as-error-code.cpp @@ -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 diff --git a/test/PCH/chain-invalid-code.cpp b/test/PCH/chain-invalid-code.cpp new file mode 100644 index 0000000000..9de88f0cee --- /dev/null +++ b/test/PCH/chain-invalid-code.cpp @@ -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