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