Enhance analyzer reasoning about sending messages to nil.  A nil receiver returns 0 for scalars of size <= sizeof(void*).


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68629 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/GRExprEngine.cpp b/lib/Analysis/GRExprEngine.cpp
index 06d61cc..b2dfe68 100644
--- a/lib/Analysis/GRExprEngine.cpp
+++ b/lib/Analysis/GRExprEngine.cpp
@@ -1682,7 +1682,7 @@
     
     // "Assume" that the receiver is not NULL.    
     bool isFeasibleNotNull = false;
-    Assume(state, L, true, isFeasibleNotNull);
+    const GRState *StNotNull = Assume(state, L, true, isFeasibleNotNull);
     
     // "Assume" that the receiver is NULL.    
     bool isFeasibleNull = false;
@@ -1724,8 +1724,21 @@
                 NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
             }
           }
+          else {
+            // Handle the safe cases where the return value is 0 if the receiver
+            // is nil.
+            SVal V = SVal::MakeZero(getBasicVals(), ME->getType());
+            MakeNode(Dst, ME, Pred, BindExpr(StNull, ME, V));
+          }
         }
       }
+      
+      // We have handled the cases where the receiver is nil.  The remainder
+      // of this method should assume that the receiver is not nil.      
+      if (!isFeasibleNotNull)
+        return;
+
+      state = StNotNull;
     }
     
     // Check if the "raise" message was sent.
@@ -2445,7 +2458,7 @@
             //    transfer functions as "0 == E".
             
             if (isa<Loc>(V)) {
-              loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
+              Loc X = Loc::MakeNull(getBasicVals());
               SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X,
                                       U->getType());
               state = BindExpr(state, U, Result);
diff --git a/lib/Analysis/SVals.cpp b/lib/Analysis/SVals.cpp
index b642857..c5900b3 100644
--- a/lib/Analysis/SVals.cpp
+++ b/lib/Analysis/SVals.cpp
@@ -202,6 +202,22 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Utility methods for constructing SVals.
+//===----------------------------------------------------------------------===//
+
+SVal SVal::MakeZero(BasicValueFactory &BasicVals, QualType T) {
+  if (Loc::IsLocType(T))
+    return Loc::MakeNull(BasicVals);
+
+  if (T->isIntegerType())
+    return NonLoc::MakeVal(BasicVals, 0, T);
+  
+  // FIXME: Handle floats.
+  // FIXME: Handle structs.
+  return UnknownVal();  
+}
+
+//===----------------------------------------------------------------------===//
 // Utility methods for constructing Non-Locs.
 //===----------------------------------------------------------------------===//
 
@@ -314,6 +330,10 @@
 
 Loc Loc::MakeVal(SymbolRef sym) { return loc::SymbolVal(sym); }
 
+Loc Loc::MakeNull(BasicValueFactory &BasicVals) {
+  return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth());
+}
+
 //===----------------------------------------------------------------------===//
 // Pretty-Printing.
 //===----------------------------------------------------------------------===//