]> granicus.if.org Git - clang/commitdiff
Don't run -Wunreachable-code on template instantiations. Different instantiations...
authorTed Kremenek <kremenek@apple.com>
Wed, 30 Nov 2011 21:22:09 +0000 (21:22 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 30 Nov 2011 21:22:09 +0000 (21:22 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145520 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/AnalysisBasedWarnings.cpp
test/SemaCXX/warn-unreachable.cpp

index 8b2e269708b2656f938d400426752bf161ba3fc6..54a26b3253ab2082a923233665a21040264b391e 100644 (file)
@@ -914,8 +914,14 @@ AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P,
   }
 
   // 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) {
index 604a3c0da38167d7e5930790cae8ee7e2bb4256e..fa740bac65821dd5b45c1c7d1c61839b89611b3d 100644 (file)
@@ -76,3 +76,25 @@ void test6() {
     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>(); 
+}
+