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