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