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