Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 1 | //=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-= |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 4af8431 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 10 | // This file defines a meta-engine for path-sensitive dataflow analysis that |
| 11 | // is built on GREngine, but provides the boilerplate to execute transfer |
| 12 | // functions and build the ExplodedGraph at the expression level. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 18 | |
| 19 | #include "llvm/Support/Streams.h" |
Ted Kremenek | b387a3f | 2008-02-14 22:16:04 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | using llvm::dyn_cast; |
| 23 | using llvm::cast; |
| 24 | using llvm::APSInt; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 26 | GRExprEngine::StateTy |
| 27 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 29 | if (!StateCleaned) { |
| 30 | St = RemoveDeadBindings(CurrentStmt, St); |
| 31 | StateCleaned = true; |
| 32 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 34 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 36 | if (S == CurrentStmt) { |
| 37 | isBlkExpr = getCFG().isBlkExpr(S); |
| 38 | |
| 39 | if (!isBlkExpr) |
| 40 | return St; |
| 41 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 43 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 44 | } |
| 45 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 46 | const GRExprEngine::StateTy::BufferTy& |
| 47 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 48 | StateTy::BufferTy& RetBuf) { |
| 49 | |
| 50 | assert (RetBuf.empty()); |
| 51 | |
| 52 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 53 | RetBuf.push_back(SetValue(St, S, *I)); |
| 54 | |
| 55 | return RetBuf; |
| 56 | } |
| 57 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 58 | GRExprEngine::StateTy |
| 59 | GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 61 | if (LV.isUnknown()) |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 62 | return St; |
| 63 | |
| 64 | if (!StateCleaned) { |
| 65 | St = RemoveDeadBindings(CurrentStmt, St); |
| 66 | StateCleaned = true; |
| 67 | } |
| 68 | |
| 69 | return StateMgr.SetValue(St, LV, V); |
| 70 | } |
| 71 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 72 | void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 73 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 75 | // Remove old bindings for subexpressions. |
| 76 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 78 | // Check for NULL conditions; e.g. "for(;;)" |
| 79 | if (!Condition) { |
| 80 | builder.markInfeasible(false); |
| 81 | |
| 82 | // Get the current block counter. |
| 83 | GRBlockCounter BC = builder.getBlockCounter(); |
| 84 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 85 | unsigned NumVisited = BC.getNumVisited(BlockID); |
| 86 | |
| 87 | if (NumVisited < 1) builder.generateNode(PrevState, true); |
| 88 | else builder.markInfeasible(true); |
| 89 | |
| 90 | return; |
| 91 | } |
| 92 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 93 | RValue V = GetValue(PrevState, Condition); |
| 94 | |
| 95 | switch (V.getBaseKind()) { |
| 96 | default: |
| 97 | break; |
| 98 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 99 | case RValue::UnknownKind: |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 100 | builder.generateNode(PrevState, true); |
| 101 | builder.generateNode(PrevState, false); |
| 102 | return; |
| 103 | |
| 104 | case RValue::UninitializedKind: { |
| 105 | NodeTy* N = builder.generateNode(PrevState, true); |
| 106 | |
| 107 | if (N) { |
| 108 | N->markAsSink(); |
| 109 | UninitBranches.insert(N); |
| 110 | } |
| 111 | |
| 112 | builder.markInfeasible(false); |
| 113 | return; |
| 114 | } |
| 115 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 117 | // Get the current block counter. |
| 118 | GRBlockCounter BC = builder.getBlockCounter(); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 119 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 120 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 122 | if (isa<nonlval::ConcreteInt>(V) || |
| 123 | BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) { |
| 124 | |
| 125 | // Process the true branch. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 127 | bool isFeasible = true; |
| 128 | |
| 129 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 130 | |
| 131 | if (isFeasible) |
| 132 | builder.generateNode(St, true); |
| 133 | else |
| 134 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 135 | } |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 136 | else |
| 137 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 139 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 140 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 142 | if (isa<nonlval::ConcreteInt>(V) || |
| 143 | BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) { |
| 144 | |
| 145 | // Process the false branch. |
| 146 | |
| 147 | bool isFeasible = false; |
| 148 | |
| 149 | StateTy St = Assume(PrevState, V, false, isFeasible); |
| 150 | |
| 151 | if (isFeasible) |
| 152 | builder.generateNode(St, false); |
| 153 | else |
| 154 | builder.markInfeasible(false); |
| 155 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 156 | else |
| 157 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 160 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 161 | /// nodes by processing the 'effects' of a computed goto jump. |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 162 | void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 163 | |
| 164 | StateTy St = builder.getState(); |
| 165 | LValue V = cast<LValue>(GetValue(St, builder.getTarget())); |
| 166 | |
| 167 | // Three possibilities: |
| 168 | // |
| 169 | // (1) We know the computed label. |
| 170 | // (2) The label is NULL (or some other constant), or Uninitialized. |
| 171 | // (3) We have no clue about the label. Dispatch to all targets. |
| 172 | // |
| 173 | |
| 174 | typedef IndirectGotoNodeBuilder::iterator iterator; |
| 175 | |
| 176 | if (isa<lval::GotoLabel>(V)) { |
| 177 | LabelStmt* L = cast<lval::GotoLabel>(V).getLabel(); |
| 178 | |
| 179 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 180 | if (I.getLabel() == L) { |
| 181 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | assert (false && "No block with label."); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) { |
| 191 | // Dispatch to the first target and mark it as a sink. |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 192 | NodeTy* N = builder.generateNode(builder.begin(), St, true); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 193 | UninitBranches.insert(N); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | // This is really a catch-all. We don't support symbolics yet. |
| 198 | |
| 199 | assert (isa<UnknownVal>(V)); |
| 200 | |
| 201 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 202 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 203 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 205 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 206 | /// nodes by processing the 'effects' of a switch statement. |
| 207 | void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) { |
| 208 | |
| 209 | typedef SwitchNodeBuilder::iterator iterator; |
| 210 | |
| 211 | StateTy St = builder.getState(); |
| 212 | NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition())); |
| 213 | |
| 214 | if (isa<UninitializedVal>(CondV)) { |
| 215 | NodeTy* N = builder.generateDefaultCaseNode(St, true); |
| 216 | UninitBranches.insert(N); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | StateTy DefaultSt = St; |
| 221 | |
| 222 | // While most of this can be assumed (such as the signedness), having it |
| 223 | // just computed makes sure everything makes the same assumptions end-to-end. |
| 224 | unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation()); |
| 225 | APSInt V1(bits, false); |
| 226 | APSInt V2 = V1; |
| 227 | |
| 228 | for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) { |
| 229 | |
| 230 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
| 231 | |
| 232 | // Evaluate the case. |
| 233 | if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) { |
| 234 | assert (false && "Case condition must evaluate to an integer constant."); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // Get the RHS of the case, if it exists. |
| 239 | |
| 240 | if (Expr* E = Case->getRHS()) { |
| 241 | if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) { |
| 242 | assert (false && |
| 243 | "Case condition (RHS) must evaluate to an integer constant."); |
| 244 | return ; |
| 245 | } |
| 246 | |
| 247 | assert (V1 <= V2); |
| 248 | } |
| 249 | else V2 = V1; |
| 250 | |
| 251 | // FIXME: Eventually we should replace the logic below with a range |
| 252 | // comparison, rather than concretize the values within the range. |
| 253 | // This should be easy once we have "ranges" for NonLValues. |
| 254 | |
| 255 | do { |
| 256 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 257 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 258 | NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 259 | |
| 260 | // Now "assume" that the case matches. |
| 261 | bool isFeasible; |
| 262 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 263 | StateTy StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 264 | |
| 265 | if (isFeasible) { |
| 266 | builder.generateCaseStmtNode(I, StNew); |
| 267 | |
| 268 | // If CondV evaluates to a constant, then we know that this |
| 269 | // is the *only* case that we can take, so stop evaluating the |
| 270 | // others. |
| 271 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | // Now "assume" that the case doesn't match. Add this state |
| 276 | // to the default state (if it is feasible). |
| 277 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 278 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 279 | |
| 280 | if (isFeasible) |
| 281 | DefaultSt = StNew; |
| 282 | |
| 283 | // Concretize the next value in the range. |
| 284 | ++V1; |
| 285 | |
| 286 | } while (V1 < V2); |
| 287 | } |
| 288 | |
| 289 | // If we reach here, than we know that the default branch is |
| 290 | // possible. |
| 291 | builder.generateDefaultCaseNode(DefaultSt); |
| 292 | } |
| 293 | |
| 294 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 295 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 296 | NodeSet& Dst) { |
| 297 | |
| 298 | bool hasR2; |
| 299 | StateTy PrevState = Pred->getState(); |
| 300 | |
| 301 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 302 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 303 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 304 | if (isa<UnknownVal>(R1) && |
| 305 | (isa<UnknownVal>(R2) || |
| 306 | isa<UninitializedVal>(R2))) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 307 | |
| 308 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 309 | return; |
| 310 | } |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 311 | else if (isa<UninitializedVal>(R1)) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 312 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 317 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 318 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 319 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 320 | if (hasR2 == false) { |
| 321 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 322 | return; |
| 323 | } |
| 324 | } |
| 325 | else { |
| 326 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 327 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 328 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 329 | if (hasR2 == false) { |
| 330 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 331 | return; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 336 | // R2 == false. |
| 337 | |
| 338 | bool isFeasible; |
| 339 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 340 | |
| 341 | if (isFeasible) |
| 342 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 343 | |
| 344 | St = Assume(PrevState, R2, false, isFeasible); |
| 345 | |
| 346 | if (isFeasible) |
| 347 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 352 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 353 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 354 | |
| 355 | StmtEntryNode = builder.getLastNode(); |
| 356 | CurrentStmt = S; |
| 357 | NodeSet Dst; |
| 358 | StateCleaned = false; |
| 359 | |
| 360 | Visit(S, StmtEntryNode, Dst); |
| 361 | |
| 362 | // If no nodes were generated, generate a new node that has all the |
| 363 | // dead mappings removed. |
| 364 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 365 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 366 | builder.generateNode(S, St, StmtEntryNode); |
| 367 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 369 | CurrentStmt = NULL; |
| 370 | StmtEntryNode = NULL; |
| 371 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 374 | GRExprEngine::NodeTy* |
| 375 | GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 376 | |
| 377 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 378 | if (St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 379 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 381 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 382 | Dst.Add(N); |
| 383 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 384 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 385 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 386 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 387 | const StateTy::BufferTy& SB) { |
| 388 | |
| 389 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 390 | Nodify(Dst, S, Pred, *I); |
| 391 | } |
| 392 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 393 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 394 | if (D != CurrentStmt) { |
| 395 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | // If we are here, we are loading the value of the decl and binding |
| 400 | // it to the block-level expression. |
| 401 | |
| 402 | StateTy St = Pred->getState(); |
| 403 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 404 | Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D))); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 407 | void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 408 | |
| 409 | QualType T = CastE->getType(); |
| 410 | |
| 411 | // Check for redundant casts. |
| 412 | if (E->getType() == T) { |
| 413 | Dst.Add(Pred); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | NodeSet S1; |
| 418 | Visit(E, Pred, S1); |
| 419 | |
| 420 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 421 | NodeTy* N = *I1; |
| 422 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 423 | const RValue& V = GetValue(St, E); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 424 | Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 425 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 428 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
| 429 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 430 | |
| 431 | StateTy St = Pred->getState(); |
| 432 | |
| 433 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 434 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 435 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 436 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 437 | E ? GetValue(St, E) : UninitializedVal()); |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 438 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 439 | |
| 440 | Nodify(Dst, DS, Pred, St); |
| 441 | |
| 442 | if (Dst.empty()) |
| 443 | Dst.Add(Pred); |
| 444 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 447 | void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 448 | NodeTy* Pred, NodeSet& Dst) { |
| 449 | |
| 450 | StateTy St = Pred->getState(); |
| 451 | |
| 452 | RValue R = GetValue(St, LHS); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 453 | if (isa<UnknownVal>(R)) R = GetValue(St, RHS); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 454 | |
| 455 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 456 | } |
| 457 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 458 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 459 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 460 | NodeTy* Pred, |
| 461 | NodeSet& Dst) { |
| 462 | |
| 463 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 464 | |
| 465 | QualType T = S->getArgumentType(); |
| 466 | |
| 467 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 468 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 469 | return; |
| 470 | |
| 471 | SourceLocation L = S->getExprLoc(); |
| 472 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 473 | |
| 474 | Nodify(Dst, S, Pred, |
| 475 | SetValue(Pred->getState(), S, |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame^] | 476 | NonLValue::GetValue(ValMgr, size, S->getType(), L))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 477 | |
| 478 | } |
| 479 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 480 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, |
| 481 | GRExprEngine::NodeTy* Pred, |
| 482 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 484 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 485 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 486 | |
| 487 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 488 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 489 | // probably just handle AddrOf and Deref in their own methods to make |
| 490 | // this cleaner. |
| 491 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 492 | isa<DeclRefExpr>(U->getSubExpr())) |
| 493 | S1.Add(Pred); |
| 494 | else |
| 495 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 496 | |
| 497 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 498 | NodeTy* N1 = *I1; |
| 499 | StateTy St = N1->getState(); |
| 500 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 501 | // Handle ++ and -- (both pre- and post-increment). |
| 502 | |
| 503 | if (U->isIncrementDecrementOp()) { |
| 504 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 505 | RValue R1 = GetValue(St, L1); |
| 506 | |
| 507 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 508 | : BinaryOperator::Sub; |
| 509 | |
| 510 | RValue Result = EvalBinaryOp(ValMgr, Op, R1, GetRValueConstant(1U, U)); |
| 511 | |
| 512 | if (U->isPostfix()) |
| 513 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 514 | else |
| 515 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 516 | |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | // Handle all other unary operators. |
| 521 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 522 | switch (U->getOpcode()) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 523 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 524 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 525 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 526 | Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 527 | break; |
| 528 | } |
| 529 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 530 | case UnaryOperator::Not: { |
| 531 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 532 | Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 536 | case UnaryOperator::LNot: { |
| 537 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 538 | // |
| 539 | // Note: technically we do "E == 0", but this is the same in the |
| 540 | // transfer functions as "0 == E". |
| 541 | |
| 542 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 543 | |
| 544 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 545 | const LValue& L1 = cast<LValue>(V1); |
| 546 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 547 | Nodify(Dst, U, N1, |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 548 | SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 549 | L1, V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 550 | } |
| 551 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 552 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 553 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 554 | Nodify(Dst, U, N1, |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 555 | SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 556 | R1, V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | break; |
| 560 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 561 | |
| 562 | case UnaryOperator::SizeOf: { |
| 563 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 564 | |
| 565 | QualType T = U->getSubExpr()->getType(); |
| 566 | |
| 567 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 568 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 569 | return; |
| 570 | |
| 571 | SourceLocation L = U->getExprLoc(); |
| 572 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 573 | |
| 574 | Nodify(Dst, U, N1, |
| 575 | SetValue(St, U, NonLValue::GetValue(ValMgr, size, |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame^] | 576 | U->getType(), L))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 577 | |
| 578 | break; |
| 579 | } |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 580 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 581 | case UnaryOperator::AddrOf: { |
| 582 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 583 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | case UnaryOperator::Deref: { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 588 | // FIXME: Stop when dereferencing an uninitialized value. |
| 589 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 590 | |
| 591 | const RValue& V = GetValue(St, U->getSubExpr()); |
| 592 | const LValue& L1 = cast<LValue>(V); |
| 593 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 594 | // After a dereference, one of two possible situations arise: |
| 595 | // (1) A crash, because the pointer was NULL. |
| 596 | // (2) The pointer is not NULL, and the dereference works. |
| 597 | // |
| 598 | // We add these assumptions. |
| 599 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 600 | bool isFeasibleNotNull; |
| 601 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 602 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 603 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 604 | |
| 605 | if (isFeasibleNotNull) { |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 606 | QualType T = U->getType(); |
| 607 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 608 | GetValue(StNotNull, L1, &T))); |
| 609 | } |
| 610 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 611 | bool isFeasibleNull; |
| 612 | |
| 613 | // "Assume" that the pointer is NULL. |
| 614 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 615 | |
| 616 | if (isFeasibleNull) { |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 617 | // We don't use "Nodify" here because the node will be a sink |
| 618 | // and we have no intention of processing it later. |
| 619 | NodeTy* NullNode = Builder->generateNode(U, StNull, N1); |
| 620 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 621 | if (NullNode) { |
| 622 | NullNode->markAsSink(); |
| 623 | |
| 624 | if (isFeasibleNotNull) |
| 625 | ImplicitNullDeref.insert(NullNode); |
| 626 | else |
| 627 | ExplicitNullDeref.insert(NullNode); |
| 628 | } |
| 629 | } |
| 630 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 631 | break; |
| 632 | } |
| 633 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 634 | default: ; |
| 635 | assert (false && "Not implemented."); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 640 | void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred, |
| 641 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 642 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 643 | if (isa<DeclRefExpr>(E)) { |
| 644 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 645 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 646 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 647 | |
| 648 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 649 | if (U->getOpcode() == UnaryOperator::Deref) { |
| 650 | Visit(U->getSubExpr(), Pred, Dst); |
| 651 | return; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | Visit(E, Pred, Dst); |
| 656 | } |
| 657 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 658 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 659 | GRExprEngine::NodeTy* Pred, |
| 660 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 661 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 662 | |
| 663 | if (B->isAssignmentOp()) |
| 664 | VisitAssignmentLHS(B->getLHS(), Pred, S1); |
| 665 | else |
| 666 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 667 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 668 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 669 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 670 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 671 | // When getting the value for the LHS, check if we are in an assignment. |
| 672 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 673 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 674 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 675 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 676 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 677 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 678 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 679 | NodeSet S2; |
| 680 | Visit(B->getRHS(), N1, S2); |
| 681 | |
| 682 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 683 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 684 | NodeTy* N2 = *I2; |
| 685 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 686 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 687 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 688 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 689 | |
| 690 | if (Op <= BinaryOperator::Or) { |
| 691 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 692 | if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 693 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 694 | continue; |
| 695 | } |
| 696 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 697 | Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, V1, V2))); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 698 | continue; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 699 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | switch (Op) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 703 | case BinaryOperator::Assign: { |
| 704 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 3434b08 | 2008-02-06 04:41:14 +0000 | [diff] [blame] | 705 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 709 | default: { // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 710 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 711 | assert (B->isCompoundAssignmentOp()); |
| 712 | |
| 713 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 714 | RValue Result = cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 715 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 716 | if (Op >= BinaryOperator::AndAssign) |
| 717 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 718 | else |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 719 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 720 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 721 | if (B->getType()->isPointerType()) { // Perform pointer arithmetic. |
| 722 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 723 | Result = EvalBinaryOp(ValMgr, Op, L1, R2); |
| 724 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 725 | else if (isa<LValue>(V2)) { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 726 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 727 | |
| 728 | if (B->getRHS()->getType()->isPointerType()) { |
| 729 | // LValue comparison. |
| 730 | Result = EvalBinaryOp(ValMgr, Op, L1, L2); |
| 731 | } |
| 732 | else { |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame^] | 733 | QualType T1 = B->getLHS()->getType(); |
| 734 | QualType T2 = B->getRHS()->getType(); |
| 735 | |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 736 | // An operation between two variables of a non-lvalue type. |
| 737 | Result = |
| 738 | EvalBinaryOp(ValMgr, Op, |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame^] | 739 | cast<NonLValue>(GetValue(N1->getState(), L1, &T1)), |
| 740 | cast<NonLValue>(GetValue(N2->getState(), L2, &T2))); |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 741 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 742 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 743 | else { // Any other operation between two Non-LValues. |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame^] | 744 | QualType T = B->getLHS()->getType(); |
| 745 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), |
| 746 | L1, &T)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 747 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 748 | Result = EvalBinaryOp(ValMgr, Op, R1, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 751 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 752 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 753 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 754 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 755 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 756 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 757 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 758 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 759 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 760 | void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred, |
| 761 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 762 | |
| 763 | // FIXME: add metadata to the CFG so that we can disable |
| 764 | // this check when we KNOW that there is no block-level subexpression. |
| 765 | // The motivation is that this check requires a hashtable lookup. |
| 766 | |
| 767 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 768 | Dst.Add(Pred); |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 773 | |
| 774 | default: |
| 775 | // Cases we intentionally have "default" handle: |
| 776 | // AddrLabelExpr, CharacterLiteral, IntegerLiteral |
| 777 | |
| 778 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 779 | break; |
| 780 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 781 | case Stmt::BinaryOperatorClass: { |
| 782 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 783 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 784 | if (B->isLogicalOp()) { |
| 785 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 786 | break; |
| 787 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 788 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 789 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 790 | Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 791 | break; |
| 792 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 793 | |
| 794 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | case Stmt::CastExprClass: { |
| 799 | CastExpr* C = cast<CastExpr>(S); |
| 800 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 801 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 802 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 803 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 804 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 805 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 806 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 807 | break; |
| 808 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 809 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 810 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 811 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 812 | break; |
| 813 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 814 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 815 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 816 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 817 | break; |
| 818 | } |
| 819 | |
| 820 | case Stmt::DeclRefExprClass: |
| 821 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 822 | break; |
| 823 | |
| 824 | case Stmt::DeclStmtClass: |
| 825 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 826 | break; |
| 827 | |
| 828 | case Stmt::ImplicitCastExprClass: { |
| 829 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 830 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | case Stmt::ParenExprClass: |
| 835 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 836 | break; |
| 837 | |
| 838 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 839 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 840 | break; |
| 841 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 842 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 843 | StmtExpr* SE = cast<StmtExpr>(S); |
| 844 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 845 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 846 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
| 847 | Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 851 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 852 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 853 | Visit(R, Pred, Dst); |
| 854 | else |
| 855 | Dst.Add(Pred); |
| 856 | |
| 857 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 858 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 859 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 860 | case Stmt::UnaryOperatorClass: |
| 861 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 862 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 863 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 866 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 867 | // "Assume" logic. |
| 868 | //===----------------------------------------------------------------------===// |
| 869 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 870 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 871 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 872 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 873 | |
| 874 | switch (Cond.getSubKind()) { |
| 875 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 876 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 877 | return St; |
| 878 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 879 | case lval::SymbolValKind: |
| 880 | if (Assumption) |
| 881 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 882 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 883 | else |
| 884 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 885 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 886 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 887 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 888 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 889 | isFeasible = Assumption; |
| 890 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 891 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 892 | case lval::ConcreteIntKind: { |
| 893 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 894 | isFeasible = b ? Assumption : !Assumption; |
| 895 | return St; |
| 896 | } |
| 897 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 900 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 901 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 902 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 903 | |
| 904 | switch (Cond.getSubKind()) { |
| 905 | default: |
| 906 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 907 | return St; |
| 908 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 909 | |
| 910 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 911 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 912 | SymbolID sym = SV.getSymbol(); |
| 913 | |
| 914 | if (Assumption) |
| 915 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 916 | isFeasible); |
| 917 | else |
| 918 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 919 | isFeasible); |
| 920 | } |
| 921 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 922 | case nonlval::SymIntConstraintValKind: |
| 923 | return |
| 924 | AssumeSymInt(St, Assumption, |
| 925 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 926 | isFeasible); |
| 927 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 928 | case nonlval::ConcreteIntKind: { |
| 929 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 930 | isFeasible = b ? Assumption : !Assumption; |
| 931 | return St; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 936 | GRExprEngine::StateTy |
| 937 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 938 | const llvm::APSInt& V, bool& isFeasible) { |
| 939 | |
| 940 | // First, determine if sym == X, where X != V. |
| 941 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 942 | isFeasible = *X != V; |
| 943 | return St; |
| 944 | } |
| 945 | |
| 946 | // Second, determine if sym != V. |
| 947 | if (St.isNotEqual(sym, V)) { |
| 948 | isFeasible = true; |
| 949 | return St; |
| 950 | } |
| 951 | |
| 952 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 953 | // Make that assumption. |
| 954 | |
| 955 | isFeasible = true; |
| 956 | return StateMgr.AddNE(St, sym, V); |
| 957 | } |
| 958 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 959 | GRExprEngine::StateTy |
| 960 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 961 | const llvm::APSInt& V, bool& isFeasible) { |
| 962 | |
| 963 | // First, determine if sym == X, where X != V. |
| 964 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 965 | isFeasible = *X == V; |
| 966 | return St; |
| 967 | } |
| 968 | |
| 969 | // Second, determine if sym != V. |
| 970 | if (St.isNotEqual(sym, V)) { |
| 971 | isFeasible = false; |
| 972 | return St; |
| 973 | } |
| 974 | |
| 975 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 976 | // Make that assumption. |
| 977 | |
| 978 | isFeasible = true; |
| 979 | return StateMgr.AddEQ(St, sym, V); |
| 980 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 981 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 982 | GRExprEngine::StateTy |
| 983 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 984 | const SymIntConstraint& C, bool& isFeasible) { |
| 985 | |
| 986 | switch (C.getOpcode()) { |
| 987 | default: |
| 988 | // No logic yet for other operators. |
| 989 | return St; |
| 990 | |
| 991 | case BinaryOperator::EQ: |
| 992 | if (Assumption) |
| 993 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 994 | else |
| 995 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 996 | |
| 997 | case BinaryOperator::NE: |
| 998 | if (Assumption) |
| 999 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1000 | else |
| 1001 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1005 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1006 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1007 | //===----------------------------------------------------------------------===// |
| 1008 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1009 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1010 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1011 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1012 | namespace llvm { |
| 1013 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1014 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1015 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1016 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1017 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1018 | |
| 1019 | Out << "Variables:\\l"; |
| 1020 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1021 | bool isFirst = true; |
| 1022 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1023 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1024 | E=St.vb_end(); I!=E;++I) { |
| 1025 | |
| 1026 | if (isFirst) |
| 1027 | isFirst = false; |
| 1028 | else |
| 1029 | Out << "\\l"; |
| 1030 | |
| 1031 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1032 | I.getData().print(Out); |
| 1033 | } |
| 1034 | |
| 1035 | } |
| 1036 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1037 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1038 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1039 | |
| 1040 | bool isFirst = true; |
| 1041 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1042 | for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1043 | I != E;++I) { |
| 1044 | |
| 1045 | if (isFirst) { |
| 1046 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1047 | isFirst = false; |
| 1048 | } |
| 1049 | else |
| 1050 | Out << "\\l"; |
| 1051 | |
| 1052 | Out << " (" << (void*) I.getKey() << ") "; |
| 1053 | I.getKey()->printPretty(Out); |
| 1054 | Out << " : "; |
| 1055 | I.getData().print(Out); |
| 1056 | } |
| 1057 | } |
| 1058 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1059 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1060 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1061 | bool isFirst = true; |
| 1062 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1063 | for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1064 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1065 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1066 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1067 | isFirst = false; |
| 1068 | } |
| 1069 | else |
| 1070 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1071 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1072 | Out << " (" << (void*) I.getKey() << ") "; |
| 1073 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1074 | Out << " : "; |
| 1075 | I.getData().print(Out); |
| 1076 | } |
| 1077 | } |
| 1078 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1079 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1080 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1081 | |
| 1082 | if (CE.isEmpty()) |
| 1083 | return; |
| 1084 | |
| 1085 | Out << "\\l\\|'==' constraints:"; |
| 1086 | |
| 1087 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1088 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1089 | } |
| 1090 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1091 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1092 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1093 | |
| 1094 | if (NE.isEmpty()) |
| 1095 | return; |
| 1096 | |
| 1097 | Out << "\\l\\|'!=' constraints:"; |
| 1098 | |
| 1099 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1100 | I != EI; ++I){ |
| 1101 | |
| 1102 | Out << "\\l $" << I.getKey() << " : "; |
| 1103 | bool isFirst = true; |
| 1104 | |
| 1105 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1106 | EJ=I.getData().end(); |
| 1107 | for ( ; J != EJ; ++J) { |
| 1108 | if (isFirst) isFirst = false; |
| 1109 | else Out << ", "; |
| 1110 | |
| 1111 | Out << (*J)->toString(); |
| 1112 | } |
| 1113 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1117 | |
| 1118 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
| 1119 | GraphPrintCheckerState->isExplicitNullDeref(N)) |
| 1120 | return "color=\"red\",style=\"filled\""; |
| 1121 | |
| 1122 | return ""; |
| 1123 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1124 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1125 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1126 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1127 | |
| 1128 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1129 | ProgramPoint Loc = N->getLocation(); |
| 1130 | |
| 1131 | switch (Loc.getKind()) { |
| 1132 | case ProgramPoint::BlockEntranceKind: |
| 1133 | Out << "Block Entrance: B" |
| 1134 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1135 | break; |
| 1136 | |
| 1137 | case ProgramPoint::BlockExitKind: |
| 1138 | assert (false); |
| 1139 | break; |
| 1140 | |
| 1141 | case ProgramPoint::PostStmtKind: { |
| 1142 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1143 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1144 | << (void*) L.getStmt() << ' '; |
| 1145 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1146 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1149 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1150 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1151 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1152 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1153 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1154 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | default: { |
| 1159 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1160 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1161 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1162 | |
| 1163 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1164 | Out << "\\|Terminator: "; |
| 1165 | E.getSrc()->printTerminator(Out); |
| 1166 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1167 | if (isa<SwitchStmt>(T)) { |
| 1168 | Stmt* Label = E.getDst()->getLabel(); |
| 1169 | |
| 1170 | if (Label) { |
| 1171 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1172 | Out << "\\lcase "; |
| 1173 | C->getLHS()->printPretty(Out); |
| 1174 | |
| 1175 | if (Stmt* RHS = C->getRHS()) { |
| 1176 | Out << " .. "; |
| 1177 | RHS->printPretty(Out); |
| 1178 | } |
| 1179 | |
| 1180 | Out << ":"; |
| 1181 | } |
| 1182 | else { |
| 1183 | assert (isa<DefaultStmt>(Label)); |
| 1184 | Out << "\\ldefault:"; |
| 1185 | } |
| 1186 | } |
| 1187 | else |
| 1188 | Out << "\\l(implicit) default:"; |
| 1189 | } |
| 1190 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1191 | // FIXME |
| 1192 | } |
| 1193 | else { |
| 1194 | Out << "\\lCondition: "; |
| 1195 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1196 | Out << "true"; |
| 1197 | else |
| 1198 | Out << "false"; |
| 1199 | } |
| 1200 | |
| 1201 | Out << "\\l"; |
| 1202 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1205 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1206 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1210 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1212 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1213 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1214 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1215 | return Out.str(); |
| 1216 | } |
| 1217 | }; |
| 1218 | } // end llvm namespace |
| 1219 | #endif |
| 1220 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1221 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1222 | #ifndef NDEBUG |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1223 | GraphPrintCheckerState = this; |
| 1224 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1225 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1226 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1227 | } |