]> granicus.if.org Git - clang/commitdiff
[-Wunreachable-code] Correctly expand artificial reachability to pruned '&&' and...
authorTed Kremenek <kremenek@apple.com>
Fri, 7 Mar 2014 02:25:53 +0000 (02:25 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 7 Mar 2014 02:25:53 +0000 (02:25 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203194 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFG.cpp
lib/Analysis/ReachableCode.cpp
test/Sema/warn-unreachable.c

index 1ed27e61cb0d31e45256fe209d9f1d046d573e6f..f11db625cd8ecd3f19618ae604d514fcd5032fdf 100644 (file)
@@ -1326,8 +1326,8 @@ CFGBuilder::VisitLogicalOperator(BinaryOperator *B,
     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(BinaryOperator *B,
 
   // 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);
index fa31dc1b0682a330dcb2c9865d16f6d9f03720f8..09abcc9b7dcb881fbbd4ec105000deedd6d3bff0 100644 (file)
@@ -459,9 +459,13 @@ static bool isConfigurationValue(const Stmt *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 CFGBlock *Start,
     // 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 CFGBlock *Start,
         if (!UB)
           break;
 
-        if (TreatAllSuccessorsAsReachable) {
+        if (!TreatAllSuccessorsAsReachable.hasValue())
+          TreatAllSuccessorsAsReachable =
+            shouldTreatSuccessorsAsReachable(item);
+
+        if (TreatAllSuccessorsAsReachable.getValue()) {
           B = UB;
           break;
         }
index 4df2ec6dcc6a24a4e40688798d9cca81b36967e4..f32134bbdb1eea196d947e46624b705d5bdf5880 100644 (file)
@@ -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
 }