[cfe-commits] r145520 - in /cfe/trunk: lib/Sema/AnalysisBasedWarnings.cpp test/SemaCXX/warn-unreachable.cpp

Ted Kremenek kremenek at apple.com
Wed Nov 30 13:22:09 PST 2011


Author: kremenek
Date: Wed Nov 30 15:22:09 2011
New Revision: 145520

URL: http://llvm.org/viewvc/llvm-project?rev=145520&view=rev
Log:
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.

Modified:
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
    cfe/trunk/test/SemaCXX/warn-unreachable.cpp

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=145520&r1=145519&r2=145520&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Wed Nov 30 15:22:09 2011
@@ -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) {

Modified: cfe/trunk/test/SemaCXX/warn-unreachable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unreachable.cpp?rev=145520&r1=145519&r2=145520&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unreachable.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unreachable.cpp Wed Nov 30 15:22:09 2011
@@ -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>(); 
+}
+





More information about the cfe-commits mailing list