r203029 - [-Wunreachable-code] generalize pruning out warning on trivial returns.

Ted Kremenek kremenek at apple.com
Wed Mar 5 15:46:07 PST 2014


Author: kremenek
Date: Wed Mar  5 17:46:07 2014
New Revision: 203029

URL: http://llvm.org/viewvc/llvm-project?rev=203029&view=rev
Log:
[-Wunreachable-code] generalize pruning out warning on trivial returns.

Previously we only pruned dead returns preceded by a call to a
'noreturn' function.  After looking at the results of the LLVM codebase,
there are many others that should be pruned as well.

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

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=203029&r1=203028&r2=203029&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Wed Mar  5 17:46:07 2014
@@ -291,12 +291,12 @@ static bool isEnumConstant(const Expr *E
 }
 
 static bool isTrivialExpression(const Expr *Ex) {
+  Ex = Ex->IgnoreParenCasts();
   return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) ||
          isEnumConstant(Ex);
 }
 
-static bool isTrivialReturnPrecededByNoReturn(const CFGBlock *B,
-                                              const Stmt *S) {
+static bool isTrivialReturn(const CFGBlock *B, const Stmt *S) {
   if (B->pred_empty())
     return false;
 
@@ -304,8 +304,6 @@ static bool isTrivialReturnPrecededByNoR
   if (!Ex)
     return false;
 
-  Ex = Ex->IgnoreParenCasts();
-
   if (!isTrivialExpression(Ex))
     return false;
 
@@ -319,14 +317,13 @@ static bool isTrivialReturnPrecededByNoR
       if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
         const Expr *RE = RS->getRetValue();
         if (RE && RE->IgnoreParenCasts() == Ex)
-          break;
+          return true;
       }
-      return false;
+      break;
     }
   }
 
-  assert(B->pred_size() == 1);
-  return bodyEndsWithNoReturn(*B->pred_begin());
+  return false;
 }
 
 void DeadCodeScan::reportDeadCode(const CFGBlock *B,
@@ -339,7 +336,7 @@ void DeadCodeScan::reportDeadCode(const
     return;
 
   // Suppress trivial 'return' statements that are dead.
-  if (isTrivialReturnPrecededByNoReturn(B, S))
+  if (isTrivialReturn(B, S))
     return;
 
   SourceRange R1, R2;

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=203029&r1=203028&r2=203029&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Wed Mar  5 17:46:07 2014
@@ -204,6 +204,27 @@ MyEnum trival_dead_return_enum() {
   return Value1; // no-warning
 }
 
+MyEnum trivial_dead_return_enum_2(int x) {
+  switch (x) {
+    case 1: return 1;
+    case 2: return 2;
+    case 3: return 3;
+  }
+
+  return 2; // no-warning
+}
+
+MyEnum nontrivial_dead_return_enum_2(int x) {
+  switch (x) {
+    case 1: return 1;
+    case 2: return 2;
+    case 3: return 3;
+    default: return 4;
+  }
+
+  return calledFun(); // expected-warning {{will never be executed}}
+}
+
 // Test unreachable code depending on configuration values
 #define CONFIG_CONSTANT 1
 int test_config_constant(int x) {
@@ -235,7 +256,7 @@ int sizeof_int() {
   return 2; // no-warning
 }
 
-enum MyEnum {
+enum MyEnum2 {
   ME_A = CONFIG_CONSTANT,
   ME_B = 1
 };





More information about the cfe-commits mailing list