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 | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 735 | void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) { |
| 736 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 737 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 738 | |
| 739 | NodeSet DstTmp; |
| 740 | |
Ted Kremenek | 018c15f | 2008-02-26 03:44:25 +0000 | [diff] [blame] | 741 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 742 | DstTmp.Add(Pred); |
| 743 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 744 | Visit(Ex, Pred, DstTmp); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 745 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 746 | for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 747 | |
| 748 | NodeTy* N = *I; |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 749 | ValueState* St = N->getState(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 750 | |
| 751 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 752 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 753 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 754 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 755 | // Check for dereferences of undefined values. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 756 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 757 | if (V.isUndef()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 758 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 759 | NodeTy* Succ = Builder->generateNode(U, St, N); |
| 760 | |
| 761 | if (Succ) { |
| 762 | Succ->markAsSink(); |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 763 | UndefDeref.insert(Succ); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | continue; |
| 767 | } |
| 768 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 769 | // Check for dereferences of unknown values. Treat as No-Ops. |
| 770 | |
| 771 | if (V.isUnknown()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 772 | Dst.Add(N); |
| 773 | continue; |
| 774 | } |
| 775 | |
| 776 | // After a dereference, one of two possible situations arise: |
| 777 | // (1) A crash, because the pointer was NULL. |
| 778 | // (2) The pointer is not NULL, and the dereference works. |
| 779 | // |
| 780 | // We add these assumptions. |
| 781 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 782 | LVal LV = cast<LVal>(V); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 783 | bool isFeasibleNotNull; |
| 784 | |
| 785 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 786 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 787 | ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 788 | |
| 789 | if (isFeasibleNotNull) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 790 | |
| 791 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 792 | // for the contents of values. We need a better approach. |
| 793 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 794 | Nodify(Dst, U, N, SetRVal(StNotNull, U, |
| 795 | GetRVal(StNotNull, LV, U->getType()))); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | bool isFeasibleNull; |
| 799 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 800 | // Now "assume" that the pointer is NULL. |
| 801 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 802 | ValueState* StNull = Assume(St, LV, false, isFeasibleNull); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 803 | |
| 804 | if (isFeasibleNull) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 805 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 806 | // We don't use "Nodify" here because the node will be a sink |
| 807 | // and we have no intention of processing it later. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 808 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 809 | NodeTy* NullNode = Builder->generateNode(U, StNull, N); |
| 810 | |
| 811 | if (NullNode) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 812 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 813 | NullNode->markAsSink(); |
| 814 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 815 | if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode); |
| 816 | else ExplicitNullDeref.insert(NullNode); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 822 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred, |
| 823 | NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 825 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 826 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 827 | assert (U->getOpcode() != UnaryOperator::Deref); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 828 | assert (U->getOpcode() != UnaryOperator::SizeOf); |
| 829 | assert (U->getOpcode() != UnaryOperator::AlignOf); |
| 830 | |
| 831 | bool use_GetLVal = false; |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 832 | |
| 833 | switch (U->getOpcode()) { |
| 834 | case UnaryOperator::PostInc: |
| 835 | case UnaryOperator::PostDec: |
| 836 | case UnaryOperator::PreInc: |
| 837 | case UnaryOperator::PreDec: |
| 838 | case UnaryOperator::AddrOf: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 839 | // Evalue subexpression as an LVal. |
| 840 | use_GetLVal = true; |
| 841 | VisitLVal(U->getSubExpr(), Pred, S1); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 842 | break; |
| 843 | |
| 844 | default: |
| 845 | Visit(U->getSubExpr(), Pred, S1); |
| 846 | break; |
| 847 | } |
| 848 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 849 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 850 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 851 | NodeTy* N1 = *I1; |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 852 | ValueState* St = N1->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 853 | |
| 854 | RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) : |
| 855 | GetRVal(St, U->getSubExpr()); |
| 856 | |
| 857 | if (SubV.isUnknown()) { |
| 858 | Dst.Add(N1); |
| 859 | continue; |
| 860 | } |
| 861 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 862 | if (SubV.isUndef()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 863 | Nodify(Dst, U, N1, SetRVal(St, U, SubV)); |
| 864 | continue; |
| 865 | } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 866 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 867 | if (U->isIncrementDecrementOp()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 868 | |
| 869 | // Handle ++ and -- (both pre- and post-increment). |
| 870 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 871 | LVal SubLV = cast<LVal>(SubV); |
| 872 | RVal V = GetRVal(St, SubLV, U->getType()); |
| 873 | |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 874 | if (V.isUnknown()) { |
| 875 | Dst.Add(N1); |
| 876 | continue; |
| 877 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 878 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 879 | // Propagate undefined values. |
| 880 | if (V.isUndef()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 881 | Nodify(Dst, U, N1, SetRVal(St, U, V)); |
| 882 | continue; |
| 883 | } |
| 884 | |
Ted Kremenek | 443003b | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 885 | // Handle all other values. |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 886 | |
| 887 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 888 | : BinaryOperator::Sub; |
| 889 | |
Ted Kremenek | 443003b | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 890 | RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U)); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 891 | |
| 892 | if (U->isPostfix()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 893 | St = SetRVal(SetRVal(St, U, V), SubLV, Result); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 894 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 895 | St = SetRVal(SetRVal(St, U, Result), SubLV, Result); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 896 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 897 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 898 | continue; |
| 899 | } |
| 900 | |
| 901 | // Handle all other unary operators. |
| 902 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 903 | switch (U->getOpcode()) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 905 | case UnaryOperator::Minus: |
| 906 | St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 907 | break; |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 908 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 909 | case UnaryOperator::Not: |
| 910 | St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV))); |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 911 | break; |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 912 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 913 | case UnaryOperator::LNot: |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 914 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 915 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 916 | // |
| 917 | // Note: technically we do "E == 0", but this is the same in the |
| 918 | // transfer functions as "0 == E". |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 919 | |
| 920 | if (isa<LVal>(SubV)) { |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 921 | lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 922 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V); |
| 923 | St = SetRVal(St, U, Result); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 924 | } |
| 925 | else { |
Ted Kremenek | f7ca696 | 2008-02-22 00:42:36 +0000 | [diff] [blame] | 926 | Expr* Ex = U->getSubExpr(); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 927 | nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType())); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 928 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V); |
| 929 | St = SetRVal(St, U, Result); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | break; |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 933 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 934 | case UnaryOperator::AddrOf: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 935 | assert (isa<LVal>(SubV)); |
| 936 | St = SetRVal(St, U, SubV); |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 937 | break; |
| 938 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 940 | default: ; |
| 941 | assert (false && "Not implemented."); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 948 | void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred, |
| 949 | NodeSet& Dst) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 950 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 951 | QualType T = U->getSubExpr()->getType(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 952 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 953 | // FIXME: Add support for VLAs. |
| 954 | if (!T.getTypePtr()->isConstantSizeType()) |
| 955 | return; |
| 956 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 957 | uint64_t size = getContext().getTypeSize(T) / 8; |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 958 | ValueState* St = Pred->getState(); |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 959 | St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType())); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 960 | |
| 961 | Nodify(Dst, U, Pred, St); |
| 962 | } |
| 963 | |
| 964 | void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 2ad8868 | 2008-02-27 07:04:16 +0000 | [diff] [blame] | 965 | |
| 966 | if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) { |
| 967 | Dst.Add(Pred); |
| 968 | return; |
| 969 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 970 | |
| 971 | Ex = Ex->IgnoreParens(); |
| 972 | |
Ted Kremenek | 0793263 | 2008-02-27 06:47:26 +0000 | [diff] [blame] | 973 | if (isa<DeclRefExpr>(Ex)) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 974 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 975 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 976 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 977 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 978 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 979 | if (U->getOpcode() == UnaryOperator::Deref) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 980 | Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 981 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 982 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 983 | Dst.Add(Pred); |
| 984 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 985 | Visit(Ex, Pred, Dst); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 986 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 987 | return; |
| 988 | } |
| 989 | } |
| 990 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 991 | Visit(Ex, Pred, Dst); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 994 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 995 | GRExprEngine::NodeTy* Pred, |
| 996 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 997 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 998 | |
| 999 | if (B->isAssignmentOp()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1000 | VisitLVal(B->getLHS(), Pred, S1); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1001 | else |
| 1002 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1003 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1004 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1005 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1006 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1007 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1008 | // 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] | 1009 | // In such cases, we want to (initially) treat the LHS as an LVal, |
| 1010 | // so we use GetLVal instead of GetRVal so that DeclRefExpr's are |
| 1011 | // evaluated to LValDecl's instead of to an NonLVal. |
| 1012 | |
| 1013 | RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS()) |
| 1014 | : GetRVal(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1015 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1016 | // Visit the RHS... |
| 1017 | |
| 1018 | NodeSet S2; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1019 | Visit(B->getRHS(), N1, S2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1020 | |
| 1021 | // Process the binary operator. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1022 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1023 | for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1024 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1025 | NodeTy* N2 = *I2; |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1026 | ValueState* St = N2->getState(); |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1027 | Expr* RHS = B->getRHS(); |
| 1028 | RVal RightV = GetRVal(St, RHS); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1029 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1030 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 1031 | |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1032 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 1033 | && RHS->getType()->isIntegerType()) { |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1034 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1035 | // Check if the denominator is undefined. |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1036 | |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1037 | if (!RightV.isUnknown()) { |
| 1038 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1039 | if (RightV.isUndef()) { |
| 1040 | NodeTy* DivUndef = Builder->generateNode(B, St, N2); |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1041 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1042 | if (DivUndef) { |
| 1043 | DivUndef->markAsSink(); |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1044 | ExplicitBadDivides.insert(DivUndef); |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | continue; |
| 1048 | } |
| 1049 | |
| 1050 | // Check for divide/remainder-by-zero. |
| 1051 | // |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1052 | // First, "assume" that the denominator is 0 or undefined. |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1053 | |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1054 | bool isFeasibleZero = false; |
| 1055 | ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero); |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1056 | |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1057 | // Second, "assume" that the denominator cannot be 0. |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1058 | |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1059 | bool isFeasibleNotZero = false; |
| 1060 | St = Assume(St, RightV, true, isFeasibleNotZero); |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1061 | |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1062 | // Create the node for the divide-by-zero (if it occurred). |
| 1063 | |
| 1064 | if (isFeasibleZero) |
| 1065 | if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) { |
| 1066 | DivZeroNode->markAsSink(); |
| 1067 | |
| 1068 | if (isFeasibleNotZero) |
| 1069 | ImplicitBadDivides.insert(DivZeroNode); |
| 1070 | else |
| 1071 | ExplicitBadDivides.insert(DivZeroNode); |
| 1072 | |
| 1073 | } |
| 1074 | |
| 1075 | if (!isFeasibleNotZero) |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1076 | continue; |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1079 | // Fall-through. The logic below processes the divide. |
| 1080 | } |
| 1081 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1082 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1083 | if (Op <= BinaryOperator::Or) { |
| 1084 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1085 | // Process non-assignements except commas or short-circuited |
| 1086 | // logical expressions (LAnd and LOr). |
| 1087 | |
| 1088 | RVal Result = EvalBinOp(Op, LeftV, RightV); |
| 1089 | |
| 1090 | if (Result.isUnknown()) { |
| 1091 | Dst.Add(N2); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1092 | continue; |
| 1093 | } |
| 1094 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1095 | if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) { |
| 1096 | |
| 1097 | // The operands were not undefined, but the result is undefined. |
| 1098 | |
| 1099 | if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) { |
| 1100 | UndefNode->markAsSink(); |
| 1101 | UndefResults.insert(UndefNode); |
| 1102 | } |
| 1103 | |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1107 | Nodify(Dst, B, N2, SetRVal(St, B, Result)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1108 | continue; |
| 1109 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Process assignments. |
| 1112 | |
| 1113 | switch (Op) { |
| 1114 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1115 | case BinaryOperator::Assign: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Simple assignments. |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1118 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1119 | if (LeftV.isUndef()) { |
| 1120 | HandleUndefinedStore(B, N2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1121 | continue; |
| 1122 | } |
| 1123 | |
| 1124 | if (LeftV.isUnknown()) { |
| 1125 | St = SetRVal(St, B, RightV); |
| 1126 | break; |
| 1127 | } |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1128 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1129 | St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1130 | break; |
| 1131 | } |
| 1132 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1133 | // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1134 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1135 | default: { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1136 | |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1137 | assert (B->isCompoundAssignmentOp()); |
| 1138 | |
| 1139 | if (Op >= BinaryOperator::AndAssign) |
| 1140 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 1141 | else |
| 1142 | ((int&) Op) -= BinaryOperator::MulAssign; |
| 1143 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1144 | // Check if the LHS is undefined. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1145 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1146 | if (LeftV.isUndef()) { |
| 1147 | HandleUndefinedStore(B, N2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1148 | continue; |
| 1149 | } |
| 1150 | |
| 1151 | if (LeftV.isUnknown()) { |
| 1152 | |
| 1153 | // While we do not know the location to store RightV, |
| 1154 | // the entire expression does evaluate to RightV. |
| 1155 | |
| 1156 | if (RightV.isUnknown()) { |
| 1157 | Dst.Add(N2); |
| 1158 | continue; |
| 1159 | } |
| 1160 | |
| 1161 | St = SetRVal(St, B, RightV); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1162 | break; |
| 1163 | } |
| 1164 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1165 | // At this pointer we know that the LHS evaluates to an LVal |
| 1166 | // that is neither "Unknown" or "Unintialized." |
| 1167 | |
| 1168 | LVal LeftLV = cast<LVal>(LeftV); |
| 1169 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1170 | // Fetch the value of the LHS (the value of the variable, etc.). |
| 1171 | |
| 1172 | RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType()); |
| 1173 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1174 | // Propagate undefined value (left-side). We |
| 1175 | // propogate undefined values for the RHS below when |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1176 | // we also check for divide-by-zero. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1177 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1178 | if (V.isUndef()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1179 | St = SetRVal(St, B, V); |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | // Propagate unknown values. |
| 1184 | |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 1185 | if (V.isUnknown()) { |
| 1186 | Dst.Add(N2); |
| 1187 | continue; |
| 1188 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1189 | |
| 1190 | if (RightV.isUnknown()) { |
| 1191 | St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV); |
| 1192 | break; |
| 1193 | } |
| 1194 | |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1195 | // At this point: |
| 1196 | // |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1197 | // The LHS is not Undef/Unknown. |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1198 | // The RHS is not Unknown. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1199 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1200 | // Get the computation type. |
| 1201 | QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType(); |
| 1202 | |
| 1203 | // Perform promotions. |
| 1204 | V = EvalCast(V, CTy); |
Ted Kremenek | 61e090c | 2008-02-21 18:46:24 +0000 | [diff] [blame] | 1205 | RightV = EvalCast(RightV, CTy); |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Evaluate operands and promote to result type. |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1208 | |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1209 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 1210 | && RHS->getType()->isIntegerType()) { |
| 1211 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1212 | // Check if the denominator is undefined. |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1213 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1214 | if (RightV.isUndef()) { |
| 1215 | NodeTy* DivUndef = Builder->generateNode(B, St, N2); |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1216 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1217 | if (DivUndef) { |
| 1218 | DivUndef->markAsSink(); |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1219 | ExplicitBadDivides.insert(DivUndef); |
Ted Kremenek | d763eb9 | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | continue; |
| 1223 | } |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1225 | // First, "assume" that the denominator is 0. |
| 1226 | |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1227 | bool isFeasibleZero = false; |
| 1228 | ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero); |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1229 | |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1230 | // Second, "assume" that the denominator cannot be 0. |
| 1231 | |
| 1232 | bool isFeasibleNotZero = false; |
| 1233 | St = Assume(St, RightV, true, isFeasibleNotZero); |
| 1234 | |
| 1235 | // Create the node for the divide-by-zero error (if it occurred). |
| 1236 | |
| 1237 | if (isFeasibleZero) { |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1238 | NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2); |
| 1239 | |
| 1240 | if (DivZeroNode) { |
| 1241 | DivZeroNode->markAsSink(); |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1242 | |
| 1243 | if (isFeasibleNotZero) |
| 1244 | ImplicitBadDivides.insert(DivZeroNode); |
| 1245 | else |
| 1246 | ExplicitBadDivides.insert(DivZeroNode); |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1250 | if (!isFeasibleNotZero) |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1251 | continue; |
| 1252 | |
| 1253 | // Fall-through. The logic below processes the divide. |
| 1254 | } |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1255 | else { |
| 1256 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1257 | // Propagate undefined values (right-side). |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1258 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1259 | if (RightV.isUndef()) { |
Ted Kremenek | 0015541 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1260 | St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV); |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | } |
Ted Kremenek | 07d83aa | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1265 | |
Ted Kremenek | 9ef1ec9 | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1266 | RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType()); |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1267 | |
| 1268 | if (Result.isUndef()) { |
| 1269 | |
| 1270 | // The operands were not undefined, but the result is undefined. |
| 1271 | |
| 1272 | if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) { |
| 1273 | UndefNode->markAsSink(); |
| 1274 | UndefResults.insert(UndefNode); |
| 1275 | } |
| 1276 | |
| 1277 | continue; |
| 1278 | } |
| 1279 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1280 | St = SetRVal(SetRVal(St, B, Result), LeftLV, Result); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1281 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1282 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1283 | |
| 1284 | Nodify(Dst, B, N2, St); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1285 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1286 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1287 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1288 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1289 | void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1290 | NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred); |
| 1291 | N->markAsSink(); |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1292 | UndefStores.insert(N); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1293 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1294 | |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1295 | void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1296 | |
| 1297 | // FIXME: add metadata to the CFG so that we can disable |
| 1298 | // this check when we KNOW that there is no block-level subexpression. |
| 1299 | // The motivation is that this check requires a hashtable lookup. |
| 1300 | |
| 1301 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 1302 | Dst.Add(Pred); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
| 1306 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1307 | |
| 1308 | default: |
| 1309 | // Cases we intentionally have "default" handle: |
Ted Kremenek | 189c305 | 2008-02-26 19:17:09 +0000 | [diff] [blame] | 1310 | // AddrLabelExpr, IntegerLiteral, CharacterLiteral |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1311 | |
| 1312 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1313 | break; |
| 1314 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1315 | case Stmt::BinaryOperatorClass: { |
| 1316 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1317 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1318 | if (B->isLogicalOp()) { |
| 1319 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1322 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1323 | ValueState* St = Pred->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1324 | Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1325 | break; |
| 1326 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1327 | |
| 1328 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1329 | break; |
| 1330 | } |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1331 | |
| 1332 | case Stmt::CallExprClass: { |
| 1333 | CallExpr* C = cast<CallExpr>(S); |
| 1334 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
| 1335 | break; |
| 1336 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1337 | |
| 1338 | case Stmt::CastExprClass: { |
| 1339 | CastExpr* C = cast<CastExpr>(S); |
| 1340 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1341 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1342 | } |
Ted Kremenek | 189c305 | 2008-02-26 19:17:09 +0000 | [diff] [blame] | 1343 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1344 | // FIXME: ChooseExpr is really a constant. We need to fix |
| 1345 | // the CFG do not model them as explicit control-flow. |
| 1346 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1347 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 1348 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1349 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1350 | break; |
| 1351 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1352 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1353 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1354 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1355 | break; |
| 1356 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1357 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 1358 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 1359 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1360 | break; |
| 1361 | } |
| 1362 | |
| 1363 | case Stmt::DeclRefExprClass: |
| 1364 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 1365 | break; |
| 1366 | |
| 1367 | case Stmt::DeclStmtClass: |
| 1368 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1369 | break; |
| 1370 | |
| 1371 | case Stmt::ImplicitCastExprClass: { |
| 1372 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1373 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1374 | break; |
| 1375 | } |
| 1376 | |
| 1377 | case Stmt::ParenExprClass: |
| 1378 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1379 | break; |
| 1380 | |
| 1381 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 1382 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 1383 | break; |
| 1384 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1385 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1386 | StmtExpr* SE = cast<StmtExpr>(S); |
| 1387 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1388 | ValueState* St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1389 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1390 | Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | } |
| 1393 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1394 | // FIXME: We may wish to always bind state to ReturnStmts so |
| 1395 | // that users can quickly query what was the state at the |
| 1396 | // exit points of a function. |
| 1397 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1398 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1399 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1400 | Visit(R, Pred, Dst); |
| 1401 | else |
| 1402 | Dst.Add(Pred); |
| 1403 | |
| 1404 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1405 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1406 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1407 | case Stmt::UnaryOperatorClass: { |
| 1408 | UnaryOperator* U = cast<UnaryOperator>(S); |
| 1409 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1410 | switch (U->getOpcode()) { |
| 1411 | case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break; |
| 1412 | case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break; |
| 1413 | case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break; |
| 1414 | default: VisitUnaryOperator(U, Pred, Dst); break; |
| 1415 | } |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1416 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1417 | break; |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1418 | } |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1419 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1422 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1423 | // "Assume" logic. |
| 1424 | //===----------------------------------------------------------------------===// |
| 1425 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1426 | ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond, |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1427 | bool Assumption, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1428 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1429 | switch (Cond.getSubKind()) { |
| 1430 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1431 | assert (false && "'Assume' not implemented for this LVal."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1432 | return St; |
| 1433 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1434 | case lval::SymbolValKind: |
| 1435 | if (Assumption) |
| 1436 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1437 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1438 | else |
| 1439 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1440 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1441 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1442 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1443 | case lval::DeclValKind: |
Ted Kremenek | dc3936b | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 1444 | case lval::FuncValKind: |
| 1445 | case lval::GotoLabelKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1446 | isFeasible = Assumption; |
| 1447 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1448 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1449 | case lval::ConcreteIntKind: { |
| 1450 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1451 | isFeasible = b ? Assumption : !Assumption; |
| 1452 | return St; |
| 1453 | } |
| 1454 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1457 | ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1458 | bool Assumption, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1459 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1460 | switch (Cond.getSubKind()) { |
| 1461 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1462 | assert (false && "'Assume' not implemented for this NonLVal."); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1463 | return St; |
| 1464 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1465 | |
| 1466 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1467 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1468 | SymbolID sym = SV.getSymbol(); |
| 1469 | |
| 1470 | if (Assumption) |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1471 | return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1472 | isFeasible); |
| 1473 | else |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1474 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1475 | isFeasible); |
| 1476 | } |
| 1477 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1478 | case nonlval::SymIntConstraintValKind: |
| 1479 | return |
| 1480 | AssumeSymInt(St, Assumption, |
| 1481 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1482 | isFeasible); |
| 1483 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1484 | case nonlval::ConcreteIntKind: { |
| 1485 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1486 | isFeasible = b ? Assumption : !Assumption; |
| 1487 | return St; |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1492 | ValueState* |
| 1493 | GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1494 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1495 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1496 | // First, determine if sym == X, where X != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1497 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1498 | isFeasible = *X != V; |
| 1499 | return St; |
| 1500 | } |
| 1501 | |
| 1502 | // Second, determine if sym != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1503 | if (St->isNotEqual(sym, V)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1504 | isFeasible = true; |
| 1505 | return St; |
| 1506 | } |
| 1507 | |
| 1508 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1509 | // Make that assumption. |
| 1510 | |
| 1511 | isFeasible = true; |
| 1512 | return StateMgr.AddNE(St, sym, V); |
| 1513 | } |
| 1514 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1515 | ValueState* |
| 1516 | GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1517 | const llvm::APSInt& V, bool& isFeasible) { |
| 1518 | |
| 1519 | // First, determine if sym == X, where X != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1520 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1521 | isFeasible = *X == V; |
| 1522 | return St; |
| 1523 | } |
| 1524 | |
| 1525 | // Second, determine if sym != V. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1526 | if (St->isNotEqual(sym, V)) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1527 | isFeasible = false; |
| 1528 | return St; |
| 1529 | } |
| 1530 | |
| 1531 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1532 | // Make that assumption. |
| 1533 | |
| 1534 | isFeasible = true; |
| 1535 | return StateMgr.AddEQ(St, sym, V); |
| 1536 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1537 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1538 | ValueState* |
| 1539 | GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1540 | const SymIntConstraint& C, bool& isFeasible) { |
| 1541 | |
| 1542 | switch (C.getOpcode()) { |
| 1543 | default: |
| 1544 | // No logic yet for other operators. |
| 1545 | return St; |
| 1546 | |
| 1547 | case BinaryOperator::EQ: |
| 1548 | if (Assumption) |
| 1549 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1550 | else |
| 1551 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1552 | |
| 1553 | case BinaryOperator::NE: |
| 1554 | if (Assumption) |
| 1555 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1556 | else |
| 1557 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1558 | } |
| 1559 | } |
| 1560 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1561 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1562 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1563 | //===----------------------------------------------------------------------===// |
| 1564 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1565 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1566 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1567 | static SourceManager* GraphPrintSourceManager; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1568 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1569 | namespace llvm { |
| 1570 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1571 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1572 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1573 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1574 | static void PrintVarBindings(std::ostream& Out, ValueState* St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1575 | |
| 1576 | Out << "Variables:\\l"; |
| 1577 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1578 | bool isFirst = true; |
| 1579 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1580 | 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] | 1581 | |
| 1582 | if (isFirst) |
| 1583 | isFirst = false; |
| 1584 | else |
| 1585 | Out << "\\l"; |
| 1586 | |
| 1587 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1588 | I.getData().print(Out); |
| 1589 | } |
| 1590 | |
| 1591 | } |
| 1592 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1593 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1594 | static void PrintSubExprBindings(std::ostream& Out, ValueState* St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1595 | |
| 1596 | bool isFirst = true; |
| 1597 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1598 | 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] | 1599 | |
| 1600 | if (isFirst) { |
| 1601 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1602 | isFirst = false; |
| 1603 | } |
| 1604 | else |
| 1605 | Out << "\\l"; |
| 1606 | |
| 1607 | Out << " (" << (void*) I.getKey() << ") "; |
| 1608 | I.getKey()->printPretty(Out); |
| 1609 | Out << " : "; |
| 1610 | I.getData().print(Out); |
| 1611 | } |
| 1612 | } |
| 1613 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1614 | static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1615 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1616 | bool isFirst = true; |
| 1617 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1618 | 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] | 1619 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1620 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1621 | isFirst = false; |
| 1622 | } |
| 1623 | else |
| 1624 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1625 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1626 | Out << " (" << (void*) I.getKey() << ") "; |
| 1627 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1628 | Out << " : "; |
| 1629 | I.getData().print(Out); |
| 1630 | } |
| 1631 | } |
| 1632 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1633 | static void PrintEQ(std::ostream& Out, ValueState* St) { |
| 1634 | ValueState::ConstEqTy CE = St->ConstEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1635 | |
| 1636 | if (CE.isEmpty()) |
| 1637 | return; |
| 1638 | |
| 1639 | Out << "\\l\\|'==' constraints:"; |
| 1640 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1641 | 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] | 1642 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1643 | } |
| 1644 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1645 | static void PrintNE(std::ostream& Out, ValueState* St) { |
| 1646 | ValueState::ConstNotEqTy NE = St->ConstNotEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1647 | |
| 1648 | if (NE.isEmpty()) |
| 1649 | return; |
| 1650 | |
| 1651 | Out << "\\l\\|'!=' constraints:"; |
| 1652 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1653 | for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1654 | I != EI; ++I){ |
| 1655 | |
| 1656 | Out << "\\l $" << I.getKey() << " : "; |
| 1657 | bool isFirst = true; |
| 1658 | |
| 1659 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1660 | EJ=I.getData().end(); |
| 1661 | for ( ; J != EJ; ++J) { |
| 1662 | if (isFirst) isFirst = false; |
| 1663 | else Out << ", "; |
| 1664 | |
| 1665 | Out << (*J)->toString(); |
| 1666 | } |
| 1667 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1671 | |
| 1672 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1673 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1674 | GraphPrintCheckerState->isUndefDeref(N) || |
| 1675 | GraphPrintCheckerState->isUndefStore(N) || |
| 1676 | GraphPrintCheckerState->isUndefControlFlow(N) || |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1677 | GraphPrintCheckerState->isExplicitBadDivide(N) || |
| 1678 | GraphPrintCheckerState->isImplicitBadDivide(N) || |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1679 | GraphPrintCheckerState->isUndefResult(N) || |
Ted Kremenek | 2ded35a | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 1680 | GraphPrintCheckerState->isBadCall(N) || |
| 1681 | GraphPrintCheckerState->isUndefArg(N)) |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1682 | return "color=\"red\",style=\"filled\""; |
| 1683 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1684 | if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 1685 | return "color=\"blue\",style=\"filled\""; |
| 1686 | |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1687 | return ""; |
| 1688 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1689 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1690 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1691 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1692 | |
| 1693 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1694 | ProgramPoint Loc = N->getLocation(); |
| 1695 | |
| 1696 | switch (Loc.getKind()) { |
| 1697 | case ProgramPoint::BlockEntranceKind: |
| 1698 | Out << "Block Entrance: B" |
| 1699 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1700 | break; |
| 1701 | |
| 1702 | case ProgramPoint::BlockExitKind: |
| 1703 | assert (false); |
| 1704 | break; |
| 1705 | |
| 1706 | case ProgramPoint::PostStmtKind: { |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1707 | const PostStmt& L = cast<PostStmt>(Loc); |
| 1708 | Stmt* S = L.getStmt(); |
| 1709 | SourceLocation SLoc = S->getLocStart(); |
| 1710 | |
| 1711 | Out << S->getStmtClassName() << ' ' << (void*) S << ' '; |
| 1712 | S->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1713 | |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1714 | Out << "\\lline=" |
| 1715 | << GraphPrintSourceManager->getLineNumber(SLoc) << " col=" |
| 1716 | << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l"; |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1717 | |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1718 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1719 | Out << "\\|Implicit-Null Dereference.\\l"; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1720 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1721 | Out << "\\|Explicit-Null Dereference.\\l"; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1722 | else if (GraphPrintCheckerState->isUndefDeref(N)) |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1723 | Out << "\\|Dereference of undefialied value.\\l"; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1724 | else if (GraphPrintCheckerState->isUndefStore(N)) |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1725 | Out << "\\|Store to Undefined LVal."; |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1726 | else if (GraphPrintCheckerState->isExplicitBadDivide(N)) |
| 1727 | Out << "\\|Explicit divide-by zero or undefined value."; |
| 1728 | else if (GraphPrintCheckerState->isImplicitBadDivide(N)) |
| 1729 | Out << "\\|Implicit divide-by zero or undefined value."; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1730 | else if (GraphPrintCheckerState->isUndefResult(N)) |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1731 | Out << "\\|Result of operation is undefined."; |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1732 | else if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 1733 | Out << "\\|Call to function marked \"noreturn\"."; |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1734 | else if (GraphPrintCheckerState->isBadCall(N)) |
| 1735 | Out << "\\|Call to NULL/Undefined."; |
Ted Kremenek | 2ded35a | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 1736 | else if (GraphPrintCheckerState->isUndefArg(N)) |
| 1737 | Out << "\\|Argument in call is undefined"; |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1738 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1739 | break; |
| 1740 | } |
| 1741 | |
| 1742 | default: { |
| 1743 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1744 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1745 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1746 | |
| 1747 | if (Stmt* T = E.getSrc()->getTerminator()) { |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1748 | |
| 1749 | SourceLocation SLoc = T->getLocStart(); |
| 1750 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1751 | Out << "\\|Terminator: "; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1752 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1753 | E.getSrc()->printTerminator(Out); |
| 1754 | |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1755 | Out << "\\lline=" |
| 1756 | << GraphPrintSourceManager->getLineNumber(SLoc) << " col=" |
| 1757 | << GraphPrintSourceManager->getColumnNumber(SLoc); |
| 1758 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1759 | if (isa<SwitchStmt>(T)) { |
| 1760 | Stmt* Label = E.getDst()->getLabel(); |
| 1761 | |
| 1762 | if (Label) { |
| 1763 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1764 | Out << "\\lcase "; |
| 1765 | C->getLHS()->printPretty(Out); |
| 1766 | |
| 1767 | if (Stmt* RHS = C->getRHS()) { |
| 1768 | Out << " .. "; |
| 1769 | RHS->printPretty(Out); |
| 1770 | } |
| 1771 | |
| 1772 | Out << ":"; |
| 1773 | } |
| 1774 | else { |
| 1775 | assert (isa<DefaultStmt>(Label)); |
| 1776 | Out << "\\ldefault:"; |
| 1777 | } |
| 1778 | } |
| 1779 | else |
| 1780 | Out << "\\l(implicit) default:"; |
| 1781 | } |
| 1782 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1783 | // FIXME |
| 1784 | } |
| 1785 | else { |
| 1786 | Out << "\\lCondition: "; |
| 1787 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1788 | Out << "true"; |
| 1789 | else |
| 1790 | Out << "false"; |
| 1791 | } |
| 1792 | |
| 1793 | Out << "\\l"; |
| 1794 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1795 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1796 | if (GraphPrintCheckerState->isUndefControlFlow(N)) { |
| 1797 | Out << "\\|Control-flow based on\\lUndefined value.\\l"; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1798 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1802 | Out << "\\|StateID: " << (void*) N->getState() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1803 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1804 | N->getState()->printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1805 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1806 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1807 | return Out.str(); |
| 1808 | } |
| 1809 | }; |
| 1810 | } // end llvm namespace |
| 1811 | #endif |
| 1812 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1813 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1814 | #ifndef NDEBUG |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1815 | GraphPrintCheckerState = this; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1816 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1817 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1818 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame^] | 1819 | GraphPrintSourceManager = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1820 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1821 | } |