ExprEngineC.cpp 27 KB

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