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 |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 27 | GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& 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 | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 36 | if (Ex == CurrentStmt) { |
| 37 | isBlkExpr = getCFG().isBlkExpr(Ex); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 38 | |
| 39 | if (!isBlkExpr) |
| 40 | return St; |
| 41 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 43 | return StateMgr.SetRVal(St, Ex, isBlkExpr, V); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 46 | const GRExprEngine::StateTy::BufferTy& |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 47 | GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 48 | StateTy::BufferTy& RetBuf) { |
| 49 | |
| 50 | assert (RetBuf.empty()); |
| 51 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 52 | for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I) |
| 53 | RetBuf.push_back(SetRVal(St, Ex, *I)); |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 54 | |
| 55 | return RetBuf; |
| 56 | } |
| 57 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 58 | GRExprEngine::StateTy |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 59 | GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 60 | |
| 61 | if (!StateCleaned) { |
| 62 | St = RemoveDeadBindings(CurrentStmt, St); |
| 63 | StateCleaned = true; |
| 64 | } |
| 65 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 66 | return StateMgr.SetRVal(St, LV, RV); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 69 | void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 70 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 72 | // Remove old bindings for subexpressions. |
| 73 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 75 | // Check for NULL conditions; e.g. "for(;;)" |
| 76 | if (!Condition) { |
| 77 | builder.markInfeasible(false); |
| 78 | |
| 79 | // Get the current block counter. |
| 80 | GRBlockCounter BC = builder.getBlockCounter(); |
| 81 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 82 | unsigned NumVisited = BC.getNumVisited(BlockID); |
| 83 | |
| 84 | if (NumVisited < 1) builder.generateNode(PrevState, true); |
| 85 | else builder.markInfeasible(true); |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 90 | RVal V = GetRVal(PrevState, Condition); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 91 | |
| 92 | switch (V.getBaseKind()) { |
| 93 | default: |
| 94 | break; |
| 95 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 96 | case RVal::UnknownKind: |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 97 | builder.generateNode(PrevState, true); |
| 98 | builder.generateNode(PrevState, false); |
| 99 | return; |
| 100 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 101 | case RVal::UninitializedKind: { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 102 | NodeTy* N = builder.generateNode(PrevState, true); |
| 103 | |
| 104 | if (N) { |
| 105 | N->markAsSink(); |
| 106 | UninitBranches.insert(N); |
| 107 | } |
| 108 | |
| 109 | builder.markInfeasible(false); |
| 110 | return; |
| 111 | } |
| 112 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 114 | // Get the current block counter. |
| 115 | GRBlockCounter BC = builder.getBlockCounter(); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 116 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 117 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 119 | if (isa<nonlval::ConcreteInt>(V) || |
| 120 | BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) { |
| 121 | |
| 122 | // Process the true branch. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 124 | bool isFeasible = true; |
| 125 | |
| 126 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 127 | |
| 128 | if (isFeasible) |
| 129 | builder.generateNode(St, true); |
| 130 | else |
| 131 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 132 | } |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 133 | else |
| 134 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 136 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 137 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 139 | if (isa<nonlval::ConcreteInt>(V) || |
| 140 | BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) { |
| 141 | |
| 142 | // Process the false branch. |
| 143 | |
| 144 | bool isFeasible = false; |
| 145 | |
| 146 | StateTy St = Assume(PrevState, V, false, isFeasible); |
| 147 | |
| 148 | if (isFeasible) |
| 149 | builder.generateNode(St, false); |
| 150 | else |
| 151 | builder.markInfeasible(false); |
| 152 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 153 | else |
| 154 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 157 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 158 | /// nodes by processing the 'effects' of a computed goto jump. |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 159 | void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 160 | |
| 161 | StateTy St = builder.getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 162 | RVal V = GetRVal(St, builder.getTarget()); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 163 | |
| 164 | // Three possibilities: |
| 165 | // |
| 166 | // (1) We know the computed label. |
| 167 | // (2) The label is NULL (or some other constant), or Uninitialized. |
| 168 | // (3) We have no clue about the label. Dispatch to all targets. |
| 169 | // |
| 170 | |
| 171 | typedef IndirectGotoNodeBuilder::iterator iterator; |
| 172 | |
| 173 | if (isa<lval::GotoLabel>(V)) { |
| 174 | LabelStmt* L = cast<lval::GotoLabel>(V).getLabel(); |
| 175 | |
| 176 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 177 | if (I.getLabel() == L) { |
| 178 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | assert (false && "No block with label."); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) { |
| 188 | // Dispatch to the first target and mark it as a sink. |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 189 | NodeTy* N = builder.generateNode(builder.begin(), St, true); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 190 | UninitBranches.insert(N); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // This is really a catch-all. We don't support symbolics yet. |
| 195 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 196 | assert (V.isUnknown()); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 197 | |
| 198 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 199 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 200 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 202 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 203 | /// nodes by processing the 'effects' of a switch statement. |
| 204 | void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) { |
| 205 | |
| 206 | typedef SwitchNodeBuilder::iterator iterator; |
| 207 | |
| 208 | StateTy St = builder.getState(); |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 209 | Expr* CondE = builder.getCondition(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 210 | RVal CondV = GetRVal(St, CondE); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 212 | if (CondV.isUninit()) { |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 213 | NodeTy* N = builder.generateDefaultCaseNode(St, true); |
| 214 | UninitBranches.insert(N); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | StateTy DefaultSt = St; |
| 219 | |
| 220 | // While most of this can be assumed (such as the signedness), having it |
| 221 | // just computed makes sure everything makes the same assumptions end-to-end. |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 222 | |
| 223 | unsigned bits = getContext().getTypeSize(CondE->getType(), |
| 224 | CondE->getExprLoc()); |
| 225 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 226 | APSInt V1(bits, false); |
| 227 | APSInt V2 = V1; |
| 228 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 229 | for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) { |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 230 | |
| 231 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
| 232 | |
| 233 | // Evaluate the case. |
| 234 | if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) { |
| 235 | assert (false && "Case condition must evaluate to an integer constant."); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | // Get the RHS of the case, if it exists. |
| 240 | |
| 241 | if (Expr* E = Case->getRHS()) { |
| 242 | if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) { |
| 243 | assert (false && |
| 244 | "Case condition (RHS) must evaluate to an integer constant."); |
| 245 | return ; |
| 246 | } |
| 247 | |
| 248 | assert (V1 <= V2); |
| 249 | } |
| 250 | else V2 = V1; |
| 251 | |
| 252 | // FIXME: Eventually we should replace the logic below with a range |
| 253 | // comparison, rather than concretize the values within the range. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 254 | // This should be easy once we have "ranges" for NonLVals. |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 255 | |
| 256 | do { |
| 257 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 258 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 259 | RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 260 | |
| 261 | // Now "assume" that the case matches. |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 262 | bool isFeasible = false; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 264 | StateTy StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 265 | |
| 266 | if (isFeasible) { |
| 267 | builder.generateCaseStmtNode(I, StNew); |
| 268 | |
| 269 | // If CondV evaluates to a constant, then we know that this |
| 270 | // is the *only* case that we can take, so stop evaluating the |
| 271 | // others. |
| 272 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | // Now "assume" that the case doesn't match. Add this state |
| 277 | // to the default state (if it is feasible). |
| 278 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 279 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 280 | |
| 281 | if (isFeasible) |
| 282 | DefaultSt = StNew; |
| 283 | |
| 284 | // Concretize the next value in the range. |
| 285 | ++V1; |
| 286 | |
| 287 | } while (V1 < V2); |
| 288 | } |
| 289 | |
| 290 | // If we reach here, than we know that the default branch is |
| 291 | // possible. |
| 292 | builder.generateDefaultCaseNode(DefaultSt); |
| 293 | } |
| 294 | |
| 295 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 296 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 297 | NodeSet& Dst) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 298 | |
| 299 | bool hasR2; |
| 300 | StateTy PrevState = Pred->getState(); |
| 301 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 302 | RVal R1 = GetRVal(PrevState, B->getLHS()); |
| 303 | RVal R2 = GetRVal(PrevState, B->getRHS(), hasR2); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 304 | |
| 305 | if (hasR2) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 306 | if (R2.isUnknownOrUninit()) { |
| 307 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, R2)); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 311 | else if (R1.isUnknownOrUninit()) { |
| 312 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, R1)); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 321 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 330 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 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) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 342 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 343 | |
| 344 | St = Assume(PrevState, R2, false, isFeasible); |
| 345 | |
| 346 | if (isFeasible) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 347 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 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 | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 354 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 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. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 364 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 365 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 366 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 367 | builder.generateNode(S, St, StmtEntryNode); |
| 368 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 370 | // For safety, NULL out these variables. |
| 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); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 387 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 388 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 390 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 391 | const StateTy::BufferTy& SB) { |
| 392 | |
| 393 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 394 | Nodify(Dst, S, Pred, *I); |
| 395 | } |
| 396 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 397 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 399 | if (VarDecl* VD = dyn_cast<VarDecl>(D->getDecl())) |
| 400 | if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) { |
| 401 | |
| 402 | StateTy StOld = Pred->getState(); |
| 403 | StateTy St = Symbolicate(StOld, VD); |
| 404 | |
| 405 | if (!(St == StOld)) { |
| 406 | if (D != CurrentStmt) |
| 407 | Nodify(Dst, D, Pred, St); |
| 408 | else |
| 409 | Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D))); |
| 410 | |
| 411 | return; |
| 412 | } |
| 413 | } |
| 414 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 415 | if (D != CurrentStmt) { |
| 416 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | // If we are here, we are loading the value of the decl and binding |
| 421 | // it to the block-level expression. |
| 422 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 423 | StateTy St = Pred->getState(); |
| 424 | Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D))); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 427 | void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 428 | CallExpr::arg_iterator AI, |
| 429 | CallExpr::arg_iterator AE, |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 430 | NodeSet& Dst) { |
| 431 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 432 | // Process the arguments. |
| 433 | |
| 434 | if (AI != AE) { |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 436 | NodeSet DstTmp; |
| 437 | Visit(*AI, Pred, DstTmp); |
| 438 | ++AI; |
| 439 | |
| 440 | for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI) |
| 441 | VisitCall(CE, *DI, AI, AE, Dst); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 442 | |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | // If we reach here we have processed all of the arguments. Evaluate |
| 447 | // the callee expression. |
Ted Kremenek | 994a09b | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 448 | NodeSet DstTmp; |
| 449 | Expr* Callee = CE->getCallee()->IgnoreParenCasts(); |
| 450 | |
| 451 | VisitLVal(Callee, Pred, DstTmp); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 452 | |
| 453 | // Finally, evaluate the function call. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 454 | for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) { |
| 455 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 456 | StateTy St = (*DI)->getState(); |
Ted Kremenek | 994a09b | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 457 | RVal L = GetLVal(St, Callee); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 458 | |
| 459 | // Check for uninitialized control-flow. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 460 | |
| 461 | if (L.isUninit()) { |
| 462 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 463 | NodeTy* N = Builder->generateNode(CE, St, *DI); |
| 464 | N->markAsSink(); |
| 465 | UninitBranches.insert(N); |
| 466 | continue; |
| 467 | } |
| 468 | |
Ted Kremenek | 03da0d7 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 469 | if (L.isUnknown()) { |
| 470 | // Invalidate all arguments passed in by reference (LVals). |
| 471 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 472 | I != E; ++I) { |
| 473 | RVal V = GetRVal(St, *I); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 474 | |
Ted Kremenek | 03da0d7 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 475 | if (isa<LVal>(V)) |
| 476 | St = SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 477 | } |
| 478 | } |
| 479 | else |
| 480 | St = EvalCall(CE, cast<LVal>(L), (*DI)->getState()); |
| 481 | |
| 482 | Nodify(Dst, CE, *DI, St); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 486 | void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 488 | NodeSet S1; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 489 | Visit(Ex, Pred, S1); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 490 | |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 491 | QualType T = CastE->getType(); |
| 492 | |
Ted Kremenek | 402563b | 2008-02-19 18:47:04 +0000 | [diff] [blame] | 493 | // Check for redundant casts or casting to "void" |
| 494 | if (T->isVoidType() || |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 495 | Ex->getType() == T || |
| 496 | (T->isPointerType() && Ex->getType()->isFunctionType())) { |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 497 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 498 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 499 | Dst.Add(*I1); |
| 500 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 504 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 505 | NodeTy* N = *I1; |
| 506 | StateTy St = N->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 507 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 508 | Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType()))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 509 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 512 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 513 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 514 | |
| 515 | StateTy St = Pred->getState(); |
| 516 | |
| 517 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 518 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | c2c95b0 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 519 | |
| 520 | // FIXME: Add support for local arrays. |
| 521 | if (VD->getType()->isArrayType()) |
| 522 | continue; |
| 523 | |
Ted Kremenek | 4dc3522 | 2008-02-26 00:20:52 +0000 | [diff] [blame] | 524 | // FIXME: static variables have an initializer, but the second |
| 525 | // time a function is called those values may not be current. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 526 | const Expr* Ex = VD->getInit(); |
| 527 | |
| 528 | St = SetRVal(St, lval::DeclVal(VD), |
| 529 | Ex ? GetRVal(St, Ex) : UninitializedVal()); |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 530 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 531 | |
| 532 | Nodify(Dst, DS, Pred, St); |
| 533 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 534 | if (Dst.empty()) { Dst.Add(Pred); } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 535 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 536 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 537 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 538 | void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 539 | NodeTy* Pred, NodeSet& Dst) { |
| 540 | |
| 541 | StateTy St = Pred->getState(); |
| 542 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 543 | RVal V = GetRVal(St, L); |
| 544 | if (isa<UnknownVal>(V)) V = GetRVal(St, R); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 546 | Nodify(Dst, Ex, Pred, SetRVal(St, Ex, V)); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 549 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 550 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex, |
| 551 | NodeTy* Pred, |
| 552 | NodeSet& Dst) { |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 553 | |
| 554 | assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented."); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 555 | |
| 556 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 557 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 558 | QualType T = Ex->getArgumentType(); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 559 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 560 | |
Ted Kremenek | 297d0d7 | 2008-02-21 18:15:29 +0000 | [diff] [blame] | 561 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 562 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 563 | return; |
| 564 | |
Ted Kremenek | 297d0d7 | 2008-02-21 18:15:29 +0000 | [diff] [blame] | 565 | |
| 566 | uint64_t size = 1; // Handle sizeof(void) |
| 567 | |
| 568 | if (T != getContext().VoidTy) { |
| 569 | SourceLocation Loc = Ex->getExprLoc(); |
| 570 | size = getContext().getTypeSize(T, Loc) / 8; |
| 571 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 572 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 573 | Nodify(Dst, Ex, Pred, |
| 574 | SetRVal(Pred->getState(), Ex, |
Ted Kremenek | 297d0d7 | 2008-02-21 18:15:29 +0000 | [diff] [blame] | 575 | NonLVal::MakeVal(ValMgr, size, Ex->getType()))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 576 | |
| 577 | } |
| 578 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 579 | void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) { |
| 580 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 581 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 582 | |
| 583 | NodeSet DstTmp; |
| 584 | |
Ted Kremenek | 018c15f | 2008-02-26 03:44:25 +0000 | [diff] [blame^] | 585 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 586 | DstTmp.Add(Pred); |
| 587 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 588 | Visit(Ex, Pred, DstTmp); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 589 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 590 | for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 591 | |
| 592 | NodeTy* N = *I; |
| 593 | StateTy St = N->getState(); |
| 594 | |
| 595 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 596 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 597 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 598 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 599 | // Check for dereferences of uninitialized values. |
| 600 | |
| 601 | if (V.isUninit()) { |
| 602 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 603 | NodeTy* Succ = Builder->generateNode(U, St, N); |
| 604 | |
| 605 | if (Succ) { |
| 606 | Succ->markAsSink(); |
| 607 | UninitDeref.insert(Succ); |
| 608 | } |
| 609 | |
| 610 | continue; |
| 611 | } |
| 612 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 613 | // Check for dereferences of unknown values. Treat as No-Ops. |
| 614 | |
| 615 | if (V.isUnknown()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 616 | Dst.Add(N); |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | // After a dereference, one of two possible situations arise: |
| 621 | // (1) A crash, because the pointer was NULL. |
| 622 | // (2) The pointer is not NULL, and the dereference works. |
| 623 | // |
| 624 | // We add these assumptions. |
| 625 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 626 | LVal LV = cast<LVal>(V); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 627 | bool isFeasibleNotNull; |
| 628 | |
| 629 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 630 | |
| 631 | StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 632 | |
| 633 | if (isFeasibleNotNull) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 634 | |
| 635 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 636 | // for the contents of values. We need a better approach. |
| 637 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 638 | Nodify(Dst, U, N, SetRVal(StNotNull, U, |
| 639 | GetRVal(StNotNull, LV, U->getType()))); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | bool isFeasibleNull; |
| 643 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 644 | // Now "assume" that the pointer is NULL. |
| 645 | |
| 646 | StateTy StNull = Assume(St, LV, false, isFeasibleNull); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 647 | |
| 648 | if (isFeasibleNull) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 649 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 650 | // We don't use "Nodify" here because the node will be a sink |
| 651 | // and we have no intention of processing it later. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 652 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 653 | NodeTy* NullNode = Builder->generateNode(U, StNull, N); |
| 654 | |
| 655 | if (NullNode) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 656 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 657 | NullNode->markAsSink(); |
| 658 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 659 | if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode); |
| 660 | else ExplicitNullDeref.insert(NullNode); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 666 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred, |
| 667 | NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 668 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 669 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 670 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 671 | assert (U->getOpcode() != UnaryOperator::Deref); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 672 | assert (U->getOpcode() != UnaryOperator::SizeOf); |
| 673 | assert (U->getOpcode() != UnaryOperator::AlignOf); |
| 674 | |
| 675 | bool use_GetLVal = false; |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 676 | |
| 677 | switch (U->getOpcode()) { |
| 678 | case UnaryOperator::PostInc: |
| 679 | case UnaryOperator::PostDec: |
| 680 | case UnaryOperator::PreInc: |
| 681 | case UnaryOperator::PreDec: |
| 682 | case UnaryOperator::AddrOf: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 683 | // Evalue subexpression as an LVal. |
| 684 | use_GetLVal = true; |
| 685 | VisitLVal(U->getSubExpr(), Pred, S1); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 686 | break; |
| 687 | |
| 688 | default: |
| 689 | Visit(U->getSubExpr(), Pred, S1); |
| 690 | break; |
| 691 | } |
| 692 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 693 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 694 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 695 | NodeTy* N1 = *I1; |
| 696 | StateTy St = N1->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 697 | |
| 698 | RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) : |
| 699 | GetRVal(St, U->getSubExpr()); |
| 700 | |
| 701 | if (SubV.isUnknown()) { |
| 702 | Dst.Add(N1); |
| 703 | continue; |
| 704 | } |
| 705 | |
| 706 | if (SubV.isUninit()) { |
| 707 | Nodify(Dst, U, N1, SetRVal(St, U, SubV)); |
| 708 | continue; |
| 709 | } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 710 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 711 | if (U->isIncrementDecrementOp()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 712 | |
| 713 | // Handle ++ and -- (both pre- and post-increment). |
| 714 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 715 | LVal SubLV = cast<LVal>(SubV); |
| 716 | RVal V = GetRVal(St, SubLV, U->getType()); |
| 717 | |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 718 | if (V.isUnknown()) { |
| 719 | Dst.Add(N1); |
| 720 | continue; |
| 721 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 722 | |
| 723 | // Propagate uninitialized values. |
| 724 | if (V.isUninit()) { |
| 725 | Nodify(Dst, U, N1, SetRVal(St, U, V)); |
| 726 | continue; |
| 727 | } |
| 728 | |
Ted Kremenek | 443003b | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 729 | // Handle all other values. |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 730 | |
| 731 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 732 | : BinaryOperator::Sub; |
| 733 | |
Ted Kremenek | 443003b | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 734 | RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U)); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 735 | |
| 736 | if (U->isPostfix()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 737 | St = SetRVal(SetRVal(St, U, V), SubLV, Result); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 738 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 739 | St = SetRVal(SetRVal(St, U, Result), SubLV, Result); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 740 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 741 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 742 | continue; |
| 743 | } |
| 744 | |
| 745 | // Handle all other unary operators. |
| 746 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 747 | switch (U->getOpcode()) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 748 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 749 | case UnaryOperator::Minus: |
| 750 | St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 751 | break; |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 752 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 753 | case UnaryOperator::Not: |
| 754 | St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV))); |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 755 | break; |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 756 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 757 | case UnaryOperator::LNot: |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 758 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 759 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 760 | // |
| 761 | // Note: technically we do "E == 0", but this is the same in the |
| 762 | // transfer functions as "0 == E". |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 763 | |
| 764 | if (isa<LVal>(SubV)) { |
| 765 | lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth()); |
| 766 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V); |
| 767 | St = SetRVal(St, U, Result); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 768 | } |
| 769 | else { |
Ted Kremenek | f7ca696 | 2008-02-22 00:42:36 +0000 | [diff] [blame] | 770 | Expr* Ex = U->getSubExpr(); |
| 771 | nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType())); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 772 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V); |
| 773 | St = SetRVal(St, U, Result); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | break; |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 777 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 778 | case UnaryOperator::AddrOf: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 779 | assert (isa<LVal>(SubV)); |
| 780 | St = SetRVal(St, U, SubV); |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 783 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 784 | default: ; |
| 785 | assert (false && "Not implemented."); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 792 | void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred, |
| 793 | NodeSet& Dst) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 794 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 795 | QualType T = U->getSubExpr()->getType(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 797 | // FIXME: Add support for VLAs. |
| 798 | if (!T.getTypePtr()->isConstantSizeType()) |
| 799 | return; |
| 800 | |
| 801 | SourceLocation Loc = U->getExprLoc(); |
| 802 | uint64_t size = getContext().getTypeSize(T, Loc) / 8; |
| 803 | StateTy St = Pred->getState(); |
| 804 | St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc)); |
| 805 | |
| 806 | Nodify(Dst, U, Pred, St); |
| 807 | } |
| 808 | |
| 809 | void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) { |
| 810 | |
| 811 | assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex)); |
| 812 | |
| 813 | Ex = Ex->IgnoreParens(); |
| 814 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 815 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) { |
| 816 | |
| 817 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 818 | if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) { |
| 819 | |
| 820 | StateTy StOld = Pred->getState(); |
| 821 | StateTy St = Symbolicate(StOld, VD); |
| 822 | |
| 823 | if (!(St == StOld)) { |
| 824 | Nodify(Dst, Ex, Pred, St); |
| 825 | return; |
| 826 | } |
| 827 | } |
| 828 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 829 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 830 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 831 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 832 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 833 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 834 | if (U->getOpcode() == UnaryOperator::Deref) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 835 | Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 836 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 837 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 838 | Dst.Add(Pred); |
| 839 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 840 | Visit(Ex, Pred, Dst); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 841 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | } |
| 845 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 846 | Visit(Ex, Pred, Dst); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 849 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 850 | GRExprEngine::NodeTy* Pred, |
| 851 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 852 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 853 | |
| 854 | if (B->isAssignmentOp()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 855 | VisitLVal(B->getLHS(), Pred, S1); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 856 | else |
| 857 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 858 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 859 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 861 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 862 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 863 | // When getting the value for the LHS, check if we are in an assignment. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 864 | // In such cases, we want to (initially) treat the LHS as an LVal, |
| 865 | // so we use GetLVal instead of GetRVal so that DeclRefExpr's are |
| 866 | // evaluated to LValDecl's instead of to an NonLVal. |
| 867 | |
| 868 | RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS()) |
| 869 | : GetRVal(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 870 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 871 | // Visit the RHS... |
| 872 | |
| 873 | NodeSet S2; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 874 | Visit(B->getRHS(), N1, S2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 875 | |
| 876 | // Process the binary operator. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 877 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 878 | for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 879 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 880 | NodeTy* N2 = *I2; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 881 | StateTy St = N2->getState(); |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 882 | Expr* RHS = B->getRHS(); |
| 883 | RVal RightV = GetRVal(St, RHS); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 884 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 885 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 886 | |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 887 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 888 | && RHS->getType()->isIntegerType()) { |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 889 | |
| 890 | // Check if the denominator is uninitialized. |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 891 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 892 | if (RightV.isUninit()) { |
| 893 | NodeTy* DivUninit = Builder->generateNode(B, St, N2); |
| 894 | |
| 895 | if (DivUninit) { |
| 896 | DivUninit->markAsSink(); |
| 897 | BadDivides.insert(DivUninit); |
| 898 | } |
| 899 | |
| 900 | continue; |
| 901 | } |
| 902 | |
| 903 | // Check for divide/remainder-by-zero. |
| 904 | // |
| 905 | // First, "assume" that the denominator is 0 or uninitialized. |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 906 | |
| 907 | bool isFeasible = false; |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 908 | StateTy ZeroSt = Assume(St, RightV, false,isFeasible); |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 909 | |
| 910 | if (isFeasible) { |
| 911 | NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2); |
| 912 | |
| 913 | if (DivZeroNode) { |
| 914 | DivZeroNode->markAsSink(); |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 915 | BadDivides.insert(DivZeroNode); |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| 919 | // Second, "assume" that the denominator cannot be 0. |
| 920 | |
| 921 | isFeasible = false; |
| 922 | St = Assume(St, RightV, true, isFeasible); |
| 923 | |
| 924 | if (!isFeasible) |
| 925 | continue; |
| 926 | |
| 927 | // Fall-through. The logic below processes the divide. |
| 928 | } |
| 929 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 930 | if (Op <= BinaryOperator::Or) { |
| 931 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 932 | // Process non-assignements except commas or short-circuited |
| 933 | // logical expressions (LAnd and LOr). |
| 934 | |
| 935 | RVal Result = EvalBinOp(Op, LeftV, RightV); |
| 936 | |
| 937 | if (Result.isUnknown()) { |
| 938 | Dst.Add(N2); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 939 | continue; |
| 940 | } |
| 941 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 942 | Nodify(Dst, B, N2, SetRVal(St, B, Result)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 943 | continue; |
| 944 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 945 | |
| 946 | // Process assignments. |
| 947 | |
| 948 | switch (Op) { |
| 949 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 950 | case BinaryOperator::Assign: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 951 | |
| 952 | // Simple assignments. |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 953 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 954 | if (LeftV.isUninit()) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 955 | HandleUninitializedStore(B, N2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 956 | continue; |
| 957 | } |
| 958 | |
| 959 | if (LeftV.isUnknown()) { |
| 960 | St = SetRVal(St, B, RightV); |
| 961 | break; |
| 962 | } |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 963 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 964 | St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 968 | // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 970 | default: { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 971 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 972 | assert (B->isCompoundAssignmentOp()); |
| 973 | |
| 974 | if (LeftV.isUninit()) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 975 | HandleUninitializedStore(B, N2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 976 | continue; |
| 977 | } |
| 978 | |
| 979 | if (LeftV.isUnknown()) { |
| 980 | |
| 981 | // While we do not know the location to store RightV, |
| 982 | // the entire expression does evaluate to RightV. |
| 983 | |
| 984 | if (RightV.isUnknown()) { |
| 985 | Dst.Add(N2); |
| 986 | continue; |
| 987 | } |
| 988 | |
| 989 | St = SetRVal(St, B, RightV); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 990 | break; |
| 991 | } |
| 992 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 993 | // At this pointer we know that the LHS evaluates to an LVal |
| 994 | // that is neither "Unknown" or "Unintialized." |
| 995 | |
| 996 | LVal LeftLV = cast<LVal>(LeftV); |
| 997 | |
| 998 | // Propagate uninitialized values (right-side). |
| 999 | |
| 1000 | if (RightV.isUninit()) { |
| 1001 | St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV); |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1002 | break; |
| 1003 | } |
| 1004 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1005 | // Fetch the value of the LHS (the value of the variable, etc.). |
| 1006 | |
| 1007 | RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType()); |
| 1008 | |
| 1009 | // Propagate uninitialized value (left-side). |
| 1010 | |
| 1011 | if (V.isUninit()) { |
| 1012 | St = SetRVal(St, B, V); |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | // Propagate unknown values. |
| 1017 | |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 1018 | if (V.isUnknown()) { |
| 1019 | Dst.Add(N2); |
| 1020 | continue; |
| 1021 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1022 | |
| 1023 | if (RightV.isUnknown()) { |
| 1024 | St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV); |
| 1025 | break; |
| 1026 | } |
| 1027 | |
| 1028 | // Neither the LHS or the RHS have Unknown/Uninit values. Process |
| 1029 | // the operation and store the result. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1030 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1031 | if (Op >= BinaryOperator::AndAssign) |
| 1032 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 1033 | else |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 1034 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1035 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1036 | // Get the computation type. |
| 1037 | QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType(); |
| 1038 | |
| 1039 | // Perform promotions. |
| 1040 | V = EvalCast(V, CTy); |
Ted Kremenek | 61e090c | 2008-02-21 18:46:24 +0000 | [diff] [blame] | 1041 | RightV = EvalCast(RightV, CTy); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1042 | |
| 1043 | // Evaluate operands and promote to result type. |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1044 | |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1045 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 1046 | && RHS->getType()->isIntegerType()) { |
| 1047 | |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1048 | // Check if the denominator is uninitialized. |
| 1049 | |
| 1050 | if (RightV.isUninit()) { |
| 1051 | NodeTy* DivUninit = Builder->generateNode(B, St, N2); |
| 1052 | |
| 1053 | if (DivUninit) { |
| 1054 | DivUninit->markAsSink(); |
| 1055 | BadDivides.insert(DivUninit); |
| 1056 | } |
| 1057 | |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1061 | // First, "assume" that the denominator is 0. |
| 1062 | |
| 1063 | bool isFeasible = false; |
| 1064 | StateTy ZeroSt = Assume(St, RightV, false, isFeasible); |
| 1065 | |
| 1066 | if (isFeasible) { |
| 1067 | NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2); |
| 1068 | |
| 1069 | if (DivZeroNode) { |
| 1070 | DivZeroNode->markAsSink(); |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1071 | BadDivides.insert(DivZeroNode); |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | // Second, "assume" that the denominator cannot be 0. |
| 1076 | |
| 1077 | isFeasible = false; |
| 1078 | St = Assume(St, RightV, true, isFeasible); |
| 1079 | |
| 1080 | if (!isFeasible) |
| 1081 | continue; |
| 1082 | |
| 1083 | // Fall-through. The logic below processes the divide. |
| 1084 | } |
| 1085 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1086 | RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1087 | St = SetRVal(SetRVal(St, B, Result), LeftLV, Result); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1088 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1089 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1090 | |
| 1091 | Nodify(Dst, B, N2, St); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1092 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1093 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1094 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1095 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1096 | void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1097 | NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred); |
| 1098 | N->markAsSink(); |
| 1099 | UninitStores.insert(N); |
| 1100 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1101 | |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1102 | void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1103 | |
| 1104 | // FIXME: add metadata to the CFG so that we can disable |
| 1105 | // this check when we KNOW that there is no block-level subexpression. |
| 1106 | // The motivation is that this check requires a hashtable lookup. |
| 1107 | |
| 1108 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 1109 | Dst.Add(Pred); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1114 | |
| 1115 | default: |
| 1116 | // Cases we intentionally have "default" handle: |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1117 | // AddrLabelExpr |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1118 | |
| 1119 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1120 | break; |
| 1121 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1122 | case Stmt::BinaryOperatorClass: { |
| 1123 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1124 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1125 | if (B->isLogicalOp()) { |
| 1126 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1127 | break; |
| 1128 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1129 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1130 | StateTy St = Pred->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1131 | Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1132 | break; |
| 1133 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1134 | |
| 1135 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1136 | break; |
| 1137 | } |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1138 | |
| 1139 | case Stmt::CallExprClass: { |
| 1140 | CallExpr* C = cast<CallExpr>(S); |
| 1141 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
| 1142 | break; |
| 1143 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1144 | |
| 1145 | case Stmt::CastExprClass: { |
| 1146 | CastExpr* C = cast<CastExpr>(S); |
| 1147 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1148 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1149 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1150 | |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1151 | // While explicitly creating a node+state for visiting a CharacterLiteral |
| 1152 | // seems wasteful, it also solves a bunch of problems when handling |
| 1153 | // the ?, &&, and ||. |
| 1154 | |
| 1155 | case Stmt::CharacterLiteralClass: { |
| 1156 | CharacterLiteral* C = cast<CharacterLiteral>(S); |
| 1157 | StateTy St = Pred->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1158 | NonLVal X = NonLVal::MakeVal(ValMgr, C->getValue(), C->getType(), |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1159 | C->getLoc()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1160 | Nodify(Dst, C, Pred, SetRVal(St, C, X)); |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1161 | break; |
| 1162 | } |
| 1163 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1164 | // FIXME: ChooseExpr is really a constant. We need to fix |
| 1165 | // the CFG do not model them as explicit control-flow. |
| 1166 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1167 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 1168 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1169 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1170 | break; |
| 1171 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1172 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1173 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1174 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1175 | break; |
| 1176 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1177 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 1178 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 1179 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | case Stmt::DeclRefExprClass: |
| 1184 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 1185 | break; |
| 1186 | |
| 1187 | case Stmt::DeclStmtClass: |
| 1188 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1189 | break; |
| 1190 | |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1191 | // While explicitly creating a node+state for visiting an IntegerLiteral |
| 1192 | // seems wasteful, it also solves a bunch of problems when handling |
| 1193 | // the ?, &&, and ||. |
| 1194 | |
| 1195 | case Stmt::IntegerLiteralClass: { |
| 1196 | StateTy St = Pred->getState(); |
| 1197 | IntegerLiteral* I = cast<IntegerLiteral>(S); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1198 | NonLVal X = NonLVal::MakeVal(ValMgr, I); |
| 1199 | Nodify(Dst, I, Pred, SetRVal(St, I, X)); |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1200 | break; |
| 1201 | } |
| 1202 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1203 | case Stmt::ImplicitCastExprClass: { |
| 1204 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1205 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1206 | break; |
| 1207 | } |
| 1208 | |
| 1209 | case Stmt::ParenExprClass: |
| 1210 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1211 | break; |
| 1212 | |
| 1213 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 1214 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 1215 | break; |
| 1216 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1217 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1218 | StmtExpr* SE = cast<StmtExpr>(S); |
| 1219 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1220 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1221 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1222 | Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1226 | // FIXME: We may wish to always bind state to ReturnStmts so |
| 1227 | // that users can quickly query what was the state at the |
| 1228 | // exit points of a function. |
| 1229 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1230 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1231 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1232 | Visit(R, Pred, Dst); |
| 1233 | else |
| 1234 | Dst.Add(Pred); |
| 1235 | |
| 1236 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1237 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1238 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1239 | case Stmt::UnaryOperatorClass: { |
| 1240 | UnaryOperator* U = cast<UnaryOperator>(S); |
| 1241 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1242 | switch (U->getOpcode()) { |
| 1243 | case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break; |
| 1244 | case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break; |
| 1245 | case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break; |
| 1246 | default: VisitUnaryOperator(U, Pred, Dst); break; |
| 1247 | } |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1248 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1249 | break; |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1250 | } |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1251 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1254 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1255 | // "Assume" logic. |
| 1256 | //===----------------------------------------------------------------------===// |
| 1257 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1258 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond, |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1259 | bool Assumption, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1260 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1261 | switch (Cond.getSubKind()) { |
| 1262 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1263 | assert (false && "'Assume' not implemented for this LVal."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1264 | return St; |
| 1265 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1266 | case lval::SymbolValKind: |
| 1267 | if (Assumption) |
| 1268 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1269 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1270 | else |
| 1271 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1272 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1273 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1274 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1275 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 1276 | case lval::FuncValKind: |
| 1277 | case lval::GotoLabelKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1278 | isFeasible = Assumption; |
| 1279 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1280 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1281 | case lval::ConcreteIntKind: { |
| 1282 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1283 | isFeasible = b ? Assumption : !Assumption; |
| 1284 | return St; |
| 1285 | } |
| 1286 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1289 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1290 | bool Assumption, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1291 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1292 | switch (Cond.getSubKind()) { |
| 1293 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1294 | assert (false && "'Assume' not implemented for this NonLVal."); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1295 | return St; |
| 1296 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1297 | |
| 1298 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1299 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1300 | SymbolID sym = SV.getSymbol(); |
| 1301 | |
| 1302 | if (Assumption) |
| 1303 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1304 | isFeasible); |
| 1305 | else |
| 1306 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1307 | isFeasible); |
| 1308 | } |
| 1309 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1310 | case nonlval::SymIntConstraintValKind: |
| 1311 | return |
| 1312 | AssumeSymInt(St, Assumption, |
| 1313 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1314 | isFeasible); |
| 1315 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1316 | case nonlval::ConcreteIntKind: { |
| 1317 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1318 | isFeasible = b ? Assumption : !Assumption; |
| 1319 | return St; |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1324 | GRExprEngine::StateTy |
| 1325 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1326 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1327 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1328 | // First, determine if sym == X, where X != V. |
| 1329 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1330 | isFeasible = *X != V; |
| 1331 | return St; |
| 1332 | } |
| 1333 | |
| 1334 | // Second, determine if sym != V. |
| 1335 | if (St.isNotEqual(sym, V)) { |
| 1336 | isFeasible = true; |
| 1337 | return St; |
| 1338 | } |
| 1339 | |
| 1340 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1341 | // Make that assumption. |
| 1342 | |
| 1343 | isFeasible = true; |
| 1344 | return StateMgr.AddNE(St, sym, V); |
| 1345 | } |
| 1346 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1347 | GRExprEngine::StateTy |
| 1348 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1349 | const llvm::APSInt& V, bool& isFeasible) { |
| 1350 | |
| 1351 | // First, determine if sym == X, where X != V. |
| 1352 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1353 | isFeasible = *X == V; |
| 1354 | return St; |
| 1355 | } |
| 1356 | |
| 1357 | // Second, determine if sym != V. |
| 1358 | if (St.isNotEqual(sym, V)) { |
| 1359 | isFeasible = false; |
| 1360 | return St; |
| 1361 | } |
| 1362 | |
| 1363 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1364 | // Make that assumption. |
| 1365 | |
| 1366 | isFeasible = true; |
| 1367 | return StateMgr.AddEQ(St, sym, V); |
| 1368 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1369 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1370 | GRExprEngine::StateTy |
| 1371 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1372 | const SymIntConstraint& C, bool& isFeasible) { |
| 1373 | |
| 1374 | switch (C.getOpcode()) { |
| 1375 | default: |
| 1376 | // No logic yet for other operators. |
| 1377 | return St; |
| 1378 | |
| 1379 | case BinaryOperator::EQ: |
| 1380 | if (Assumption) |
| 1381 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1382 | else |
| 1383 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1384 | |
| 1385 | case BinaryOperator::NE: |
| 1386 | if (Assumption) |
| 1387 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1388 | else |
| 1389 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1390 | } |
| 1391 | } |
| 1392 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1393 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1394 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1395 | //===----------------------------------------------------------------------===// |
| 1396 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1397 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1398 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1399 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1400 | namespace llvm { |
| 1401 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1402 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1403 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1404 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1405 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1406 | |
| 1407 | Out << "Variables:\\l"; |
| 1408 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1409 | bool isFirst = true; |
| 1410 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1411 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1412 | E=St.vb_end(); I!=E;++I) { |
| 1413 | |
| 1414 | if (isFirst) |
| 1415 | isFirst = false; |
| 1416 | else |
| 1417 | Out << "\\l"; |
| 1418 | |
| 1419 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1420 | I.getData().print(Out); |
| 1421 | } |
| 1422 | |
| 1423 | } |
| 1424 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1425 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1426 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1427 | |
| 1428 | bool isFirst = true; |
| 1429 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1430 | 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] | 1431 | I != E;++I) { |
| 1432 | |
| 1433 | if (isFirst) { |
| 1434 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1435 | isFirst = false; |
| 1436 | } |
| 1437 | else |
| 1438 | Out << "\\l"; |
| 1439 | |
| 1440 | Out << " (" << (void*) I.getKey() << ") "; |
| 1441 | I.getKey()->printPretty(Out); |
| 1442 | Out << " : "; |
| 1443 | I.getData().print(Out); |
| 1444 | } |
| 1445 | } |
| 1446 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1447 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1448 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1449 | bool isFirst = true; |
| 1450 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1451 | 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] | 1452 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1453 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1454 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1455 | isFirst = false; |
| 1456 | } |
| 1457 | else |
| 1458 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1459 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1460 | Out << " (" << (void*) I.getKey() << ") "; |
| 1461 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1462 | Out << " : "; |
| 1463 | I.getData().print(Out); |
| 1464 | } |
| 1465 | } |
| 1466 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1467 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1468 | ValueState::ConstEqTy CE = St.getImpl()->ConstEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (CE.isEmpty()) |
| 1471 | return; |
| 1472 | |
| 1473 | Out << "\\l\\|'==' constraints:"; |
| 1474 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1475 | for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1476 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1477 | } |
| 1478 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1479 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1480 | ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1481 | |
| 1482 | if (NE.isEmpty()) |
| 1483 | return; |
| 1484 | |
| 1485 | Out << "\\l\\|'!=' constraints:"; |
| 1486 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1487 | for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1488 | I != EI; ++I){ |
| 1489 | |
| 1490 | Out << "\\l $" << I.getKey() << " : "; |
| 1491 | bool isFirst = true; |
| 1492 | |
| 1493 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1494 | EJ=I.getData().end(); |
| 1495 | for ( ; J != EJ; ++J) { |
| 1496 | if (isFirst) isFirst = false; |
| 1497 | else Out << ", "; |
| 1498 | |
| 1499 | Out << (*J)->toString(); |
| 1500 | } |
| 1501 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1505 | |
| 1506 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1507 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1508 | GraphPrintCheckerState->isUninitDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1509 | GraphPrintCheckerState->isUninitStore(N) || |
| 1510 | GraphPrintCheckerState->isUninitControlFlow(N)) |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1511 | return "color=\"red\",style=\"filled\""; |
| 1512 | |
| 1513 | return ""; |
| 1514 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1515 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1516 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1517 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1520 | ProgramPoint Loc = N->getLocation(); |
| 1521 | |
| 1522 | switch (Loc.getKind()) { |
| 1523 | case ProgramPoint::BlockEntranceKind: |
| 1524 | Out << "Block Entrance: B" |
| 1525 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1526 | break; |
| 1527 | |
| 1528 | case ProgramPoint::BlockExitKind: |
| 1529 | assert (false); |
| 1530 | break; |
| 1531 | |
| 1532 | case ProgramPoint::PostStmtKind: { |
| 1533 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1534 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1535 | << (void*) L.getStmt() << ' '; |
| 1536 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1537 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1540 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1541 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1542 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1543 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1544 | } |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1545 | else if (GraphPrintCheckerState->isUninitDeref(N)) { |
| 1546 | Out << "\\|Dereference of uninitialied value.\\l"; |
| 1547 | } |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1548 | else if (GraphPrintCheckerState->isUninitStore(N)) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1549 | Out << "\\|Store to Uninitialized LVal."; |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1550 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1551 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1552 | break; |
| 1553 | } |
| 1554 | |
| 1555 | default: { |
| 1556 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1557 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1558 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1559 | |
| 1560 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1561 | Out << "\\|Terminator: "; |
| 1562 | E.getSrc()->printTerminator(Out); |
| 1563 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1564 | if (isa<SwitchStmt>(T)) { |
| 1565 | Stmt* Label = E.getDst()->getLabel(); |
| 1566 | |
| 1567 | if (Label) { |
| 1568 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1569 | Out << "\\lcase "; |
| 1570 | C->getLHS()->printPretty(Out); |
| 1571 | |
| 1572 | if (Stmt* RHS = C->getRHS()) { |
| 1573 | Out << " .. "; |
| 1574 | RHS->printPretty(Out); |
| 1575 | } |
| 1576 | |
| 1577 | Out << ":"; |
| 1578 | } |
| 1579 | else { |
| 1580 | assert (isa<DefaultStmt>(Label)); |
| 1581 | Out << "\\ldefault:"; |
| 1582 | } |
| 1583 | } |
| 1584 | else |
| 1585 | Out << "\\l(implicit) default:"; |
| 1586 | } |
| 1587 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1588 | // FIXME |
| 1589 | } |
| 1590 | else { |
| 1591 | Out << "\\lCondition: "; |
| 1592 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1593 | Out << "true"; |
| 1594 | else |
| 1595 | Out << "false"; |
| 1596 | } |
| 1597 | |
| 1598 | Out << "\\l"; |
| 1599 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1600 | |
| 1601 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1602 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1603 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1607 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1608 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1609 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1610 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1611 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1612 | return Out.str(); |
| 1613 | } |
| 1614 | }; |
| 1615 | } // end llvm namespace |
| 1616 | #endif |
| 1617 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1618 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1619 | #ifndef NDEBUG |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1620 | GraphPrintCheckerState = this; |
| 1621 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1622 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1623 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1624 | } |