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