ExprEngineC.cpp revision 249423
1226586Sdim//=-- ExprEngineC.cpp - ExprEngine support for C expressions ----*- C++ -*-===//
2226586Sdim//
3226586Sdim//                     The LLVM Compiler Infrastructure
4226586Sdim//
5226586Sdim// This file is distributed under the University of Illinois Open Source
6226586Sdim// License. See LICENSE.TXT for details.
7226586Sdim//
8226586Sdim//===----------------------------------------------------------------------===//
9226586Sdim//
10226586Sdim//  This file defines ExprEngine's support for C expressions.
11226586Sdim//
12226586Sdim//===----------------------------------------------------------------------===//
13226586Sdim
14249423Sdim#include "clang/AST/ExprCXX.h"
15226586Sdim#include "clang/StaticAnalyzer/Core/CheckerManager.h"
16226586Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
17226586Sdim
18226586Sdimusing namespace clang;
19226586Sdimusing namespace ento;
20226586Sdimusing llvm::APSInt;
21226586Sdim
22226586Sdimvoid ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
23226586Sdim                                     ExplodedNode *Pred,
24226586Sdim                                     ExplodedNodeSet &Dst) {
25226586Sdim
26226586Sdim  Expr *LHS = B->getLHS()->IgnoreParens();
27226586Sdim  Expr *RHS = B->getRHS()->IgnoreParens();
28226586Sdim
29226586Sdim  // FIXME: Prechecks eventually go in ::Visit().
30226586Sdim  ExplodedNodeSet CheckedSet;
31226586Sdim  ExplodedNodeSet Tmp2;
32226586Sdim  getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, B, *this);
33226586Sdim
34226586Sdim  // With both the LHS and RHS evaluated, process the operation itself.
35226586Sdim  for (ExplodedNodeSet::iterator it=CheckedSet.begin(), ei=CheckedSet.end();
36226586Sdim         it != ei; ++it) {
37226586Sdim
38234353Sdim    ProgramStateRef state = (*it)->getState();
39234353Sdim    const LocationContext *LCtx = (*it)->getLocationContext();
40234353Sdim    SVal LeftV = state->getSVal(LHS, LCtx);
41234353Sdim    SVal RightV = state->getSVal(RHS, LCtx);
42226586Sdim
43226586Sdim    BinaryOperator::Opcode Op = B->getOpcode();
44226586Sdim
45226586Sdim    if (Op == BO_Assign) {
46226586Sdim      // EXPERIMENTAL: "Conjured" symbols.
47226586Sdim      // FIXME: Handle structs.
48234353Sdim      if (RightV.isUnknown()) {
49243830Sdim        unsigned Count = currBldrCtx->blockCount();
50243830Sdim        RightV = svalBuilder.conjureSymbolVal(0, B->getRHS(), LCtx, Count);
51226586Sdim      }
52226586Sdim      // Simulate the effects of a "store":  bind the value of the RHS
53226586Sdim      // to the L-Value represented by the LHS.
54239462Sdim      SVal ExprVal = B->isGLValue() ? LeftV : RightV;
55234353Sdim      evalStore(Tmp2, B, LHS, *it, state->BindExpr(B, LCtx, ExprVal),
56234353Sdim                LeftV, RightV);
57226586Sdim      continue;
58226586Sdim    }
59226586Sdim
60226586Sdim    if (!B->isAssignmentOp()) {
61243830Sdim      StmtNodeBuilder Bldr(*it, Tmp2, *currBldrCtx);
62239462Sdim
63239462Sdim      if (B->isAdditiveOp()) {
64239462Sdim        // If one of the operands is a location, conjure a symbol for the other
65239462Sdim        // one (offset) if it's unknown so that memory arithmetic always
66239462Sdim        // results in an ElementRegion.
67239462Sdim        // TODO: This can be removed after we enable history tracking with
68239462Sdim        // SymSymExpr.
69243830Sdim        unsigned Count = currBldrCtx->blockCount();
70249423Sdim        if (LeftV.getAs<Loc>() &&
71239462Sdim            RHS->getType()->isIntegerType() && RightV.isUnknown()) {
72243830Sdim          RightV = svalBuilder.conjureSymbolVal(RHS, LCtx, RHS->getType(),
73243830Sdim                                                Count);
74239462Sdim        }
75249423Sdim        if (RightV.getAs<Loc>() &&
76239462Sdim            LHS->getType()->isIntegerType() && LeftV.isUnknown()) {
77243830Sdim          LeftV = svalBuilder.conjureSymbolVal(LHS, LCtx, LHS->getType(),
78243830Sdim                                               Count);
79239462Sdim        }
80239462Sdim      }
81239462Sdim
82226586Sdim      // Process non-assignments except commas or short-circuited
83226586Sdim      // logical expressions (LAnd and LOr).
84226586Sdim      SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
85226586Sdim      if (Result.isUnknown()) {
86234353Sdim        Bldr.generateNode(B, *it, state);
87226586Sdim        continue;
88226586Sdim      }
89226586Sdim
90234353Sdim      state = state->BindExpr(B, LCtx, Result);
91234353Sdim      Bldr.generateNode(B, *it, state);
92226586Sdim      continue;
93226586Sdim    }
94226586Sdim
95226586Sdim    assert (B->isCompoundAssignmentOp());
96226586Sdim
97226586Sdim    switch (Op) {
98226586Sdim      default:
99226586Sdim        llvm_unreachable("Invalid opcode for compound assignment.");
100226586Sdim      case BO_MulAssign: Op = BO_Mul; break;
101226586Sdim      case BO_DivAssign: Op = BO_Div; break;
102226586Sdim      case BO_RemAssign: Op = BO_Rem; break;
103226586Sdim      case BO_AddAssign: Op = BO_Add; break;
104226586Sdim      case BO_SubAssign: Op = BO_Sub; break;
105226586Sdim      case BO_ShlAssign: Op = BO_Shl; break;
106226586Sdim      case BO_ShrAssign: Op = BO_Shr; break;
107226586Sdim      case BO_AndAssign: Op = BO_And; break;
108226586Sdim      case BO_XorAssign: Op = BO_Xor; break;
109226586Sdim      case BO_OrAssign:  Op = BO_Or;  break;
110226586Sdim    }
111226586Sdim
112226586Sdim    // Perform a load (the LHS).  This performs the checks for
113226586Sdim    // null dereferences, and so on.
114226586Sdim    ExplodedNodeSet Tmp;
115226586Sdim    SVal location = LeftV;
116234353Sdim    evalLoad(Tmp, B, LHS, *it, state, location);
117226586Sdim
118226586Sdim    for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E;
119226586Sdim         ++I) {
120226586Sdim
121226586Sdim      state = (*I)->getState();
122234353Sdim      const LocationContext *LCtx = (*I)->getLocationContext();
123234353Sdim      SVal V = state->getSVal(LHS, LCtx);
124226586Sdim
125226586Sdim      // Get the computation type.
126226586Sdim      QualType CTy =
127226586Sdim        cast<CompoundAssignOperator>(B)->getComputationResultType();
128226586Sdim      CTy = getContext().getCanonicalType(CTy);
129226586Sdim
130226586Sdim      QualType CLHSTy =
131226586Sdim        cast<CompoundAssignOperator>(B)->getComputationLHSType();
132226586Sdim      CLHSTy = getContext().getCanonicalType(CLHSTy);
133226586Sdim
134226586Sdim      QualType LTy = getContext().getCanonicalType(LHS->getType());
135226586Sdim
136226586Sdim      // Promote LHS.
137226586Sdim      V = svalBuilder.evalCast(V, CLHSTy, LTy);
138226586Sdim
139226586Sdim      // Compute the result of the operation.
140226586Sdim      SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy),
141226586Sdim                                         B->getType(), CTy);
142226586Sdim
143226586Sdim      // EXPERIMENTAL: "Conjured" symbols.
144226586Sdim      // FIXME: Handle structs.
145226586Sdim
146226586Sdim      SVal LHSVal;
147226586Sdim
148234353Sdim      if (Result.isUnknown()) {
149226586Sdim        // The symbolic value is actually for the type of the left-hand side
150226586Sdim        // expression, not the computation type, as this is the value the
151226586Sdim        // LValue on the LHS will bind to.
152243830Sdim        LHSVal = svalBuilder.conjureSymbolVal(0, B->getRHS(), LCtx, LTy,
153243830Sdim                                              currBldrCtx->blockCount());
154226586Sdim        // However, we need to convert the symbol to the computation type.
155226586Sdim        Result = svalBuilder.evalCast(LHSVal, CTy, LTy);
156226586Sdim      }
157226586Sdim      else {
158226586Sdim        // The left-hand side may bind to a different value then the
159226586Sdim        // computation type.
160226586Sdim        LHSVal = svalBuilder.evalCast(Result, LTy, CTy);
161226586Sdim      }
162226586Sdim
163226586Sdim      // In C++, assignment and compound assignment operators return an
164226586Sdim      // lvalue.
165239462Sdim      if (B->isGLValue())
166234353Sdim        state = state->BindExpr(B, LCtx, location);
167226586Sdim      else
168234353Sdim        state = state->BindExpr(B, LCtx, Result);
169226586Sdim
170226586Sdim      evalStore(Tmp2, B, LHS, *I, state, location, LHSVal);
171226586Sdim    }
172226586Sdim  }
173226586Sdim
174226586Sdim  // FIXME: postvisits eventually go in ::Visit()
175226586Sdim  getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
176226586Sdim}
177226586Sdim
178226586Sdimvoid ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
179226586Sdim                                ExplodedNodeSet &Dst) {
180226586Sdim
181226586Sdim  CanQualType T = getContext().getCanonicalType(BE->getType());
182239462Sdim
183239462Sdim  // Get the value of the block itself.
184226586Sdim  SVal V = svalBuilder.getBlockPointer(BE->getBlockDecl(), T,
185226586Sdim                                       Pred->getLocationContext());
186226586Sdim
187239462Sdim  ProgramStateRef State = Pred->getState();
188239462Sdim
189239462Sdim  // If we created a new MemRegion for the block, we should explicitly bind
190239462Sdim  // the captured variables.
191239462Sdim  if (const BlockDataRegion *BDR =
192239462Sdim      dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
193239462Sdim
194239462Sdim    BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
195239462Sdim                                              E = BDR->referenced_vars_end();
196239462Sdim
197239462Sdim    for (; I != E; ++I) {
198239462Sdim      const MemRegion *capturedR = I.getCapturedRegion();
199239462Sdim      const MemRegion *originalR = I.getOriginalRegion();
200239462Sdim      if (capturedR != originalR) {
201239462Sdim        SVal originalV = State->getSVal(loc::MemRegionVal(originalR));
202239462Sdim        State = State->bindLoc(loc::MemRegionVal(capturedR), originalV);
203239462Sdim      }
204239462Sdim    }
205239462Sdim  }
206239462Sdim
207226586Sdim  ExplodedNodeSet Tmp;
208243830Sdim  StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
209234353Sdim  Bldr.generateNode(BE, Pred,
210239462Sdim                    State->BindExpr(BE, Pred->getLocationContext(), V),
211243830Sdim                    0, ProgramPoint::PostLValueKind);
212226586Sdim
213226586Sdim  // FIXME: Move all post/pre visits to ::Visit().
214226586Sdim  getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this);
215226586Sdim}
216226586Sdim
217226586Sdimvoid ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
218226586Sdim                           ExplodedNode *Pred, ExplodedNodeSet &Dst) {
219226586Sdim
220226586Sdim  ExplodedNodeSet dstPreStmt;
221226586Sdim  getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);
222226586Sdim
223234353Sdim  if (CastE->getCastKind() == CK_LValueToRValue) {
224226586Sdim    for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
225226586Sdim         I!=E; ++I) {
226226586Sdim      ExplodedNode *subExprNode = *I;
227234353Sdim      ProgramStateRef state = subExprNode->getState();
228234353Sdim      const LocationContext *LCtx = subExprNode->getLocationContext();
229234353Sdim      evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx));
230226586Sdim    }
231226586Sdim    return;
232226586Sdim  }
233226586Sdim
234226586Sdim  // All other casts.
235226586Sdim  QualType T = CastE->getType();
236226586Sdim  QualType ExTy = Ex->getType();
237226586Sdim
238226586Sdim  if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
239226586Sdim    T = ExCast->getTypeAsWritten();
240226586Sdim
241243830Sdim  StmtNodeBuilder Bldr(dstPreStmt, Dst, *currBldrCtx);
242226586Sdim  for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
243226586Sdim       I != E; ++I) {
244226586Sdim
245226586Sdim    Pred = *I;
246243830Sdim    ProgramStateRef state = Pred->getState();
247243830Sdim    const LocationContext *LCtx = Pred->getLocationContext();
248243830Sdim
249226586Sdim    switch (CastE->getCastKind()) {
250226586Sdim      case CK_LValueToRValue:
251226586Sdim        llvm_unreachable("LValueToRValue casts handled earlier.");
252226586Sdim      case CK_ToVoid:
253226586Sdim        continue;
254226586Sdim        // The analyzer doesn't do anything special with these casts,
255226586Sdim        // since it understands retain/release semantics already.
256226586Sdim      case CK_ARCProduceObject:
257226586Sdim      case CK_ARCConsumeObject:
258226586Sdim      case CK_ARCReclaimReturnedObject:
259226586Sdim      case CK_ARCExtendBlockObject: // Fall-through.
260234353Sdim      case CK_CopyAndAutoreleaseBlockObject:
261234353Sdim        // The analyser can ignore atomic casts for now, although some future
262234353Sdim        // checkers may want to make certain that you're not modifying the same
263234353Sdim        // value through atomic and nonatomic pointers.
264234353Sdim      case CK_AtomicToNonAtomic:
265234353Sdim      case CK_NonAtomicToAtomic:
266226586Sdim        // True no-ops.
267226586Sdim      case CK_NoOp:
268243830Sdim      case CK_ConstructorConversion:
269243830Sdim      case CK_UserDefinedConversion:
270243830Sdim      case CK_FunctionToPointerDecay:
271243830Sdim      case CK_BuiltinFnToFnPtr: {
272226586Sdim        // Copy the SVal of Ex to CastE.
273234353Sdim        ProgramStateRef state = Pred->getState();
274234353Sdim        const LocationContext *LCtx = Pred->getLocationContext();
275234353Sdim        SVal V = state->getSVal(Ex, LCtx);
276234353Sdim        state = state->BindExpr(CastE, LCtx, V);
277234353Sdim        Bldr.generateNode(CastE, Pred, state);
278226586Sdim        continue;
279226586Sdim      }
280243830Sdim      case CK_MemberPointerToBoolean:
281243830Sdim        // FIXME: For now, member pointers are represented by void *.
282243830Sdim        // FALLTHROUGH
283226586Sdim      case CK_Dependent:
284226586Sdim      case CK_ArrayToPointerDecay:
285226586Sdim      case CK_BitCast:
286226586Sdim      case CK_IntegralCast:
287226586Sdim      case CK_NullToPointer:
288226586Sdim      case CK_IntegralToPointer:
289226586Sdim      case CK_PointerToIntegral:
290226586Sdim      case CK_PointerToBoolean:
291226586Sdim      case CK_IntegralToBoolean:
292226586Sdim      case CK_IntegralToFloating:
293226586Sdim      case CK_FloatingToIntegral:
294226586Sdim      case CK_FloatingToBoolean:
295226586Sdim      case CK_FloatingCast:
296226586Sdim      case CK_FloatingRealToComplex:
297226586Sdim      case CK_FloatingComplexToReal:
298226586Sdim      case CK_FloatingComplexToBoolean:
299226586Sdim      case CK_FloatingComplexCast:
300226586Sdim      case CK_FloatingComplexToIntegralComplex:
301226586Sdim      case CK_IntegralRealToComplex:
302226586Sdim      case CK_IntegralComplexToReal:
303226586Sdim      case CK_IntegralComplexToBoolean:
304226586Sdim      case CK_IntegralComplexCast:
305226586Sdim      case CK_IntegralComplexToFloatingComplex:
306226586Sdim      case CK_CPointerToObjCPointerCast:
307226586Sdim      case CK_BlockPointerToObjCPointerCast:
308226586Sdim      case CK_AnyPointerToBlockPointerCast:
309249423Sdim      case CK_ObjCObjectLValueCast:
310249423Sdim      case CK_ZeroToOCLEvent: {
311226586Sdim        // Delegate to SValBuilder to process.
312234353Sdim        SVal V = state->getSVal(Ex, LCtx);
313226586Sdim        V = svalBuilder.evalCast(V, T, ExTy);
314234353Sdim        state = state->BindExpr(CastE, LCtx, V);
315234353Sdim        Bldr.generateNode(CastE, Pred, state);
316226586Sdim        continue;
317226586Sdim      }
318226586Sdim      case CK_DerivedToBase:
319226586Sdim      case CK_UncheckedDerivedToBase: {
320226586Sdim        // For DerivedToBase cast, delegate to the store manager.
321234353Sdim        SVal val = state->getSVal(Ex, LCtx);
322239462Sdim        val = getStoreManager().evalDerivedToBase(val, CastE);
323234353Sdim        state = state->BindExpr(CastE, LCtx, val);
324234353Sdim        Bldr.generateNode(CastE, Pred, state);
325226586Sdim        continue;
326226586Sdim      }
327234353Sdim      // Handle C++ dyn_cast.
328234353Sdim      case CK_Dynamic: {
329234353Sdim        SVal val = state->getSVal(Ex, LCtx);
330234353Sdim
331234353Sdim        // Compute the type of the result.
332234353Sdim        QualType resultType = CastE->getType();
333239462Sdim        if (CastE->isGLValue())
334234353Sdim          resultType = getContext().getPointerType(resultType);
335234353Sdim
336234353Sdim        bool Failed = false;
337234353Sdim
338234353Sdim        // Check if the value being cast evaluates to 0.
339234353Sdim        if (val.isZeroConstant())
340234353Sdim          Failed = true;
341234353Sdim        // Else, evaluate the cast.
342234353Sdim        else
343234353Sdim          val = getStoreManager().evalDynamicCast(val, T, Failed);
344234353Sdim
345234353Sdim        if (Failed) {
346234353Sdim          if (T->isReferenceType()) {
347234353Sdim            // A bad_cast exception is thrown if input value is a reference.
348234353Sdim            // Currently, we model this, by generating a sink.
349243830Sdim            Bldr.generateSink(CastE, Pred, state);
350234353Sdim            continue;
351234353Sdim          } else {
352234353Sdim            // If the cast fails on a pointer, bind to 0.
353234353Sdim            state = state->BindExpr(CastE, LCtx, svalBuilder.makeNull());
354234353Sdim          }
355234353Sdim        } else {
356234353Sdim          // If we don't know if the cast succeeded, conjure a new symbol.
357234353Sdim          if (val.isUnknown()) {
358243830Sdim            DefinedOrUnknownSVal NewSym =
359243830Sdim              svalBuilder.conjureSymbolVal(0, CastE, LCtx, resultType,
360243830Sdim                                           currBldrCtx->blockCount());
361234353Sdim            state = state->BindExpr(CastE, LCtx, NewSym);
362234353Sdim          } else
363234353Sdim            // Else, bind to the derived region value.
364234353Sdim            state = state->BindExpr(CastE, LCtx, val);
365234353Sdim        }
366234353Sdim        Bldr.generateNode(CastE, Pred, state);
367234353Sdim        continue;
368234353Sdim      }
369243830Sdim      case CK_NullToMemberPointer: {
370243830Sdim        // FIXME: For now, member pointers are represented by void *.
371243830Sdim        SVal V = svalBuilder.makeIntValWithPtrWidth(0, true);
372243830Sdim        state = state->BindExpr(CastE, LCtx, V);
373243830Sdim        Bldr.generateNode(CastE, Pred, state);
374243830Sdim        continue;
375243830Sdim      }
376234353Sdim      // Various C++ casts that are not handled yet.
377226586Sdim      case CK_ToUnion:
378226586Sdim      case CK_BaseToDerived:
379226586Sdim      case CK_BaseToDerivedMemberPointer:
380226586Sdim      case CK_DerivedToBaseMemberPointer:
381234353Sdim      case CK_ReinterpretMemberPointer:
382226586Sdim      case CK_VectorSplat:
383239462Sdim      case CK_LValueBitCast: {
384226586Sdim        // Recover some path-sensitivty by conjuring a new value.
385226586Sdim        QualType resultType = CastE->getType();
386239462Sdim        if (CastE->isGLValue())
387226586Sdim          resultType = getContext().getPointerType(resultType);
388243830Sdim        SVal result = svalBuilder.conjureSymbolVal(0, CastE, LCtx,
389243830Sdim                                                   resultType,
390243830Sdim                                                   currBldrCtx->blockCount());
391243830Sdim        state = state->BindExpr(CastE, LCtx, result);
392234353Sdim        Bldr.generateNode(CastE, Pred, state);
393226586Sdim        continue;
394226586Sdim      }
395226586Sdim    }
396226586Sdim  }
397226586Sdim}
398226586Sdim
399226586Sdimvoid ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL,
400226586Sdim                                          ExplodedNode *Pred,
401226586Sdim                                          ExplodedNodeSet &Dst) {
402243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
403234353Sdim
404226586Sdim  const InitListExpr *ILE
405226586Sdim    = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
406226586Sdim
407234353Sdim  ProgramStateRef state = Pred->getState();
408234353Sdim  SVal ILV = state->getSVal(ILE, Pred->getLocationContext());
409226586Sdim  const LocationContext *LC = Pred->getLocationContext();
410226586Sdim  state = state->bindCompoundLiteral(CL, LC, ILV);
411239462Sdim
412239462Sdim  // Compound literal expressions are a GNU extension in C++.
413239462Sdim  // Unlike in C, where CLs are lvalues, in C++ CLs are prvalues,
414239462Sdim  // and like temporary objects created by the functional notation T()
415239462Sdim  // CLs are destroyed at the end of the containing full-expression.
416239462Sdim  // HOWEVER, an rvalue of array type is not something the analyzer can
417239462Sdim  // reason about, since we expect all regions to be wrapped in Locs.
418239462Sdim  // So we treat array CLs as lvalues as well, knowing that they will decay
419239462Sdim  // to pointers as soon as they are used.
420239462Sdim  if (CL->isGLValue() || CL->getType()->isArrayType())
421234353Sdim    B.generateNode(CL, Pred, state->BindExpr(CL, LC, state->getLValue(CL, LC)));
422226586Sdim  else
423234353Sdim    B.generateNode(CL, Pred, state->BindExpr(CL, LC, ILV));
424226586Sdim}
425226586Sdim
426226586Sdimvoid ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
427226586Sdim                               ExplodedNodeSet &Dst) {
428226586Sdim  // Assumption: The CFG has one DeclStmt per Decl.
429249423Sdim  const VarDecl *VD = dyn_cast_or_null<VarDecl>(*DS->decl_begin());
430249423Sdim
431249423Sdim  if (!VD) {
432234353Sdim    //TODO:AZ: remove explicit insertion after refactoring is done.
433234353Sdim    Dst.insert(Pred);
434226586Sdim    return;
435234353Sdim  }
436226586Sdim
437226586Sdim  // FIXME: all pre/post visits should eventually be handled by ::Visit().
438226586Sdim  ExplodedNodeSet dstPreVisit;
439226586Sdim  getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this);
440226586Sdim
441243830Sdim  StmtNodeBuilder B(dstPreVisit, Dst, *currBldrCtx);
442226586Sdim  for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
443226586Sdim       I!=E; ++I) {
444226586Sdim    ExplodedNode *N = *I;
445234353Sdim    ProgramStateRef state = N->getState();
446249423Sdim    const LocationContext *LC = N->getLocationContext();
447249423Sdim
448226586Sdim    // Decls without InitExpr are not initialized explicitly.
449226586Sdim    if (const Expr *InitEx = VD->getInit()) {
450249423Sdim
451249423Sdim      // Note in the state that the initialization has occurred.
452249423Sdim      ExplodedNode *UpdatedN = N;
453239462Sdim      SVal InitVal = state->getSVal(InitEx, LC);
454234353Sdim
455249423Sdim      if (isa<CXXConstructExpr>(InitEx->IgnoreImplicit())) {
456239462Sdim        // We constructed the object directly in the variable.
457239462Sdim        // No need to bind anything.
458249423Sdim        B.generateNode(DS, UpdatedN, state);
459239462Sdim      } else {
460239462Sdim        // We bound the temp obj region to the CXXConstructExpr. Now recover
461239462Sdim        // the lazy compound value when the variable is not a reference.
462249423Sdim        if (AMgr.getLangOpts().CPlusPlus && VD->getType()->isRecordType() &&
463249423Sdim            !VD->getType()->isReferenceType()) {
464249423Sdim          if (Optional<loc::MemRegionVal> M =
465249423Sdim                  InitVal.getAs<loc::MemRegionVal>()) {
466249423Sdim            InitVal = state->getSVal(M->getRegion());
467249423Sdim            assert(InitVal.getAs<nonloc::LazyCompoundVal>());
468249423Sdim          }
469239462Sdim        }
470239462Sdim
471239462Sdim        // Recover some path-sensitivity if a scalar value evaluated to
472239462Sdim        // UnknownVal.
473239462Sdim        if (InitVal.isUnknown()) {
474239462Sdim          QualType Ty = InitEx->getType();
475239462Sdim          if (InitEx->isGLValue()) {
476239462Sdim            Ty = getContext().getPointerType(Ty);
477239462Sdim          }
478239462Sdim
479243830Sdim          InitVal = svalBuilder.conjureSymbolVal(0, InitEx, LC, Ty,
480243830Sdim                                                 currBldrCtx->blockCount());
481239462Sdim        }
482249423Sdim
483249423Sdim
484249423Sdim        B.takeNodes(UpdatedN);
485239462Sdim        ExplodedNodeSet Dst2;
486249423Sdim        evalBind(Dst2, DS, UpdatedN, state->getLValue(VD, LC), InitVal, true);
487239462Sdim        B.addNodes(Dst2);
488226586Sdim      }
489226586Sdim    }
490226586Sdim    else {
491243830Sdim      B.generateNode(DS, N, state);
492226586Sdim    }
493226586Sdim  }
494226586Sdim}
495226586Sdim
496226586Sdimvoid ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
497226586Sdim                                  ExplodedNodeSet &Dst) {
498226586Sdim  assert(B->getOpcode() == BO_LAnd ||
499226586Sdim         B->getOpcode() == BO_LOr);
500234353Sdim
501243830Sdim  StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
502234353Sdim  ProgramStateRef state = Pred->getState();
503239462Sdim
504239462Sdim  ExplodedNode *N = Pred;
505249423Sdim  while (!N->getLocation().getAs<BlockEntrance>()) {
506239462Sdim    ProgramPoint P = N->getLocation();
507249423Sdim    assert(P.getAs<PreStmt>()|| P.getAs<PreStmtPurgeDeadSymbols>());
508239462Sdim    (void) P;
509239462Sdim    assert(N->pred_size() == 1);
510239462Sdim    N = *N->pred_begin();
511226586Sdim  }
512239462Sdim  assert(N->pred_size() == 1);
513239462Sdim  N = *N->pred_begin();
514249423Sdim  BlockEdge BE = N->getLocation().castAs<BlockEdge>();
515239462Sdim  SVal X;
516239462Sdim
517239462Sdim  // Determine the value of the expression by introspecting how we
518239462Sdim  // got this location in the CFG.  This requires looking at the previous
519239462Sdim  // block we were in and what kind of control-flow transfer was involved.
520239462Sdim  const CFGBlock *SrcBlock = BE.getSrc();
521239462Sdim  // The only terminator (if there is one) that makes sense is a logical op.
522239462Sdim  CFGTerminator T = SrcBlock->getTerminator();
523239462Sdim  if (const BinaryOperator *Term = cast_or_null<BinaryOperator>(T.getStmt())) {
524239462Sdim    (void) Term;
525239462Sdim    assert(Term->isLogicalOp());
526239462Sdim    assert(SrcBlock->succ_size() == 2);
527239462Sdim    // Did we take the true or false branch?
528239462Sdim    unsigned constant = (*SrcBlock->succ_begin() == BE.getDst()) ? 1 : 0;
529239462Sdim    X = svalBuilder.makeIntVal(constant, B->getType());
530239462Sdim  }
531226586Sdim  else {
532239462Sdim    // If there is no terminator, by construction the last statement
533239462Sdim    // in SrcBlock is the value of the enclosing expression.
534243830Sdim    // However, we still need to constrain that value to be 0 or 1.
535239462Sdim    assert(!SrcBlock->empty());
536249423Sdim    CFGStmt Elem = SrcBlock->rbegin()->castAs<CFGStmt>();
537243830Sdim    const Expr *RHS = cast<Expr>(Elem.getStmt());
538243830Sdim    SVal RHSVal = N->getState()->getSVal(RHS, Pred->getLocationContext());
539243830Sdim
540249423Sdim    if (RHSVal.isUndef()) {
541249423Sdim      X = RHSVal;
542249423Sdim    } else {
543249423Sdim      DefinedOrUnknownSVal DefinedRHS = RHSVal.castAs<DefinedOrUnknownSVal>();
544249423Sdim      ProgramStateRef StTrue, StFalse;
545249423Sdim      llvm::tie(StTrue, StFalse) = N->getState()->assume(DefinedRHS);
546249423Sdim      if (StTrue) {
547249423Sdim        if (StFalse) {
548249423Sdim          // We can't constrain the value to 0 or 1.
549249423Sdim          // The best we can do is a cast.
550249423Sdim          X = getSValBuilder().evalCast(RHSVal, B->getType(), RHS->getType());
551249423Sdim        } else {
552249423Sdim          // The value is known to be true.
553249423Sdim          X = getSValBuilder().makeIntVal(1, B->getType());
554249423Sdim        }
555243830Sdim      } else {
556249423Sdim        // The value is known to be false.
557249423Sdim        assert(StFalse && "Infeasible path!");
558249423Sdim        X = getSValBuilder().makeIntVal(0, B->getType());
559243830Sdim      }
560243830Sdim    }
561226586Sdim  }
562239462Sdim  Bldr.generateNode(B, Pred, state->BindExpr(B, Pred->getLocationContext(), X));
563226586Sdim}
564226586Sdim
565226586Sdimvoid ExprEngine::VisitInitListExpr(const InitListExpr *IE,
566226586Sdim                                   ExplodedNode *Pred,
567226586Sdim                                   ExplodedNodeSet &Dst) {
568243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
569226586Sdim
570234353Sdim  ProgramStateRef state = Pred->getState();
571234353Sdim  const LocationContext *LCtx = Pred->getLocationContext();
572226586Sdim  QualType T = getContext().getCanonicalType(IE->getType());
573226586Sdim  unsigned NumInitElements = IE->getNumInits();
574226586Sdim
575243830Sdim  if (T->isArrayType() || T->isRecordType() || T->isVectorType() ||
576243830Sdim      T->isAnyComplexType()) {
577226586Sdim    llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList();
578226586Sdim
579226586Sdim    // Handle base case where the initializer has no elements.
580226586Sdim    // e.g: static int* myArray[] = {};
581226586Sdim    if (NumInitElements == 0) {
582226586Sdim      SVal V = svalBuilder.makeCompoundVal(T, vals);
583234353Sdim      B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
584226586Sdim      return;
585226586Sdim    }
586226586Sdim
587226586Sdim    for (InitListExpr::const_reverse_iterator it = IE->rbegin(),
588226586Sdim         ei = IE->rend(); it != ei; ++it) {
589249423Sdim      SVal V = state->getSVal(cast<Expr>(*it), LCtx);
590249423Sdim      if (dyn_cast_or_null<CXXTempObjectRegion>(V.getAsRegion()))
591249423Sdim        V = UnknownVal();
592249423Sdim      vals = getBasicVals().consVals(V, vals);
593226586Sdim    }
594226586Sdim
595234353Sdim    B.generateNode(IE, Pred,
596234353Sdim                   state->BindExpr(IE, LCtx,
597234353Sdim                                   svalBuilder.makeCompoundVal(T, vals)));
598226586Sdim    return;
599226586Sdim  }
600239462Sdim
601239462Sdim  // Handle scalars: int{5} and int{}.
602239462Sdim  assert(NumInitElements <= 1);
603239462Sdim
604239462Sdim  SVal V;
605239462Sdim  if (NumInitElements == 0)
606239462Sdim    V = getSValBuilder().makeZeroVal(T);
607239462Sdim  else
608239462Sdim    V = state->getSVal(IE->getInit(0), LCtx);
609239462Sdim
610239462Sdim  B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
611226586Sdim}
612226586Sdim
613226586Sdimvoid ExprEngine::VisitGuardedExpr(const Expr *Ex,
614226586Sdim                                  const Expr *L,
615226586Sdim                                  const Expr *R,
616226586Sdim                                  ExplodedNode *Pred,
617226586Sdim                                  ExplodedNodeSet &Dst) {
618243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
619234353Sdim  ProgramStateRef state = Pred->getState();
620234353Sdim  const LocationContext *LCtx = Pred->getLocationContext();
621239462Sdim  const CFGBlock *SrcBlock = 0;
622239462Sdim
623239462Sdim  for (const ExplodedNode *N = Pred ; N ; N = *N->pred_begin()) {
624239462Sdim    ProgramPoint PP = N->getLocation();
625249423Sdim    if (PP.getAs<PreStmtPurgeDeadSymbols>() || PP.getAs<BlockEntrance>()) {
626239462Sdim      assert(N->pred_size() == 1);
627239462Sdim      continue;
628239462Sdim    }
629249423Sdim    SrcBlock = PP.castAs<BlockEdge>().getSrc();
630239462Sdim    break;
631239462Sdim  }
632239462Sdim
633249423Sdim  assert(SrcBlock && "missing function entry");
634249423Sdim
635239462Sdim  // Find the last expression in the predecessor block.  That is the
636239462Sdim  // expression that is used for the value of the ternary expression.
637239462Sdim  bool hasValue = false;
638239462Sdim  SVal V;
639239462Sdim
640239462Sdim  for (CFGBlock::const_reverse_iterator I = SrcBlock->rbegin(),
641239462Sdim                                        E = SrcBlock->rend(); I != E; ++I) {
642239462Sdim    CFGElement CE = *I;
643249423Sdim    if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
644239462Sdim      const Expr *ValEx = cast<Expr>(CS->getStmt());
645239462Sdim      hasValue = true;
646239462Sdim      V = state->getSVal(ValEx, LCtx);
647239462Sdim      break;
648239462Sdim    }
649239462Sdim  }
650239462Sdim
651239462Sdim  assert(hasValue);
652239462Sdim  (void) hasValue;
653239462Sdim
654239462Sdim  // Generate a new node with the binding from the appropriate path.
655239462Sdim  B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V, true));
656226586Sdim}
657226586Sdim
658226586Sdimvoid ExprEngine::
659226586SdimVisitOffsetOfExpr(const OffsetOfExpr *OOE,
660226586Sdim                  ExplodedNode *Pred, ExplodedNodeSet &Dst) {
661243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
662234353Sdim  APSInt IV;
663234353Sdim  if (OOE->EvaluateAsInt(IV, getContext())) {
664226586Sdim    assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
665226586Sdim    assert(OOE->getType()->isIntegerType());
666226586Sdim    assert(IV.isSigned() == OOE->getType()->isSignedIntegerOrEnumerationType());
667226586Sdim    SVal X = svalBuilder.makeIntVal(IV);
668234353Sdim    B.generateNode(OOE, Pred,
669234353Sdim                   Pred->getState()->BindExpr(OOE, Pred->getLocationContext(),
670234353Sdim                                              X));
671226586Sdim  }
672226586Sdim  // FIXME: Handle the case where __builtin_offsetof is not a constant.
673226586Sdim}
674226586Sdim
675226586Sdim
676226586Sdimvoid ExprEngine::
677226586SdimVisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
678226586Sdim                              ExplodedNode *Pred,
679226586Sdim                              ExplodedNodeSet &Dst) {
680243830Sdim  StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
681226586Sdim
682226586Sdim  QualType T = Ex->getTypeOfArgument();
683226586Sdim
684226586Sdim  if (Ex->getKind() == UETT_SizeOf) {
685226586Sdim    if (!T->isIncompleteType() && !T->isConstantSizeType()) {
686226586Sdim      assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
687226586Sdim
688226586Sdim      // FIXME: Add support for VLA type arguments and VLA expressions.
689226586Sdim      // When that happens, we should probably refactor VLASizeChecker's code.
690226586Sdim      return;
691226586Sdim    }
692226586Sdim    else if (T->getAs<ObjCObjectType>()) {
693226586Sdim      // Some code tries to take the sizeof an ObjCObjectType, relying that
694226586Sdim      // the compiler has laid out its representation.  Just report Unknown
695226586Sdim      // for these.
696226586Sdim      return;
697226586Sdim    }
698226586Sdim  }
699226586Sdim
700234353Sdim  APSInt Value = Ex->EvaluateKnownConstInt(getContext());
701234353Sdim  CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue());
702226586Sdim
703234353Sdim  ProgramStateRef state = Pred->getState();
704234353Sdim  state = state->BindExpr(Ex, Pred->getLocationContext(),
705234353Sdim                          svalBuilder.makeIntVal(amt.getQuantity(),
706226586Sdim                                                     Ex->getType()));
707234353Sdim  Bldr.generateNode(Ex, Pred, state);
708226586Sdim}
709226586Sdim
710226586Sdimvoid ExprEngine::VisitUnaryOperator(const UnaryOperator* U,
711226586Sdim                                    ExplodedNode *Pred,
712234353Sdim                                    ExplodedNodeSet &Dst) {
713243830Sdim  StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
714226586Sdim  switch (U->getOpcode()) {
715234353Sdim    default: {
716234353Sdim      Bldr.takeNodes(Pred);
717234353Sdim      ExplodedNodeSet Tmp;
718234353Sdim      VisitIncrementDecrementOperator(U, Pred, Tmp);
719234353Sdim      Bldr.addNodes(Tmp);
720234353Sdim    }
721226586Sdim      break;
722226586Sdim    case UO_Real: {
723226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
724226586Sdim
725234353Sdim      // FIXME: We don't have complex SValues yet.
726234353Sdim      if (Ex->getType()->isAnyComplexType()) {
727234353Sdim        // Just report "Unknown."
728234353Sdim        break;
729234353Sdim      }
730226586Sdim
731234353Sdim      // For all other types, UO_Real is an identity operation.
732234353Sdim      assert (U->getType() == Ex->getType());
733234353Sdim      ProgramStateRef state = Pred->getState();
734234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
735234353Sdim      Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx,
736234353Sdim                                                 state->getSVal(Ex, LCtx)));
737234353Sdim      break;
738226586Sdim    }
739226586Sdim
740234353Sdim    case UO_Imag: {
741226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
742234353Sdim      // FIXME: We don't have complex SValues yet.
743234353Sdim      if (Ex->getType()->isAnyComplexType()) {
744234353Sdim        // Just report "Unknown."
745234353Sdim        break;
746226586Sdim      }
747234353Sdim      // For all other types, UO_Imag returns 0.
748234353Sdim      ProgramStateRef state = Pred->getState();
749234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
750234353Sdim      SVal X = svalBuilder.makeZeroVal(Ex->getType());
751234353Sdim      Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, X));
752234353Sdim      break;
753226586Sdim    }
754226586Sdim
755226586Sdim    case UO_Plus:
756239462Sdim      assert(!U->isGLValue());
757226586Sdim      // FALL-THROUGH.
758226586Sdim    case UO_Deref:
759226586Sdim    case UO_AddrOf:
760226586Sdim    case UO_Extension: {
761234353Sdim      // FIXME: We can probably just have some magic in Environment::getSVal()
762234353Sdim      // that propagates values, instead of creating a new node here.
763234353Sdim      //
764226586Sdim      // Unary "+" is a no-op, similar to a parentheses.  We still have places
765226586Sdim      // where it may be a block-level expression, so we need to
766226586Sdim      // generate an extra node that just propagates the value of the
767234353Sdim      // subexpression.
768226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
769234353Sdim      ProgramStateRef state = Pred->getState();
770234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
771234353Sdim      Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx,
772234353Sdim                                                 state->getSVal(Ex, LCtx)));
773234353Sdim      break;
774226586Sdim    }
775226586Sdim
776226586Sdim    case UO_LNot:
777226586Sdim    case UO_Minus:
778226586Sdim    case UO_Not: {
779239462Sdim      assert (!U->isGLValue());
780226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
781234353Sdim      ProgramStateRef state = Pred->getState();
782234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
783226586Sdim
784234353Sdim      // Get the value of the subexpression.
785234353Sdim      SVal V = state->getSVal(Ex, LCtx);
786226586Sdim
787234353Sdim      if (V.isUnknownOrUndef()) {
788234353Sdim        Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, V));
789234353Sdim        break;
790234353Sdim      }
791226586Sdim
792234353Sdim      switch (U->getOpcode()) {
793234353Sdim        default:
794234353Sdim          llvm_unreachable("Invalid Opcode.");
795234353Sdim        case UO_Not:
796234353Sdim          // FIXME: Do we need to handle promotions?
797249423Sdim          state = state->BindExpr(U, LCtx, evalComplement(V.castAs<NonLoc>()));
798234353Sdim          break;
799234353Sdim        case UO_Minus:
800234353Sdim          // FIXME: Do we need to handle promotions?
801249423Sdim          state = state->BindExpr(U, LCtx, evalMinus(V.castAs<NonLoc>()));
802234353Sdim          break;
803234353Sdim        case UO_LNot:
804234353Sdim          // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
805234353Sdim          //
806234353Sdim          //  Note: technically we do "E == 0", but this is the same in the
807234353Sdim          //    transfer functions as "0 == E".
808234353Sdim          SVal Result;
809249423Sdim          if (Optional<Loc> LV = V.getAs<Loc>()) {
810234353Sdim            Loc X = svalBuilder.makeNull();
811249423Sdim            Result = evalBinOp(state, BO_EQ, *LV, X, U->getType());
812234353Sdim          }
813249423Sdim          else if (Ex->getType()->isFloatingType()) {
814249423Sdim            // FIXME: handle floating point types.
815249423Sdim            Result = UnknownVal();
816249423Sdim          } else {
817234353Sdim            nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
818249423Sdim            Result = evalBinOp(state, BO_EQ, V.castAs<NonLoc>(), X,
819234353Sdim                               U->getType());
820234353Sdim          }
821234353Sdim
822234353Sdim          state = state->BindExpr(U, LCtx, Result);
823234353Sdim          break;
824226586Sdim      }
825234353Sdim      Bldr.generateNode(U, Pred, state);
826234353Sdim      break;
827226586Sdim    }
828226586Sdim  }
829234353Sdim
830234353Sdim}
831234353Sdim
832234353Sdimvoid ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U,
833234353Sdim                                                 ExplodedNode *Pred,
834234353Sdim                                                 ExplodedNodeSet &Dst) {
835226586Sdim  // Handle ++ and -- (both pre- and post-increment).
836226586Sdim  assert (U->isIncrementDecrementOp());
837226586Sdim  const Expr *Ex = U->getSubExpr()->IgnoreParens();
838226586Sdim
839234353Sdim  const LocationContext *LCtx = Pred->getLocationContext();
840234353Sdim  ProgramStateRef state = Pred->getState();
841234353Sdim  SVal loc = state->getSVal(Ex, LCtx);
842234353Sdim
843234353Sdim  // Perform a load.
844234353Sdim  ExplodedNodeSet Tmp;
845234353Sdim  evalLoad(Tmp, U, Ex, Pred, state, loc);
846234353Sdim
847234353Sdim  ExplodedNodeSet Dst2;
848243830Sdim  StmtNodeBuilder Bldr(Tmp, Dst2, *currBldrCtx);
849234353Sdim  for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) {
850226586Sdim
851234353Sdim    state = (*I)->getState();
852234353Sdim    assert(LCtx == (*I)->getLocationContext());
853234353Sdim    SVal V2_untested = state->getSVal(Ex, LCtx);
854226586Sdim
855234353Sdim    // Propagate unknown and undefined values.
856234353Sdim    if (V2_untested.isUnknownOrUndef()) {
857234353Sdim      Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V2_untested));
858234353Sdim      continue;
859234353Sdim    }
860249423Sdim    DefinedSVal V2 = V2_untested.castAs<DefinedSVal>();
861226586Sdim
862234353Sdim    // Handle all other values.
863234353Sdim    BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub;
864234353Sdim
865234353Sdim    // If the UnaryOperator has non-location type, use its type to create the
866234353Sdim    // constant value. If the UnaryOperator has location type, create the
867234353Sdim    // constant with int type and pointer width.
868234353Sdim    SVal RHS;
869234353Sdim
870234353Sdim    if (U->getType()->isAnyPointerType())
871234353Sdim      RHS = svalBuilder.makeArrayIndex(1);
872243830Sdim    else if (U->getType()->isIntegralOrEnumerationType())
873243830Sdim      RHS = svalBuilder.makeIntVal(1, U->getType());
874234353Sdim    else
875243830Sdim      RHS = UnknownVal();
876234353Sdim
877234353Sdim    SVal Result = evalBinOp(state, Op, V2, RHS, U->getType());
878234353Sdim
879234353Sdim    // Conjure a new symbol if necessary to recover precision.
880234353Sdim    if (Result.isUnknown()){
881234353Sdim      DefinedOrUnknownSVal SymVal =
882243830Sdim        svalBuilder.conjureSymbolVal(0, Ex, LCtx, currBldrCtx->blockCount());
883234353Sdim      Result = SymVal;
884226586Sdim
885234353Sdim      // If the value is a location, ++/-- should always preserve
886234353Sdim      // non-nullness.  Check if the original value was non-null, and if so
887234353Sdim      // propagate that constraint.
888234353Sdim      if (Loc::isLocType(U->getType())) {
889234353Sdim        DefinedOrUnknownSVal Constraint =
890234353Sdim        svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType()));
891226586Sdim
892234353Sdim        if (!state->assume(Constraint, true)) {
893234353Sdim          // It isn't feasible for the original value to be null.
894234353Sdim          // Propagate this constraint.
895234353Sdim          Constraint = svalBuilder.evalEQ(state, SymVal,
896234353Sdim                                       svalBuilder.makeZeroVal(U->getType()));
897226586Sdim
898234353Sdim
899234353Sdim          state = state->assume(Constraint, false);
900234353Sdim          assert(state);
901226586Sdim        }
902226586Sdim      }
903226586Sdim    }
904234353Sdim
905234353Sdim    // Since the lvalue-to-rvalue conversion is explicit in the AST,
906234353Sdim    // we bind an l-value if the operator is prefix and an lvalue (in C++).
907239462Sdim    if (U->isGLValue())
908234353Sdim      state = state->BindExpr(U, LCtx, loc);
909234353Sdim    else
910234353Sdim      state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result);
911234353Sdim
912234353Sdim    // Perform the store.
913234353Sdim    Bldr.takeNodes(*I);
914234353Sdim    ExplodedNodeSet Dst3;
915234353Sdim    evalStore(Dst3, U, U, *I, state, loc, Result);
916234353Sdim    Bldr.addNodes(Dst3);
917226586Sdim  }
918234353Sdim  Dst.insert(Dst2);
919226586Sdim}
920