ExprEngineC.cpp revision 3133f79cf451e6302dd05262b4bb53a3e4fd6300
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 17using namespace clang; 18using namespace ento; 19using llvm::APSInt; 20 21void 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 ProgramStateRef 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(), LCtx, 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(), LCtx, 135 LTy, 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 161void 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 180void 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 ProgramStateRef 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 // The analyser can ignore atomic casts for now, although some future 222 // checkers may want to make certain that you're not modifying the same 223 // value through atomic and nonatomic pointers. 224 case CK_AtomicToNonAtomic: 225 case CK_NonAtomicToAtomic: 226 // True no-ops. 227 case CK_NoOp: 228 case CK_FunctionToPointerDecay: { 229 // Copy the SVal of Ex to CastE. 230 ProgramStateRef state = Pred->getState(); 231 const LocationContext *LCtx = Pred->getLocationContext(); 232 SVal V = state->getSVal(Ex, LCtx); 233 state = state->BindExpr(CastE, LCtx, V); 234 Bldr.generateNode(CastE, Pred, state); 235 continue; 236 } 237 case CK_Dependent: 238 case CK_ArrayToPointerDecay: 239 case CK_BitCast: 240 case CK_LValueBitCast: 241 case CK_IntegralCast: 242 case CK_NullToPointer: 243 case CK_IntegralToPointer: 244 case CK_PointerToIntegral: 245 case CK_PointerToBoolean: 246 case CK_IntegralToBoolean: 247 case CK_IntegralToFloating: 248 case CK_FloatingToIntegral: 249 case CK_FloatingToBoolean: 250 case CK_FloatingCast: 251 case CK_FloatingRealToComplex: 252 case CK_FloatingComplexToReal: 253 case CK_FloatingComplexToBoolean: 254 case CK_FloatingComplexCast: 255 case CK_FloatingComplexToIntegralComplex: 256 case CK_IntegralRealToComplex: 257 case CK_IntegralComplexToReal: 258 case CK_IntegralComplexToBoolean: 259 case CK_IntegralComplexCast: 260 case CK_IntegralComplexToFloatingComplex: 261 case CK_CPointerToObjCPointerCast: 262 case CK_BlockPointerToObjCPointerCast: 263 case CK_AnyPointerToBlockPointerCast: 264 case CK_ObjCObjectLValueCast: { 265 // Delegate to SValBuilder to process. 266 ProgramStateRef state = Pred->getState(); 267 const LocationContext *LCtx = Pred->getLocationContext(); 268 SVal V = state->getSVal(Ex, LCtx); 269 V = svalBuilder.evalCast(V, T, ExTy); 270 state = state->BindExpr(CastE, LCtx, V); 271 Bldr.generateNode(CastE, Pred, state); 272 continue; 273 } 274 case CK_DerivedToBase: 275 case CK_UncheckedDerivedToBase: { 276 // For DerivedToBase cast, delegate to the store manager. 277 ProgramStateRef state = Pred->getState(); 278 const LocationContext *LCtx = Pred->getLocationContext(); 279 SVal val = state->getSVal(Ex, LCtx); 280 val = getStoreManager().evalDerivedToBase(val, T); 281 state = state->BindExpr(CastE, LCtx, val); 282 Bldr.generateNode(CastE, Pred, state); 283 continue; 284 } 285 // Various C++ casts that are not handled yet. 286 case CK_Dynamic: 287 case CK_ToUnion: 288 case CK_BaseToDerived: 289 case CK_NullToMemberPointer: 290 case CK_BaseToDerivedMemberPointer: 291 case CK_DerivedToBaseMemberPointer: 292 case CK_ReinterpretMemberPointer: 293 case CK_UserDefinedConversion: 294 case CK_ConstructorConversion: 295 case CK_VectorSplat: 296 case CK_MemberPointerToBoolean: { 297 // Recover some path-sensitivty by conjuring a new value. 298 QualType resultType = CastE->getType(); 299 if (CastE->isLValue()) 300 resultType = getContext().getPointerType(resultType); 301 const LocationContext *LCtx = Pred->getLocationContext(); 302 SVal result = 303 svalBuilder.getConjuredSymbolVal(NULL, CastE, LCtx, resultType, 304 currentBuilderContext->getCurrentBlockCount()); 305 ProgramStateRef state = Pred->getState()->BindExpr(CastE, LCtx, 306 result); 307 Bldr.generateNode(CastE, Pred, state); 308 continue; 309 } 310 } 311 } 312} 313 314void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL, 315 ExplodedNode *Pred, 316 ExplodedNodeSet &Dst) { 317 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 318 319 const InitListExpr *ILE 320 = cast<InitListExpr>(CL->getInitializer()->IgnoreParens()); 321 322 ProgramStateRef state = Pred->getState(); 323 SVal ILV = state->getSVal(ILE, Pred->getLocationContext()); 324 const LocationContext *LC = Pred->getLocationContext(); 325 state = state->bindCompoundLiteral(CL, LC, ILV); 326 327 if (CL->isLValue()) 328 B.generateNode(CL, Pred, state->BindExpr(CL, LC, state->getLValue(CL, LC))); 329 else 330 B.generateNode(CL, Pred, state->BindExpr(CL, LC, ILV)); 331} 332 333void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred, 334 ExplodedNodeSet &Dst) { 335 336 // FIXME: static variables may have an initializer, but the second 337 // time a function is called those values may not be current. 338 // This may need to be reflected in the CFG. 339 340 // Assumption: The CFG has one DeclStmt per Decl. 341 const Decl *D = *DS->decl_begin(); 342 343 if (!D || !isa<VarDecl>(D)) { 344 //TODO:AZ: remove explicit insertion after refactoring is done. 345 Dst.insert(Pred); 346 return; 347 } 348 349 // FIXME: all pre/post visits should eventually be handled by ::Visit(). 350 ExplodedNodeSet dstPreVisit; 351 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this); 352 353 StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext); 354 const VarDecl *VD = dyn_cast<VarDecl>(D); 355 for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end(); 356 I!=E; ++I) { 357 ExplodedNode *N = *I; 358 ProgramStateRef state = N->getState(); 359 360 // Decls without InitExpr are not initialized explicitly. 361 const LocationContext *LC = N->getLocationContext(); 362 363 if (const Expr *InitEx = VD->getInit()) { 364 SVal InitVal = state->getSVal(InitEx, Pred->getLocationContext()); 365 366 // We bound the temp obj region to the CXXConstructExpr. Now recover 367 // the lazy compound value when the variable is not a reference. 368 if (AMgr.getLangOptions().CPlusPlus && VD->getType()->isRecordType() && 369 !VD->getType()->isReferenceType() && isa<loc::MemRegionVal>(InitVal)){ 370 InitVal = state->getSVal(cast<loc::MemRegionVal>(InitVal).getRegion()); 371 assert(isa<nonloc::LazyCompoundVal>(InitVal)); 372 } 373 374 // Recover some path-sensitivity if a scalar value evaluated to 375 // UnknownVal. 376 if (InitVal.isUnknown()) { 377 InitVal = svalBuilder.getConjuredSymbolVal(NULL, InitEx, LC, 378 currentBuilderContext->getCurrentBlockCount()); 379 } 380 B.takeNodes(N); 381 ExplodedNodeSet Dst2; 382 evalBind(Dst2, DS, N, state->getLValue(VD, LC), InitVal, true); 383 B.addNodes(Dst2); 384 } 385 else { 386 B.generateNode(DS, N,state->bindDeclWithNoInit(state->getRegion(VD, LC))); 387 } 388 } 389} 390 391void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred, 392 ExplodedNodeSet &Dst) { 393 assert(B->getOpcode() == BO_LAnd || 394 B->getOpcode() == BO_LOr); 395 396 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 397 ProgramStateRef state = Pred->getState(); 398 const LocationContext *LCtx = Pred->getLocationContext(); 399 SVal X = state->getSVal(B, LCtx); 400 assert(X.isUndef()); 401 402 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData(); 403 assert(Ex); 404 405 if (Ex == B->getRHS()) { 406 X = state->getSVal(Ex, LCtx); 407 408 // Handle undefined values. 409 if (X.isUndef()) { 410 Bldr.generateNode(B, Pred, state->BindExpr(B, LCtx, X)); 411 return; 412 } 413 414 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X); 415 416 // We took the RHS. Because the value of the '&&' or '||' expression must 417 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0 418 // or 1. Alternatively, we could take a lazy approach, and calculate this 419 // value later when necessary. We don't have the machinery in place for 420 // this right now, and since most logical expressions are used for branches, 421 // the payoff is not likely to be large. Instead, we do eager evaluation. 422 if (ProgramStateRef newState = state->assume(XD, true)) 423 Bldr.generateNode(B, Pred, 424 newState->BindExpr(B, LCtx, 425 svalBuilder.makeIntVal(1U, B->getType()))); 426 427 if (ProgramStateRef newState = state->assume(XD, false)) 428 Bldr.generateNode(B, Pred, 429 newState->BindExpr(B, LCtx, 430 svalBuilder.makeIntVal(0U, B->getType()))); 431 } 432 else { 433 // We took the LHS expression. Depending on whether we are '&&' or 434 // '||' we know what the value of the expression is via properties of 435 // the short-circuiting. 436 X = svalBuilder.makeIntVal(B->getOpcode() == BO_LAnd ? 0U : 1U, 437 B->getType()); 438 Bldr.generateNode(B, Pred, state->BindExpr(B, LCtx, X)); 439 } 440} 441 442void ExprEngine::VisitInitListExpr(const InitListExpr *IE, 443 ExplodedNode *Pred, 444 ExplodedNodeSet &Dst) { 445 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 446 447 ProgramStateRef state = Pred->getState(); 448 const LocationContext *LCtx = Pred->getLocationContext(); 449 QualType T = getContext().getCanonicalType(IE->getType()); 450 unsigned NumInitElements = IE->getNumInits(); 451 452 if (T->isArrayType() || T->isRecordType() || T->isVectorType()) { 453 llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList(); 454 455 // Handle base case where the initializer has no elements. 456 // e.g: static int* myArray[] = {}; 457 if (NumInitElements == 0) { 458 SVal V = svalBuilder.makeCompoundVal(T, vals); 459 B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V)); 460 return; 461 } 462 463 for (InitListExpr::const_reverse_iterator it = IE->rbegin(), 464 ei = IE->rend(); it != ei; ++it) { 465 vals = getBasicVals().consVals(state->getSVal(cast<Expr>(*it), LCtx), 466 vals); 467 } 468 469 B.generateNode(IE, Pred, 470 state->BindExpr(IE, LCtx, 471 svalBuilder.makeCompoundVal(T, vals))); 472 return; 473 } 474 475 if (Loc::isLocType(T) || T->isIntegerType()) { 476 assert(IE->getNumInits() == 1); 477 const Expr *initEx = IE->getInit(0); 478 B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, 479 state->getSVal(initEx, LCtx))); 480 return; 481 } 482 483 llvm_unreachable("unprocessed InitListExpr type"); 484} 485 486void ExprEngine::VisitGuardedExpr(const Expr *Ex, 487 const Expr *L, 488 const Expr *R, 489 ExplodedNode *Pred, 490 ExplodedNodeSet &Dst) { 491 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 492 493 ProgramStateRef state = Pred->getState(); 494 const LocationContext *LCtx = Pred->getLocationContext(); 495 SVal X = state->getSVal(Ex, LCtx); 496 assert (X.isUndef()); 497 const Expr *SE = (Expr*) cast<UndefinedVal>(X).getData(); 498 assert(SE); 499 X = state->getSVal(SE, LCtx); 500 501 // Make sure that we invalidate the previous binding. 502 B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, X, true)); 503} 504 505void ExprEngine:: 506VisitOffsetOfExpr(const OffsetOfExpr *OOE, 507 ExplodedNode *Pred, ExplodedNodeSet &Dst) { 508 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 509 APSInt IV; 510 if (OOE->EvaluateAsInt(IV, getContext())) { 511 assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType())); 512 assert(OOE->getType()->isIntegerType()); 513 assert(IV.isSigned() == OOE->getType()->isSignedIntegerOrEnumerationType()); 514 SVal X = svalBuilder.makeIntVal(IV); 515 B.generateNode(OOE, Pred, 516 Pred->getState()->BindExpr(OOE, Pred->getLocationContext(), 517 X)); 518 } 519 // FIXME: Handle the case where __builtin_offsetof is not a constant. 520} 521 522 523void ExprEngine:: 524VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex, 525 ExplodedNode *Pred, 526 ExplodedNodeSet &Dst) { 527 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 528 529 QualType T = Ex->getTypeOfArgument(); 530 531 if (Ex->getKind() == UETT_SizeOf) { 532 if (!T->isIncompleteType() && !T->isConstantSizeType()) { 533 assert(T->isVariableArrayType() && "Unknown non-constant-sized type."); 534 535 // FIXME: Add support for VLA type arguments and VLA expressions. 536 // When that happens, we should probably refactor VLASizeChecker's code. 537 return; 538 } 539 else if (T->getAs<ObjCObjectType>()) { 540 // Some code tries to take the sizeof an ObjCObjectType, relying that 541 // the compiler has laid out its representation. Just report Unknown 542 // for these. 543 return; 544 } 545 } 546 547 APSInt Value = Ex->EvaluateKnownConstInt(getContext()); 548 CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue()); 549 550 ProgramStateRef state = Pred->getState(); 551 state = state->BindExpr(Ex, Pred->getLocationContext(), 552 svalBuilder.makeIntVal(amt.getQuantity(), 553 Ex->getType())); 554 Bldr.generateNode(Ex, Pred, state); 555} 556 557void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, 558 ExplodedNode *Pred, 559 ExplodedNodeSet &Dst) { 560 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 561 switch (U->getOpcode()) { 562 default: { 563 Bldr.takeNodes(Pred); 564 ExplodedNodeSet Tmp; 565 VisitIncrementDecrementOperator(U, Pred, Tmp); 566 Bldr.addNodes(Tmp); 567 } 568 break; 569 case UO_Real: { 570 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 571 572 // FIXME: We don't have complex SValues yet. 573 if (Ex->getType()->isAnyComplexType()) { 574 // Just report "Unknown." 575 break; 576 } 577 578 // For all other types, UO_Real is an identity operation. 579 assert (U->getType() == Ex->getType()); 580 ProgramStateRef state = Pred->getState(); 581 const LocationContext *LCtx = Pred->getLocationContext(); 582 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, 583 state->getSVal(Ex, LCtx))); 584 break; 585 } 586 587 case UO_Imag: { 588 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 589 // FIXME: We don't have complex SValues yet. 590 if (Ex->getType()->isAnyComplexType()) { 591 // Just report "Unknown." 592 break; 593 } 594 // For all other types, UO_Imag returns 0. 595 ProgramStateRef state = Pred->getState(); 596 const LocationContext *LCtx = Pred->getLocationContext(); 597 SVal X = svalBuilder.makeZeroVal(Ex->getType()); 598 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, X)); 599 break; 600 } 601 602 case UO_Plus: 603 assert(!U->isLValue()); 604 // FALL-THROUGH. 605 case UO_Deref: 606 case UO_AddrOf: 607 case UO_Extension: { 608 // FIXME: We can probably just have some magic in Environment::getSVal() 609 // that propagates values, instead of creating a new node here. 610 // 611 // Unary "+" is a no-op, similar to a parentheses. We still have places 612 // where it may be a block-level expression, so we need to 613 // generate an extra node that just propagates the value of the 614 // subexpression. 615 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 616 ProgramStateRef state = Pred->getState(); 617 const LocationContext *LCtx = Pred->getLocationContext(); 618 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, 619 state->getSVal(Ex, LCtx))); 620 break; 621 } 622 623 case UO_LNot: 624 case UO_Minus: 625 case UO_Not: { 626 assert (!U->isLValue()); 627 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 628 ProgramStateRef state = Pred->getState(); 629 const LocationContext *LCtx = Pred->getLocationContext(); 630 631 // Get the value of the subexpression. 632 SVal V = state->getSVal(Ex, LCtx); 633 634 if (V.isUnknownOrUndef()) { 635 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, V)); 636 break; 637 } 638 639 switch (U->getOpcode()) { 640 default: 641 llvm_unreachable("Invalid Opcode."); 642 case UO_Not: 643 // FIXME: Do we need to handle promotions? 644 state = state->BindExpr(U, LCtx, evalComplement(cast<NonLoc>(V))); 645 break; 646 case UO_Minus: 647 // FIXME: Do we need to handle promotions? 648 state = state->BindExpr(U, LCtx, evalMinus(cast<NonLoc>(V))); 649 break; 650 case UO_LNot: 651 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." 652 // 653 // Note: technically we do "E == 0", but this is the same in the 654 // transfer functions as "0 == E". 655 SVal Result; 656 if (isa<Loc>(V)) { 657 Loc X = svalBuilder.makeNull(); 658 Result = evalBinOp(state, BO_EQ, cast<Loc>(V), X, 659 U->getType()); 660 } 661 else { 662 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType())); 663 Result = evalBinOp(state, BO_EQ, cast<NonLoc>(V), X, 664 U->getType()); 665 } 666 667 state = state->BindExpr(U, LCtx, Result); 668 break; 669 } 670 Bldr.generateNode(U, Pred, state); 671 break; 672 } 673 } 674 675} 676 677void ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U, 678 ExplodedNode *Pred, 679 ExplodedNodeSet &Dst) { 680 // Handle ++ and -- (both pre- and post-increment). 681 assert (U->isIncrementDecrementOp()); 682 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 683 684 const LocationContext *LCtx = Pred->getLocationContext(); 685 ProgramStateRef state = Pred->getState(); 686 SVal loc = state->getSVal(Ex, LCtx); 687 688 // Perform a load. 689 ExplodedNodeSet Tmp; 690 evalLoad(Tmp, Ex, Pred, state, loc); 691 692 ExplodedNodeSet Dst2; 693 StmtNodeBuilder Bldr(Tmp, Dst2, *currentBuilderContext); 694 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) { 695 696 state = (*I)->getState(); 697 assert(LCtx == (*I)->getLocationContext()); 698 SVal V2_untested = state->getSVal(Ex, LCtx); 699 700 // Propagate unknown and undefined values. 701 if (V2_untested.isUnknownOrUndef()) { 702 Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V2_untested)); 703 continue; 704 } 705 DefinedSVal V2 = cast<DefinedSVal>(V2_untested); 706 707 // Handle all other values. 708 BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub; 709 710 // If the UnaryOperator has non-location type, use its type to create the 711 // constant value. If the UnaryOperator has location type, create the 712 // constant with int type and pointer width. 713 SVal RHS; 714 715 if (U->getType()->isAnyPointerType()) 716 RHS = svalBuilder.makeArrayIndex(1); 717 else 718 RHS = svalBuilder.makeIntVal(1, U->getType()); 719 720 SVal Result = evalBinOp(state, Op, V2, RHS, U->getType()); 721 722 // Conjure a new symbol if necessary to recover precision. 723 if (Result.isUnknown()){ 724 DefinedOrUnknownSVal SymVal = 725 svalBuilder.getConjuredSymbolVal(NULL, Ex, LCtx, 726 currentBuilderContext->getCurrentBlockCount()); 727 Result = SymVal; 728 729 // If the value is a location, ++/-- should always preserve 730 // non-nullness. Check if the original value was non-null, and if so 731 // propagate that constraint. 732 if (Loc::isLocType(U->getType())) { 733 DefinedOrUnknownSVal Constraint = 734 svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType())); 735 736 if (!state->assume(Constraint, true)) { 737 // It isn't feasible for the original value to be null. 738 // Propagate this constraint. 739 Constraint = svalBuilder.evalEQ(state, SymVal, 740 svalBuilder.makeZeroVal(U->getType())); 741 742 743 state = state->assume(Constraint, false); 744 assert(state); 745 } 746 } 747 } 748 749 // Since the lvalue-to-rvalue conversion is explicit in the AST, 750 // we bind an l-value if the operator is prefix and an lvalue (in C++). 751 if (U->isLValue()) 752 state = state->BindExpr(U, LCtx, loc); 753 else 754 state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result); 755 756 // Perform the store. 757 Bldr.takeNodes(*I); 758 ExplodedNodeSet Dst3; 759 evalStore(Dst3, NULL, U, *I, state, loc, Result); 760 Bldr.addNodes(Dst3); 761 } 762 Dst.insert(Dst2); 763} 764