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