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