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