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(); |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 212 | Expr* CondE = builder.getCondition(); |
| 213 | NonLValue CondV = cast<NonLValue>(GetValue(St, CondE)); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 214 | |
| 215 | if (isa<UninitializedVal>(CondV)) { |
| 216 | NodeTy* N = builder.generateDefaultCaseNode(St, true); |
| 217 | UninitBranches.insert(N); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | StateTy DefaultSt = St; |
| 222 | |
| 223 | // While most of this can be assumed (such as the signedness), having it |
| 224 | // just computed makes sure everything makes the same assumptions end-to-end. |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 225 | |
| 226 | unsigned bits = getContext().getTypeSize(CondE->getType(), |
| 227 | CondE->getExprLoc()); |
| 228 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 229 | APSInt V1(bits, false); |
| 230 | APSInt V2 = V1; |
| 231 | |
| 232 | for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) { |
| 233 | |
| 234 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
| 235 | |
| 236 | // Evaluate the case. |
| 237 | if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) { |
| 238 | assert (false && "Case condition must evaluate to an integer constant."); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // Get the RHS of the case, if it exists. |
| 243 | |
| 244 | if (Expr* E = Case->getRHS()) { |
| 245 | if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) { |
| 246 | assert (false && |
| 247 | "Case condition (RHS) must evaluate to an integer constant."); |
| 248 | return ; |
| 249 | } |
| 250 | |
| 251 | assert (V1 <= V2); |
| 252 | } |
| 253 | else V2 = V1; |
| 254 | |
| 255 | // FIXME: Eventually we should replace the logic below with a range |
| 256 | // comparison, rather than concretize the values within the range. |
| 257 | // This should be easy once we have "ranges" for NonLValues. |
| 258 | |
| 259 | do { |
| 260 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 261 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 262 | NonLValue Res = EvalBinaryOp(BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 263 | |
| 264 | // Now "assume" that the case matches. |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 265 | bool isFeasible = false; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 266 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 267 | StateTy StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 268 | |
| 269 | if (isFeasible) { |
| 270 | builder.generateCaseStmtNode(I, StNew); |
| 271 | |
| 272 | // If CondV evaluates to a constant, then we know that this |
| 273 | // is the *only* case that we can take, so stop evaluating the |
| 274 | // others. |
| 275 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // Now "assume" that the case doesn't match. Add this state |
| 280 | // to the default state (if it is feasible). |
| 281 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 282 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 283 | |
| 284 | if (isFeasible) |
| 285 | DefaultSt = StNew; |
| 286 | |
| 287 | // Concretize the next value in the range. |
| 288 | ++V1; |
| 289 | |
| 290 | } while (V1 < V2); |
| 291 | } |
| 292 | |
| 293 | // If we reach here, than we know that the default branch is |
| 294 | // possible. |
| 295 | builder.generateDefaultCaseNode(DefaultSt); |
| 296 | } |
| 297 | |
| 298 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 299 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 300 | NodeSet& Dst) { |
| 301 | |
| 302 | bool hasR2; |
| 303 | StateTy PrevState = Pred->getState(); |
| 304 | |
| 305 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 306 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 307 | |
| 308 | if (hasR2) { |
| 309 | if (isa<UninitializedVal>(R2) || isa<UnknownVal>(R2)) { |
| 310 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 311 | return; |
| 312 | } |
| 313 | } |
| 314 | else if (isa<UninitializedVal>(R1) || isa<UnknownVal>(R1)) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 315 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 320 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 321 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 322 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 323 | if (hasR2 == false) { |
| 324 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 325 | return; |
| 326 | } |
| 327 | } |
| 328 | else { |
| 329 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 330 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 331 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 332 | if (hasR2 == false) { |
| 333 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 334 | return; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 339 | // R2 == false. |
| 340 | |
| 341 | bool isFeasible; |
| 342 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 343 | |
| 344 | if (isFeasible) |
| 345 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 346 | |
| 347 | St = Assume(PrevState, R2, false, isFeasible); |
| 348 | |
| 349 | if (isFeasible) |
| 350 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 355 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 356 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 357 | |
| 358 | StmtEntryNode = builder.getLastNode(); |
| 359 | CurrentStmt = S; |
| 360 | NodeSet Dst; |
| 361 | StateCleaned = false; |
| 362 | |
| 363 | Visit(S, StmtEntryNode, Dst); |
| 364 | |
| 365 | // If no nodes were generated, generate a new node that has all the |
| 366 | // dead mappings removed. |
| 367 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 368 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 369 | builder.generateNode(S, St, StmtEntryNode); |
| 370 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 372 | CurrentStmt = NULL; |
| 373 | StmtEntryNode = NULL; |
| 374 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 377 | GRExprEngine::NodeTy* |
| 378 | GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 379 | |
| 380 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 381 | if (St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 382 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 383 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 384 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 385 | Dst.Add(N); |
| 386 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 387 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 389 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 390 | const StateTy::BufferTy& SB) { |
| 391 | |
| 392 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 393 | Nodify(Dst, S, Pred, *I); |
| 394 | } |
| 395 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 396 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 397 | if (D != CurrentStmt) { |
| 398 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | // If we are here, we are loading the value of the decl and binding |
| 403 | // it to the block-level expression. |
| 404 | |
| 405 | StateTy St = Pred->getState(); |
| 406 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 407 | Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D))); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 410 | void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred, |
| 411 | CallExpr::arg_iterator I, CallExpr::arg_iterator E, |
| 412 | NodeSet& Dst) { |
| 413 | |
| 414 | if (I != E) { |
| 415 | NodeSet DstTmp; |
| 416 | Visit(*I, Pred, DstTmp); |
| 417 | ++I; |
| 418 | |
| 419 | for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI!=DE; ++DI) |
| 420 | VisitCall(CE, *DI, I, E, Dst); |
| 421 | |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | // If we reach here we have processed all of the arguments. Evaluate |
| 426 | // the callee expression. |
| 427 | NodeSet DstTmp; |
| 428 | Visit(CE->getCallee(), Pred, DstTmp); |
| 429 | |
| 430 | // Finally, evaluate the function call. |
| 431 | for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI!=DE; ++DI) { |
| 432 | StateTy St = (*DI)->getState(); |
| 433 | LValue L = GetLValue(St, CE->getCallee()); |
| 434 | |
| 435 | // Check for uninitialized control-flow. |
| 436 | if (isa<UninitializedVal>(L)) { |
| 437 | NodeTy* N = Builder->generateNode(CE, St, *DI); |
| 438 | N->markAsSink(); |
| 439 | UninitBranches.insert(N); |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | // Note: EvalCall must handle the case where the callee is "UnknownVal." |
| 444 | Nodify(Dst, CE, *DI, EvalCall(CE, (*DI)->getState())); |
| 445 | } |
| 446 | } |
| 447 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 448 | void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 449 | |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 450 | NodeSet S1; |
| 451 | Visit(E, Pred, S1); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 453 | QualType T = CastE->getType(); |
| 454 | |
Ted Kremenek | 402563b | 2008-02-19 18:47:04 +0000 | [diff] [blame] | 455 | // Check for redundant casts or casting to "void" |
| 456 | if (T->isVoidType() || |
| 457 | E->getType() == T || |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 458 | (T->isPointerType() && E->getType()->isFunctionType())) { |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 459 | |
| 460 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) |
| 461 | Dst.Add(*I1); |
| 462 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 466 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 467 | NodeTy* N = *I1; |
| 468 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 469 | const RValue& V = GetValue(St, E); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 470 | Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 471 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 474 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
| 475 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 476 | |
| 477 | StateTy St = Pred->getState(); |
| 478 | |
| 479 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 480 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | c2c95b0 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 481 | |
| 482 | // FIXME: Add support for local arrays. |
| 483 | if (VD->getType()->isArrayType()) |
| 484 | continue; |
| 485 | |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 486 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 487 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 488 | E ? GetValue(St, E) : UninitializedVal()); |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 489 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 490 | |
| 491 | Nodify(Dst, DS, Pred, St); |
| 492 | |
| 493 | if (Dst.empty()) |
| 494 | Dst.Add(Pred); |
| 495 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 496 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 497 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 498 | void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 499 | NodeTy* Pred, NodeSet& Dst) { |
| 500 | |
| 501 | StateTy St = Pred->getState(); |
| 502 | |
| 503 | RValue R = GetValue(St, LHS); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 504 | if (isa<UnknownVal>(R)) R = GetValue(St, RHS); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 505 | |
| 506 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 507 | } |
| 508 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 509 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 510 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 511 | NodeTy* Pred, |
| 512 | NodeSet& Dst) { |
| 513 | |
| 514 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 515 | |
| 516 | QualType T = S->getArgumentType(); |
| 517 | |
| 518 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 519 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 520 | return; |
| 521 | |
| 522 | SourceLocation L = S->getExprLoc(); |
| 523 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 524 | |
| 525 | Nodify(Dst, S, Pred, |
| 526 | SetValue(Pred->getState(), S, |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 527 | NonLValue::GetValue(ValMgr, size, S->getType(), L))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 528 | |
| 529 | } |
| 530 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 531 | void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) { |
| 532 | |
| 533 | Expr* E = U->getSubExpr()->IgnoreParens(); |
| 534 | |
| 535 | NodeSet DstTmp; |
| 536 | |
| 537 | if (!isa<DeclRefExpr>(E)) |
| 538 | DstTmp.Add(Pred); |
| 539 | else |
| 540 | Visit(E, Pred, DstTmp); |
| 541 | |
| 542 | for (NodeSet::iterator I=DstTmp.begin(), DE=DstTmp.end(); I != DE; ++I) { |
| 543 | |
| 544 | NodeTy* N = *I; |
| 545 | StateTy St = N->getState(); |
| 546 | |
| 547 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 548 | |
| 549 | LValue L = cast<LValue>(GetValue(St, E)); |
| 550 | |
| 551 | if (isa<UninitializedVal>(L)) { |
| 552 | NodeTy* Succ = Builder->generateNode(U, St, N); |
| 553 | |
| 554 | if (Succ) { |
| 555 | Succ->markAsSink(); |
| 556 | UninitDeref.insert(Succ); |
| 557 | } |
| 558 | |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | if (L.isUnknown()) { |
| 563 | Dst.Add(N); |
| 564 | continue; |
| 565 | } |
| 566 | |
| 567 | // After a dereference, one of two possible situations arise: |
| 568 | // (1) A crash, because the pointer was NULL. |
| 569 | // (2) The pointer is not NULL, and the dereference works. |
| 570 | // |
| 571 | // We add these assumptions. |
| 572 | |
| 573 | bool isFeasibleNotNull; |
| 574 | |
| 575 | // "Assume" that the pointer is Not-NULL. |
| 576 | StateTy StNotNull = Assume(St, L, true, isFeasibleNotNull); |
| 577 | |
| 578 | if (isFeasibleNotNull) { |
| 579 | QualType T = U->getType(); |
| 580 | |
| 581 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 582 | // for the contents of values. We need a better approach. |
| 583 | |
| 584 | Nodify(Dst, U, N, SetValue(StNotNull, U, GetValue(StNotNull, L, &T))); |
| 585 | } |
| 586 | |
| 587 | bool isFeasibleNull; |
| 588 | |
| 589 | // "Assume" that the pointer is NULL. |
| 590 | StateTy StNull = Assume(St, L, false, isFeasibleNull); |
| 591 | |
| 592 | if (isFeasibleNull) { |
| 593 | // We don't use "Nodify" here because the node will be a sink |
| 594 | // and we have no intention of processing it later. |
| 595 | NodeTy* NullNode = Builder->generateNode(U, StNull, N); |
| 596 | |
| 597 | if (NullNode) { |
| 598 | NullNode->markAsSink(); |
| 599 | |
| 600 | if (isFeasibleNotNull) |
| 601 | ImplicitNullDeref.insert(NullNode); |
| 602 | else |
| 603 | ExplicitNullDeref.insert(NullNode); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 609 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 610 | NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 611 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 612 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 613 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 614 | assert (U->getOpcode() != UnaryOperator::Deref); |
| 615 | |
| 616 | switch (U->getOpcode()) { |
| 617 | case UnaryOperator::PostInc: |
| 618 | case UnaryOperator::PostDec: |
| 619 | case UnaryOperator::PreInc: |
| 620 | case UnaryOperator::PreDec: |
| 621 | case UnaryOperator::AddrOf: |
| 622 | // Evalue subexpression as an LValue. |
| 623 | VisitLValue(U->getSubExpr(), Pred, S1); |
| 624 | break; |
| 625 | |
| 626 | case UnaryOperator::SizeOf: |
| 627 | case UnaryOperator::AlignOf: |
| 628 | // Do not evaluate subexpression. |
| 629 | S1.Add(Pred); |
| 630 | break; |
| 631 | |
| 632 | default: |
| 633 | Visit(U->getSubExpr(), Pred, S1); |
| 634 | break; |
| 635 | } |
| 636 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 637 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 638 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 639 | NodeTy* N1 = *I1; |
| 640 | StateTy St = N1->getState(); |
| 641 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 642 | if (U->isIncrementDecrementOp()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 643 | |
| 644 | // Handle ++ and -- (both pre- and post-increment). |
| 645 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 646 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 647 | QualType T = U->getType(); |
| 648 | RValue R1 = GetValue(St, L1, &T); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 649 | |
| 650 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 651 | : BinaryOperator::Sub; |
| 652 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 653 | RValue Result = EvalBinaryOp(Op, R1, GetRValueConstant(1U, U)); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 654 | |
| 655 | if (U->isPostfix()) |
| 656 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 657 | else |
| 658 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 659 | |
| 660 | continue; |
| 661 | } |
| 662 | |
| 663 | // Handle all other unary operators. |
| 664 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 665 | switch (U->getOpcode()) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 666 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 667 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 668 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 669 | Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 670 | break; |
| 671 | } |
| 672 | |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 673 | case UnaryOperator::Plus: { |
| 674 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
| 675 | Nodify(Dst, U, N1, SetValue(St, U, EvalPlus(ValMgr, U, R1))); |
| 676 | break; |
| 677 | } |
| 678 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 679 | case UnaryOperator::Not: { |
| 680 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 681 | Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 685 | case UnaryOperator::LNot: { |
| 686 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 687 | // |
| 688 | // Note: technically we do "E == 0", but this is the same in the |
| 689 | // transfer functions as "0 == E". |
| 690 | |
| 691 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 692 | |
| 693 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 694 | const LValue& L1 = cast<LValue>(V1); |
| 695 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 696 | Nodify(Dst, U, N1, |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 697 | SetValue(St, U, EvalBinaryOp(BinaryOperator::EQ, |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 698 | L1, V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 699 | } |
| 700 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 701 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 702 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 703 | Nodify(Dst, U, N1, |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 704 | SetValue(St, U, EvalBinaryOp(BinaryOperator::EQ, |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 705 | R1, V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | break; |
| 709 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 710 | |
| 711 | case UnaryOperator::SizeOf: { |
| 712 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 713 | |
| 714 | QualType T = U->getSubExpr()->getType(); |
| 715 | |
| 716 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 717 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 718 | return; |
| 719 | |
| 720 | SourceLocation L = U->getExprLoc(); |
| 721 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 722 | |
| 723 | Nodify(Dst, U, N1, |
| 724 | SetValue(St, U, NonLValue::GetValue(ValMgr, size, |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 725 | U->getType(), L))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 726 | |
| 727 | break; |
| 728 | } |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 729 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 730 | case UnaryOperator::AddrOf: { |
| 731 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 732 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 733 | break; |
| 734 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 735 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 736 | default: ; |
| 737 | assert (false && "Not implemented."); |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 742 | void GRExprEngine::VisitLValue(Expr* E, NodeTy* Pred, NodeSet& Dst) { |
| 743 | |
| 744 | E = E->IgnoreParens(); |
| 745 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 746 | if (isa<DeclRefExpr>(E)) { |
| 747 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 748 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 749 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 750 | |
| 751 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 752 | if (U->getOpcode() == UnaryOperator::Deref) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 753 | E = U->getSubExpr()->IgnoreParens(); |
| 754 | |
| 755 | if (isa<DeclRefExpr>(E)) |
| 756 | Dst.Add(Pred); |
| 757 | else |
| 758 | Visit(E, Pred, Dst); |
| 759 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | Visit(E, Pred, Dst); |
| 765 | } |
| 766 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 767 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 768 | GRExprEngine::NodeTy* Pred, |
| 769 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 770 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 771 | |
| 772 | if (B->isAssignmentOp()) |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 773 | VisitLValue(B->getLHS(), Pred, S1); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 774 | else |
| 775 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 776 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 777 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 778 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 779 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 780 | // When getting the value for the LHS, check if we are in an assignment. |
| 781 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 782 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 783 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 784 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 785 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 786 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 787 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 788 | NodeSet S2; |
| 789 | Visit(B->getRHS(), N1, S2); |
| 790 | |
| 791 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 792 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 793 | NodeTy* N2 = *I2; |
| 794 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 795 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 797 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 798 | |
| 799 | if (Op <= BinaryOperator::Or) { |
| 800 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 801 | if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 802 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 803 | continue; |
| 804 | } |
| 805 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 806 | Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(Op, V1, V2))); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 807 | continue; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 808 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | switch (Op) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 812 | case BinaryOperator::Assign: { |
| 813 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 814 | |
| 815 | if (isa<UninitializedVal>(L1)) |
| 816 | HandleUninitializedStore(B, N2); |
| 817 | else |
| 818 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
| 819 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 820 | break; |
| 821 | } |
| 822 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 823 | default: { // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 825 | assert (B->isCompoundAssignmentOp()); |
| 826 | |
| 827 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 828 | |
| 829 | if (isa<UninitializedVal>(L1)) { |
| 830 | HandleUninitializedStore(B, N2); |
| 831 | break; |
| 832 | } |
| 833 | |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 834 | if (isa<UninitializedVal>(V2)) { |
| 835 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
| 836 | break; |
| 837 | } |
| 838 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 839 | RValue Result = cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 840 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 841 | if (Op >= BinaryOperator::AndAssign) |
| 842 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 843 | else |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 844 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 845 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 846 | if (B->getType()->isPointerType()) { // Perform pointer arithmetic. |
| 847 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 848 | Result = EvalBinaryOp(Op, L1, R2); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 849 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 850 | else if (isa<LValue>(V2)) { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 851 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 852 | |
| 853 | if (B->getRHS()->getType()->isPointerType()) { |
| 854 | // LValue comparison. |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 855 | Result = EvalBinaryOp(Op, L1, L2); |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 856 | } |
| 857 | else { |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 858 | QualType T1 = B->getLHS()->getType(); |
| 859 | QualType T2 = B->getRHS()->getType(); |
| 860 | |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 861 | // An operation between two variables of a non-lvalue type. |
| 862 | Result = |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 863 | EvalBinaryOp(Op, |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 864 | cast<NonLValue>(GetValue(N1->getState(), L1, &T1)), |
| 865 | cast<NonLValue>(GetValue(N2->getState(), L2, &T2))); |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 866 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 867 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 868 | else { // Any other operation between two Non-LValues. |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 869 | QualType T = B->getLHS()->getType(); |
| 870 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), |
| 871 | L1, &T)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 872 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 873 | Result = EvalBinaryOp(Op, R1, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 876 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 877 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 878 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 879 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 880 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 881 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 882 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 883 | |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 884 | void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) { |
| 885 | |
| 886 | NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred); |
| 887 | N->markAsSink(); |
| 888 | UninitStores.insert(N); |
| 889 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 890 | |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 891 | void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 892 | |
| 893 | // FIXME: add metadata to the CFG so that we can disable |
| 894 | // this check when we KNOW that there is no block-level subexpression. |
| 895 | // The motivation is that this check requires a hashtable lookup. |
| 896 | |
| 897 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 898 | Dst.Add(Pred); |
| 899 | return; |
| 900 | } |
| 901 | |
| 902 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 903 | |
| 904 | default: |
| 905 | // Cases we intentionally have "default" handle: |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 906 | // AddrLabelExpr |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 907 | |
| 908 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 909 | break; |
| 910 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 911 | case Stmt::BinaryOperatorClass: { |
| 912 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 913 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 914 | if (B->isLogicalOp()) { |
| 915 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 916 | break; |
| 917 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 918 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 919 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 920 | Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 921 | break; |
| 922 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 923 | |
| 924 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 925 | break; |
| 926 | } |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 927 | |
| 928 | case Stmt::CallExprClass: { |
| 929 | CallExpr* C = cast<CallExpr>(S); |
| 930 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
| 931 | break; |
| 932 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 933 | |
| 934 | case Stmt::CastExprClass: { |
| 935 | CastExpr* C = cast<CastExpr>(S); |
| 936 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 937 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 938 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 940 | // While explicitly creating a node+state for visiting a CharacterLiteral |
| 941 | // seems wasteful, it also solves a bunch of problems when handling |
| 942 | // the ?, &&, and ||. |
| 943 | |
| 944 | case Stmt::CharacterLiteralClass: { |
| 945 | CharacterLiteral* C = cast<CharacterLiteral>(S); |
| 946 | StateTy St = Pred->getState(); |
| 947 | NonLValue X = NonLValue::GetValue(ValMgr, C->getValue(), C->getType(), |
| 948 | C->getLoc()); |
| 949 | Nodify(Dst, C, Pred, SetValue(St, C, X)); |
| 950 | break; |
| 951 | } |
| 952 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 953 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 954 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 955 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 956 | break; |
| 957 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 958 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 959 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 960 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 961 | break; |
| 962 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 963 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 964 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 965 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 966 | break; |
| 967 | } |
| 968 | |
| 969 | case Stmt::DeclRefExprClass: |
| 970 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 971 | break; |
| 972 | |
| 973 | case Stmt::DeclStmtClass: |
| 974 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 975 | break; |
| 976 | |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 977 | // While explicitly creating a node+state for visiting an IntegerLiteral |
| 978 | // seems wasteful, it also solves a bunch of problems when handling |
| 979 | // the ?, &&, and ||. |
| 980 | |
| 981 | case Stmt::IntegerLiteralClass: { |
| 982 | StateTy St = Pred->getState(); |
| 983 | IntegerLiteral* I = cast<IntegerLiteral>(S); |
| 984 | NonLValue X = NonLValue::GetValue(ValMgr, I); |
| 985 | Nodify(Dst, I, Pred, SetValue(St, I, X)); |
| 986 | break; |
| 987 | } |
| 988 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 989 | case Stmt::ImplicitCastExprClass: { |
| 990 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 991 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | case Stmt::ParenExprClass: |
| 996 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 997 | break; |
| 998 | |
| 999 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 1000 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 1001 | break; |
| 1002 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1003 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1004 | StmtExpr* SE = cast<StmtExpr>(S); |
| 1005 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1006 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1007 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
| 1008 | Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1012 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1013 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1014 | Visit(R, Pred, Dst); |
| 1015 | else |
| 1016 | Dst.Add(Pred); |
| 1017 | |
| 1018 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1019 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1020 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1021 | case Stmt::UnaryOperatorClass: { |
| 1022 | UnaryOperator* U = cast<UnaryOperator>(S); |
| 1023 | |
| 1024 | if (U->getOpcode() == UnaryOperator::Deref) |
| 1025 | VisitDeref(U, Pred, Dst); |
| 1026 | else |
| 1027 | VisitUnaryOperator(U, Pred, Dst); |
| 1028 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1029 | break; |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1030 | } |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1031 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1034 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1035 | // "Assume" logic. |
| 1036 | //===----------------------------------------------------------------------===// |
| 1037 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1038 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond, |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1039 | bool Assumption, |
| 1040 | bool& isFeasible) { |
| 1041 | |
| 1042 | assert (!isa<UninitializedVal>(Cond)); |
| 1043 | |
| 1044 | if (isa<UnknownVal>(Cond)) { |
| 1045 | isFeasible = true; |
| 1046 | return St; |
| 1047 | } |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1048 | |
| 1049 | switch (Cond.getSubKind()) { |
| 1050 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1051 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1052 | return St; |
| 1053 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1054 | case lval::SymbolValKind: |
| 1055 | if (Assumption) |
| 1056 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1057 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1058 | else |
| 1059 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1060 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1061 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1062 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1063 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1064 | isFeasible = Assumption; |
| 1065 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1066 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1067 | case lval::ConcreteIntKind: { |
| 1068 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1069 | isFeasible = b ? Assumption : !Assumption; |
| 1070 | return St; |
| 1071 | } |
| 1072 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1075 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1076 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1077 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1078 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1079 | assert (!isa<UninitializedVal>(Cond)); |
| 1080 | |
| 1081 | if (isa<UnknownVal>(Cond)) { |
| 1082 | isFeasible = true; |
| 1083 | return St; |
| 1084 | } |
| 1085 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1086 | switch (Cond.getSubKind()) { |
| 1087 | default: |
| 1088 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 1089 | return St; |
| 1090 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1091 | |
| 1092 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1093 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1094 | SymbolID sym = SV.getSymbol(); |
| 1095 | |
| 1096 | if (Assumption) |
| 1097 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1098 | isFeasible); |
| 1099 | else |
| 1100 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1101 | isFeasible); |
| 1102 | } |
| 1103 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1104 | case nonlval::SymIntConstraintValKind: |
| 1105 | return |
| 1106 | AssumeSymInt(St, Assumption, |
| 1107 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1108 | isFeasible); |
| 1109 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1110 | case nonlval::ConcreteIntKind: { |
| 1111 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1112 | isFeasible = b ? Assumption : !Assumption; |
| 1113 | return St; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1118 | GRExprEngine::StateTy |
| 1119 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1120 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1121 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1122 | // First, determine if sym == X, where X != V. |
| 1123 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1124 | isFeasible = *X != V; |
| 1125 | return St; |
| 1126 | } |
| 1127 | |
| 1128 | // Second, determine if sym != V. |
| 1129 | if (St.isNotEqual(sym, V)) { |
| 1130 | isFeasible = true; |
| 1131 | return St; |
| 1132 | } |
| 1133 | |
| 1134 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1135 | // Make that assumption. |
| 1136 | |
| 1137 | isFeasible = true; |
| 1138 | return StateMgr.AddNE(St, sym, V); |
| 1139 | } |
| 1140 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1141 | GRExprEngine::StateTy |
| 1142 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1143 | const llvm::APSInt& V, bool& isFeasible) { |
| 1144 | |
| 1145 | // First, determine if sym == X, where X != V. |
| 1146 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1147 | isFeasible = *X == V; |
| 1148 | return St; |
| 1149 | } |
| 1150 | |
| 1151 | // Second, determine if sym != V. |
| 1152 | if (St.isNotEqual(sym, V)) { |
| 1153 | isFeasible = false; |
| 1154 | return St; |
| 1155 | } |
| 1156 | |
| 1157 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1158 | // Make that assumption. |
| 1159 | |
| 1160 | isFeasible = true; |
| 1161 | return StateMgr.AddEQ(St, sym, V); |
| 1162 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1163 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1164 | GRExprEngine::StateTy |
| 1165 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1166 | const SymIntConstraint& C, bool& isFeasible) { |
| 1167 | |
| 1168 | switch (C.getOpcode()) { |
| 1169 | default: |
| 1170 | // No logic yet for other operators. |
| 1171 | return St; |
| 1172 | |
| 1173 | case BinaryOperator::EQ: |
| 1174 | if (Assumption) |
| 1175 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1176 | else |
| 1177 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1178 | |
| 1179 | case BinaryOperator::NE: |
| 1180 | if (Assumption) |
| 1181 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1182 | else |
| 1183 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1184 | } |
| 1185 | } |
| 1186 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1187 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1188 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1189 | //===----------------------------------------------------------------------===// |
| 1190 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1191 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1192 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1193 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1194 | namespace llvm { |
| 1195 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1196 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1197 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1198 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1199 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1200 | |
| 1201 | Out << "Variables:\\l"; |
| 1202 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1203 | bool isFirst = true; |
| 1204 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1205 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1206 | E=St.vb_end(); I!=E;++I) { |
| 1207 | |
| 1208 | if (isFirst) |
| 1209 | isFirst = false; |
| 1210 | else |
| 1211 | Out << "\\l"; |
| 1212 | |
| 1213 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1214 | I.getData().print(Out); |
| 1215 | } |
| 1216 | |
| 1217 | } |
| 1218 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1219 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1220 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1221 | |
| 1222 | bool isFirst = true; |
| 1223 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1224 | 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] | 1225 | I != E;++I) { |
| 1226 | |
| 1227 | if (isFirst) { |
| 1228 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1229 | isFirst = false; |
| 1230 | } |
| 1231 | else |
| 1232 | Out << "\\l"; |
| 1233 | |
| 1234 | Out << " (" << (void*) I.getKey() << ") "; |
| 1235 | I.getKey()->printPretty(Out); |
| 1236 | Out << " : "; |
| 1237 | I.getData().print(Out); |
| 1238 | } |
| 1239 | } |
| 1240 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1241 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1242 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1243 | bool isFirst = true; |
| 1244 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1245 | 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] | 1246 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1247 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1248 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1249 | isFirst = false; |
| 1250 | } |
| 1251 | else |
| 1252 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1253 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1254 | Out << " (" << (void*) I.getKey() << ") "; |
| 1255 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1256 | Out << " : "; |
| 1257 | I.getData().print(Out); |
| 1258 | } |
| 1259 | } |
| 1260 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1261 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1262 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1263 | |
| 1264 | if (CE.isEmpty()) |
| 1265 | return; |
| 1266 | |
| 1267 | Out << "\\l\\|'==' constraints:"; |
| 1268 | |
| 1269 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1270 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1271 | } |
| 1272 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1273 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1274 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1275 | |
| 1276 | if (NE.isEmpty()) |
| 1277 | return; |
| 1278 | |
| 1279 | Out << "\\l\\|'!=' constraints:"; |
| 1280 | |
| 1281 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1282 | I != EI; ++I){ |
| 1283 | |
| 1284 | Out << "\\l $" << I.getKey() << " : "; |
| 1285 | bool isFirst = true; |
| 1286 | |
| 1287 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1288 | EJ=I.getData().end(); |
| 1289 | for ( ; J != EJ; ++J) { |
| 1290 | if (isFirst) isFirst = false; |
| 1291 | else Out << ", "; |
| 1292 | |
| 1293 | Out << (*J)->toString(); |
| 1294 | } |
| 1295 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1299 | |
| 1300 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1301 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1302 | GraphPrintCheckerState->isUninitDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1303 | GraphPrintCheckerState->isUninitStore(N) || |
| 1304 | GraphPrintCheckerState->isUninitControlFlow(N)) |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1305 | return "color=\"red\",style=\"filled\""; |
| 1306 | |
| 1307 | return ""; |
| 1308 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1310 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1311 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1312 | |
| 1313 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1314 | ProgramPoint Loc = N->getLocation(); |
| 1315 | |
| 1316 | switch (Loc.getKind()) { |
| 1317 | case ProgramPoint::BlockEntranceKind: |
| 1318 | Out << "Block Entrance: B" |
| 1319 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1320 | break; |
| 1321 | |
| 1322 | case ProgramPoint::BlockExitKind: |
| 1323 | assert (false); |
| 1324 | break; |
| 1325 | |
| 1326 | case ProgramPoint::PostStmtKind: { |
| 1327 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1328 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1329 | << (void*) L.getStmt() << ' '; |
| 1330 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1331 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1334 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1335 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1336 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1337 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1338 | } |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1339 | else if (GraphPrintCheckerState->isUninitDeref(N)) { |
| 1340 | Out << "\\|Dereference of uninitialied value.\\l"; |
| 1341 | } |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1342 | else if (GraphPrintCheckerState->isUninitStore(N)) { |
| 1343 | Out << "\\|Store to Uninitialized LValue."; |
| 1344 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1345 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | } |
| 1348 | |
| 1349 | default: { |
| 1350 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1351 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1352 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1355 | Out << "\\|Terminator: "; |
| 1356 | E.getSrc()->printTerminator(Out); |
| 1357 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1358 | if (isa<SwitchStmt>(T)) { |
| 1359 | Stmt* Label = E.getDst()->getLabel(); |
| 1360 | |
| 1361 | if (Label) { |
| 1362 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1363 | Out << "\\lcase "; |
| 1364 | C->getLHS()->printPretty(Out); |
| 1365 | |
| 1366 | if (Stmt* RHS = C->getRHS()) { |
| 1367 | Out << " .. "; |
| 1368 | RHS->printPretty(Out); |
| 1369 | } |
| 1370 | |
| 1371 | Out << ":"; |
| 1372 | } |
| 1373 | else { |
| 1374 | assert (isa<DefaultStmt>(Label)); |
| 1375 | Out << "\\ldefault:"; |
| 1376 | } |
| 1377 | } |
| 1378 | else |
| 1379 | Out << "\\l(implicit) default:"; |
| 1380 | } |
| 1381 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1382 | // FIXME |
| 1383 | } |
| 1384 | else { |
| 1385 | Out << "\\lCondition: "; |
| 1386 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1387 | Out << "true"; |
| 1388 | else |
| 1389 | Out << "false"; |
| 1390 | } |
| 1391 | |
| 1392 | Out << "\\l"; |
| 1393 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1394 | |
| 1395 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1396 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1397 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1401 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1402 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1403 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1404 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1405 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1406 | return Out.str(); |
| 1407 | } |
| 1408 | }; |
| 1409 | } // end llvm namespace |
| 1410 | #endif |
| 1411 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1412 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1413 | #ifndef NDEBUG |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1414 | GraphPrintCheckerState = this; |
| 1415 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1416 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1417 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1418 | } |