1 //=-- ExprEngineC.cpp - ExprEngine support for C expressions ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines ExprEngine's support for C expressions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
16 
17 using namespace clang;
18 using namespace ento;
19 using llvm::APSInt;
20 
21 void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
22                                      ExplodedNode *Pred,
23                                      ExplodedNodeSet &Dst) {
24 
25   Expr *LHS = B->getLHS()->IgnoreParens();
26   Expr *RHS = B->getRHS()->IgnoreParens();
27 
28   // FIXME: Prechecks eventually go in ::Visit().
29   ExplodedNodeSet CheckedSet;
30   ExplodedNodeSet Tmp2;
31   getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, B, *this);
32 
33   // With both the LHS and RHS evaluated, process the operation itself.
34   for (ExplodedNodeSet::iterator it=CheckedSet.begin(), ei=CheckedSet.end();
35          it != ei; ++it) {
36 
37     const ProgramState *state = (*it)->getState();
38     SVal LeftV = state->getSVal(LHS);
39     SVal RightV = state->getSVal(RHS);
40 
41     BinaryOperator::Opcode Op = B->getOpcode();
42 
43     if (Op == BO_Assign) {
44       // EXPERIMENTAL: "Conjured" symbols.
45       // FIXME: Handle structs.
46       if (RightV.isUnknown() ||
47           !getConstraintManager().canReasonAbout(RightV)) {
48         unsigned Count = currentBuilderContext->getCurrentBlockCount();
49         RightV = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), Count);
50       }
51       // Simulate the effects of a "store":  bind the value of the RHS
52       // to the L-Value represented by the LHS.
53       SVal ExprVal = B->isLValue() ? LeftV : RightV;
54       evalStore(Tmp2, B, LHS, *it, state->BindExpr(B, ExprVal), LeftV, RightV);
55       continue;
56     }
57 
58     if (!B->isAssignmentOp()) {
59       StmtNodeBuilder Bldr(*it, Tmp2, *currentBuilderContext);
60       // Process non-assignments except commas or short-circuited
61       // logical expressions (LAnd and LOr).
62       SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
63       if (Result.isUnknown()) {
64         Bldr.generateNode(B, *it, state);
65         continue;
66       }
67 
68       state = state->BindExpr(B, Result);
69       Bldr.generateNode(B, *it, state);
70       continue;
71     }
72 
73     assert (B->isCompoundAssignmentOp());
74 
75     switch (Op) {
76       default:
77         llvm_unreachable("Invalid opcode for compound assignment.");
78       case BO_MulAssign: Op = BO_Mul; break;
79       case BO_DivAssign: Op = BO_Div; break;
80       case BO_RemAssign: Op = BO_Rem; break;
81       case BO_AddAssign: Op = BO_Add; break;
82       case BO_SubAssign: Op = BO_Sub; break;
83       case BO_ShlAssign: Op = BO_Shl; break;
84       case BO_ShrAssign: Op = BO_Shr; break;
85       case BO_AndAssign: Op = BO_And; break;
86       case BO_XorAssign: Op = BO_Xor; break;
87       case BO_OrAssign:  Op = BO_Or;  break;
88     }
89 
90     // Perform a load (the LHS).  This performs the checks for
91     // null dereferences, and so on.
92     ExplodedNodeSet Tmp;
93     SVal location = LeftV;
94     evalLoad(Tmp, LHS, *it, state, location);
95 
96     for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E;
97          ++I) {
98 
99       state = (*I)->getState();
100       SVal V = state->getSVal(LHS);
101 
102       // Get the computation type.
103       QualType CTy =
104         cast<CompoundAssignOperator>(B)->getComputationResultType();
105       CTy = getContext().getCanonicalType(CTy);
106 
107       QualType CLHSTy =
108         cast<CompoundAssignOperator>(B)->getComputationLHSType();
109       CLHSTy = getContext().getCanonicalType(CLHSTy);
110 
111       QualType LTy = getContext().getCanonicalType(LHS->getType());
112 
113       // Promote LHS.
114       V = svalBuilder.evalCast(V, CLHSTy, LTy);
115 
116       // Compute the result of the operation.
117       SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy),
118                                          B->getType(), CTy);
119 
120       // EXPERIMENTAL: "Conjured" symbols.
121       // FIXME: Handle structs.
122 
123       SVal LHSVal;
124 
125       if (Result.isUnknown() ||
126           !getConstraintManager().canReasonAbout(Result)) {
127 
128         unsigned Count = currentBuilderContext->getCurrentBlockCount();
129 
130         // The symbolic value is actually for the type of the left-hand side
131         // expression, not the computation type, as this is the value the
132         // LValue on the LHS will bind to.
133         LHSVal = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), LTy,
134                                                   Count);
135 
136         // However, we need to convert the symbol to the computation type.
137         Result = svalBuilder.evalCast(LHSVal, CTy, LTy);
138       }
139       else {
140         // The left-hand side may bind to a different value then the
141         // computation type.
142         LHSVal = svalBuilder.evalCast(Result, LTy, CTy);
143       }
144 
145       // In C++, assignment and compound assignment operators return an
146       // lvalue.
147       if (B->isLValue())
148         state = state->BindExpr(B, location);
149       else
150         state = state->BindExpr(B, Result);
151 
152       evalStore(Tmp2, B, LHS, *I, state, location, LHSVal);
153     }
154   }
155 
156   // FIXME: postvisits eventually go in ::Visit()
157   getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
158 }
159 
160 void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
161                                 ExplodedNodeSet &Dst) {
162 
163   CanQualType T = getContext().getCanonicalType(BE->getType());
164   SVal V = svalBuilder.getBlockPointer(BE->getBlockDecl(), T,
165                                        Pred->getLocationContext());
166 
167   ExplodedNodeSet Tmp;
168   StmtNodeBuilder Bldr(Pred, Tmp, *currentBuilderContext);
169   Bldr.generateNode(BE, Pred, Pred->getState()->BindExpr(BE, V), false, 0,
170                     ProgramPoint::PostLValueKind);
171 
172   // FIXME: Move all post/pre visits to ::Visit().
173   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this);
174 }
175 
176 void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
177                            ExplodedNode *Pred, ExplodedNodeSet &Dst) {
178 
179   ExplodedNodeSet dstPreStmt;
180   getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);
181 
182   if (CastE->getCastKind() == CK_LValueToRValue ||
183       CastE->getCastKind() == CK_GetObjCProperty) {
184     for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
185          I!=E; ++I) {
186       ExplodedNode *subExprNode = *I;
187       const ProgramState *state = subExprNode->getState();
188       evalLoad(Dst, CastE, subExprNode, state, state->getSVal(Ex));
189     }
190     return;
191   }
192 
193   // All other casts.
194   QualType T = CastE->getType();
195   QualType ExTy = Ex->getType();
196 
197   if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
198     T = ExCast->getTypeAsWritten();
199 
200   StmtNodeBuilder Bldr(dstPreStmt, Dst, *currentBuilderContext);
201   for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
202        I != E; ++I) {
203 
204     Pred = *I;
205 
206     switch (CastE->getCastKind()) {
207       case CK_LValueToRValue:
208         llvm_unreachable("LValueToRValue casts handled earlier.");
209       case CK_GetObjCProperty:
210         llvm_unreachable("GetObjCProperty casts handled earlier.");
211       case CK_ToVoid:
212         continue;
213         // The analyzer doesn't do anything special with these casts,
214         // since it understands retain/release semantics already.
215       case CK_ARCProduceObject:
216       case CK_ARCConsumeObject:
217       case CK_ARCReclaimReturnedObject:
218       case CK_ARCExtendBlockObject: // Fall-through.
219         // True no-ops.
220       case CK_NoOp:
221       case CK_FunctionToPointerDecay: {
222         // Copy the SVal of Ex to CastE.
223         const ProgramState *state = Pred->getState();
224         SVal V = state->getSVal(Ex);
225         state = state->BindExpr(CastE, V);
226         Bldr.generateNode(CastE, Pred, state);
227         continue;
228       }
229       case CK_Dependent:
230       case CK_ArrayToPointerDecay:
231       case CK_BitCast:
232       case CK_LValueBitCast:
233       case CK_IntegralCast:
234       case CK_NullToPointer:
235       case CK_IntegralToPointer:
236       case CK_PointerToIntegral:
237       case CK_PointerToBoolean:
238       case CK_IntegralToBoolean:
239       case CK_IntegralToFloating:
240       case CK_FloatingToIntegral:
241       case CK_FloatingToBoolean:
242       case CK_FloatingCast:
243       case CK_FloatingRealToComplex:
244       case CK_FloatingComplexToReal:
245       case CK_FloatingComplexToBoolean:
246       case CK_FloatingComplexCast:
247       case CK_FloatingComplexToIntegralComplex:
248       case CK_IntegralRealToComplex:
249       case CK_IntegralComplexToReal:
250       case CK_IntegralComplexToBoolean:
251       case CK_IntegralComplexCast:
252       case CK_IntegralComplexToFloatingComplex:
253       case CK_CPointerToObjCPointerCast:
254       case CK_BlockPointerToObjCPointerCast:
255       case CK_AnyPointerToBlockPointerCast:
256       case CK_ObjCObjectLValueCast: {
257         // Delegate to SValBuilder to process.
258         const ProgramState *state = Pred->getState();
259         SVal V = state->getSVal(Ex);
260         V = svalBuilder.evalCast(V, T, ExTy);
261         state = state->BindExpr(CastE, V);
262         Bldr.generateNode(CastE, Pred, state);
263         continue;
264       }
265       case CK_DerivedToBase:
266       case CK_UncheckedDerivedToBase: {
267         // For DerivedToBase cast, delegate to the store manager.
268         const ProgramState *state = Pred->getState();
269         SVal val = state->getSVal(Ex);
270         val = getStoreManager().evalDerivedToBase(val, T);
271         state = state->BindExpr(CastE, val);
272         Bldr.generateNode(CastE, Pred, state);
273         continue;
274       }
275         // Various C++ casts that are not handled yet.
276       case CK_Dynamic:
277       case CK_ToUnion:
278       case CK_BaseToDerived:
279       case CK_NullToMemberPointer:
280       case CK_BaseToDerivedMemberPointer:
281       case CK_DerivedToBaseMemberPointer:
282       case CK_UserDefinedConversion:
283       case CK_ConstructorConversion:
284       case CK_VectorSplat:
285       case CK_MemberPointerToBoolean: {
286         // Recover some path-sensitivty by conjuring a new value.
287         QualType resultType = CastE->getType();
288         if (CastE->isLValue())
289           resultType = getContext().getPointerType(resultType);
290 
291         SVal result =
292         svalBuilder.getConjuredSymbolVal(NULL, CastE, resultType,
293                                currentBuilderContext->getCurrentBlockCount());
294 
295         const ProgramState *state = Pred->getState()->BindExpr(CastE, result);
296         Bldr.generateNode(CastE, Pred, state);
297         continue;
298       }
299     }
300   }
301 }
302 
303 void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL,
304                                           ExplodedNode *Pred,
305                                           ExplodedNodeSet &Dst) {
306   StmtNodeBuilder B(Pred, Dst, *currentBuilderContext);
307 
308   const InitListExpr *ILE
309     = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
310 
311   const ProgramState *state = Pred->getState();
312   SVal ILV = state->getSVal(ILE);
313   const LocationContext *LC = Pred->getLocationContext();
314   state = state->bindCompoundLiteral(CL, LC, ILV);
315 
316   if (CL->isLValue())
317     B.generateNode(CL, Pred, state->BindExpr(CL, state->getLValue(CL, LC)));
318   else
319     B.generateNode(CL, Pred, state->BindExpr(CL, ILV));
320 }
321 
322 void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
323                                ExplodedNodeSet &Dst) {
324 
325   // FIXME: static variables may have an initializer, but the second
326   //  time a function is called those values may not be current.
327   //  This may need to be reflected in the CFG.
328 
329   // Assumption: The CFG has one DeclStmt per Decl.
330   const Decl *D = *DS->decl_begin();
331 
332   if (!D || !isa<VarDecl>(D)) {
333     //TODO:AZ: remove explicit insertion after refactoring is done.
334     Dst.insert(Pred);
335     return;
336   }
337 
338   // FIXME: all pre/post visits should eventually be handled by ::Visit().
339   ExplodedNodeSet dstPreVisit;
340   getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this);
341 
342   StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext);
343   const VarDecl *VD = dyn_cast<VarDecl>(D);
344   for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
345        I!=E; ++I) {
346     ExplodedNode *N = *I;
347     const ProgramState *state = N->getState();
348 
349     // Decls without InitExpr are not initialized explicitly.
350     const LocationContext *LC = N->getLocationContext();
351 
352     if (const Expr *InitEx = VD->getInit()) {
353       SVal InitVal = state->getSVal(InitEx);
354 
355       // We bound the temp obj region to the CXXConstructExpr. Now recover
356       // the lazy compound value when the variable is not a reference.
357       if (AMgr.getLangOptions().CPlusPlus && VD->getType()->isRecordType() &&
358           !VD->getType()->isReferenceType() && isa<loc::MemRegionVal>(InitVal)){
359         InitVal = state->getSVal(cast<loc::MemRegionVal>(InitVal).getRegion());
360         assert(isa<nonloc::LazyCompoundVal>(InitVal));
361       }
362 
363       // Recover some path-sensitivity if a scalar value evaluated to
364       // UnknownVal.
365       if ((InitVal.isUnknown() ||
366            !getConstraintManager().canReasonAbout(InitVal)) &&
367           !VD->getType()->isReferenceType()) {
368         InitVal = svalBuilder.getConjuredSymbolVal(NULL, InitEx,
369                                  currentBuilderContext->getCurrentBlockCount());
370       }
371       B.takeNodes(N);
372       ExplodedNodeSet Dst2;
373       evalBind(Dst2, DS, N, state->getLValue(VD, LC), InitVal, true);
374       B.addNodes(Dst2);
375     }
376     else {
377       B.generateNode(DS, N,state->bindDeclWithNoInit(state->getRegion(VD, LC)));
378     }
379   }
380 }
381 
382 void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
383                                   ExplodedNodeSet &Dst) {
384   assert(B->getOpcode() == BO_LAnd ||
385          B->getOpcode() == BO_LOr);
386 
387   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
388   const ProgramState *state = Pred->getState();
389   SVal X = state->getSVal(B);
390   assert(X.isUndef());
391 
392   const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData();
393   assert(Ex);
394 
395   if (Ex == B->getRHS()) {
396     X = state->getSVal(Ex);
397 
398     // Handle undefined values.
399     if (X.isUndef()) {
400       Bldr.generateNode(B, Pred, state->BindExpr(B, X));
401       return;
402     }
403 
404     DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X);
405 
406     // We took the RHS.  Because the value of the '&&' or '||' expression must
407     // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
408     // or 1.  Alternatively, we could take a lazy approach, and calculate this
409     // value later when necessary.  We don't have the machinery in place for
410     // this right now, and since most logical expressions are used for branches,
411     // the payoff is not likely to be large.  Instead, we do eager evaluation.
412     if (const ProgramState *newState = state->assume(XD, true))
413       Bldr.generateNode(B, Pred,
414                newState->BindExpr(B, svalBuilder.makeIntVal(1U, B->getType())));
415 
416     if (const ProgramState *newState = state->assume(XD, false))
417       Bldr.generateNode(B, Pred,
418                newState->BindExpr(B, svalBuilder.makeIntVal(0U, B->getType())));
419   }
420   else {
421     // We took the LHS expression.  Depending on whether we are '&&' or
422     // '||' we know what the value of the expression is via properties of
423     // the short-circuiting.
424     X = svalBuilder.makeIntVal(B->getOpcode() == BO_LAnd ? 0U : 1U,
425                                B->getType());
426     Bldr.generateNode(B, Pred, state->BindExpr(B, X));
427   }
428 }
429 
430 void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
431                                    ExplodedNode *Pred,
432                                    ExplodedNodeSet &Dst) {
433   StmtNodeBuilder B(Pred, Dst, *currentBuilderContext);
434 
435   const ProgramState *state = Pred->getState();
436   QualType T = getContext().getCanonicalType(IE->getType());
437   unsigned NumInitElements = IE->getNumInits();
438 
439   if (T->isArrayType() || T->isRecordType() || T->isVectorType()) {
440     llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList();
441 
442     // Handle base case where the initializer has no elements.
443     // e.g: static int* myArray[] = {};
444     if (NumInitElements == 0) {
445       SVal V = svalBuilder.makeCompoundVal(T, vals);
446       B.generateNode(IE, Pred, state->BindExpr(IE, V));
447       return;
448     }
449 
450     for (InitListExpr::const_reverse_iterator it = IE->rbegin(),
451          ei = IE->rend(); it != ei; ++it) {
452       vals = getBasicVals().consVals(state->getSVal(cast<Expr>(*it)), vals);
453     }
454 
455     B.generateNode(IE, Pred,
456                    state->BindExpr(IE, svalBuilder.makeCompoundVal(T, vals)));
457     return;
458   }
459 
460   if (Loc::isLocType(T) || T->isIntegerType()) {
461     assert(IE->getNumInits() == 1);
462     const Expr *initEx = IE->getInit(0);
463     B.generateNode(IE, Pred, state->BindExpr(IE, state->getSVal(initEx)));
464     return;
465   }
466 
467   llvm_unreachable("unprocessed InitListExpr type");
468 }
469 
470 void ExprEngine::VisitGuardedExpr(const Expr *Ex,
471                                   const Expr *L,
472                                   const Expr *R,
473                                   ExplodedNode *Pred,
474                                   ExplodedNodeSet &Dst) {
475   StmtNodeBuilder B(Pred, Dst, *currentBuilderContext);
476 
477   const ProgramState *state = Pred->getState();
478   SVal X = state->getSVal(Ex);
479   assert (X.isUndef());
480   const Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
481   assert(SE);
482   X = state->getSVal(SE);
483 
484   // Make sure that we invalidate the previous binding.
485   B.generateNode(Ex, Pred, state->BindExpr(Ex, X, true));
486 }
487 
488 void ExprEngine::
489 VisitOffsetOfExpr(const OffsetOfExpr *OOE,
490                   ExplodedNode *Pred, ExplodedNodeSet &Dst) {
491   StmtNodeBuilder B(Pred, Dst, *currentBuilderContext);
492   Expr::EvalResult Res;
493   if (OOE->EvaluateAsRValue(Res, getContext()) && Res.Val.isInt()) {
494     const APSInt &IV = Res.Val.getInt();
495     assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
496     assert(OOE->getType()->isIntegerType());
497     assert(IV.isSigned() == OOE->getType()->isSignedIntegerOrEnumerationType());
498     SVal X = svalBuilder.makeIntVal(IV);
499     B.generateNode(OOE, Pred, Pred->getState()->BindExpr(OOE, X));
500   }
501   // FIXME: Handle the case where __builtin_offsetof is not a constant.
502 }
503 
504 
505 void ExprEngine::
506 VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
507                               ExplodedNode *Pred,
508                               ExplodedNodeSet &Dst) {
509   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
510 
511   QualType T = Ex->getTypeOfArgument();
512 
513   if (Ex->getKind() == UETT_SizeOf) {
514     if (!T->isIncompleteType() && !T->isConstantSizeType()) {
515       assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
516 
517       // FIXME: Add support for VLA type arguments and VLA expressions.
518       // When that happens, we should probably refactor VLASizeChecker's code.
519       return;
520     }
521     else if (T->getAs<ObjCObjectType>()) {
522       // Some code tries to take the sizeof an ObjCObjectType, relying that
523       // the compiler has laid out its representation.  Just report Unknown
524       // for these.
525       return;
526     }
527   }
528 
529   Expr::EvalResult Result;
530   Ex->EvaluateAsRValue(Result, getContext());
531   CharUnits amt = CharUnits::fromQuantity(Result.Val.getInt().getZExtValue());
532 
533   const ProgramState *state = Pred->getState();
534   state = state->BindExpr(Ex, svalBuilder.makeIntVal(amt.getQuantity(),
535                                                      Ex->getType()));
536   Bldr.generateNode(Ex, Pred, state);
537 }
538 
539 void ExprEngine::VisitUnaryOperator(const UnaryOperator* U,
540                                     ExplodedNode *Pred,
541                                     ExplodedNodeSet &Dst) {
542   StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext);
543   bool IncDec = false;
544   switch (U->getOpcode()) {
545     default: {
546       Bldr.takeNodes(Pred);
547       IncDec = true;
548       ExplodedNodeSet Tmp;
549       VisitIncrementDecrementOperator(U, Pred, Tmp);
550       Bldr.addNodes(Tmp);
551     }
552       break;
553     case UO_Real: {
554       const Expr *Ex = U->getSubExpr()->IgnoreParens();
555       ExplodedNodeSet Tmp;
556       Visit(Ex, Pred, Tmp);
557 
558       for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
559 
560         // FIXME: We don't have complex SValues yet.
561         if (Ex->getType()->isAnyComplexType()) {
562           // Just report "Unknown."
563           continue;
564         }
565 
566         // For all other types, UO_Real is an identity operation.
567         assert (U->getType() == Ex->getType());
568         const ProgramState *state = (*I)->getState();
569         Bldr.generateNode(U, *I, state->BindExpr(U, state->getSVal(Ex)));
570       }
571 
572       break;
573     }
574 
575     case UO_Imag: {
576 
577       const Expr *Ex = U->getSubExpr()->IgnoreParens();
578       ExplodedNodeSet Tmp;
579       Visit(Ex, Pred, Tmp);
580 
581       for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
582         // FIXME: We don't have complex SValues yet.
583         if (Ex->getType()->isAnyComplexType()) {
584           // Just report "Unknown."
585           continue;
586         }
587 
588         // For all other types, UO_Imag returns 0.
589         const ProgramState *state = (*I)->getState();
590         SVal X = svalBuilder.makeZeroVal(Ex->getType());
591         Bldr.generateNode(U, *I, state->BindExpr(U, X));
592       }
593 
594       break;
595     }
596 
597     case UO_Plus:
598       assert(!U->isLValue());
599       // FALL-THROUGH.
600     case UO_Deref:
601     case UO_AddrOf:
602     case UO_Extension: {
603 
604       // Unary "+" is a no-op, similar to a parentheses.  We still have places
605       // where it may be a block-level expression, so we need to
606       // generate an extra node that just propagates the value of the
607       // subexpression.
608 
609       const Expr *Ex = U->getSubExpr()->IgnoreParens();
610       ExplodedNodeSet Tmp;
611       Visit(Ex, Pred, Tmp);
612 
613       for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
614         const ProgramState *state = (*I)->getState();
615         Bldr.generateNode(U, *I, state->BindExpr(U, state->getSVal(Ex)));
616       }
617 
618       break;
619     }
620 
621     case UO_LNot:
622     case UO_Minus:
623     case UO_Not: {
624       assert (!U->isLValue());
625       const Expr *Ex = U->getSubExpr()->IgnoreParens();
626       ExplodedNodeSet Tmp;
627       Visit(Ex, Pred, Tmp);
628 
629       for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
630         const ProgramState *state = (*I)->getState();
631 
632         // Get the value of the subexpression.
633         SVal V = state->getSVal(Ex);
634 
635         if (V.isUnknownOrUndef()) {
636           Bldr.generateNode(U, *I, state->BindExpr(U, V));
637           continue;
638         }
639 
640         switch (U->getOpcode()) {
641           default:
642             llvm_unreachable("Invalid Opcode.");
643 
644           case UO_Not:
645             // FIXME: Do we need to handle promotions?
646             state = state->BindExpr(U, evalComplement(cast<NonLoc>(V)));
647             break;
648 
649           case UO_Minus:
650             // FIXME: Do we need to handle promotions?
651             state = state->BindExpr(U, evalMinus(cast<NonLoc>(V)));
652             break;
653 
654           case UO_LNot:
655 
656             // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
657             //
658             //  Note: technically we do "E == 0", but this is the same in the
659             //    transfer functions as "0 == E".
660             SVal Result;
661 
662             if (isa<Loc>(V)) {
663               Loc X = svalBuilder.makeNull();
664               Result = evalBinOp(state, BO_EQ, cast<Loc>(V), X,
665                                  U->getType());
666             }
667             else {
668               nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
669               Result = evalBinOp(state, BO_EQ, cast<NonLoc>(V), X,
670                                  U->getType());
671             }
672 
673             state = state->BindExpr(U, Result);
674 
675             break;
676         }
677         Bldr.generateNode(U, *I, state);
678       }
679       break;
680     }
681   }
682 
683 }
684 
685 void ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U,
686                                                  ExplodedNode *Pred,
687                                                  ExplodedNodeSet &Dst) {
688   // Handle ++ and -- (both pre- and post-increment).
689   assert (U->isIncrementDecrementOp());
690   ExplodedNodeSet Tmp;
691   const Expr *Ex = U->getSubExpr()->IgnoreParens();
692   Visit(Ex, Pred, Tmp);
693 
694   for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
695 
696     const ProgramState *state = (*I)->getState();
697     SVal loc = state->getSVal(Ex);
698 
699     // Perform a load.
700     ExplodedNodeSet Tmp2;
701     evalLoad(Tmp2, Ex, *I, state, loc);
702 
703     ExplodedNodeSet Dst2;
704     StmtNodeBuilder Bldr(Tmp2, Dst2, *currentBuilderContext);
705     for (ExplodedNodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end();I2!=E2;++I2) {
706 
707       state = (*I2)->getState();
708       SVal V2_untested = state->getSVal(Ex);
709 
710       // Propagate unknown and undefined values.
711       if (V2_untested.isUnknownOrUndef()) {
712         Bldr.generateNode(U, *I2, state->BindExpr(U, V2_untested));
713         continue;
714       }
715       DefinedSVal V2 = cast<DefinedSVal>(V2_untested);
716 
717       // Handle all other values.
718       BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add
719       : BO_Sub;
720 
721       // If the UnaryOperator has non-location type, use its type to create the
722       // constant value. If the UnaryOperator has location type, create the
723       // constant with int type and pointer width.
724       SVal RHS;
725 
726       if (U->getType()->isAnyPointerType())
727         RHS = svalBuilder.makeArrayIndex(1);
728       else
729         RHS = svalBuilder.makeIntVal(1, U->getType());
730 
731       SVal Result = evalBinOp(state, Op, V2, RHS, U->getType());
732 
733       // Conjure a new symbol if necessary to recover precision.
734       if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
735         DefinedOrUnknownSVal SymVal =
736         svalBuilder.getConjuredSymbolVal(NULL, Ex,
737                                  currentBuilderContext->getCurrentBlockCount());
738         Result = SymVal;
739 
740         // If the value is a location, ++/-- should always preserve
741         // non-nullness.  Check if the original value was non-null, and if so
742         // propagate that constraint.
743         if (Loc::isLocType(U->getType())) {
744           DefinedOrUnknownSVal Constraint =
745           svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType()));
746 
747           if (!state->assume(Constraint, true)) {
748             // It isn't feasible for the original value to be null.
749             // Propagate this constraint.
750             Constraint = svalBuilder.evalEQ(state, SymVal,
751                                          svalBuilder.makeZeroVal(U->getType()));
752 
753 
754             state = state->assume(Constraint, false);
755             assert(state);
756           }
757         }
758       }
759 
760       // Since the lvalue-to-rvalue conversion is explicit in the AST,
761       // we bind an l-value if the operator is prefix and an lvalue (in C++).
762       if (U->isLValue())
763         state = state->BindExpr(U, loc);
764       else
765         state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
766 
767       // Perform the store.
768       Bldr.takeNodes(*I2);
769       ExplodedNodeSet Dst4;
770       evalStore(Dst4, NULL, U, *I2, state, loc, Result);
771       Bldr.addNodes(Dst4);
772     }
773     Dst.insert(Dst2);
774   }
775 }
776