[Sema] Avoid instantiating templates only when UncompilableErrorOccurred
authorAkira Hatanaka <ahatanaka@apple.com>
Thu, 3 Nov 2016 15:04:58 +0000 (15:04 +0000)
committerAkira Hatanaka <ahatanaka@apple.com>
Thu, 3 Nov 2016 15:04:58 +0000 (15:04 +0000)
and FatalErrorOccurred are both set.

This fixes a crash that occurs when a warning promoted to a fatal error
leaves the AST in an incomplete state, and then later CFG analysis is
run on the incomplete AST.

rdar://problem/28558923

Differential Revision: https://reviews.llvm.org/D26166

llvm-svn: 285923

clang/lib/Sema/SemaTemplateInstantiate.cpp
clang/test/SemaCXX/instantiate-template-fatal-error.cpp [new file with mode: 0644]

index b715c13b077340fcb6b33e3e2221730009096fc2..6e715d25b4ddef77e1ba3721cd6da42706f36746 100644 (file)
@@ -209,9 +209,11 @@ Sema::InstantiatingTemplate::InstantiatingTemplate(
     sema::TemplateDeductionInfo *DeductionInfo)
     : SemaRef(SemaRef), SavedInNonInstantiationSFINAEContext(
                             SemaRef.InNonInstantiationSFINAEContext) {
-  // Don't allow further instantiation if a fatal error has occcured.  Any
-  // diagnostics we might have raised will not be visible.
-  if (SemaRef.Diags.hasFatalErrorOccurred()) {
+  // Don't allow further instantiation if a fatal error and an uncompilable
+  // error have occcured. Any diagnostics we might have raised will not be
+  // visible, and we do not need to construct a correct AST.
+  if (SemaRef.Diags.hasFatalErrorOccurred() &&
+      SemaRef.Diags.hasUncompilableErrorOccurred()) {
     Invalid = true;
     return;
   }
diff --git a/clang/test/SemaCXX/instantiate-template-fatal-error.cpp b/clang/test/SemaCXX/instantiate-template-fatal-error.cpp
new file mode 100644 (file)
index 0000000..9c8916e
--- /dev/null
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
+
+#pragma clang diagnostic fatal "-Wall"
+#pragma clang diagnostic fatal "-Wold-style-cast"
+
+template <class T> bool foo0(const long long *a, T* b) {
+  return a == (const long long*)b; // expected-error {{use of old-style cast}}
+}
+
+template<class T>
+struct S1 {
+};
+
+template<class T>
+struct S2 : S1<T> {
+  bool m1(const long long *a, T *b) const { return foo0(a, b); }
+};
+
+bool foo1(const long long *a, int *b) {
+  S2<int> s2;
+  return s2.m1(a, b);
+}