Don't run -Wunreachable-code on template instantiations. Different instantiations may produce different unreachable code results, and it is very difficult for us to prove that ALL instantiations of a template have specific unreachable code. If we come up with a better solution, then we can revisit this, but this approach will at least greatly reduce the noise of this warning for code that makes use of templates.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145520 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/AnalysisBasedWarnings.cpp b/lib/Sema/AnalysisBasedWarnings.cpp
index 8b2e269..54a26b3 100644
--- a/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/lib/Sema/AnalysisBasedWarnings.cpp
@@ -914,8 +914,14 @@
}
// Warning: check for unreachable code
- if (P.enableCheckUnreachable)
- CheckUnreachable(S, AC);
+ if (P.enableCheckUnreachable) {
+ // Only check for unreachable code on non-template instantiations.
+ // Different template instantiations can effectively change the control-flow
+ // and it is very difficult to prove that a snippet of code in a template
+ // is unreachable for all instantiations.
+ if (S.ActiveTemplateInstantiations.empty())
+ CheckUnreachable(S, AC);
+ }
// Check for thread safety violations
if (P.enableThreadSafetyAnalysis) {
diff --git a/test/SemaCXX/warn-unreachable.cpp b/test/SemaCXX/warn-unreachable.cpp
index 604a3c0..fa740ba 100644
--- a/test/SemaCXX/warn-unreachable.cpp
+++ b/test/SemaCXX/warn-unreachable.cpp
@@ -76,3 +76,25 @@
S
(halt()); // expected-warning {{will never be executed}}
}
+
+// Don't warn about unreachable code in template instantiations, as
+// they may only be unreachable in that specific instantiation.
+void isUnreachable();
+
+template <typename T> void test_unreachable_templates() {
+ T::foo();
+ isUnreachable(); // no-warning
+}
+
+struct TestUnreachableA {
+ static void foo() __attribute__((noreturn));
+};
+struct TestUnreachableB {
+ static void foo();
+};
+
+void test_unreachable_templates_harness() {
+ test_unreachable_templates<TestUnreachableA>();
+ test_unreachable_templates<TestUnreachableB>();
+}
+