r203051 - [-Wunreachable-code] Handle idiomatic do...while() with an uninteresting condition.

Ted Kremenek kremenek at apple.com
Wed Mar 5 17:09:45 PST 2014


Author: kremenek
Date: Wed Mar  5 19:09:45 2014
New Revision: 203051

URL: http://llvm.org/viewvc/llvm-project?rev=203051&view=rev
Log:
[-Wunreachable-code] Handle idiomatic do...while() with an uninteresting condition.

Sometimes do..while() is used to create a scope that can be left early.
In such cases, the unreachable 'while()' test is not usually interesting
unless it actually does something that is observable.

Modified:
    cfe/trunk/lib/Analysis/ReachableCode.cpp
    cfe/trunk/test/Sema/warn-unreachable.c
    cfe/trunk/test/SemaCXX/unreachable-code.cpp

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=203051&r1=203050&r2=203051&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Wed Mar  5 19:09:45 2014
@@ -312,7 +312,8 @@ static bool isTrivialReturnOrDoWhile(con
         return true;
   }
 
-
+  if (B->pred_size() != 1)
+    return false;
 
   // Look to see if the block ends with a 'return', and see if 'S'
   // is a substatement.  The 'return' may not be the last element in
@@ -324,15 +325,11 @@ static bool isTrivialReturnOrDoWhile(con
       if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
         const Expr *RE = RS->getRetValue();
         if (RE && RE->IgnoreParenCasts() == Ex)
-          break;
+          return bodyEndsWithNoReturn(*B->pred_begin());
       }
-      return false;
+      break;
     }
   }
-
-  if (B->pred_size() == 1)
-    return bodyEndsWithNoReturn(*B->pred_begin());
-
   return false;
 }
 

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=203051&r1=203050&r2=203051&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Wed Mar  5 19:09:45 2014
@@ -209,9 +209,10 @@ MyEnum trivial_dead_return_enum_2(int x)
     case 1: return 1;
     case 2: return 2;
     case 3: return 3;
+    default: return 4;
   }
 
-  return 2; // no-warning
+  return 2; // expected-warning {{will never be executed}}
 }
 
 MyEnum nontrivial_dead_return_enum_2(int x) {

Modified: cfe/trunk/test/SemaCXX/unreachable-code.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/unreachable-code.cpp?rev=203051&r1=203050&r2=203051&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/unreachable-code.cpp (original)
+++ cfe/trunk/test/SemaCXX/unreachable-code.cpp Wed Mar  5 19:09:45 2014
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -Wunreachable-code -fblocks -verify %s
 
 int j;
-void bar() { }
+int bar();
 int test1() {
   for (int i = 0;
        i != 10;
@@ -11,7 +11,19 @@ int test1() {
       return 1;
   }
   return 0;
-  return 1;    // expected-warning {{will never be executed}}
+  return 1; // expected-warning {{will never be executed}}
+}
+
+int test1_B() {
+  for (int i = 0;
+       i != 10;
+       ++i) {  // expected-warning {{will never be executed}}
+    if (j == 23) // missing {}'s
+      bar();
+      return 1;
+  }
+  return 0;
+  return bar(); // expected-warning {{will never be executed}}
 }
 
 void test2(int i) {





More information about the cfe-commits mailing list