Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 1 | //=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-= |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 4af8431 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 10 | // This file defines a meta-engine for path-sensitive dataflow analysis that |
| 11 | // is built on GREngine, but provides the boilerplate to execute transfer |
| 12 | // functions and build the ExplodedGraph at the expression level. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 18 | |
| 19 | #include "llvm/Support/Streams.h" |
Ted Kremenek | b387a3f | 2008-02-14 22:16:04 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | using llvm::dyn_cast; |
| 23 | using llvm::cast; |
| 24 | using llvm::APSInt; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 26 | GRExprEngine::StateTy |
| 27 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 29 | if (!StateCleaned) { |
| 30 | St = RemoveDeadBindings(CurrentStmt, St); |
| 31 | StateCleaned = true; |
| 32 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 34 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 36 | if (S == CurrentStmt) { |
| 37 | isBlkExpr = getCFG().isBlkExpr(S); |
| 38 | |
| 39 | if (!isBlkExpr) |
| 40 | return St; |
| 41 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 43 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 44 | } |
| 45 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 46 | const GRExprEngine::StateTy::BufferTy& |
| 47 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 48 | StateTy::BufferTy& RetBuf) { |
| 49 | |
| 50 | assert (RetBuf.empty()); |
| 51 | |
| 52 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 53 | RetBuf.push_back(SetValue(St, S, *I)); |
| 54 | |
| 55 | return RetBuf; |
| 56 | } |
| 57 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 58 | GRExprEngine::StateTy |
| 59 | GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 61 | if (LV.isUnknown()) |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 62 | return St; |
| 63 | |
| 64 | if (!StateCleaned) { |
| 65 | St = RemoveDeadBindings(CurrentStmt, St); |
| 66 | StateCleaned = true; |
| 67 | } |
| 68 | |
| 69 | return StateMgr.SetValue(St, LV, V); |
| 70 | } |
| 71 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 72 | void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 73 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 75 | // Remove old bindings for subexpressions. |
| 76 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 78 | RValue V = GetValue(PrevState, Condition); |
| 79 | |
| 80 | switch (V.getBaseKind()) { |
| 81 | default: |
| 82 | break; |
| 83 | |
Ted Kremenek | 53c641a | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 84 | case RValue::UnknownKind: |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 85 | builder.generateNode(PrevState, true); |
| 86 | builder.generateNode(PrevState, false); |
| 87 | return; |
| 88 | |
| 89 | case RValue::UninitializedKind: { |
| 90 | NodeTy* N = builder.generateNode(PrevState, true); |
| 91 | |
| 92 | if (N) { |
| 93 | N->markAsSink(); |
| 94 | UninitBranches.insert(N); |
| 95 | } |
| 96 | |
| 97 | builder.markInfeasible(false); |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 102 | // Get the current block counter. |
| 103 | GRBlockCounter BC = builder.getBlockCounter(); |
| 104 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 105 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 106 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 108 | if (isa<nonlval::ConcreteInt>(V) || |
| 109 | BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) { |
| 110 | |
| 111 | // Process the true branch. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 113 | bool isFeasible = true; |
| 114 | |
| 115 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 116 | |
| 117 | if (isFeasible) |
| 118 | builder.generateNode(St, true); |
| 119 | else |
| 120 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 121 | } |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 122 | else |
| 123 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 125 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 126 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 128 | if (isa<nonlval::ConcreteInt>(V) || |
| 129 | BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) { |
| 130 | |
| 131 | // Process the false branch. |
| 132 | |
| 133 | bool isFeasible = false; |
| 134 | |
| 135 | StateTy St = Assume(PrevState, V, false, isFeasible); |
| 136 | |
| 137 | if (isFeasible) |
| 138 | builder.generateNode(St, false); |
| 139 | else |
| 140 | builder.markInfeasible(false); |
| 141 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 142 | else |
| 143 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 146 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 147 | /// nodes by processing the 'effects' of a computed goto jump. |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 148 | void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 149 | |
| 150 | StateTy St = builder.getState(); |
| 151 | LValue V = cast<LValue>(GetValue(St, builder.getTarget())); |
| 152 | |
| 153 | // Three possibilities: |
| 154 | // |
| 155 | // (1) We know the computed label. |
| 156 | // (2) The label is NULL (or some other constant), or Uninitialized. |
| 157 | // (3) We have no clue about the label. Dispatch to all targets. |
| 158 | // |
| 159 | |
| 160 | typedef IndirectGotoNodeBuilder::iterator iterator; |
| 161 | |
| 162 | if (isa<lval::GotoLabel>(V)) { |
| 163 | LabelStmt* L = cast<lval::GotoLabel>(V).getLabel(); |
| 164 | |
| 165 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 166 | if (I.getLabel() == L) { |
| 167 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | assert (false && "No block with label."); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) { |
| 177 | // Dispatch to the first target and mark it as a sink. |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 178 | NodeTy* N = builder.generateNode(builder.begin(), St, true); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 179 | UninitBranches.insert(N); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // This is really a catch-all. We don't support symbolics yet. |
| 184 | |
| 185 | assert (isa<UnknownVal>(V)); |
| 186 | |
| 187 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 188 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 191 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 192 | /// nodes by processing the 'effects' of a switch statement. |
| 193 | void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) { |
| 194 | |
| 195 | typedef SwitchNodeBuilder::iterator iterator; |
| 196 | |
| 197 | StateTy St = builder.getState(); |
| 198 | NonLValue CondV = cast<NonLValue>(GetValue(St, builder.getCondition())); |
| 199 | |
| 200 | if (isa<UninitializedVal>(CondV)) { |
| 201 | NodeTy* N = builder.generateDefaultCaseNode(St, true); |
| 202 | UninitBranches.insert(N); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | StateTy DefaultSt = St; |
| 207 | |
| 208 | // While most of this can be assumed (such as the signedness), having it |
| 209 | // just computed makes sure everything makes the same assumptions end-to-end. |
| 210 | unsigned bits = getContext().getTypeSize(getContext().IntTy,SourceLocation()); |
| 211 | APSInt V1(bits, false); |
| 212 | APSInt V2 = V1; |
| 213 | |
| 214 | for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) { |
| 215 | |
| 216 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
| 217 | |
| 218 | // Evaluate the case. |
| 219 | if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) { |
| 220 | assert (false && "Case condition must evaluate to an integer constant."); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | // Get the RHS of the case, if it exists. |
| 225 | |
| 226 | if (Expr* E = Case->getRHS()) { |
| 227 | if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) { |
| 228 | assert (false && |
| 229 | "Case condition (RHS) must evaluate to an integer constant."); |
| 230 | return ; |
| 231 | } |
| 232 | |
| 233 | assert (V1 <= V2); |
| 234 | } |
| 235 | else V2 = V1; |
| 236 | |
| 237 | // FIXME: Eventually we should replace the logic below with a range |
| 238 | // comparison, rather than concretize the values within the range. |
| 239 | // This should be easy once we have "ranges" for NonLValues. |
| 240 | |
| 241 | do { |
| 242 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 243 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 244 | NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 245 | |
| 246 | // Now "assume" that the case matches. |
| 247 | bool isFeasible; |
| 248 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 249 | StateTy StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 250 | |
| 251 | if (isFeasible) { |
| 252 | builder.generateCaseStmtNode(I, StNew); |
| 253 | |
| 254 | // If CondV evaluates to a constant, then we know that this |
| 255 | // is the *only* case that we can take, so stop evaluating the |
| 256 | // others. |
| 257 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // Now "assume" that the case doesn't match. Add this state |
| 262 | // to the default state (if it is feasible). |
| 263 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 264 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 265 | |
| 266 | if (isFeasible) |
| 267 | DefaultSt = StNew; |
| 268 | |
| 269 | // Concretize the next value in the range. |
| 270 | ++V1; |
| 271 | |
| 272 | } while (V1 < V2); |
| 273 | } |
| 274 | |
| 275 | // If we reach here, than we know that the default branch is |
| 276 | // possible. |
| 277 | builder.generateDefaultCaseNode(DefaultSt); |
| 278 | } |
| 279 | |
| 280 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 281 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 282 | NodeSet& Dst) { |
| 283 | |
| 284 | bool hasR2; |
| 285 | StateTy PrevState = Pred->getState(); |
| 286 | |
| 287 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 288 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 289 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 290 | if (isa<UnknownVal>(R1) && |
| 291 | (isa<UnknownVal>(R2) || |
| 292 | isa<UninitializedVal>(R2))) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 293 | |
| 294 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 295 | return; |
| 296 | } |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 297 | else if (isa<UninitializedVal>(R1)) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 298 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 303 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 304 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 305 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 306 | if (hasR2 == false) { |
| 307 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 308 | return; |
| 309 | } |
| 310 | } |
| 311 | else { |
| 312 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 313 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 314 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 315 | if (hasR2 == false) { |
| 316 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 317 | return; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 322 | // R2 == false. |
| 323 | |
| 324 | bool isFeasible; |
| 325 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 326 | |
| 327 | if (isFeasible) |
| 328 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 329 | |
| 330 | St = Assume(PrevState, R2, false, isFeasible); |
| 331 | |
| 332 | if (isFeasible) |
| 333 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 338 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 339 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 340 | |
| 341 | StmtEntryNode = builder.getLastNode(); |
| 342 | CurrentStmt = S; |
| 343 | NodeSet Dst; |
| 344 | StateCleaned = false; |
| 345 | |
| 346 | Visit(S, StmtEntryNode, Dst); |
| 347 | |
| 348 | // If no nodes were generated, generate a new node that has all the |
| 349 | // dead mappings removed. |
| 350 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 351 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 352 | builder.generateNode(S, St, StmtEntryNode); |
| 353 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 355 | CurrentStmt = NULL; |
| 356 | StmtEntryNode = NULL; |
| 357 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 360 | GRExprEngine::NodeTy* |
| 361 | GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 362 | |
| 363 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 364 | if (St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 365 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 367 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 368 | Dst.Add(N); |
| 369 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 370 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 372 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 373 | const StateTy::BufferTy& SB) { |
| 374 | |
| 375 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 376 | Nodify(Dst, S, Pred, *I); |
| 377 | } |
| 378 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 379 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 380 | if (D != CurrentStmt) { |
| 381 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | // If we are here, we are loading the value of the decl and binding |
| 386 | // it to the block-level expression. |
| 387 | |
| 388 | StateTy St = Pred->getState(); |
| 389 | |
| 390 | Nodify(Dst, D, Pred, |
| 391 | SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl())))); |
| 392 | } |
| 393 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 394 | void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 395 | |
| 396 | QualType T = CastE->getType(); |
| 397 | |
| 398 | // Check for redundant casts. |
| 399 | if (E->getType() == T) { |
| 400 | Dst.Add(Pred); |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | NodeSet S1; |
| 405 | Visit(E, Pred, S1); |
| 406 | |
| 407 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 408 | NodeTy* N = *I1; |
| 409 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 410 | const RValue& V = GetValue(St, E); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 411 | Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 412 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 415 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
| 416 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 417 | |
| 418 | StateTy St = Pred->getState(); |
| 419 | |
| 420 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 421 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 422 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 423 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 424 | E ? GetValue(St, E) : UninitializedVal()); |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 425 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 426 | |
| 427 | Nodify(Dst, DS, Pred, St); |
| 428 | |
| 429 | if (Dst.empty()) |
| 430 | Dst.Add(Pred); |
| 431 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 432 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 434 | void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 435 | NodeTy* Pred, NodeSet& Dst) { |
| 436 | |
| 437 | StateTy St = Pred->getState(); |
| 438 | |
| 439 | RValue R = GetValue(St, LHS); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 440 | if (isa<UnknownVal>(R)) R = GetValue(St, RHS); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 441 | |
| 442 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 443 | } |
| 444 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 445 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 446 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 447 | NodeTy* Pred, |
| 448 | NodeSet& Dst) { |
| 449 | |
| 450 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 451 | |
| 452 | QualType T = S->getArgumentType(); |
| 453 | |
| 454 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame^] | 455 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 456 | return; |
| 457 | |
| 458 | SourceLocation L = S->getExprLoc(); |
| 459 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 460 | |
| 461 | Nodify(Dst, S, Pred, |
| 462 | SetValue(Pred->getState(), S, |
| 463 | NonLValue::GetValue(ValMgr, size, getContext().IntTy, L))); |
| 464 | |
| 465 | } |
| 466 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 467 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, |
| 468 | GRExprEngine::NodeTy* Pred, |
| 469 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 470 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 471 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 472 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 473 | |
| 474 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 475 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 476 | // probably just handle AddrOf and Deref in their own methods to make |
| 477 | // this cleaner. |
| 478 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 479 | isa<DeclRefExpr>(U->getSubExpr())) |
| 480 | S1.Add(Pred); |
| 481 | else |
| 482 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 483 | |
| 484 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 485 | NodeTy* N1 = *I1; |
| 486 | StateTy St = N1->getState(); |
| 487 | |
| 488 | switch (U->getOpcode()) { |
| 489 | case UnaryOperator::PostInc: { |
| 490 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 491 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 493 | NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 494 | R1, GetRValueConstant(1U, U)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 495 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 496 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | case UnaryOperator::PostDec: { |
| 501 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 502 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 503 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 504 | NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 505 | R1, GetRValueConstant(1U, U)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 506 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 507 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | case UnaryOperator::PreInc: { |
| 512 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 513 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 515 | NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 516 | R1, GetRValueConstant(1U, U)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 517 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 518 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | case UnaryOperator::PreDec: { |
| 523 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 524 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 526 | NonLValue Result = EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 527 | R1, GetRValueConstant(1U, U)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 528 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 529 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 530 | break; |
| 531 | } |
| 532 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 533 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 534 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 535 | Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 539 | case UnaryOperator::Not: { |
| 540 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 541 | Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 545 | case UnaryOperator::LNot: { |
| 546 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 547 | // |
| 548 | // Note: technically we do "E == 0", but this is the same in the |
| 549 | // transfer functions as "0 == E". |
| 550 | |
| 551 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 552 | |
| 553 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 554 | const LValue& L1 = cast<LValue>(V1); |
| 555 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 556 | Nodify(Dst, U, N1, |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 557 | SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 558 | L1, V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 559 | } |
| 560 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 561 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 562 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 563 | Nodify(Dst, U, N1, |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 564 | SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 565 | R1, V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | break; |
| 569 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 570 | |
| 571 | case UnaryOperator::SizeOf: { |
| 572 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 573 | |
| 574 | QualType T = U->getSubExpr()->getType(); |
| 575 | |
| 576 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame^] | 577 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 578 | return; |
| 579 | |
| 580 | SourceLocation L = U->getExprLoc(); |
| 581 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 582 | |
| 583 | Nodify(Dst, U, N1, |
| 584 | SetValue(St, U, NonLValue::GetValue(ValMgr, size, |
| 585 | getContext().IntTy, L))); |
| 586 | |
| 587 | break; |
| 588 | } |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 589 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 590 | case UnaryOperator::AddrOf: { |
| 591 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 592 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | case UnaryOperator::Deref: { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 597 | // FIXME: Stop when dereferencing an uninitialized value. |
| 598 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 599 | |
| 600 | const RValue& V = GetValue(St, U->getSubExpr()); |
| 601 | const LValue& L1 = cast<LValue>(V); |
| 602 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 603 | // After a dereference, one of two possible situations arise: |
| 604 | // (1) A crash, because the pointer was NULL. |
| 605 | // (2) The pointer is not NULL, and the dereference works. |
| 606 | // |
| 607 | // We add these assumptions. |
| 608 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 609 | bool isFeasibleNotNull; |
| 610 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 611 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 612 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 613 | |
| 614 | if (isFeasibleNotNull) { |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 615 | QualType T = U->getType(); |
| 616 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 617 | GetValue(StNotNull, L1, &T))); |
| 618 | } |
| 619 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 620 | bool isFeasibleNull; |
| 621 | |
| 622 | // "Assume" that the pointer is NULL. |
| 623 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 624 | |
| 625 | if (isFeasibleNull) { |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 626 | // We don't use "Nodify" here because the node will be a sink |
| 627 | // and we have no intention of processing it later. |
| 628 | NodeTy* NullNode = Builder->generateNode(U, StNull, N1); |
| 629 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 630 | if (NullNode) { |
| 631 | NullNode->markAsSink(); |
| 632 | |
| 633 | if (isFeasibleNotNull) |
| 634 | ImplicitNullDeref.insert(NullNode); |
| 635 | else |
| 636 | ExplicitNullDeref.insert(NullNode); |
| 637 | } |
| 638 | } |
| 639 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 643 | default: ; |
| 644 | assert (false && "Not implemented."); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 649 | void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred, |
| 650 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 651 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 652 | if (isa<DeclRefExpr>(E)) { |
| 653 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 654 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 655 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 656 | |
| 657 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 658 | if (U->getOpcode() == UnaryOperator::Deref) { |
| 659 | Visit(U->getSubExpr(), Pred, Dst); |
| 660 | return; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | Visit(E, Pred, Dst); |
| 665 | } |
| 666 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 667 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 668 | GRExprEngine::NodeTy* Pred, |
| 669 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 670 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 671 | |
| 672 | if (B->isAssignmentOp()) |
| 673 | VisitAssignmentLHS(B->getLHS(), Pred, S1); |
| 674 | else |
| 675 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 676 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 677 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 678 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 679 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 680 | // When getting the value for the LHS, check if we are in an assignment. |
| 681 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 682 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 683 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 684 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 685 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 686 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 687 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 688 | NodeSet S2; |
| 689 | Visit(B->getRHS(), N1, S2); |
| 690 | |
| 691 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 692 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 693 | NodeTy* N2 = *I2; |
| 694 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 695 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 696 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 697 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 698 | |
| 699 | if (Op <= BinaryOperator::Or) { |
| 700 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 701 | if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 702 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 703 | continue; |
| 704 | } |
| 705 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 706 | if (isa<LValue>(V1)) { |
| 707 | // FIXME: Add support for RHS being a non-lvalue. |
| 708 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 709 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 710 | if (isa<LValue>(V2)) { |
| 711 | const LValue& L2 = cast<LValue>(V2); |
| 712 | Nodify(Dst, B, N2, SetValue(St, B, |
| 713 | EvalBinaryOp(ValMgr, Op, L1, L2))); |
| 714 | } |
| 715 | else { |
| 716 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 717 | Nodify(Dst, B, N2, SetValue(St, B, |
| 718 | EvalBinaryOp(ValMgr, Op, L1, R2))); |
| 719 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 720 | } |
| 721 | else { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 722 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 723 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 724 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 725 | Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, R1, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 726 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 727 | |
| 728 | continue; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 729 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | switch (Op) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 733 | case BinaryOperator::Assign: { |
| 734 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 3434b08 | 2008-02-06 04:41:14 +0000 | [diff] [blame] | 735 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 736 | break; |
| 737 | } |
| 738 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 739 | default: { // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 740 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 741 | assert (B->isCompoundAssignmentOp()); |
| 742 | |
| 743 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 744 | RValue Result = cast<NonLValue>(UnknownVal()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 745 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 746 | if (Op >= BinaryOperator::AndAssign) |
| 747 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 748 | else |
| 749 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 750 | |
| 751 | if (isa<LValue>(V2)) { |
| 752 | // FIXME: Add support for Non-LValues on RHS. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 753 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 754 | Result = EvalBinaryOp(ValMgr, Op, L1, L2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 755 | } |
| 756 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 757 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 758 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 759 | Result = EvalBinaryOp(ValMgr, Op, R1, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 762 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 763 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 764 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 765 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 766 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 767 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 768 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 769 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 770 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 771 | void GRExprEngine::Visit(Stmt* S, GRExprEngine::NodeTy* Pred, |
| 772 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 773 | |
| 774 | // FIXME: add metadata to the CFG so that we can disable |
| 775 | // this check when we KNOW that there is no block-level subexpression. |
| 776 | // The motivation is that this check requires a hashtable lookup. |
| 777 | |
| 778 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 779 | Dst.Add(Pred); |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 784 | |
| 785 | default: |
| 786 | // Cases we intentionally have "default" handle: |
| 787 | // AddrLabelExpr, CharacterLiteral, IntegerLiteral |
| 788 | |
| 789 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 790 | break; |
| 791 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 792 | case Stmt::BinaryOperatorClass: { |
| 793 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 794 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 795 | if (B->isLogicalOp()) { |
| 796 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 797 | break; |
| 798 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 799 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 800 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 801 | Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 802 | break; |
| 803 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 804 | |
| 805 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 806 | break; |
| 807 | } |
| 808 | |
| 809 | case Stmt::CastExprClass: { |
| 810 | CastExpr* C = cast<CastExpr>(S); |
| 811 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 812 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 813 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 814 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 815 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 816 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 817 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 818 | break; |
| 819 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 820 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 821 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 822 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 823 | break; |
| 824 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 825 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 826 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 827 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 828 | break; |
| 829 | } |
| 830 | |
| 831 | case Stmt::DeclRefExprClass: |
| 832 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 833 | break; |
| 834 | |
| 835 | case Stmt::DeclStmtClass: |
| 836 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 837 | break; |
| 838 | |
| 839 | case Stmt::ImplicitCastExprClass: { |
| 840 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 841 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 842 | break; |
| 843 | } |
| 844 | |
| 845 | case Stmt::ParenExprClass: |
| 846 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 847 | break; |
| 848 | |
| 849 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 850 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 851 | break; |
| 852 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 853 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 854 | StmtExpr* SE = cast<StmtExpr>(S); |
| 855 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 856 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 857 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
| 858 | Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 859 | break; |
| 860 | } |
| 861 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 862 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 863 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 864 | Visit(R, Pred, Dst); |
| 865 | else |
| 866 | Dst.Add(Pred); |
| 867 | |
| 868 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 869 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 870 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 871 | case Stmt::UnaryOperatorClass: |
| 872 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 873 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 874 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 877 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 878 | // "Assume" logic. |
| 879 | //===----------------------------------------------------------------------===// |
| 880 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 881 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 882 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 883 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 884 | |
| 885 | switch (Cond.getSubKind()) { |
| 886 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 887 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 888 | return St; |
| 889 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 890 | case lval::SymbolValKind: |
| 891 | if (Assumption) |
| 892 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 893 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 894 | else |
| 895 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 896 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 897 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 898 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 899 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 900 | isFeasible = Assumption; |
| 901 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 902 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 903 | case lval::ConcreteIntKind: { |
| 904 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 905 | isFeasible = b ? Assumption : !Assumption; |
| 906 | return St; |
| 907 | } |
| 908 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 911 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 912 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 913 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 914 | |
| 915 | switch (Cond.getSubKind()) { |
| 916 | default: |
| 917 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 918 | return St; |
| 919 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 920 | |
| 921 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 922 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 923 | SymbolID sym = SV.getSymbol(); |
| 924 | |
| 925 | if (Assumption) |
| 926 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 927 | isFeasible); |
| 928 | else |
| 929 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 930 | isFeasible); |
| 931 | } |
| 932 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 933 | case nonlval::SymIntConstraintValKind: |
| 934 | return |
| 935 | AssumeSymInt(St, Assumption, |
| 936 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 937 | isFeasible); |
| 938 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 939 | case nonlval::ConcreteIntKind: { |
| 940 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 941 | isFeasible = b ? Assumption : !Assumption; |
| 942 | return St; |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 947 | GRExprEngine::StateTy |
| 948 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 949 | const llvm::APSInt& V, bool& isFeasible) { |
| 950 | |
| 951 | // First, determine if sym == X, where X != V. |
| 952 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 953 | isFeasible = *X != V; |
| 954 | return St; |
| 955 | } |
| 956 | |
| 957 | // Second, determine if sym != V. |
| 958 | if (St.isNotEqual(sym, V)) { |
| 959 | isFeasible = true; |
| 960 | return St; |
| 961 | } |
| 962 | |
| 963 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 964 | // Make that assumption. |
| 965 | |
| 966 | isFeasible = true; |
| 967 | return StateMgr.AddNE(St, sym, V); |
| 968 | } |
| 969 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 970 | GRExprEngine::StateTy |
| 971 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 972 | const llvm::APSInt& V, bool& isFeasible) { |
| 973 | |
| 974 | // First, determine if sym == X, where X != V. |
| 975 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 976 | isFeasible = *X == V; |
| 977 | return St; |
| 978 | } |
| 979 | |
| 980 | // Second, determine if sym != V. |
| 981 | if (St.isNotEqual(sym, V)) { |
| 982 | isFeasible = false; |
| 983 | return St; |
| 984 | } |
| 985 | |
| 986 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 987 | // Make that assumption. |
| 988 | |
| 989 | isFeasible = true; |
| 990 | return StateMgr.AddEQ(St, sym, V); |
| 991 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 992 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 993 | GRExprEngine::StateTy |
| 994 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 995 | const SymIntConstraint& C, bool& isFeasible) { |
| 996 | |
| 997 | switch (C.getOpcode()) { |
| 998 | default: |
| 999 | // No logic yet for other operators. |
| 1000 | return St; |
| 1001 | |
| 1002 | case BinaryOperator::EQ: |
| 1003 | if (Assumption) |
| 1004 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1005 | else |
| 1006 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1007 | |
| 1008 | case BinaryOperator::NE: |
| 1009 | if (Assumption) |
| 1010 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1011 | else |
| 1012 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1013 | } |
| 1014 | } |
| 1015 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1016 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1017 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1018 | //===----------------------------------------------------------------------===// |
| 1019 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1020 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1021 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1022 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1023 | namespace llvm { |
| 1024 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1025 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1026 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1027 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1028 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1029 | |
| 1030 | Out << "Variables:\\l"; |
| 1031 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1032 | bool isFirst = true; |
| 1033 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1034 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1035 | E=St.vb_end(); I!=E;++I) { |
| 1036 | |
| 1037 | if (isFirst) |
| 1038 | isFirst = false; |
| 1039 | else |
| 1040 | Out << "\\l"; |
| 1041 | |
| 1042 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1043 | I.getData().print(Out); |
| 1044 | } |
| 1045 | |
| 1046 | } |
| 1047 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1048 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1049 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1050 | |
| 1051 | bool isFirst = true; |
| 1052 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1053 | 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] | 1054 | I != E;++I) { |
| 1055 | |
| 1056 | if (isFirst) { |
| 1057 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1058 | isFirst = false; |
| 1059 | } |
| 1060 | else |
| 1061 | Out << "\\l"; |
| 1062 | |
| 1063 | Out << " (" << (void*) I.getKey() << ") "; |
| 1064 | I.getKey()->printPretty(Out); |
| 1065 | Out << " : "; |
| 1066 | I.getData().print(Out); |
| 1067 | } |
| 1068 | } |
| 1069 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1070 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1071 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1072 | bool isFirst = true; |
| 1073 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1074 | 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] | 1075 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1076 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1077 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1078 | isFirst = false; |
| 1079 | } |
| 1080 | else |
| 1081 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1082 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1083 | Out << " (" << (void*) I.getKey() << ") "; |
| 1084 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1085 | Out << " : "; |
| 1086 | I.getData().print(Out); |
| 1087 | } |
| 1088 | } |
| 1089 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1090 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1091 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1092 | |
| 1093 | if (CE.isEmpty()) |
| 1094 | return; |
| 1095 | |
| 1096 | Out << "\\l\\|'==' constraints:"; |
| 1097 | |
| 1098 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1099 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1100 | } |
| 1101 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1102 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1103 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1104 | |
| 1105 | if (NE.isEmpty()) |
| 1106 | return; |
| 1107 | |
| 1108 | Out << "\\l\\|'!=' constraints:"; |
| 1109 | |
| 1110 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1111 | I != EI; ++I){ |
| 1112 | |
| 1113 | Out << "\\l $" << I.getKey() << " : "; |
| 1114 | bool isFirst = true; |
| 1115 | |
| 1116 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1117 | EJ=I.getData().end(); |
| 1118 | for ( ; J != EJ; ++J) { |
| 1119 | if (isFirst) isFirst = false; |
| 1120 | else Out << ", "; |
| 1121 | |
| 1122 | Out << (*J)->toString(); |
| 1123 | } |
| 1124 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1128 | |
| 1129 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
| 1130 | GraphPrintCheckerState->isExplicitNullDeref(N)) |
| 1131 | return "color=\"red\",style=\"filled\""; |
| 1132 | |
| 1133 | return ""; |
| 1134 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1135 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1136 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1137 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1140 | ProgramPoint Loc = N->getLocation(); |
| 1141 | |
| 1142 | switch (Loc.getKind()) { |
| 1143 | case ProgramPoint::BlockEntranceKind: |
| 1144 | Out << "Block Entrance: B" |
| 1145 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1146 | break; |
| 1147 | |
| 1148 | case ProgramPoint::BlockExitKind: |
| 1149 | assert (false); |
| 1150 | break; |
| 1151 | |
| 1152 | case ProgramPoint::PostStmtKind: { |
| 1153 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1154 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1155 | << (void*) L.getStmt() << ' '; |
| 1156 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1157 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1158 | |
| 1159 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1160 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1161 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1162 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1163 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1164 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1165 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1166 | break; |
| 1167 | } |
| 1168 | |
| 1169 | default: { |
| 1170 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1171 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1172 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1173 | |
| 1174 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1175 | Out << "\\|Terminator: "; |
| 1176 | E.getSrc()->printTerminator(Out); |
| 1177 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1178 | if (isa<SwitchStmt>(T)) { |
| 1179 | Stmt* Label = E.getDst()->getLabel(); |
| 1180 | |
| 1181 | if (Label) { |
| 1182 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1183 | Out << "\\lcase "; |
| 1184 | C->getLHS()->printPretty(Out); |
| 1185 | |
| 1186 | if (Stmt* RHS = C->getRHS()) { |
| 1187 | Out << " .. "; |
| 1188 | RHS->printPretty(Out); |
| 1189 | } |
| 1190 | |
| 1191 | Out << ":"; |
| 1192 | } |
| 1193 | else { |
| 1194 | assert (isa<DefaultStmt>(Label)); |
| 1195 | Out << "\\ldefault:"; |
| 1196 | } |
| 1197 | } |
| 1198 | else |
| 1199 | Out << "\\l(implicit) default:"; |
| 1200 | } |
| 1201 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1202 | // FIXME |
| 1203 | } |
| 1204 | else { |
| 1205 | Out << "\\lCondition: "; |
| 1206 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1207 | Out << "true"; |
| 1208 | else |
| 1209 | Out << "false"; |
| 1210 | } |
| 1211 | |
| 1212 | Out << "\\l"; |
| 1213 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1214 | |
| 1215 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1216 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1217 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1221 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1222 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1223 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1225 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1226 | return Out.str(); |
| 1227 | } |
| 1228 | }; |
| 1229 | } // end llvm namespace |
| 1230 | #endif |
| 1231 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1232 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1233 | #ifndef NDEBUG |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1234 | GraphPrintCheckerState = this; |
| 1235 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1236 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1237 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1238 | } |