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