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