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