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, |
| 476 | NonLValue::GetValue(ValMgr, size, getContext().IntTy, L))); |
| 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, |
| 576 | getContext().IntTy, L))); |
| 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 { |
| 733 | // An operation between two variables of a non-lvalue type. |
| 734 | Result = |
| 735 | EvalBinaryOp(ValMgr, Op, |
| 736 | cast<NonLValue>(GetValue(N1->getState(), L1)), |
| 737 | cast<NonLValue>(GetValue(N2->getState(), L2))); |
| 738 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 739 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 740 | else { // Any other operation between two Non-LValues. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 741 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 742 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 743 | Result = EvalBinaryOp(ValMgr, Op, R1, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 746 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 747 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 748 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 749 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 750 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 751 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 752 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 753 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 754 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 755 | void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred, |
| 756 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 757 | |
| 758 | // FIXME: add metadata to the CFG so that we can disable |
| 759 | // this check when we KNOW that there is no block-level subexpression. |
| 760 | // The motivation is that this check requires a hashtable lookup. |
| 761 | |
| 762 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 763 | Dst.Add(Pred); |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 768 | |
| 769 | default: |
| 770 | // Cases we intentionally have "default" handle: |
| 771 | // AddrLabelExpr, CharacterLiteral, IntegerLiteral |
| 772 | |
| 773 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 774 | break; |
| 775 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 776 | case Stmt::BinaryOperatorClass: { |
| 777 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 778 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 779 | if (B->isLogicalOp()) { |
| 780 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 783 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 784 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 785 | Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 786 | break; |
| 787 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 788 | |
| 789 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | case Stmt::CastExprClass: { |
| 794 | CastExpr* C = cast<CastExpr>(S); |
| 795 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 796 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 797 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 798 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 799 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 800 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 801 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 802 | break; |
| 803 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 804 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 805 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 806 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 807 | break; |
| 808 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 809 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 810 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 811 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | case Stmt::DeclRefExprClass: |
| 816 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 817 | break; |
| 818 | |
| 819 | case Stmt::DeclStmtClass: |
| 820 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 821 | break; |
| 822 | |
| 823 | case Stmt::ImplicitCastExprClass: { |
| 824 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 825 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 826 | break; |
| 827 | } |
| 828 | |
| 829 | case Stmt::ParenExprClass: |
| 830 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 831 | break; |
| 832 | |
| 833 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 834 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 835 | break; |
| 836 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 837 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 838 | StmtExpr* SE = cast<StmtExpr>(S); |
| 839 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 840 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 841 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
| 842 | Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 843 | break; |
| 844 | } |
| 845 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 846 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 847 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 848 | Visit(R, Pred, Dst); |
| 849 | else |
| 850 | Dst.Add(Pred); |
| 851 | |
| 852 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 853 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 854 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 855 | case Stmt::UnaryOperatorClass: |
| 856 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 857 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 858 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 861 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 862 | // "Assume" logic. |
| 863 | //===----------------------------------------------------------------------===// |
| 864 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 865 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 866 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 867 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 868 | |
| 869 | switch (Cond.getSubKind()) { |
| 870 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 871 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 872 | return St; |
| 873 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 874 | case lval::SymbolValKind: |
| 875 | if (Assumption) |
| 876 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 877 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 878 | else |
| 879 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 880 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 881 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 882 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 883 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 884 | isFeasible = Assumption; |
| 885 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 886 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 887 | case lval::ConcreteIntKind: { |
| 888 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 889 | isFeasible = b ? Assumption : !Assumption; |
| 890 | return St; |
| 891 | } |
| 892 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 895 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 896 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 897 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 898 | |
| 899 | switch (Cond.getSubKind()) { |
| 900 | default: |
| 901 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 902 | return St; |
| 903 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 904 | |
| 905 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 906 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 907 | SymbolID sym = SV.getSymbol(); |
| 908 | |
| 909 | if (Assumption) |
| 910 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 911 | isFeasible); |
| 912 | else |
| 913 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 914 | isFeasible); |
| 915 | } |
| 916 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 917 | case nonlval::SymIntConstraintValKind: |
| 918 | return |
| 919 | AssumeSymInt(St, Assumption, |
| 920 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 921 | isFeasible); |
| 922 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 923 | case nonlval::ConcreteIntKind: { |
| 924 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 925 | isFeasible = b ? Assumption : !Assumption; |
| 926 | return St; |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 931 | GRExprEngine::StateTy |
| 932 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 933 | const llvm::APSInt& V, bool& isFeasible) { |
| 934 | |
| 935 | // First, determine if sym == X, where X != V. |
| 936 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 937 | isFeasible = *X != V; |
| 938 | return St; |
| 939 | } |
| 940 | |
| 941 | // Second, determine if sym != V. |
| 942 | if (St.isNotEqual(sym, V)) { |
| 943 | isFeasible = true; |
| 944 | return St; |
| 945 | } |
| 946 | |
| 947 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 948 | // Make that assumption. |
| 949 | |
| 950 | isFeasible = true; |
| 951 | return StateMgr.AddNE(St, sym, V); |
| 952 | } |
| 953 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 954 | GRExprEngine::StateTy |
| 955 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 956 | const llvm::APSInt& V, bool& isFeasible) { |
| 957 | |
| 958 | // First, determine if sym == X, where X != V. |
| 959 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 960 | isFeasible = *X == V; |
| 961 | return St; |
| 962 | } |
| 963 | |
| 964 | // Second, determine if sym != V. |
| 965 | if (St.isNotEqual(sym, V)) { |
| 966 | isFeasible = false; |
| 967 | return St; |
| 968 | } |
| 969 | |
| 970 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 971 | // Make that assumption. |
| 972 | |
| 973 | isFeasible = true; |
| 974 | return StateMgr.AddEQ(St, sym, V); |
| 975 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 976 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 977 | GRExprEngine::StateTy |
| 978 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 979 | const SymIntConstraint& C, bool& isFeasible) { |
| 980 | |
| 981 | switch (C.getOpcode()) { |
| 982 | default: |
| 983 | // No logic yet for other operators. |
| 984 | return St; |
| 985 | |
| 986 | case BinaryOperator::EQ: |
| 987 | if (Assumption) |
| 988 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 989 | else |
| 990 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 991 | |
| 992 | case BinaryOperator::NE: |
| 993 | if (Assumption) |
| 994 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 995 | else |
| 996 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 997 | } |
| 998 | } |
| 999 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1000 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1001 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1002 | //===----------------------------------------------------------------------===// |
| 1003 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1004 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1005 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1006 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1007 | namespace llvm { |
| 1008 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1009 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1010 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1011 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1012 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1013 | |
| 1014 | Out << "Variables:\\l"; |
| 1015 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1016 | bool isFirst = true; |
| 1017 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1018 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1019 | E=St.vb_end(); I!=E;++I) { |
| 1020 | |
| 1021 | if (isFirst) |
| 1022 | isFirst = false; |
| 1023 | else |
| 1024 | Out << "\\l"; |
| 1025 | |
| 1026 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1027 | I.getData().print(Out); |
| 1028 | } |
| 1029 | |
| 1030 | } |
| 1031 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1032 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1033 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1034 | |
| 1035 | bool isFirst = true; |
| 1036 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1037 | 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] | 1038 | I != E;++I) { |
| 1039 | |
| 1040 | if (isFirst) { |
| 1041 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1042 | isFirst = false; |
| 1043 | } |
| 1044 | else |
| 1045 | Out << "\\l"; |
| 1046 | |
| 1047 | Out << " (" << (void*) I.getKey() << ") "; |
| 1048 | I.getKey()->printPretty(Out); |
| 1049 | Out << " : "; |
| 1050 | I.getData().print(Out); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1054 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1055 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1056 | bool isFirst = true; |
| 1057 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1058 | 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] | 1059 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1060 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1061 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1062 | isFirst = false; |
| 1063 | } |
| 1064 | else |
| 1065 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1066 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1067 | Out << " (" << (void*) I.getKey() << ") "; |
| 1068 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1069 | Out << " : "; |
| 1070 | I.getData().print(Out); |
| 1071 | } |
| 1072 | } |
| 1073 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1074 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1075 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1076 | |
| 1077 | if (CE.isEmpty()) |
| 1078 | return; |
| 1079 | |
| 1080 | Out << "\\l\\|'==' constraints:"; |
| 1081 | |
| 1082 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1083 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1084 | } |
| 1085 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1086 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1087 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1088 | |
| 1089 | if (NE.isEmpty()) |
| 1090 | return; |
| 1091 | |
| 1092 | Out << "\\l\\|'!=' constraints:"; |
| 1093 | |
| 1094 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1095 | I != EI; ++I){ |
| 1096 | |
| 1097 | Out << "\\l $" << I.getKey() << " : "; |
| 1098 | bool isFirst = true; |
| 1099 | |
| 1100 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1101 | EJ=I.getData().end(); |
| 1102 | for ( ; J != EJ; ++J) { |
| 1103 | if (isFirst) isFirst = false; |
| 1104 | else Out << ", "; |
| 1105 | |
| 1106 | Out << (*J)->toString(); |
| 1107 | } |
| 1108 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1112 | |
| 1113 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
| 1114 | GraphPrintCheckerState->isExplicitNullDeref(N)) |
| 1115 | return "color=\"red\",style=\"filled\""; |
| 1116 | |
| 1117 | return ""; |
| 1118 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1119 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1120 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1121 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1124 | ProgramPoint Loc = N->getLocation(); |
| 1125 | |
| 1126 | switch (Loc.getKind()) { |
| 1127 | case ProgramPoint::BlockEntranceKind: |
| 1128 | Out << "Block Entrance: B" |
| 1129 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1130 | break; |
| 1131 | |
| 1132 | case ProgramPoint::BlockExitKind: |
| 1133 | assert (false); |
| 1134 | break; |
| 1135 | |
| 1136 | case ProgramPoint::PostStmtKind: { |
| 1137 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1138 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1139 | << (void*) L.getStmt() << ' '; |
| 1140 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1141 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1144 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1145 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1146 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1147 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1148 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1149 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | default: { |
| 1154 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1155 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1156 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1157 | |
| 1158 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1159 | Out << "\\|Terminator: "; |
| 1160 | E.getSrc()->printTerminator(Out); |
| 1161 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1162 | if (isa<SwitchStmt>(T)) { |
| 1163 | Stmt* Label = E.getDst()->getLabel(); |
| 1164 | |
| 1165 | if (Label) { |
| 1166 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1167 | Out << "\\lcase "; |
| 1168 | C->getLHS()->printPretty(Out); |
| 1169 | |
| 1170 | if (Stmt* RHS = C->getRHS()) { |
| 1171 | Out << " .. "; |
| 1172 | RHS->printPretty(Out); |
| 1173 | } |
| 1174 | |
| 1175 | Out << ":"; |
| 1176 | } |
| 1177 | else { |
| 1178 | assert (isa<DefaultStmt>(Label)); |
| 1179 | Out << "\\ldefault:"; |
| 1180 | } |
| 1181 | } |
| 1182 | else |
| 1183 | Out << "\\l(implicit) default:"; |
| 1184 | } |
| 1185 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1186 | // FIXME |
| 1187 | } |
| 1188 | else { |
| 1189 | Out << "\\lCondition: "; |
| 1190 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1191 | Out << "true"; |
| 1192 | else |
| 1193 | Out << "false"; |
| 1194 | } |
| 1195 | |
| 1196 | Out << "\\l"; |
| 1197 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1200 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1201 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1205 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1206 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1207 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1208 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1209 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1210 | return Out.str(); |
| 1211 | } |
| 1212 | }; |
| 1213 | } // end llvm namespace |
| 1214 | #endif |
| 1215 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1216 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1217 | #ifndef NDEBUG |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1218 | GraphPrintCheckerState = this; |
| 1219 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1220 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1221 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1222 | } |