ExprEngineC.cpp revision 251662
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>() &&
71251662Sdim            RHS->getType()->isIntegralOrEnumerationType() &&
72251662Sdim            RightV.isUnknown()) {
73243830Sdim          RightV = svalBuilder.conjureSymbolVal(RHS, LCtx, RHS->getType(),
74243830Sdim                                                Count);
75239462Sdim        }
76249423Sdim        if (RightV.getAs<Loc>() &&
77251662Sdim            LHS->getType()->isIntegralOrEnumerationType() &&
78251662Sdim            LeftV.isUnknown()) {
79243830Sdim          LeftV = svalBuilder.conjureSymbolVal(LHS, LCtx, LHS->getType(),
80243830Sdim                                               Count);
81239462Sdim        }
82239462Sdim      }
83239462Sdim
84226586Sdim      // Process non-assignments except commas or short-circuited
85226586Sdim      // logical expressions (LAnd and LOr).
86226586Sdim      SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
87226586Sdim      if (Result.isUnknown()) {
88234353Sdim        Bldr.generateNode(B, *it, state);
89226586Sdim        continue;
90226586Sdim      }
91226586Sdim
92234353Sdim      state = state->BindExpr(B, LCtx, Result);
93234353Sdim      Bldr.generateNode(B, *it, state);
94226586Sdim      continue;
95226586Sdim    }
96226586Sdim
97226586Sdim    assert (B->isCompoundAssignmentOp());
98226586Sdim
99226586Sdim    switch (Op) {
100226586Sdim      default:
101226586Sdim        llvm_unreachable("Invalid opcode for compound assignment.");
102226586Sdim      case BO_MulAssign: Op = BO_Mul; break;
103226586Sdim      case BO_DivAssign: Op = BO_Div; break;
104226586Sdim      case BO_RemAssign: Op = BO_Rem; break;
105226586Sdim      case BO_AddAssign: Op = BO_Add; break;
106226586Sdim      case BO_SubAssign: Op = BO_Sub; break;
107226586Sdim      case BO_ShlAssign: Op = BO_Shl; break;
108226586Sdim      case BO_ShrAssign: Op = BO_Shr; break;
109226586Sdim      case BO_AndAssign: Op = BO_And; break;
110226586Sdim      case BO_XorAssign: Op = BO_Xor; break;
111226586Sdim      case BO_OrAssign:  Op = BO_Or;  break;
112226586Sdim    }
113226586Sdim
114226586Sdim    // Perform a load (the LHS).  This performs the checks for
115226586Sdim    // null dereferences, and so on.
116226586Sdim    ExplodedNodeSet Tmp;
117226586Sdim    SVal location = LeftV;
118234353Sdim    evalLoad(Tmp, B, LHS, *it, state, location);
119226586Sdim
120226586Sdim    for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E;
121226586Sdim         ++I) {
122226586Sdim
123226586Sdim      state = (*I)->getState();
124234353Sdim      const LocationContext *LCtx = (*I)->getLocationContext();
125234353Sdim      SVal V = state->getSVal(LHS, LCtx);
126226586Sdim
127226586Sdim      // Get the computation type.
128226586Sdim      QualType CTy =
129226586Sdim        cast<CompoundAssignOperator>(B)->getComputationResultType();
130226586Sdim      CTy = getContext().getCanonicalType(CTy);
131226586Sdim
132226586Sdim      QualType CLHSTy =
133226586Sdim        cast<CompoundAssignOperator>(B)->getComputationLHSType();
134226586Sdim      CLHSTy = getContext().getCanonicalType(CLHSTy);
135226586Sdim
136226586Sdim      QualType LTy = getContext().getCanonicalType(LHS->getType());
137226586Sdim
138226586Sdim      // Promote LHS.
139226586Sdim      V = svalBuilder.evalCast(V, CLHSTy, LTy);
140226586Sdim
141226586Sdim      // Compute the result of the operation.
142226586Sdim      SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy),
143226586Sdim                                         B->getType(), CTy);
144226586Sdim
145226586Sdim      // EXPERIMENTAL: "Conjured" symbols.
146226586Sdim      // FIXME: Handle structs.
147226586Sdim
148226586Sdim      SVal LHSVal;
149226586Sdim
150234353Sdim      if (Result.isUnknown()) {
151226586Sdim        // The symbolic value is actually for the type of the left-hand side
152226586Sdim        // expression, not the computation type, as this is the value the
153226586Sdim        // LValue on the LHS will bind to.
154243830Sdim        LHSVal = svalBuilder.conjureSymbolVal(0, B->getRHS(), LCtx, LTy,
155243830Sdim                                              currBldrCtx->blockCount());
156226586Sdim        // However, we need to convert the symbol to the computation type.
157226586Sdim        Result = svalBuilder.evalCast(LHSVal, CTy, LTy);
158226586Sdim      }
159226586Sdim      else {
160226586Sdim        // The left-hand side may bind to a different value then the
161226586Sdim        // computation type.
162226586Sdim        LHSVal = svalBuilder.evalCast(Result, LTy, CTy);
163226586Sdim      }
164226586Sdim
165226586Sdim      // In C++, assignment and compound assignment operators return an
166226586Sdim      // lvalue.
167239462Sdim      if (B->isGLValue())
168234353Sdim        state = state->BindExpr(B, LCtx, location);
169226586Sdim      else
170234353Sdim        state = state->BindExpr(B, LCtx, Result);
171226586Sdim
172226586Sdim      evalStore(Tmp2, B, LHS, *I, state, location, LHSVal);
173226586Sdim    }
174226586Sdim  }
175226586Sdim
176226586Sdim  // FIXME: postvisits eventually go in ::Visit()
177226586Sdim  getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
178226586Sdim}
179226586Sdim
180226586Sdimvoid ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
181226586Sdim                                ExplodedNodeSet &Dst) {
182226586Sdim
183226586Sdim  CanQualType T = getContext().getCanonicalType(BE->getType());
184239462Sdim
185239462Sdim  // Get the value of the block itself.
186226586Sdim  SVal V = svalBuilder.getBlockPointer(BE->getBlockDecl(), T,
187226586Sdim                                       Pred->getLocationContext());
188226586Sdim
189239462Sdim  ProgramStateRef State = Pred->getState();
190239462Sdim
191239462Sdim  // If we created a new MemRegion for the block, we should explicitly bind
192239462Sdim  // the captured variables.
193239462Sdim  if (const BlockDataRegion *BDR =
194239462Sdim      dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
195239462Sdim
196239462Sdim    BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
197239462Sdim                                              E = BDR->referenced_vars_end();
198239462Sdim
199239462Sdim    for (; I != E; ++I) {
200239462Sdim      const MemRegion *capturedR = I.getCapturedRegion();
201239462Sdim      const MemRegion *originalR = I.getOriginalRegion();
202239462Sdim      if (capturedR != originalR) {
203239462Sdim        SVal originalV = State->getSVal(loc::MemRegionVal(originalR));
204239462Sdim        State = State->bindLoc(loc::MemRegionVal(capturedR), originalV);
205239462Sdim      }
206239462Sdim    }
207239462Sdim  }
208239462Sdim
209226586Sdim  ExplodedNodeSet Tmp;
210243830Sdim  StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
211234353Sdim  Bldr.generateNode(BE, Pred,
212239462Sdim                    State->BindExpr(BE, Pred->getLocationContext(), V),
213243830Sdim                    0, ProgramPoint::PostLValueKind);
214226586Sdim
215226586Sdim  // FIXME: Move all post/pre visits to ::Visit().
216226586Sdim  getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this);
217226586Sdim}
218226586Sdim
219226586Sdimvoid ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
220226586Sdim                           ExplodedNode *Pred, ExplodedNodeSet &Dst) {
221226586Sdim
222226586Sdim  ExplodedNodeSet dstPreStmt;
223226586Sdim  getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);
224226586Sdim
225234353Sdim  if (CastE->getCastKind() == CK_LValueToRValue) {
226226586Sdim    for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
227226586Sdim         I!=E; ++I) {
228226586Sdim      ExplodedNode *subExprNode = *I;
229234353Sdim      ProgramStateRef state = subExprNode->getState();
230234353Sdim      const LocationContext *LCtx = subExprNode->getLocationContext();
231234353Sdim      evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx));
232226586Sdim    }
233226586Sdim    return;
234226586Sdim  }
235226586Sdim
236226586Sdim  // All other casts.
237226586Sdim  QualType T = CastE->getType();
238226586Sdim  QualType ExTy = Ex->getType();
239226586Sdim
240226586Sdim  if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
241226586Sdim    T = ExCast->getTypeAsWritten();
242226586Sdim
243243830Sdim  StmtNodeBuilder Bldr(dstPreStmt, Dst, *currBldrCtx);
244226586Sdim  for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
245226586Sdim       I != E; ++I) {
246226586Sdim
247226586Sdim    Pred = *I;
248243830Sdim    ProgramStateRef state = Pred->getState();
249243830Sdim    const LocationContext *LCtx = Pred->getLocationContext();
250243830Sdim
251226586Sdim    switch (CastE->getCastKind()) {
252226586Sdim      case CK_LValueToRValue:
253226586Sdim        llvm_unreachable("LValueToRValue casts handled earlier.");
254226586Sdim      case CK_ToVoid:
255226586Sdim        continue;
256226586Sdim        // The analyzer doesn't do anything special with these casts,
257226586Sdim        // since it understands retain/release semantics already.
258226586Sdim      case CK_ARCProduceObject:
259226586Sdim      case CK_ARCConsumeObject:
260226586Sdim      case CK_ARCReclaimReturnedObject:
261226586Sdim      case CK_ARCExtendBlockObject: // Fall-through.
262234353Sdim      case CK_CopyAndAutoreleaseBlockObject:
263234353Sdim        // The analyser can ignore atomic casts for now, although some future
264234353Sdim        // checkers may want to make certain that you're not modifying the same
265234353Sdim        // value through atomic and nonatomic pointers.
266234353Sdim      case CK_AtomicToNonAtomic:
267234353Sdim      case CK_NonAtomicToAtomic:
268226586Sdim        // True no-ops.
269226586Sdim      case CK_NoOp:
270243830Sdim      case CK_ConstructorConversion:
271243830Sdim      case CK_UserDefinedConversion:
272243830Sdim      case CK_FunctionToPointerDecay:
273243830Sdim      case CK_BuiltinFnToFnPtr: {
274226586Sdim        // Copy the SVal of Ex to CastE.
275234353Sdim        ProgramStateRef state = Pred->getState();
276234353Sdim        const LocationContext *LCtx = Pred->getLocationContext();
277234353Sdim        SVal V = state->getSVal(Ex, LCtx);
278234353Sdim        state = state->BindExpr(CastE, LCtx, V);
279234353Sdim        Bldr.generateNode(CastE, Pred, state);
280226586Sdim        continue;
281226586Sdim      }
282243830Sdim      case CK_MemberPointerToBoolean:
283243830Sdim        // FIXME: For now, member pointers are represented by void *.
284243830Sdim        // FALLTHROUGH
285226586Sdim      case CK_Dependent:
286226586Sdim      case CK_ArrayToPointerDecay:
287226586Sdim      case CK_BitCast:
288226586Sdim      case CK_IntegralCast:
289226586Sdim      case CK_NullToPointer:
290226586Sdim      case CK_IntegralToPointer:
291226586Sdim      case CK_PointerToIntegral:
292226586Sdim      case CK_PointerToBoolean:
293226586Sdim      case CK_IntegralToBoolean:
294226586Sdim      case CK_IntegralToFloating:
295226586Sdim      case CK_FloatingToIntegral:
296226586Sdim      case CK_FloatingToBoolean:
297226586Sdim      case CK_FloatingCast:
298226586Sdim      case CK_FloatingRealToComplex:
299226586Sdim      case CK_FloatingComplexToReal:
300226586Sdim      case CK_FloatingComplexToBoolean:
301226586Sdim      case CK_FloatingComplexCast:
302226586Sdim      case CK_FloatingComplexToIntegralComplex:
303226586Sdim      case CK_IntegralRealToComplex:
304226586Sdim      case CK_IntegralComplexToReal:
305226586Sdim      case CK_IntegralComplexToBoolean:
306226586Sdim      case CK_IntegralComplexCast:
307226586Sdim      case CK_IntegralComplexToFloatingComplex:
308226586Sdim      case CK_CPointerToObjCPointerCast:
309226586Sdim      case CK_BlockPointerToObjCPointerCast:
310226586Sdim      case CK_AnyPointerToBlockPointerCast:
311249423Sdim      case CK_ObjCObjectLValueCast:
312249423Sdim      case CK_ZeroToOCLEvent: {
313226586Sdim        // Delegate to SValBuilder to process.
314234353Sdim        SVal V = state->getSVal(Ex, LCtx);
315226586Sdim        V = svalBuilder.evalCast(V, T, ExTy);
316234353Sdim        state = state->BindExpr(CastE, LCtx, V);
317234353Sdim        Bldr.generateNode(CastE, Pred, state);
318226586Sdim        continue;
319226586Sdim      }
320226586Sdim      case CK_DerivedToBase:
321226586Sdim      case CK_UncheckedDerivedToBase: {
322226586Sdim        // For DerivedToBase cast, delegate to the store manager.
323234353Sdim        SVal val = state->getSVal(Ex, LCtx);
324239462Sdim        val = getStoreManager().evalDerivedToBase(val, CastE);
325234353Sdim        state = state->BindExpr(CastE, LCtx, val);
326234353Sdim        Bldr.generateNode(CastE, Pred, state);
327226586Sdim        continue;
328226586Sdim      }
329234353Sdim      // Handle C++ dyn_cast.
330234353Sdim      case CK_Dynamic: {
331234353Sdim        SVal val = state->getSVal(Ex, LCtx);
332234353Sdim
333234353Sdim        // Compute the type of the result.
334234353Sdim        QualType resultType = CastE->getType();
335239462Sdim        if (CastE->isGLValue())
336234353Sdim          resultType = getContext().getPointerType(resultType);
337234353Sdim
338234353Sdim        bool Failed = false;
339234353Sdim
340234353Sdim        // Check if the value being cast evaluates to 0.
341234353Sdim        if (val.isZeroConstant())
342234353Sdim          Failed = true;
343234353Sdim        // Else, evaluate the cast.
344234353Sdim        else
345234353Sdim          val = getStoreManager().evalDynamicCast(val, T, Failed);
346234353Sdim
347234353Sdim        if (Failed) {
348234353Sdim          if (T->isReferenceType()) {
349234353Sdim            // A bad_cast exception is thrown if input value is a reference.
350234353Sdim            // Currently, we model this, by generating a sink.
351243830Sdim            Bldr.generateSink(CastE, Pred, state);
352234353Sdim            continue;
353234353Sdim          } else {
354234353Sdim            // If the cast fails on a pointer, bind to 0.
355234353Sdim            state = state->BindExpr(CastE, LCtx, svalBuilder.makeNull());
356234353Sdim          }
357234353Sdim        } else {
358234353Sdim          // If we don't know if the cast succeeded, conjure a new symbol.
359234353Sdim          if (val.isUnknown()) {
360243830Sdim            DefinedOrUnknownSVal NewSym =
361243830Sdim              svalBuilder.conjureSymbolVal(0, CastE, LCtx, resultType,
362243830Sdim                                           currBldrCtx->blockCount());
363234353Sdim            state = state->BindExpr(CastE, LCtx, NewSym);
364234353Sdim          } else
365234353Sdim            // Else, bind to the derived region value.
366234353Sdim            state = state->BindExpr(CastE, LCtx, val);
367234353Sdim        }
368234353Sdim        Bldr.generateNode(CastE, Pred, state);
369234353Sdim        continue;
370234353Sdim      }
371243830Sdim      case CK_NullToMemberPointer: {
372243830Sdim        // FIXME: For now, member pointers are represented by void *.
373243830Sdim        SVal V = svalBuilder.makeIntValWithPtrWidth(0, true);
374243830Sdim        state = state->BindExpr(CastE, LCtx, V);
375243830Sdim        Bldr.generateNode(CastE, Pred, state);
376243830Sdim        continue;
377243830Sdim      }
378234353Sdim      // Various C++ casts that are not handled yet.
379226586Sdim      case CK_ToUnion:
380226586Sdim      case CK_BaseToDerived:
381226586Sdim      case CK_BaseToDerivedMemberPointer:
382226586Sdim      case CK_DerivedToBaseMemberPointer:
383234353Sdim      case CK_ReinterpretMemberPointer:
384226586Sdim      case CK_VectorSplat:
385239462Sdim      case CK_LValueBitCast: {
386226586Sdim        // Recover some path-sensitivty by conjuring a new value.
387226586Sdim        QualType resultType = CastE->getType();
388239462Sdim        if (CastE->isGLValue())
389226586Sdim          resultType = getContext().getPointerType(resultType);
390243830Sdim        SVal result = svalBuilder.conjureSymbolVal(0, CastE, LCtx,
391243830Sdim                                                   resultType,
392243830Sdim                                                   currBldrCtx->blockCount());
393243830Sdim        state = state->BindExpr(CastE, LCtx, result);
394234353Sdim        Bldr.generateNode(CastE, Pred, state);
395226586Sdim        continue;
396226586Sdim      }
397226586Sdim    }
398226586Sdim  }
399226586Sdim}
400226586Sdim
401226586Sdimvoid ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL,
402226586Sdim                                          ExplodedNode *Pred,
403226586Sdim                                          ExplodedNodeSet &Dst) {
404243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
405234353Sdim
406251662Sdim  ProgramStateRef State = Pred->getState();
407251662Sdim  const LocationContext *LCtx = Pred->getLocationContext();
408251662Sdim
409251662Sdim  const Expr *Init = CL->getInitializer();
410251662Sdim  SVal V = State->getSVal(CL->getInitializer(), LCtx);
411226586Sdim
412251662Sdim  if (isa<CXXConstructExpr>(Init)) {
413251662Sdim    // No work needed. Just pass the value up to this expression.
414251662Sdim  } else {
415251662Sdim    assert(isa<InitListExpr>(Init));
416251662Sdim    Loc CLLoc = State->getLValue(CL, LCtx);
417251662Sdim    State = State->bindLoc(CLLoc, V);
418239462Sdim
419251662Sdim    // Compound literal expressions are a GNU extension in C++.
420251662Sdim    // Unlike in C, where CLs are lvalues, in C++ CLs are prvalues,
421251662Sdim    // and like temporary objects created by the functional notation T()
422251662Sdim    // CLs are destroyed at the end of the containing full-expression.
423251662Sdim    // HOWEVER, an rvalue of array type is not something the analyzer can
424251662Sdim    // reason about, since we expect all regions to be wrapped in Locs.
425251662Sdim    // So we treat array CLs as lvalues as well, knowing that they will decay
426251662Sdim    // to pointers as soon as they are used.
427251662Sdim    if (CL->isGLValue() || CL->getType()->isArrayType())
428251662Sdim      V = CLLoc;
429251662Sdim  }
430251662Sdim
431251662Sdim  B.generateNode(CL, Pred, State->BindExpr(CL, LCtx, V));
432226586Sdim}
433226586Sdim
434226586Sdimvoid ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
435226586Sdim                               ExplodedNodeSet &Dst) {
436226586Sdim  // Assumption: The CFG has one DeclStmt per Decl.
437249423Sdim  const VarDecl *VD = dyn_cast_or_null<VarDecl>(*DS->decl_begin());
438249423Sdim
439249423Sdim  if (!VD) {
440234353Sdim    //TODO:AZ: remove explicit insertion after refactoring is done.
441234353Sdim    Dst.insert(Pred);
442226586Sdim    return;
443234353Sdim  }
444226586Sdim
445226586Sdim  // FIXME: all pre/post visits should eventually be handled by ::Visit().
446226586Sdim  ExplodedNodeSet dstPreVisit;
447226586Sdim  getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this);
448226586Sdim
449243830Sdim  StmtNodeBuilder B(dstPreVisit, Dst, *currBldrCtx);
450226586Sdim  for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
451226586Sdim       I!=E; ++I) {
452226586Sdim    ExplodedNode *N = *I;
453234353Sdim    ProgramStateRef state = N->getState();
454249423Sdim    const LocationContext *LC = N->getLocationContext();
455249423Sdim
456226586Sdim    // Decls without InitExpr are not initialized explicitly.
457226586Sdim    if (const Expr *InitEx = VD->getInit()) {
458249423Sdim
459249423Sdim      // Note in the state that the initialization has occurred.
460249423Sdim      ExplodedNode *UpdatedN = N;
461239462Sdim      SVal InitVal = state->getSVal(InitEx, LC);
462234353Sdim
463249423Sdim      if (isa<CXXConstructExpr>(InitEx->IgnoreImplicit())) {
464239462Sdim        // We constructed the object directly in the variable.
465239462Sdim        // No need to bind anything.
466249423Sdim        B.generateNode(DS, UpdatedN, state);
467239462Sdim      } else {
468239462Sdim        // We bound the temp obj region to the CXXConstructExpr. Now recover
469239462Sdim        // the lazy compound value when the variable is not a reference.
470249423Sdim        if (AMgr.getLangOpts().CPlusPlus && VD->getType()->isRecordType() &&
471249423Sdim            !VD->getType()->isReferenceType()) {
472249423Sdim          if (Optional<loc::MemRegionVal> M =
473249423Sdim                  InitVal.getAs<loc::MemRegionVal>()) {
474249423Sdim            InitVal = state->getSVal(M->getRegion());
475249423Sdim            assert(InitVal.getAs<nonloc::LazyCompoundVal>());
476249423Sdim          }
477239462Sdim        }
478239462Sdim
479239462Sdim        // Recover some path-sensitivity if a scalar value evaluated to
480239462Sdim        // UnknownVal.
481239462Sdim        if (InitVal.isUnknown()) {
482239462Sdim          QualType Ty = InitEx->getType();
483239462Sdim          if (InitEx->isGLValue()) {
484239462Sdim            Ty = getContext().getPointerType(Ty);
485239462Sdim          }
486239462Sdim
487243830Sdim          InitVal = svalBuilder.conjureSymbolVal(0, InitEx, LC, Ty,
488243830Sdim                                                 currBldrCtx->blockCount());
489239462Sdim        }
490249423Sdim
491249423Sdim
492249423Sdim        B.takeNodes(UpdatedN);
493239462Sdim        ExplodedNodeSet Dst2;
494249423Sdim        evalBind(Dst2, DS, UpdatedN, state->getLValue(VD, LC), InitVal, true);
495239462Sdim        B.addNodes(Dst2);
496226586Sdim      }
497226586Sdim    }
498226586Sdim    else {
499243830Sdim      B.generateNode(DS, N, state);
500226586Sdim    }
501226586Sdim  }
502226586Sdim}
503226586Sdim
504226586Sdimvoid ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
505226586Sdim                                  ExplodedNodeSet &Dst) {
506226586Sdim  assert(B->getOpcode() == BO_LAnd ||
507226586Sdim         B->getOpcode() == BO_LOr);
508234353Sdim
509243830Sdim  StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
510234353Sdim  ProgramStateRef state = Pred->getState();
511239462Sdim
512239462Sdim  ExplodedNode *N = Pred;
513249423Sdim  while (!N->getLocation().getAs<BlockEntrance>()) {
514239462Sdim    ProgramPoint P = N->getLocation();
515249423Sdim    assert(P.getAs<PreStmt>()|| P.getAs<PreStmtPurgeDeadSymbols>());
516239462Sdim    (void) P;
517239462Sdim    assert(N->pred_size() == 1);
518239462Sdim    N = *N->pred_begin();
519226586Sdim  }
520239462Sdim  assert(N->pred_size() == 1);
521239462Sdim  N = *N->pred_begin();
522249423Sdim  BlockEdge BE = N->getLocation().castAs<BlockEdge>();
523239462Sdim  SVal X;
524239462Sdim
525239462Sdim  // Determine the value of the expression by introspecting how we
526239462Sdim  // got this location in the CFG.  This requires looking at the previous
527239462Sdim  // block we were in and what kind of control-flow transfer was involved.
528239462Sdim  const CFGBlock *SrcBlock = BE.getSrc();
529239462Sdim  // The only terminator (if there is one) that makes sense is a logical op.
530239462Sdim  CFGTerminator T = SrcBlock->getTerminator();
531239462Sdim  if (const BinaryOperator *Term = cast_or_null<BinaryOperator>(T.getStmt())) {
532239462Sdim    (void) Term;
533239462Sdim    assert(Term->isLogicalOp());
534239462Sdim    assert(SrcBlock->succ_size() == 2);
535239462Sdim    // Did we take the true or false branch?
536239462Sdim    unsigned constant = (*SrcBlock->succ_begin() == BE.getDst()) ? 1 : 0;
537239462Sdim    X = svalBuilder.makeIntVal(constant, B->getType());
538239462Sdim  }
539226586Sdim  else {
540239462Sdim    // If there is no terminator, by construction the last statement
541239462Sdim    // in SrcBlock is the value of the enclosing expression.
542243830Sdim    // However, we still need to constrain that value to be 0 or 1.
543239462Sdim    assert(!SrcBlock->empty());
544249423Sdim    CFGStmt Elem = SrcBlock->rbegin()->castAs<CFGStmt>();
545243830Sdim    const Expr *RHS = cast<Expr>(Elem.getStmt());
546243830Sdim    SVal RHSVal = N->getState()->getSVal(RHS, Pred->getLocationContext());
547243830Sdim
548249423Sdim    if (RHSVal.isUndef()) {
549249423Sdim      X = RHSVal;
550249423Sdim    } else {
551249423Sdim      DefinedOrUnknownSVal DefinedRHS = RHSVal.castAs<DefinedOrUnknownSVal>();
552249423Sdim      ProgramStateRef StTrue, StFalse;
553249423Sdim      llvm::tie(StTrue, StFalse) = N->getState()->assume(DefinedRHS);
554249423Sdim      if (StTrue) {
555249423Sdim        if (StFalse) {
556249423Sdim          // We can't constrain the value to 0 or 1.
557249423Sdim          // The best we can do is a cast.
558249423Sdim          X = getSValBuilder().evalCast(RHSVal, B->getType(), RHS->getType());
559249423Sdim        } else {
560249423Sdim          // The value is known to be true.
561249423Sdim          X = getSValBuilder().makeIntVal(1, B->getType());
562249423Sdim        }
563243830Sdim      } else {
564249423Sdim        // The value is known to be false.
565249423Sdim        assert(StFalse && "Infeasible path!");
566249423Sdim        X = getSValBuilder().makeIntVal(0, B->getType());
567243830Sdim      }
568243830Sdim    }
569226586Sdim  }
570239462Sdim  Bldr.generateNode(B, Pred, state->BindExpr(B, Pred->getLocationContext(), X));
571226586Sdim}
572226586Sdim
573226586Sdimvoid ExprEngine::VisitInitListExpr(const InitListExpr *IE,
574226586Sdim                                   ExplodedNode *Pred,
575226586Sdim                                   ExplodedNodeSet &Dst) {
576243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
577226586Sdim
578234353Sdim  ProgramStateRef state = Pred->getState();
579234353Sdim  const LocationContext *LCtx = Pred->getLocationContext();
580226586Sdim  QualType T = getContext().getCanonicalType(IE->getType());
581226586Sdim  unsigned NumInitElements = IE->getNumInits();
582226586Sdim
583243830Sdim  if (T->isArrayType() || T->isRecordType() || T->isVectorType() ||
584243830Sdim      T->isAnyComplexType()) {
585226586Sdim    llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList();
586226586Sdim
587226586Sdim    // Handle base case where the initializer has no elements.
588226586Sdim    // e.g: static int* myArray[] = {};
589226586Sdim    if (NumInitElements == 0) {
590226586Sdim      SVal V = svalBuilder.makeCompoundVal(T, vals);
591234353Sdim      B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
592226586Sdim      return;
593226586Sdim    }
594226586Sdim
595226586Sdim    for (InitListExpr::const_reverse_iterator it = IE->rbegin(),
596226586Sdim         ei = IE->rend(); it != ei; ++it) {
597249423Sdim      SVal V = state->getSVal(cast<Expr>(*it), LCtx);
598249423Sdim      if (dyn_cast_or_null<CXXTempObjectRegion>(V.getAsRegion()))
599249423Sdim        V = UnknownVal();
600249423Sdim      vals = getBasicVals().consVals(V, vals);
601226586Sdim    }
602226586Sdim
603234353Sdim    B.generateNode(IE, Pred,
604234353Sdim                   state->BindExpr(IE, LCtx,
605234353Sdim                                   svalBuilder.makeCompoundVal(T, vals)));
606226586Sdim    return;
607226586Sdim  }
608239462Sdim
609239462Sdim  // Handle scalars: int{5} and int{}.
610239462Sdim  assert(NumInitElements <= 1);
611239462Sdim
612239462Sdim  SVal V;
613239462Sdim  if (NumInitElements == 0)
614239462Sdim    V = getSValBuilder().makeZeroVal(T);
615239462Sdim  else
616239462Sdim    V = state->getSVal(IE->getInit(0), LCtx);
617239462Sdim
618239462Sdim  B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
619226586Sdim}
620226586Sdim
621226586Sdimvoid ExprEngine::VisitGuardedExpr(const Expr *Ex,
622226586Sdim                                  const Expr *L,
623226586Sdim                                  const Expr *R,
624226586Sdim                                  ExplodedNode *Pred,
625226586Sdim                                  ExplodedNodeSet &Dst) {
626251662Sdim  assert(L && R);
627251662Sdim
628243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
629234353Sdim  ProgramStateRef state = Pred->getState();
630234353Sdim  const LocationContext *LCtx = Pred->getLocationContext();
631239462Sdim  const CFGBlock *SrcBlock = 0;
632239462Sdim
633251662Sdim  // Find the predecessor block.
634251662Sdim  ProgramStateRef SrcState = state;
635239462Sdim  for (const ExplodedNode *N = Pred ; N ; N = *N->pred_begin()) {
636239462Sdim    ProgramPoint PP = N->getLocation();
637249423Sdim    if (PP.getAs<PreStmtPurgeDeadSymbols>() || PP.getAs<BlockEntrance>()) {
638239462Sdim      assert(N->pred_size() == 1);
639239462Sdim      continue;
640239462Sdim    }
641249423Sdim    SrcBlock = PP.castAs<BlockEdge>().getSrc();
642251662Sdim    SrcState = N->getState();
643239462Sdim    break;
644239462Sdim  }
645239462Sdim
646249423Sdim  assert(SrcBlock && "missing function entry");
647249423Sdim
648239462Sdim  // Find the last expression in the predecessor block.  That is the
649239462Sdim  // expression that is used for the value of the ternary expression.
650239462Sdim  bool hasValue = false;
651239462Sdim  SVal V;
652239462Sdim
653239462Sdim  for (CFGBlock::const_reverse_iterator I = SrcBlock->rbegin(),
654239462Sdim                                        E = SrcBlock->rend(); I != E; ++I) {
655239462Sdim    CFGElement CE = *I;
656249423Sdim    if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
657239462Sdim      const Expr *ValEx = cast<Expr>(CS->getStmt());
658251662Sdim      ValEx = ValEx->IgnoreParens();
659251662Sdim
660251662Sdim      // For GNU extension '?:' operator, the left hand side will be an
661251662Sdim      // OpaqueValueExpr, so get the underlying expression.
662251662Sdim      if (const OpaqueValueExpr *OpaqueEx = dyn_cast<OpaqueValueExpr>(L))
663251662Sdim        L = OpaqueEx->getSourceExpr();
664251662Sdim
665251662Sdim      // If the last expression in the predecessor block matches true or false
666251662Sdim      // subexpression, get its the value.
667251662Sdim      if (ValEx == L->IgnoreParens() || ValEx == R->IgnoreParens()) {
668251662Sdim        hasValue = true;
669251662Sdim        V = SrcState->getSVal(ValEx, LCtx);
670251662Sdim      }
671239462Sdim      break;
672239462Sdim    }
673239462Sdim  }
674239462Sdim
675251662Sdim  if (!hasValue)
676251662Sdim    V = svalBuilder.conjureSymbolVal(0, Ex, LCtx, currBldrCtx->blockCount());
677239462Sdim
678239462Sdim  // Generate a new node with the binding from the appropriate path.
679239462Sdim  B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V, true));
680226586Sdim}
681226586Sdim
682226586Sdimvoid ExprEngine::
683226586SdimVisitOffsetOfExpr(const OffsetOfExpr *OOE,
684226586Sdim                  ExplodedNode *Pred, ExplodedNodeSet &Dst) {
685243830Sdim  StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
686234353Sdim  APSInt IV;
687234353Sdim  if (OOE->EvaluateAsInt(IV, getContext())) {
688226586Sdim    assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
689251662Sdim    assert(OOE->getType()->isBuiltinType());
690251662Sdim    assert(OOE->getType()->getAs<BuiltinType>()->isInteger());
691251662Sdim    assert(IV.isSigned() == OOE->getType()->isSignedIntegerType());
692226586Sdim    SVal X = svalBuilder.makeIntVal(IV);
693234353Sdim    B.generateNode(OOE, Pred,
694234353Sdim                   Pred->getState()->BindExpr(OOE, Pred->getLocationContext(),
695234353Sdim                                              X));
696226586Sdim  }
697226586Sdim  // FIXME: Handle the case where __builtin_offsetof is not a constant.
698226586Sdim}
699226586Sdim
700226586Sdim
701226586Sdimvoid ExprEngine::
702226586SdimVisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
703226586Sdim                              ExplodedNode *Pred,
704226586Sdim                              ExplodedNodeSet &Dst) {
705243830Sdim  StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
706226586Sdim
707226586Sdim  QualType T = Ex->getTypeOfArgument();
708226586Sdim
709226586Sdim  if (Ex->getKind() == UETT_SizeOf) {
710226586Sdim    if (!T->isIncompleteType() && !T->isConstantSizeType()) {
711226586Sdim      assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
712226586Sdim
713226586Sdim      // FIXME: Add support for VLA type arguments and VLA expressions.
714226586Sdim      // When that happens, we should probably refactor VLASizeChecker's code.
715226586Sdim      return;
716226586Sdim    }
717226586Sdim    else if (T->getAs<ObjCObjectType>()) {
718226586Sdim      // Some code tries to take the sizeof an ObjCObjectType, relying that
719226586Sdim      // the compiler has laid out its representation.  Just report Unknown
720226586Sdim      // for these.
721226586Sdim      return;
722226586Sdim    }
723226586Sdim  }
724226586Sdim
725234353Sdim  APSInt Value = Ex->EvaluateKnownConstInt(getContext());
726234353Sdim  CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue());
727226586Sdim
728234353Sdim  ProgramStateRef state = Pred->getState();
729234353Sdim  state = state->BindExpr(Ex, Pred->getLocationContext(),
730234353Sdim                          svalBuilder.makeIntVal(amt.getQuantity(),
731226586Sdim                                                     Ex->getType()));
732234353Sdim  Bldr.generateNode(Ex, Pred, state);
733226586Sdim}
734226586Sdim
735226586Sdimvoid ExprEngine::VisitUnaryOperator(const UnaryOperator* U,
736226586Sdim                                    ExplodedNode *Pred,
737234353Sdim                                    ExplodedNodeSet &Dst) {
738243830Sdim  StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
739226586Sdim  switch (U->getOpcode()) {
740234353Sdim    default: {
741234353Sdim      Bldr.takeNodes(Pred);
742234353Sdim      ExplodedNodeSet Tmp;
743234353Sdim      VisitIncrementDecrementOperator(U, Pred, Tmp);
744234353Sdim      Bldr.addNodes(Tmp);
745234353Sdim    }
746226586Sdim      break;
747226586Sdim    case UO_Real: {
748226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
749226586Sdim
750234353Sdim      // FIXME: We don't have complex SValues yet.
751234353Sdim      if (Ex->getType()->isAnyComplexType()) {
752234353Sdim        // Just report "Unknown."
753234353Sdim        break;
754234353Sdim      }
755226586Sdim
756234353Sdim      // For all other types, UO_Real is an identity operation.
757234353Sdim      assert (U->getType() == Ex->getType());
758234353Sdim      ProgramStateRef state = Pred->getState();
759234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
760234353Sdim      Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx,
761234353Sdim                                                 state->getSVal(Ex, LCtx)));
762234353Sdim      break;
763226586Sdim    }
764226586Sdim
765234353Sdim    case UO_Imag: {
766226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
767234353Sdim      // FIXME: We don't have complex SValues yet.
768234353Sdim      if (Ex->getType()->isAnyComplexType()) {
769234353Sdim        // Just report "Unknown."
770234353Sdim        break;
771226586Sdim      }
772234353Sdim      // For all other types, UO_Imag returns 0.
773234353Sdim      ProgramStateRef state = Pred->getState();
774234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
775234353Sdim      SVal X = svalBuilder.makeZeroVal(Ex->getType());
776234353Sdim      Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, X));
777234353Sdim      break;
778226586Sdim    }
779226586Sdim
780226586Sdim    case UO_Plus:
781239462Sdim      assert(!U->isGLValue());
782226586Sdim      // FALL-THROUGH.
783226586Sdim    case UO_Deref:
784226586Sdim    case UO_AddrOf:
785226586Sdim    case UO_Extension: {
786234353Sdim      // FIXME: We can probably just have some magic in Environment::getSVal()
787234353Sdim      // that propagates values, instead of creating a new node here.
788234353Sdim      //
789226586Sdim      // Unary "+" is a no-op, similar to a parentheses.  We still have places
790226586Sdim      // where it may be a block-level expression, so we need to
791226586Sdim      // generate an extra node that just propagates the value of the
792234353Sdim      // subexpression.
793226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
794234353Sdim      ProgramStateRef state = Pred->getState();
795234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
796234353Sdim      Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx,
797234353Sdim                                                 state->getSVal(Ex, LCtx)));
798234353Sdim      break;
799226586Sdim    }
800226586Sdim
801226586Sdim    case UO_LNot:
802226586Sdim    case UO_Minus:
803226586Sdim    case UO_Not: {
804239462Sdim      assert (!U->isGLValue());
805226586Sdim      const Expr *Ex = U->getSubExpr()->IgnoreParens();
806234353Sdim      ProgramStateRef state = Pred->getState();
807234353Sdim      const LocationContext *LCtx = Pred->getLocationContext();
808226586Sdim
809234353Sdim      // Get the value of the subexpression.
810234353Sdim      SVal V = state->getSVal(Ex, LCtx);
811226586Sdim
812234353Sdim      if (V.isUnknownOrUndef()) {
813234353Sdim        Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, V));
814234353Sdim        break;
815234353Sdim      }
816226586Sdim
817234353Sdim      switch (U->getOpcode()) {
818234353Sdim        default:
819234353Sdim          llvm_unreachable("Invalid Opcode.");
820234353Sdim        case UO_Not:
821234353Sdim          // FIXME: Do we need to handle promotions?
822249423Sdim          state = state->BindExpr(U, LCtx, evalComplement(V.castAs<NonLoc>()));
823234353Sdim          break;
824234353Sdim        case UO_Minus:
825234353Sdim          // FIXME: Do we need to handle promotions?
826249423Sdim          state = state->BindExpr(U, LCtx, evalMinus(V.castAs<NonLoc>()));
827234353Sdim          break;
828234353Sdim        case UO_LNot:
829234353Sdim          // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
830234353Sdim          //
831234353Sdim          //  Note: technically we do "E == 0", but this is the same in the
832234353Sdim          //    transfer functions as "0 == E".
833234353Sdim          SVal Result;
834249423Sdim          if (Optional<Loc> LV = V.getAs<Loc>()) {
835234353Sdim            Loc X = svalBuilder.makeNull();
836249423Sdim            Result = evalBinOp(state, BO_EQ, *LV, X, U->getType());
837234353Sdim          }
838249423Sdim          else if (Ex->getType()->isFloatingType()) {
839249423Sdim            // FIXME: handle floating point types.
840249423Sdim            Result = UnknownVal();
841249423Sdim          } else {
842234353Sdim            nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
843249423Sdim            Result = evalBinOp(state, BO_EQ, V.castAs<NonLoc>(), X,
844234353Sdim                               U->getType());
845234353Sdim          }
846234353Sdim
847234353Sdim          state = state->BindExpr(U, LCtx, Result);
848234353Sdim          break;
849226586Sdim      }
850234353Sdim      Bldr.generateNode(U, Pred, state);
851234353Sdim      break;
852226586Sdim    }
853226586Sdim  }
854234353Sdim
855234353Sdim}
856234353Sdim
857234353Sdimvoid ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U,
858234353Sdim                                                 ExplodedNode *Pred,
859234353Sdim                                                 ExplodedNodeSet &Dst) {
860226586Sdim  // Handle ++ and -- (both pre- and post-increment).
861226586Sdim  assert (U->isIncrementDecrementOp());
862226586Sdim  const Expr *Ex = U->getSubExpr()->IgnoreParens();
863226586Sdim
864234353Sdim  const LocationContext *LCtx = Pred->getLocationContext();
865234353Sdim  ProgramStateRef state = Pred->getState();
866234353Sdim  SVal loc = state->getSVal(Ex, LCtx);
867234353Sdim
868234353Sdim  // Perform a load.
869234353Sdim  ExplodedNodeSet Tmp;
870234353Sdim  evalLoad(Tmp, U, Ex, Pred, state, loc);
871234353Sdim
872234353Sdim  ExplodedNodeSet Dst2;
873243830Sdim  StmtNodeBuilder Bldr(Tmp, Dst2, *currBldrCtx);
874234353Sdim  for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) {
875226586Sdim
876234353Sdim    state = (*I)->getState();
877234353Sdim    assert(LCtx == (*I)->getLocationContext());
878234353Sdim    SVal V2_untested = state->getSVal(Ex, LCtx);
879226586Sdim
880234353Sdim    // Propagate unknown and undefined values.
881234353Sdim    if (V2_untested.isUnknownOrUndef()) {
882234353Sdim      Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V2_untested));
883234353Sdim      continue;
884234353Sdim    }
885249423Sdim    DefinedSVal V2 = V2_untested.castAs<DefinedSVal>();
886226586Sdim
887234353Sdim    // Handle all other values.
888234353Sdim    BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub;
889234353Sdim
890234353Sdim    // If the UnaryOperator has non-location type, use its type to create the
891234353Sdim    // constant value. If the UnaryOperator has location type, create the
892234353Sdim    // constant with int type and pointer width.
893234353Sdim    SVal RHS;
894234353Sdim
895234353Sdim    if (U->getType()->isAnyPointerType())
896234353Sdim      RHS = svalBuilder.makeArrayIndex(1);
897243830Sdim    else if (U->getType()->isIntegralOrEnumerationType())
898243830Sdim      RHS = svalBuilder.makeIntVal(1, U->getType());
899234353Sdim    else
900243830Sdim      RHS = UnknownVal();
901234353Sdim
902234353Sdim    SVal Result = evalBinOp(state, Op, V2, RHS, U->getType());
903234353Sdim
904234353Sdim    // Conjure a new symbol if necessary to recover precision.
905234353Sdim    if (Result.isUnknown()){
906234353Sdim      DefinedOrUnknownSVal SymVal =
907243830Sdim        svalBuilder.conjureSymbolVal(0, Ex, LCtx, currBldrCtx->blockCount());
908234353Sdim      Result = SymVal;
909226586Sdim
910234353Sdim      // If the value is a location, ++/-- should always preserve
911234353Sdim      // non-nullness.  Check if the original value was non-null, and if so
912234353Sdim      // propagate that constraint.
913234353Sdim      if (Loc::isLocType(U->getType())) {
914234353Sdim        DefinedOrUnknownSVal Constraint =
915234353Sdim        svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType()));
916226586Sdim
917234353Sdim        if (!state->assume(Constraint, true)) {
918234353Sdim          // It isn't feasible for the original value to be null.
919234353Sdim          // Propagate this constraint.
920234353Sdim          Constraint = svalBuilder.evalEQ(state, SymVal,
921234353Sdim                                       svalBuilder.makeZeroVal(U->getType()));
922226586Sdim
923234353Sdim
924234353Sdim          state = state->assume(Constraint, false);
925234353Sdim          assert(state);
926226586Sdim        }
927226586Sdim      }
928226586Sdim    }
929234353Sdim
930234353Sdim    // Since the lvalue-to-rvalue conversion is explicit in the AST,
931234353Sdim    // we bind an l-value if the operator is prefix and an lvalue (in C++).
932239462Sdim    if (U->isGLValue())
933234353Sdim      state = state->BindExpr(U, LCtx, loc);
934234353Sdim    else
935234353Sdim      state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result);
936234353Sdim
937234353Sdim    // Perform the store.
938234353Sdim    Bldr.takeNodes(*I);
939234353Sdim    ExplodedNodeSet Dst3;
940234353Sdim    evalStore(Dst3, U, U, *I, state, loc, Result);
941234353Sdim    Bldr.addNodes(Dst3);
942226586Sdim  }
943234353Sdim  Dst.insert(Dst2);
944226586Sdim}
945