Ted Kremenek | 50df4f4 | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 1 | //=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-= |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 2e16060 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | 68d70a8 | 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 | 50df4f4 | 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 | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 50df4f4 | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 3862eb1 | 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 | d446743 | 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 | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 26 | GRExprEngine::StateTy |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 27 | GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) { |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 29 | if (!StateCleaned) { |
| 30 | St = RemoveDeadBindings(CurrentStmt, St); |
| 31 | StateCleaned = true; |
| 32 | } |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 34 | bool isBlkExpr = false; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 36 | if (Ex == CurrentStmt) { |
| 37 | isBlkExpr = getCFG().isBlkExpr(Ex); |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 38 | |
| 39 | if (!isBlkExpr) |
| 40 | return St; |
| 41 | } |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | ad5d5c5 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 43 | return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false); |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 46 | const GRExprEngine::StateTy::BufferTy& |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 47 | GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB, |
Ted Kremenek | f20c201 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 48 | StateTy::BufferTy& RetBuf) { |
| 49 | |
| 50 | assert (RetBuf.empty()); |
| 51 | |
Ted Kremenek | 07baa25 | 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 | f20c201 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 54 | |
| 55 | return RetBuf; |
| 56 | } |
| 57 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 58 | GRExprEngine::StateTy |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 59 | GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) { |
Ted Kremenek | 7f5ebc7 | 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 | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 66 | return StateMgr.SetRVal(St, LV, RV); |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Ted Kremenek | 99ecce7 | 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 | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 123 | void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 124 | BranchNodeBuilder& builder) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 126 | // Remove old bindings for subexpressions. |
| 127 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 022b605 | 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 | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 144 | RVal V = GetRVal(PrevState, Condition); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 145 | |
| 146 | switch (V.getBaseKind()) { |
| 147 | default: |
| 148 | break; |
| 149 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 150 | case RVal::UnknownKind: |
Ted Kremenek | 5f2eb19 | 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 | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 153 | return; |
| 154 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 155 | case RVal::UninitializedKind: { |
Ted Kremenek | 9096097 | 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 | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 168 | // Get the current block counter. |
| 169 | GRBlockCounter BC = builder.getBlockCounter(); |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 170 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 171 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 172 | |
Ted Kremenek | 4b170e5 | 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 | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 4b170e5 | 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 | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 183 | builder.generateNode(MarkBranch(St, Term, true), true); |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 184 | else |
| 185 | builder.markInfeasible(true); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 186 | } |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 187 | else |
| 188 | builder.markInfeasible(true); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 189 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 190 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 191 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | 4b170e5 | 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 | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 203 | builder.generateNode(MarkBranch(St, Term, false), false); |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 204 | else |
| 205 | builder.markInfeasible(false); |
| 206 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 207 | else |
| 208 | builder.markInfeasible(false); |
Ted Kremenek | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 211 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 212 | /// nodes by processing the 'effects' of a computed goto jump. |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 213 | void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 214 | |
| 215 | StateTy St = builder.getState(); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 216 | RVal V = GetRVal(St, builder.getTarget()); |
Ted Kremenek | 677f4ef | 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 | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 231 | if (I.getLabel() == L) { |
| 232 | builder.generateNode(I, St); |
Ted Kremenek | 677f4ef | 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 | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 243 | NodeTy* N = builder.generateNode(builder.begin(), St, true); |
Ted Kremenek | 677f4ef | 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 | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 250 | assert (V.isUnknown()); |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 251 | |
| 252 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 253 | builder.generateNode(I, St); |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 254 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | aee121c | 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 | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 263 | Expr* CondE = builder.getCondition(); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 264 | RVal CondV = GetRVal(St, CondE); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 266 | if (CondV.isUninit()) { |
Ted Kremenek | aee121c | 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 | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 276 | |
| 277 | unsigned bits = getContext().getTypeSize(CondE->getType(), |
| 278 | CondE->getExprLoc()); |
| 279 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 280 | APSInt V1(bits, false); |
| 281 | APSInt V2 = V1; |
| 282 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 283 | for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) { |
Ted Kremenek | aee121c | 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 | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 308 | // This should be easy once we have "ranges" for NonLVals. |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 309 | |
| 310 | do { |
| 311 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 312 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 313 | RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 314 | |
| 315 | // Now "assume" that the case matches. |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 316 | bool isFeasible = false; |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 318 | StateTy StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | aee121c | 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 | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 333 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | aee121c | 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 | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 350 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 351 | NodeSet& Dst) { |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 99ecce7 | 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 | 5f2eb19 | 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 | 99ecce7 | 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 | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 396 | } |
| 397 | else { |
Ted Kremenek | 99ecce7 | 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 | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 404 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 405 | } |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 406 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 407 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 408 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 410 | Builder = &builder; |
Ted Kremenek | f031b87 | 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 | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 420 | |
Ted Kremenek | f031b87 | 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 | a57214f | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 425 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 426 | // For safety, NULL out these variables. |
| 427 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 428 | CurrentStmt = NULL; |
| 429 | StmtEntryNode = NULL; |
| 430 | Builder = NULL; |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Ted Kremenek | 30fa28b | 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 | f031b87 | 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 | 02b5b40 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 437 | if (St == Pred->getState()) |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 438 | return NULL; |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 439 | |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 440 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 441 | Dst.Add(N); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 443 | return N; |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 444 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 446 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | f20c201 | 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 | 6d40992 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 453 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | ef0007f | 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 | 9b32cd0 | 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 | 07baa25 | 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 | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 483 | void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 484 | CallExpr::arg_iterator AI, |
| 485 | CallExpr::arg_iterator AE, |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 486 | NodeSet& Dst) { |
| 487 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 488 | // Process the arguments. |
| 489 | |
| 490 | if (AI != AE) { |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 492 | NodeSet DstTmp; |
Ted Kremenek | cde193a | 2008-02-27 00:44:11 +0000 | [diff] [blame^] | 493 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 494 | Visit(*AI, Pred, DstTmp); |
Ted Kremenek | cde193a | 2008-02-27 00:44:11 +0000 | [diff] [blame^] | 495 | if (DstTmp.empty()) DstTmp.Add(Pred); |
| 496 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 497 | ++AI; |
| 498 | |
| 499 | for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI) |
| 500 | VisitCall(CE, *DI, AI, AE, Dst); |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 501 | |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | // If we reach here we have processed all of the arguments. Evaluate |
| 506 | // the callee expression. |
Ted Kremenek | c71901d | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 507 | NodeSet DstTmp; |
| 508 | Expr* Callee = CE->getCallee()->IgnoreParenCasts(); |
| 509 | |
| 510 | VisitLVal(Callee, Pred, DstTmp); |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 511 | |
| 512 | // Finally, evaluate the function call. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 513 | for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) { |
| 514 | |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 515 | StateTy St = (*DI)->getState(); |
Ted Kremenek | c71901d | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 516 | RVal L = GetLVal(St, Callee); |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 517 | |
| 518 | // Check for uninitialized control-flow. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 519 | |
| 520 | if (L.isUninit()) { |
| 521 | |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 522 | NodeTy* N = Builder->generateNode(CE, St, *DI); |
| 523 | N->markAsSink(); |
| 524 | UninitBranches.insert(N); |
| 525 | continue; |
| 526 | } |
| 527 | |
Ted Kremenek | b9a20e0 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 528 | if (L.isUnknown()) { |
| 529 | // Invalidate all arguments passed in by reference (LVals). |
| 530 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 531 | I != E; ++I) { |
| 532 | RVal V = GetRVal(St, *I); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 533 | |
Ted Kremenek | b9a20e0 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 534 | if (isa<LVal>(V)) |
| 535 | St = SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 536 | } |
| 537 | } |
| 538 | else |
| 539 | St = EvalCall(CE, cast<LVal>(L), (*DI)->getState()); |
| 540 | |
| 541 | Nodify(Dst, CE, *DI, St); |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 545 | void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 546 | |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 547 | NodeSet S1; |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 548 | Visit(Ex, Pred, S1); |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 549 | |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 550 | QualType T = CastE->getType(); |
| 551 | |
Ted Kremenek | 5a9b6aa | 2008-02-19 18:47:04 +0000 | [diff] [blame] | 552 | // Check for redundant casts or casting to "void" |
| 553 | if (T->isVoidType() || |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 554 | Ex->getType() == T || |
| 555 | (T->isPointerType() && Ex->getType()->isFunctionType())) { |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 556 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 557 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 558 | Dst.Add(*I1); |
| 559 | |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 563 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 564 | NodeTy* N = *I1; |
| 565 | StateTy St = N->getState(); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 566 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 567 | Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType()))); |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 568 | } |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 571 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 572 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 573 | |
| 574 | StateTy St = Pred->getState(); |
| 575 | |
| 576 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 8c9bc34 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 577 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | e025800 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 578 | |
| 579 | // FIXME: Add support for local arrays. |
| 580 | if (VD->getType()->isArrayType()) |
| 581 | continue; |
| 582 | |
Ted Kremenek | 1fc70b3 | 2008-02-26 00:20:52 +0000 | [diff] [blame] | 583 | // FIXME: static variables have an initializer, but the second |
| 584 | // time a function is called those values may not be current. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 585 | const Expr* Ex = VD->getInit(); |
| 586 | |
| 587 | St = SetRVal(St, lval::DeclVal(VD), |
| 588 | Ex ? GetRVal(St, Ex) : UninitializedVal()); |
Ted Kremenek | 8c9bc34 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 589 | } |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 590 | |
| 591 | Nodify(Dst, DS, Pred, St); |
| 592 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 593 | if (Dst.empty()) { Dst.Add(Pred); } |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 594 | } |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 595 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 597 | void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R, |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 598 | NodeTy* Pred, NodeSet& Dst) { |
| 599 | |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 600 | assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex)); |
| 601 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 602 | StateTy St = Pred->getState(); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 603 | RVal X = GetBlkExprRVal(St, Ex); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 604 | |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 605 | assert (X.isUninit()); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 606 | |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 607 | Expr* SE = (Expr*) cast<UninitializedVal>(X).getData(); |
| 608 | |
| 609 | assert (SE); |
| 610 | |
| 611 | X = GetBlkExprRVal(St, SE); |
Ted Kremenek | ad5d5c5 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 612 | |
| 613 | // Make sure that we invalidate the previous binding. |
| 614 | Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true)); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 617 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 618 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex, |
| 619 | NodeTy* Pred, |
| 620 | NodeSet& Dst) { |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 621 | |
| 622 | assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented."); |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 623 | |
| 624 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 625 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 626 | QualType T = Ex->getArgumentType(); |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 627 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | 571f519 | 2008-02-21 18:15:29 +0000 | [diff] [blame] | 629 | // FIXME: Add support for VLAs. |
Eli Friedman | 82ce9a8 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 630 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 631 | return; |
| 632 | |
Ted Kremenek | 571f519 | 2008-02-21 18:15:29 +0000 | [diff] [blame] | 633 | |
| 634 | uint64_t size = 1; // Handle sizeof(void) |
| 635 | |
| 636 | if (T != getContext().VoidTy) { |
| 637 | SourceLocation Loc = Ex->getExprLoc(); |
| 638 | size = getContext().getTypeSize(T, Loc) / 8; |
| 639 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 641 | Nodify(Dst, Ex, Pred, |
| 642 | SetRVal(Pred->getState(), Ex, |
Ted Kremenek | 571f519 | 2008-02-21 18:15:29 +0000 | [diff] [blame] | 643 | NonLVal::MakeVal(ValMgr, size, Ex->getType()))); |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 644 | |
| 645 | } |
| 646 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 647 | void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) { |
| 648 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 649 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 650 | |
| 651 | NodeSet DstTmp; |
| 652 | |
Ted Kremenek | 56a8999 | 2008-02-26 03:44:25 +0000 | [diff] [blame] | 653 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 654 | DstTmp.Add(Pred); |
| 655 | else |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 656 | Visit(Ex, Pred, DstTmp); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 657 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 658 | for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 659 | |
| 660 | NodeTy* N = *I; |
| 661 | StateTy St = N->getState(); |
| 662 | |
| 663 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 664 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 665 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 666 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 667 | // Check for dereferences of uninitialized values. |
| 668 | |
| 669 | if (V.isUninit()) { |
| 670 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 671 | NodeTy* Succ = Builder->generateNode(U, St, N); |
| 672 | |
| 673 | if (Succ) { |
| 674 | Succ->markAsSink(); |
| 675 | UninitDeref.insert(Succ); |
| 676 | } |
| 677 | |
| 678 | continue; |
| 679 | } |
| 680 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 681 | // Check for dereferences of unknown values. Treat as No-Ops. |
| 682 | |
| 683 | if (V.isUnknown()) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 684 | Dst.Add(N); |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | // After a dereference, one of two possible situations arise: |
| 689 | // (1) A crash, because the pointer was NULL. |
| 690 | // (2) The pointer is not NULL, and the dereference works. |
| 691 | // |
| 692 | // We add these assumptions. |
| 693 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 694 | LVal LV = cast<LVal>(V); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 695 | bool isFeasibleNotNull; |
| 696 | |
| 697 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 698 | |
| 699 | StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 700 | |
| 701 | if (isFeasibleNotNull) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 702 | |
| 703 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 704 | // for the contents of values. We need a better approach. |
| 705 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 706 | Nodify(Dst, U, N, SetRVal(StNotNull, U, |
| 707 | GetRVal(StNotNull, LV, U->getType()))); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | bool isFeasibleNull; |
| 711 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 712 | // Now "assume" that the pointer is NULL. |
| 713 | |
| 714 | StateTy StNull = Assume(St, LV, false, isFeasibleNull); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 715 | |
| 716 | if (isFeasibleNull) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 717 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 718 | // We don't use "Nodify" here because the node will be a sink |
| 719 | // and we have no intention of processing it later. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 720 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 721 | NodeTy* NullNode = Builder->generateNode(U, StNull, N); |
| 722 | |
| 723 | if (NullNode) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 724 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 725 | NullNode->markAsSink(); |
| 726 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 727 | if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode); |
| 728 | else ExplicitNullDeref.insert(NullNode); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 734 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred, |
| 735 | NodeSet& Dst) { |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 736 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 737 | NodeSet S1; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 738 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 739 | assert (U->getOpcode() != UnaryOperator::Deref); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 740 | assert (U->getOpcode() != UnaryOperator::SizeOf); |
| 741 | assert (U->getOpcode() != UnaryOperator::AlignOf); |
| 742 | |
| 743 | bool use_GetLVal = false; |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 744 | |
| 745 | switch (U->getOpcode()) { |
| 746 | case UnaryOperator::PostInc: |
| 747 | case UnaryOperator::PostDec: |
| 748 | case UnaryOperator::PreInc: |
| 749 | case UnaryOperator::PreDec: |
| 750 | case UnaryOperator::AddrOf: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 751 | // Evalue subexpression as an LVal. |
| 752 | use_GetLVal = true; |
| 753 | VisitLVal(U->getSubExpr(), Pred, S1); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 754 | break; |
| 755 | |
| 756 | default: |
| 757 | Visit(U->getSubExpr(), Pred, S1); |
| 758 | break; |
| 759 | } |
| 760 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 761 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 762 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 763 | NodeTy* N1 = *I1; |
| 764 | StateTy St = N1->getState(); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 765 | |
| 766 | RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) : |
| 767 | GetRVal(St, U->getSubExpr()); |
| 768 | |
| 769 | if (SubV.isUnknown()) { |
| 770 | Dst.Add(N1); |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | if (SubV.isUninit()) { |
| 775 | Nodify(Dst, U, N1, SetRVal(St, U, SubV)); |
| 776 | continue; |
| 777 | } |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 778 | |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 779 | if (U->isIncrementDecrementOp()) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 780 | |
| 781 | // Handle ++ and -- (both pre- and post-increment). |
| 782 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 783 | LVal SubLV = cast<LVal>(SubV); |
| 784 | RVal V = GetRVal(St, SubLV, U->getType()); |
| 785 | |
Ted Kremenek | b8782e1 | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 786 | if (V.isUnknown()) { |
| 787 | Dst.Add(N1); |
| 788 | continue; |
| 789 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 790 | |
| 791 | // Propagate uninitialized values. |
| 792 | if (V.isUninit()) { |
| 793 | Nodify(Dst, U, N1, SetRVal(St, U, V)); |
| 794 | continue; |
| 795 | } |
| 796 | |
Ted Kremenek | b1669d4 | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 797 | // Handle all other values. |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 798 | |
| 799 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 800 | : BinaryOperator::Sub; |
| 801 | |
Ted Kremenek | b1669d4 | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 802 | RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U)); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 803 | |
| 804 | if (U->isPostfix()) |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 805 | St = SetRVal(SetRVal(St, U, V), SubLV, Result); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 806 | else |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 807 | St = SetRVal(SetRVal(St, U, Result), SubLV, Result); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 808 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 809 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 810 | continue; |
| 811 | } |
| 812 | |
| 813 | // Handle all other unary operators. |
| 814 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 815 | switch (U->getOpcode()) { |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 816 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 817 | case UnaryOperator::Minus: |
| 818 | St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV))); |
Ted Kremenek | cacd736 | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 819 | break; |
Ted Kremenek | cacd736 | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 820 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 821 | case UnaryOperator::Not: |
| 822 | St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV))); |
Ted Kremenek | 0cd9635 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 823 | break; |
Ted Kremenek | 0cd9635 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 825 | case UnaryOperator::LNot: |
Ted Kremenek | 2cb4664 | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 826 | |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 827 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 828 | // |
| 829 | // Note: technically we do "E == 0", but this is the same in the |
| 830 | // transfer functions as "0 == E". |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 831 | |
| 832 | if (isa<LVal>(SubV)) { |
| 833 | lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth()); |
| 834 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V); |
| 835 | St = SetRVal(St, U, Result); |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 836 | } |
| 837 | else { |
Ted Kremenek | a089467 | 2008-02-22 00:42:36 +0000 | [diff] [blame] | 838 | Expr* Ex = U->getSubExpr(); |
| 839 | nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType())); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 840 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V); |
| 841 | St = SetRVal(St, U, Result); |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | break; |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 845 | |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 846 | case UnaryOperator::AddrOf: { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 847 | assert (isa<LVal>(SubV)); |
| 848 | St = SetRVal(St, U, SubV); |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 849 | break; |
| 850 | } |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 851 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 852 | default: ; |
| 853 | assert (false && "Not implemented."); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 860 | void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred, |
| 861 | NodeSet& Dst) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 862 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 863 | QualType T = U->getSubExpr()->getType(); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 864 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 865 | // FIXME: Add support for VLAs. |
| 866 | if (!T.getTypePtr()->isConstantSizeType()) |
| 867 | return; |
| 868 | |
| 869 | SourceLocation Loc = U->getExprLoc(); |
| 870 | uint64_t size = getContext().getTypeSize(T, Loc) / 8; |
| 871 | StateTy St = Pred->getState(); |
| 872 | St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc)); |
| 873 | |
| 874 | Nodify(Dst, U, Pred, St); |
| 875 | } |
| 876 | |
| 877 | void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) { |
| 878 | |
| 879 | assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex)); |
| 880 | |
| 881 | Ex = Ex->IgnoreParens(); |
| 882 | |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 883 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(Ex)) { |
| 884 | |
| 885 | if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 886 | if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) { |
| 887 | |
| 888 | StateTy StOld = Pred->getState(); |
| 889 | StateTy St = Symbolicate(StOld, VD); |
| 890 | |
| 891 | if (!(St == StOld)) { |
| 892 | Nodify(Dst, Ex, Pred, St); |
| 893 | return; |
| 894 | } |
| 895 | } |
| 896 | |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 897 | Dst.Add(Pred); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 898 | return; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 899 | } |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 900 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 901 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) { |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 902 | if (U->getOpcode() == UnaryOperator::Deref) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 903 | Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 905 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 906 | Dst.Add(Pred); |
| 907 | else |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 908 | Visit(Ex, Pred, Dst); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 909 | |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 910 | return; |
| 911 | } |
| 912 | } |
| 913 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 914 | Visit(Ex, Pred, Dst); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 917 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 918 | GRExprEngine::NodeTy* Pred, |
| 919 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 920 | NodeSet S1; |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 921 | |
| 922 | if (B->isAssignmentOp()) |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 923 | VisitLVal(B->getLHS(), Pred, S1); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 924 | else |
| 925 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 926 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 927 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 928 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 929 | NodeTy* N1 = *I1; |
Ted Kremenek | e860db8 | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 930 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 931 | // When getting the value for the LHS, check if we are in an assignment. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 932 | // In such cases, we want to (initially) treat the LHS as an LVal, |
| 933 | // so we use GetLVal instead of GetRVal so that DeclRefExpr's are |
| 934 | // evaluated to LValDecl's instead of to an NonLVal. |
| 935 | |
| 936 | RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS()) |
| 937 | : GetRVal(N1->getState(), B->getLHS()); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 938 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 939 | // Visit the RHS... |
| 940 | |
| 941 | NodeSet S2; |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 942 | Visit(B->getRHS(), N1, S2); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 943 | |
| 944 | // Process the binary operator. |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 945 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 946 | for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 947 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 948 | NodeTy* N2 = *I2; |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 949 | StateTy St = N2->getState(); |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 950 | Expr* RHS = B->getRHS(); |
| 951 | RVal RightV = GetRVal(St, RHS); |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 952 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 953 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 954 | |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 955 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 956 | && RHS->getType()->isIntegerType()) { |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 957 | |
| 958 | // Check if the denominator is uninitialized. |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 959 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 960 | if (!RightV.isUnknown()) { |
| 961 | |
| 962 | if (RightV.isUninit()) { |
| 963 | NodeTy* DivUninit = Builder->generateNode(B, St, N2); |
| 964 | |
| 965 | if (DivUninit) { |
| 966 | DivUninit->markAsSink(); |
| 967 | BadDivides.insert(DivUninit); |
| 968 | } |
| 969 | |
| 970 | continue; |
| 971 | } |
| 972 | |
| 973 | // Check for divide/remainder-by-zero. |
| 974 | // |
| 975 | // First, "assume" that the denominator is 0 or uninitialized. |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 976 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 977 | bool isFeasible = false; |
| 978 | StateTy ZeroSt = Assume(St, RightV, false, isFeasible); |
| 979 | |
| 980 | if (isFeasible) { |
| 981 | NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2); |
| 982 | |
| 983 | if (DivZeroNode) { |
| 984 | DivZeroNode->markAsSink(); |
| 985 | BadDivides.insert(DivZeroNode); |
| 986 | } |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 989 | // Second, "assume" that the denominator cannot be 0. |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 990 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 991 | isFeasible = false; |
| 992 | St = Assume(St, RightV, true, isFeasible); |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 993 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 994 | if (!isFeasible) |
| 995 | continue; |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 998 | // Fall-through. The logic below processes the divide. |
| 999 | } |
| 1000 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1001 | if (Op <= BinaryOperator::Or) { |
| 1002 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1003 | // Process non-assignements except commas or short-circuited |
| 1004 | // logical expressions (LAnd and LOr). |
| 1005 | |
| 1006 | RVal Result = EvalBinOp(Op, LeftV, RightV); |
| 1007 | |
| 1008 | if (Result.isUnknown()) { |
| 1009 | Dst.Add(N2); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1010 | continue; |
| 1011 | } |
| 1012 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1013 | Nodify(Dst, B, N2, SetRVal(St, B, Result)); |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1014 | continue; |
| 1015 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1016 | |
| 1017 | // Process assignments. |
| 1018 | |
| 1019 | switch (Op) { |
| 1020 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1021 | case BinaryOperator::Assign: { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Simple assignments. |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1024 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1025 | if (LeftV.isUninit()) { |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1026 | HandleUninitializedStore(B, N2); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1027 | continue; |
| 1028 | } |
| 1029 | |
| 1030 | if (LeftV.isUnknown()) { |
| 1031 | St = SetRVal(St, B, RightV); |
| 1032 | break; |
| 1033 | } |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1034 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1035 | St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV); |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | } |
| 1038 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1039 | // Compound assignment operators. |
Ted Kremenek | 19072e0 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1040 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1041 | default: { |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1042 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1043 | assert (B->isCompoundAssignmentOp()); |
| 1044 | |
| 1045 | if (Op >= BinaryOperator::AndAssign) |
| 1046 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 1047 | else |
| 1048 | ((int&) Op) -= BinaryOperator::MulAssign; |
| 1049 | |
| 1050 | // Check if the LHS is uninitialized. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1051 | |
| 1052 | if (LeftV.isUninit()) { |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1053 | HandleUninitializedStore(B, N2); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1054 | continue; |
| 1055 | } |
| 1056 | |
| 1057 | if (LeftV.isUnknown()) { |
| 1058 | |
| 1059 | // While we do not know the location to store RightV, |
| 1060 | // the entire expression does evaluate to RightV. |
| 1061 | |
| 1062 | if (RightV.isUnknown()) { |
| 1063 | Dst.Add(N2); |
| 1064 | continue; |
| 1065 | } |
| 1066 | |
| 1067 | St = SetRVal(St, B, RightV); |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1068 | break; |
| 1069 | } |
| 1070 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1071 | // At this pointer we know that the LHS evaluates to an LVal |
| 1072 | // that is neither "Unknown" or "Unintialized." |
| 1073 | |
| 1074 | LVal LeftLV = cast<LVal>(LeftV); |
| 1075 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1076 | // Fetch the value of the LHS (the value of the variable, etc.). |
| 1077 | |
| 1078 | RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType()); |
| 1079 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1080 | // Propagate uninitialized value (left-side). We |
| 1081 | // propogate uninitialized values for the RHS below when |
| 1082 | // we also check for divide-by-zero. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (V.isUninit()) { |
| 1085 | St = SetRVal(St, B, V); |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | // Propagate unknown values. |
| 1090 | |
Ted Kremenek | b8782e1 | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 1091 | if (V.isUnknown()) { |
| 1092 | Dst.Add(N2); |
| 1093 | continue; |
| 1094 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (RightV.isUnknown()) { |
| 1097 | St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV); |
| 1098 | break; |
| 1099 | } |
| 1100 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1101 | // At this point: |
| 1102 | // |
| 1103 | // The LHS is not Uninit/Unknown. |
| 1104 | // The RHS is not Unknown. |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1105 | |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1106 | // Get the computation type. |
| 1107 | QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType(); |
| 1108 | |
| 1109 | // Perform promotions. |
| 1110 | V = EvalCast(V, CTy); |
Ted Kremenek | 666ab92 | 2008-02-21 18:46:24 +0000 | [diff] [blame] | 1111 | RightV = EvalCast(RightV, CTy); |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Evaluate operands and promote to result type. |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1114 | |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1115 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 1116 | && RHS->getType()->isIntegerType()) { |
| 1117 | |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1118 | // Check if the denominator is uninitialized. |
| 1119 | |
| 1120 | if (RightV.isUninit()) { |
| 1121 | NodeTy* DivUninit = Builder->generateNode(B, St, N2); |
| 1122 | |
| 1123 | if (DivUninit) { |
| 1124 | DivUninit->markAsSink(); |
| 1125 | BadDivides.insert(DivUninit); |
| 1126 | } |
| 1127 | |
| 1128 | continue; |
| 1129 | } |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1130 | |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1131 | // First, "assume" that the denominator is 0. |
| 1132 | |
| 1133 | bool isFeasible = false; |
| 1134 | StateTy ZeroSt = Assume(St, RightV, false, isFeasible); |
| 1135 | |
| 1136 | if (isFeasible) { |
| 1137 | NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2); |
| 1138 | |
| 1139 | if (DivZeroNode) { |
| 1140 | DivZeroNode->markAsSink(); |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1141 | BadDivides.insert(DivZeroNode); |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | // Second, "assume" that the denominator cannot be 0. |
| 1146 | |
| 1147 | isFeasible = false; |
| 1148 | St = Assume(St, RightV, true, isFeasible); |
| 1149 | |
| 1150 | if (!isFeasible) |
| 1151 | continue; |
| 1152 | |
| 1153 | // Fall-through. The logic below processes the divide. |
| 1154 | } |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1155 | else { |
| 1156 | |
| 1157 | // Propagate uninitialized values (right-side). |
| 1158 | |
| 1159 | if (RightV.isUninit()) { |
| 1160 | St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV); |
| 1161 | break; |
| 1162 | } |
| 1163 | |
| 1164 | } |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1165 | |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1166 | RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType()); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1167 | St = SetRVal(SetRVal(St, B, Result), LeftLV, Result); |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1168 | } |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1169 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1170 | |
| 1171 | Nodify(Dst, B, N2, St); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1172 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1173 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1174 | } |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1175 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1176 | void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) { |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1177 | NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred); |
| 1178 | N->markAsSink(); |
| 1179 | UninitStores.insert(N); |
| 1180 | } |
Ted Kremenek | be96245 | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1181 | |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1182 | void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1183 | |
| 1184 | // FIXME: add metadata to the CFG so that we can disable |
| 1185 | // this check when we KNOW that there is no block-level subexpression. |
| 1186 | // The motivation is that this check requires a hashtable lookup. |
| 1187 | |
| 1188 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 1189 | Dst.Add(Pred); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | switch (S->getStmtClass()) { |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1194 | |
| 1195 | default: |
| 1196 | // Cases we intentionally have "default" handle: |
Ted Kremenek | 9b496f9 | 2008-02-26 19:17:09 +0000 | [diff] [blame] | 1197 | // AddrLabelExpr, IntegerLiteral, CharacterLiteral |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1198 | |
| 1199 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1200 | break; |
| 1201 | |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1202 | case Stmt::BinaryOperatorClass: { |
| 1203 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1204 | |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1205 | if (B->isLogicalOp()) { |
| 1206 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | } |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1209 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1210 | StateTy St = Pred->getState(); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1211 | Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS()))); |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1214 | |
| 1215 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1216 | break; |
| 1217 | } |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1218 | |
| 1219 | case Stmt::CallExprClass: { |
| 1220 | CallExpr* C = cast<CallExpr>(S); |
| 1221 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
| 1222 | break; |
| 1223 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1224 | |
| 1225 | case Stmt::CastExprClass: { |
| 1226 | CastExpr* C = cast<CastExpr>(S); |
| 1227 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1228 | break; |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1229 | } |
Ted Kremenek | 9b496f9 | 2008-02-26 19:17:09 +0000 | [diff] [blame] | 1230 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1231 | // FIXME: ChooseExpr is really a constant. We need to fix |
| 1232 | // the CFG do not model them as explicit control-flow. |
| 1233 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1234 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 1235 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1236 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1237 | break; |
| 1238 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1239 | |
Ted Kremenek | 9914e9c | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1240 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1241 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1242 | break; |
| 1243 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1244 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 1245 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 1246 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1247 | break; |
| 1248 | } |
| 1249 | |
| 1250 | case Stmt::DeclRefExprClass: |
| 1251 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 1252 | break; |
| 1253 | |
| 1254 | case Stmt::DeclStmtClass: |
| 1255 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1256 | break; |
| 1257 | |
| 1258 | case Stmt::ImplicitCastExprClass: { |
| 1259 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1260 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | case Stmt::ParenExprClass: |
| 1265 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1266 | break; |
| 1267 | |
| 1268 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 1269 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 1270 | break; |
| 1271 | |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1272 | case Stmt::StmtExprClass: { |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1273 | StmtExpr* SE = cast<StmtExpr>(S); |
| 1274 | |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1275 | StateTy St = Pred->getState(); |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1276 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1277 | Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr))); |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1278 | break; |
| 1279 | } |
| 1280 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1281 | // FIXME: We may wish to always bind state to ReturnStmts so |
| 1282 | // that users can quickly query what was the state at the |
| 1283 | // exit points of a function. |
| 1284 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1285 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1286 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1287 | Visit(R, Pred, Dst); |
| 1288 | else |
| 1289 | Dst.Add(Pred); |
| 1290 | |
| 1291 | break; |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1292 | } |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1293 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1294 | case Stmt::UnaryOperatorClass: { |
| 1295 | UnaryOperator* U = cast<UnaryOperator>(S); |
| 1296 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1297 | switch (U->getOpcode()) { |
| 1298 | case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break; |
| 1299 | case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break; |
| 1300 | case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break; |
| 1301 | default: VisitUnaryOperator(U, Pred, Dst); break; |
| 1302 | } |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1303 | |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1304 | break; |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1305 | } |
Ted Kremenek | ab8ed95 | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1306 | } |
Ted Kremenek | be96245 | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1309 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1310 | // "Assume" logic. |
| 1311 | //===----------------------------------------------------------------------===// |
| 1312 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1313 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond, |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1314 | bool Assumption, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1315 | bool& isFeasible) { |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1316 | switch (Cond.getSubKind()) { |
| 1317 | default: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1318 | assert (false && "'Assume' not implemented for this LVal."); |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1319 | return St; |
| 1320 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1321 | case lval::SymbolValKind: |
| 1322 | if (Assumption) |
| 1323 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1324 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1325 | else |
| 1326 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1327 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1328 | |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1329 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1330 | case lval::DeclValKind: |
Ted Kremenek | 3870619 | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 1331 | case lval::FuncValKind: |
| 1332 | case lval::GotoLabelKind: |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1333 | isFeasible = Assumption; |
| 1334 | return St; |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1335 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1336 | case lval::ConcreteIntKind: { |
| 1337 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1338 | isFeasible = b ? Assumption : !Assumption; |
| 1339 | return St; |
| 1340 | } |
| 1341 | } |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1344 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1345 | bool Assumption, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1346 | bool& isFeasible) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1347 | switch (Cond.getSubKind()) { |
| 1348 | default: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1349 | assert (false && "'Assume' not implemented for this NonLVal."); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1350 | return St; |
| 1351 | |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1352 | |
| 1353 | case nonlval::SymbolValKind: { |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1354 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1355 | SymbolID sym = SV.getSymbol(); |
| 1356 | |
| 1357 | if (Assumption) |
| 1358 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1359 | isFeasible); |
| 1360 | else |
| 1361 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1362 | isFeasible); |
| 1363 | } |
| 1364 | |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1365 | case nonlval::SymIntConstraintValKind: |
| 1366 | return |
| 1367 | AssumeSymInt(St, Assumption, |
| 1368 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1369 | isFeasible); |
| 1370 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1371 | case nonlval::ConcreteIntKind: { |
| 1372 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1373 | isFeasible = b ? Assumption : !Assumption; |
| 1374 | return St; |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1379 | GRExprEngine::StateTy |
| 1380 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1381 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1382 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1383 | // First, determine if sym == X, where X != V. |
| 1384 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1385 | isFeasible = *X != V; |
| 1386 | return St; |
| 1387 | } |
| 1388 | |
| 1389 | // Second, determine if sym != V. |
| 1390 | if (St.isNotEqual(sym, V)) { |
| 1391 | isFeasible = true; |
| 1392 | return St; |
| 1393 | } |
| 1394 | |
| 1395 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1396 | // Make that assumption. |
| 1397 | |
| 1398 | isFeasible = true; |
| 1399 | return StateMgr.AddNE(St, sym, V); |
| 1400 | } |
| 1401 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1402 | GRExprEngine::StateTy |
| 1403 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1404 | const llvm::APSInt& V, bool& isFeasible) { |
| 1405 | |
| 1406 | // First, determine if sym == X, where X != V. |
| 1407 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1408 | isFeasible = *X == V; |
| 1409 | return St; |
| 1410 | } |
| 1411 | |
| 1412 | // Second, determine if sym != V. |
| 1413 | if (St.isNotEqual(sym, V)) { |
| 1414 | isFeasible = false; |
| 1415 | return St; |
| 1416 | } |
| 1417 | |
| 1418 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1419 | // Make that assumption. |
| 1420 | |
| 1421 | isFeasible = true; |
| 1422 | return StateMgr.AddEQ(St, sym, V); |
| 1423 | } |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1424 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1425 | GRExprEngine::StateTy |
| 1426 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1427 | const SymIntConstraint& C, bool& isFeasible) { |
| 1428 | |
| 1429 | switch (C.getOpcode()) { |
| 1430 | default: |
| 1431 | // No logic yet for other operators. |
| 1432 | return St; |
| 1433 | |
| 1434 | case BinaryOperator::EQ: |
| 1435 | if (Assumption) |
| 1436 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1437 | else |
| 1438 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1439 | |
| 1440 | case BinaryOperator::NE: |
| 1441 | if (Assumption) |
| 1442 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1443 | else |
| 1444 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1445 | } |
| 1446 | } |
| 1447 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1448 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1449 | // Visualization. |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1450 | //===----------------------------------------------------------------------===// |
| 1451 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1452 | #ifndef NDEBUG |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1453 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1454 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1455 | namespace llvm { |
| 1456 | template<> |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1457 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1458 | public DefaultDOTGraphTraits { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1459 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1460 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1461 | |
| 1462 | Out << "Variables:\\l"; |
| 1463 | |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1464 | bool isFirst = true; |
| 1465 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1466 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1467 | E=St.vb_end(); I!=E;++I) { |
| 1468 | |
| 1469 | if (isFirst) |
| 1470 | isFirst = false; |
| 1471 | else |
| 1472 | Out << "\\l"; |
| 1473 | |
| 1474 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1475 | I.getData().print(Out); |
| 1476 | } |
| 1477 | |
| 1478 | } |
| 1479 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1480 | |
Ted Kremenek | 6d40992 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1481 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1482 | |
| 1483 | bool isFirst = true; |
| 1484 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1485 | for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end(); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1486 | I != E;++I) { |
| 1487 | |
| 1488 | if (isFirst) { |
| 1489 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1490 | isFirst = false; |
| 1491 | } |
| 1492 | else |
| 1493 | Out << "\\l"; |
| 1494 | |
| 1495 | Out << " (" << (void*) I.getKey() << ") "; |
| 1496 | I.getKey()->printPretty(Out); |
| 1497 | Out << " : "; |
| 1498 | I.getData().print(Out); |
| 1499 | } |
| 1500 | } |
| 1501 | |
Ted Kremenek | 6d40992 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1502 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1503 | |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1504 | bool isFirst = true; |
| 1505 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1506 | for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end(); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1507 | I != E; ++I) { |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1508 | if (isFirst) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1509 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1510 | isFirst = false; |
| 1511 | } |
| 1512 | else |
| 1513 | Out << "\\l"; |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1514 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1515 | Out << " (" << (void*) I.getKey() << ") "; |
| 1516 | I.getKey()->printPretty(Out); |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1517 | Out << " : "; |
| 1518 | I.getData().print(Out); |
| 1519 | } |
| 1520 | } |
| 1521 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1522 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1523 | ValueState::ConstEqTy CE = St.getImpl()->ConstEq; |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1524 | |
| 1525 | if (CE.isEmpty()) |
| 1526 | return; |
| 1527 | |
| 1528 | Out << "\\l\\|'==' constraints:"; |
| 1529 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1530 | for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1531 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1532 | } |
| 1533 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1534 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1535 | ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq; |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1536 | |
| 1537 | if (NE.isEmpty()) |
| 1538 | return; |
| 1539 | |
| 1540 | Out << "\\l\\|'!=' constraints:"; |
| 1541 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1542 | for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1543 | I != EI; ++I){ |
| 1544 | |
| 1545 | Out << "\\l $" << I.getKey() << " : "; |
| 1546 | bool isFirst = true; |
| 1547 | |
| 1548 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1549 | EJ=I.getData().end(); |
| 1550 | for ( ; J != EJ; ++J) { |
| 1551 | if (isFirst) isFirst = false; |
| 1552 | else Out << ", "; |
| 1553 | |
| 1554 | Out << (*J)->toString(); |
| 1555 | } |
| 1556 | } |
Ted Kremenek | a853de6 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1560 | |
| 1561 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1562 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | 9d4895c | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1563 | GraphPrintCheckerState->isUninitDeref(N) || |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1564 | GraphPrintCheckerState->isUninitStore(N) || |
Ted Kremenek | f567a82 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 1565 | GraphPrintCheckerState->isUninitControlFlow(N) || |
| 1566 | GraphPrintCheckerState->isBadDivide(N)) |
Ted Kremenek | a853de6 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1567 | return "color=\"red\",style=\"filled\""; |
| 1568 | |
| 1569 | return ""; |
| 1570 | } |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1571 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1572 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1573 | std::ostringstream Out; |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1574 | |
| 1575 | // Program Location. |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1576 | ProgramPoint Loc = N->getLocation(); |
| 1577 | |
| 1578 | switch (Loc.getKind()) { |
| 1579 | case ProgramPoint::BlockEntranceKind: |
| 1580 | Out << "Block Entrance: B" |
| 1581 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1582 | break; |
| 1583 | |
| 1584 | case ProgramPoint::BlockExitKind: |
| 1585 | assert (false); |
| 1586 | break; |
| 1587 | |
| 1588 | case ProgramPoint::PostStmtKind: { |
| 1589 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1590 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1591 | << (void*) L.getStmt() << ' '; |
| 1592 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1593 | L.getStmt()->printPretty(Out); |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1594 | |
| 1595 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1596 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1597 | } |
Ted Kremenek | ae7bdc1 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1598 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1599 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1600 | } |
Ted Kremenek | 9d4895c | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1601 | else if (GraphPrintCheckerState->isUninitDeref(N)) { |
| 1602 | Out << "\\|Dereference of uninitialied value.\\l"; |
| 1603 | } |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1604 | else if (GraphPrintCheckerState->isUninitStore(N)) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1605 | Out << "\\|Store to Uninitialized LVal."; |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1606 | } |
Ted Kremenek | f567a82 | 2008-02-26 21:31:18 +0000 | [diff] [blame] | 1607 | else if (GraphPrintCheckerState->isBadDivide(N)) { |
| 1608 | Out << "\\|Divide-by zero or uninitialized value."; |
| 1609 | } |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1610 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | } |
| 1613 | |
| 1614 | default: { |
| 1615 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1616 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1617 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1618 | |
| 1619 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1620 | Out << "\\|Terminator: "; |
| 1621 | E.getSrc()->printTerminator(Out); |
| 1622 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1623 | if (isa<SwitchStmt>(T)) { |
| 1624 | Stmt* Label = E.getDst()->getLabel(); |
| 1625 | |
| 1626 | if (Label) { |
| 1627 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1628 | Out << "\\lcase "; |
| 1629 | C->getLHS()->printPretty(Out); |
| 1630 | |
| 1631 | if (Stmt* RHS = C->getRHS()) { |
| 1632 | Out << " .. "; |
| 1633 | RHS->printPretty(Out); |
| 1634 | } |
| 1635 | |
| 1636 | Out << ":"; |
| 1637 | } |
| 1638 | else { |
| 1639 | assert (isa<DefaultStmt>(Label)); |
| 1640 | Out << "\\ldefault:"; |
| 1641 | } |
| 1642 | } |
| 1643 | else |
| 1644 | Out << "\\l(implicit) default:"; |
| 1645 | } |
| 1646 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1647 | // FIXME |
| 1648 | } |
| 1649 | else { |
| 1650 | Out << "\\lCondition: "; |
| 1651 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1652 | Out << "true"; |
| 1653 | else |
| 1654 | Out << "false"; |
| 1655 | } |
| 1656 | |
| 1657 | Out << "\\l"; |
| 1658 | } |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1659 | |
| 1660 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1661 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1662 | } |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1666 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1667 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1668 | N->getState().printDOT(Out); |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1669 | |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1670 | Out << "\\l"; |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1671 | return Out.str(); |
| 1672 | } |
| 1673 | }; |
| 1674 | } // end llvm namespace |
| 1675 | #endif |
| 1676 | |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1677 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1678 | #ifndef NDEBUG |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1679 | GraphPrintCheckerState = this; |
| 1680 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1681 | GraphPrintCheckerState = NULL; |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1682 | #endif |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1683 | } |