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