r203194 - [-Wunreachable-code] Correctly expand artificial reachability to pruned '&&' and '||' branches involving configuration values.

Ted Kremenek kremenek at apple.com
Thu Mar 6 18:25:53 PST 2014


Author: kremenek
Date: Thu Mar  6 20:25:53 2014
New Revision: 203194

URL: http://llvm.org/viewvc/llvm-project?rev=203194&view=rev
Log:
[-Wunreachable-code] Correctly expand artificial reachability to pruned '&&' and '||' branches involving configuration values.

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

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=203194&r1=203193&r2=203194&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Thu Mar  6 20:25:53 2014
@@ -1326,8 +1326,8 @@ CFGBuilder::VisitLogicalOperator(BinaryO
     else {
       RHSBlock->setTerminator(Term);
       TryResult KnownVal = tryEvaluateBool(RHS);
-      addSuccessor(RHSBlock, KnownVal.isFalse() ? NULL : TrueBlock);
-      addSuccessor(RHSBlock, KnownVal.isTrue() ? NULL : FalseBlock);
+      addSuccessor(RHSBlock, TrueBlock, !KnownVal.isFalse());
+      addSuccessor(RHSBlock, FalseBlock, !KnownVal.isTrue());
     }
 
     Block = RHSBlock;
@@ -1370,12 +1370,12 @@ CFGBuilder::VisitLogicalOperator(BinaryO
 
   // Now link the LHSBlock with RHSBlock.
   if (B->getOpcode() == BO_LOr) {
-    addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : TrueBlock);
-    addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : RHSBlock);
+    addSuccessor(LHSBlock, TrueBlock, !KnownVal.isFalse());
+    addSuccessor(LHSBlock, RHSBlock, !KnownVal.isTrue());
   } else {
     assert(B->getOpcode() == BO_LAnd);
-    addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
-    addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : FalseBlock);
+    addSuccessor(LHSBlock, RHSBlock, !KnownVal.isFalse());
+    addSuccessor(LHSBlock, FalseBlock, !KnownVal.isTrue());
   }
 
   return std::make_pair(EntryLHSBlock, ExitBlock);

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=203194&r1=203193&r2=203194&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Thu Mar  6 20:25:53 2014
@@ -459,9 +459,13 @@ static bool isConfigurationValue(const S
 
 /// Returns true if we should always explore all successors of a block.
 static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B) {
-  if (const Stmt *Term = B->getTerminator())
+  if (const Stmt *Term = B->getTerminator()) {
     if (isa<SwitchStmt>(Term))
       return true;
+    // Specially handle '||' and '&&'.
+    if (isa<BinaryOperator>(Term))
+      return isConfigurationValue(Term);
+  }
 
   return isConfigurationValue(B->getTerminatorCondition());
 }
@@ -491,9 +495,9 @@ unsigned ScanReachableFromBlock(const CF
     // and that we should forge ahead and explore those branches anyway.
     // This allows us to potentially uncover some "always unreachable" code
     // within the "sometimes unreachable" code.
-    bool TreatAllSuccessorsAsReachable = shouldTreatSuccessorsAsReachable(item);
-
     // Look at the successors and mark then reachable.
+    Optional<bool> TreatAllSuccessorsAsReachable;
+
     for (CFGBlock::const_succ_iterator I = item->succ_begin(), 
          E = item->succ_end(); I != E; ++I) {
       const CFGBlock *B = *I;
@@ -502,7 +506,11 @@ unsigned ScanReachableFromBlock(const CF
         if (!UB)
           break;
 
-        if (TreatAllSuccessorsAsReachable) {
+        if (!TreatAllSuccessorsAsReachable.hasValue())
+          TreatAllSuccessorsAsReachable =
+            shouldTreatSuccessorsAsReachable(item);
+
+        if (TreatAllSuccessorsAsReachable.getValue()) {
           B = UB;
           break;
         }

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=203194&r1=203193&r2=203194&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Thu Mar  6 20:25:53 2014
@@ -275,8 +275,8 @@ int sizeof_int(int x, int y) {
     return 1; // no-warning
   if (sizeof(long) != sizeof(int))
     return 0; // no-warning
-  if (x && y && sizeof(long) > sizeof(int))
-    return 0;
+  if (x && y && sizeof(long) < sizeof(char))
+    return 0; // no-warning
   return 2; // no-warning
 }
 





More information about the cfe-commits mailing list