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 | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 180 | bool isFeasible = false; |
Ted Kremenek | 5c6eeb1 | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 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 | } |
Ted Kremenek | f1d623e | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 291 | else |
| 292 | V2 = V1; |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 293 | |
| 294 | // FIXME: Eventually we should replace the logic below with a range |
| 295 | // comparison, rather than concretize the values within the range. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 296 | // This should be easy once we have "ranges" for NonLVals. |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | f1d623e | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 298 | do { |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 299 | nonlval::ConcreteInt CaseVal(BasicVals.getValue(V1)); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 301 | RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 302 | |
| 303 | // Now "assume" that the case matches. |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 305 | bool isFeasible = false; |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 306 | ValueState* StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 307 | |
| 308 | if (isFeasible) { |
| 309 | builder.generateCaseStmtNode(I, StNew); |
| 310 | |
| 311 | // If CondV evaluates to a constant, then we know that this |
| 312 | // is the *only* case that we can take, so stop evaluating the |
| 313 | // others. |
| 314 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | // Now "assume" that the case doesn't match. Add this state |
| 319 | // to the default state (if it is feasible). |
| 320 | |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 321 | isFeasible = false; |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 322 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 323 | |
| 324 | if (isFeasible) |
| 325 | DefaultSt = StNew; |
| 326 | |
Ted Kremenek | f1d623e | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 327 | // Concretize the next value in the range. |
| 328 | if (V1 == V2) |
| 329 | break; |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | f1d623e | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 331 | ++V1; |
Ted Kremenek | 539269c | 2008-03-17 22:18:22 +0000 | [diff] [blame] | 332 | assert (V1 <= V2); |
Ted Kremenek | f1d623e | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 333 | |
| 334 | } while (true); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // If we reach here, than we know that the default branch is |
| 338 | // possible. |
| 339 | builder.generateDefaultCaseNode(DefaultSt); |
| 340 | } |
| 341 | |
| 342 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 343 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 344 | NodeSet& Dst) { |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 346 | assert (B->getOpcode() == BinaryOperator::LAnd || |
| 347 | B->getOpcode() == BinaryOperator::LOr); |
| 348 | |
| 349 | assert (B == CurrentStmt && getCFG().isBlkExpr(B)); |
| 350 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 351 | ValueState* St = GetState(Pred); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 352 | RVal X = GetBlkExprRVal(St, B); |
| 353 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 354 | assert (X.isUndef()); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 356 | Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData(); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 357 | |
| 358 | assert (Ex); |
| 359 | |
| 360 | if (Ex == B->getRHS()) { |
| 361 | |
| 362 | X = GetBlkExprRVal(St, Ex); |
| 363 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 364 | // Handle undefined values. |
Ted Kremenek | 5f2eb19 | 2008-02-26 19:40:44 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 366 | if (X.isUndef()) { |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 367 | MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X)); |
Ted Kremenek | 5f2eb19 | 2008-02-26 19:40:44 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 371 | // We took the RHS. Because the value of the '&&' or '||' expression must |
| 372 | // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0 |
| 373 | // or 1. Alternatively, we could take a lazy approach, and calculate this |
| 374 | // value later when necessary. We don't have the machinery in place for |
| 375 | // this right now, and since most logical expressions are used for branches, |
| 376 | // the payoff is not likely to be large. Instead, we do eager evaluation. |
| 377 | |
| 378 | bool isFeasible = false; |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 379 | ValueState* NewState = Assume(St, X, true, isFeasible); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 380 | |
| 381 | if (isFeasible) |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 382 | MakeNode(Dst, B, Pred, |
| 383 | SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B))); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 384 | |
| 385 | isFeasible = false; |
| 386 | NewState = Assume(St, X, false, isFeasible); |
| 387 | |
| 388 | if (isFeasible) |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 389 | MakeNode(Dst, B, Pred, |
| 390 | SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B))); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 391 | } |
| 392 | else { |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 393 | // We took the LHS expression. Depending on whether we are '&&' or |
| 394 | // '||' we know what the value of the expression is via properties of |
| 395 | // the short-circuiting. |
| 396 | |
| 397 | X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B); |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 398 | MakeNode(Dst, B, Pred, SetBlkExprRVal(St, B, X)); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 399 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 400 | } |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 402 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 403 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 405 | Builder = &builder; |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 406 | StmtEntryNode = builder.getLastNode(); |
| 407 | CurrentStmt = S; |
| 408 | NodeSet Dst; |
Ted Kremenek | 0233146 | 2008-03-09 18:28:41 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 410 | // Set up our simple checks. |
| 411 | |
| 412 | if (!MsgExprChecks.empty()) |
| 413 | Builder->setObjCMsgExprAuditors( |
| 414 | (GRNodeAuditor<ValueState>**) &MsgExprChecks[0], |
| 415 | (GRNodeAuditor<ValueState>**) (&MsgExprChecks[0] + MsgExprChecks.size())); |
| 416 | |
| 417 | |
| 418 | if (!CallChecks.empty()) |
| 419 | Builder->setCallExprAuditors( |
| 420 | (GRNodeAuditor<ValueState>**) &CallChecks[0], |
| 421 | (GRNodeAuditor<ValueState>**) (&CallChecks[0] + CallChecks.size())); |
| 422 | |
Ted Kremenek | 0233146 | 2008-03-09 18:28:41 +0000 | [diff] [blame] | 423 | // Create the cleaned state. |
| 424 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 425 | CleanedState = StateMgr.RemoveDeadBindings(StmtEntryNode->getState(), |
| 426 | CurrentStmt, Liveness); |
Ted Kremenek | a1d070b | 2008-03-10 04:45:00 +0000 | [diff] [blame] | 427 | |
| 428 | Builder->SetCleanedState(CleanedState); |
Ted Kremenek | 0233146 | 2008-03-09 18:28:41 +0000 | [diff] [blame] | 429 | |
| 430 | // Visit the statement. |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 431 | |
| 432 | Visit(S, StmtEntryNode, Dst); |
| 433 | |
| 434 | // If no nodes were generated, generate a new node that has all the |
| 435 | // dead mappings removed. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 437 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) |
| 438 | builder.generateNode(S, GetState(StmtEntryNode), StmtEntryNode); |
Ted Kremenek | a57214f | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 439 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 440 | // NULL out these variables to cleanup. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 442 | CurrentStmt = NULL; |
| 443 | StmtEntryNode = NULL; |
| 444 | Builder = NULL; |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 445 | CleanedState = NULL; |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Ted Kremenek | 6d40992 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 448 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 449 | |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 450 | if (D != CurrentStmt) { |
| 451 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | // If we are here, we are loading the value of the decl and binding |
| 456 | // it to the block-level expression. |
| 457 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 458 | ValueState* St = GetState(Pred); |
Ted Kremenek | f97c668 | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 459 | RVal X = RVal::MakeVal(BasicVals, D); |
| 460 | RVal Y = isa<lval::DeclVal>(X) ? GetRVal(St, cast<lval::DeclVal>(X)) : X; |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 461 | MakeNode(Dst, D, Pred, SetBlkExprRVal(St, D, Y)); |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 464 | void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 465 | CallExpr::arg_iterator AI, |
| 466 | CallExpr::arg_iterator AE, |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 467 | NodeSet& Dst) { |
| 468 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 469 | // Process the arguments. |
| 470 | |
| 471 | if (AI != AE) { |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | ef7ea07 | 2008-03-04 00:56:45 +0000 | [diff] [blame] | 473 | NodeSet DstTmp; |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 474 | Visit(*AI, Pred, DstTmp); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 475 | ++AI; |
| 476 | |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 477 | for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI) |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 478 | VisitCall(CE, *DI, AI, AE, Dst); |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 479 | |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | // If we reach here we have processed all of the arguments. Evaluate |
| 484 | // the callee expression. |
Ted Kremenek | cda2efd | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | c71901d | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 486 | NodeSet DstTmp; |
| 487 | Expr* Callee = CE->getCallee()->IgnoreParenCasts(); |
Ted Kremenek | cda2efd | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 488 | |
Ted Kremenek | c71901d | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 489 | VisitLVal(Callee, Pred, DstTmp); |
Ted Kremenek | cda2efd | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 490 | |
| 491 | if (DstTmp.empty()) |
| 492 | DstTmp.Add(Pred); |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 493 | |
| 494 | // Finally, evaluate the function call. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 495 | for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) { |
| 496 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 497 | ValueState* St = GetState(*DI); |
Ted Kremenek | c71901d | 2008-02-25 21:16:03 +0000 | [diff] [blame] | 498 | RVal L = GetLVal(St, Callee); |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | cda2efd | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 500 | // FIXME: Add support for symbolic function calls (calls involving |
| 501 | // function pointer values that are symbolic). |
| 502 | |
| 503 | // Check for undefined control-flow or calls to NULL. |
| 504 | |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 505 | if (L.isUndef() || isa<lval::ConcreteInt>(L)) { |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 506 | NodeTy* N = Builder->generateNode(CE, St, *DI); |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 507 | |
Ted Kremenek | 9b31f5b | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 508 | if (N) { |
| 509 | N->markAsSink(); |
| 510 | BadCalls.insert(N); |
| 511 | } |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 512 | |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 513 | continue; |
Ted Kremenek | b451dd3 | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Check for the "noreturn" attribute. |
| 517 | |
| 518 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 519 | |
Ted Kremenek | 02b1ff7 | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 520 | if (isa<lval::FuncVal>(L)) { |
| 521 | |
| 522 | FunctionDecl* FD = cast<lval::FuncVal>(L).getDecl(); |
| 523 | |
| 524 | if (FD->getAttr<NoReturnAttr>()) |
Ted Kremenek | b451dd3 | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 525 | Builder->BuildSinks = true; |
Ted Kremenek | 02b1ff7 | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 526 | else { |
| 527 | // HACK: Some functions are not marked noreturn, and don't return. |
| 528 | // Here are a few hardwired ones. If this takes too long, we can |
| 529 | // potentially cache these results. |
| 530 | const char* s = FD->getIdentifier()->getName(); |
| 531 | unsigned n = strlen(s); |
| 532 | |
| 533 | switch (n) { |
| 534 | default: |
| 535 | break; |
Ted Kremenek | 550025b | 2008-03-14 23:25:49 +0000 | [diff] [blame] | 536 | |
Ted Kremenek | 02b1ff7 | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 537 | case 4: |
Ted Kremenek | 550025b | 2008-03-14 23:25:49 +0000 | [diff] [blame] | 538 | if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true; |
| 539 | break; |
| 540 | |
| 541 | case 5: |
| 542 | if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true; |
| 543 | break; |
Ted Kremenek | 02b1ff7 | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | } |
Ted Kremenek | b451dd3 | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 547 | |
| 548 | // Evaluate the call. |
| 549 | |
Ted Kremenek | cda2efd | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 550 | |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 551 | bool invalidateArgs = false; |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 552 | |
Ted Kremenek | b9a20e0 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 553 | if (L.isUnknown()) { |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 554 | // Check for an "unknown" callee. |
| 555 | invalidateArgs = true; |
| 556 | } |
| 557 | else if (isa<lval::FuncVal>(L)) { |
| 558 | |
| 559 | IdentifierInfo* Info = cast<lval::FuncVal>(L).getDecl()->getIdentifier(); |
| 560 | |
Ted Kremenek | 21581c6 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 561 | if (unsigned id = Info->getBuiltinID()) { |
| 562 | switch (id) { |
| 563 | case Builtin::BI__builtin_expect: { |
| 564 | // For __builtin_expect, just return the value of the subexpression. |
| 565 | assert (CE->arg_begin() != CE->arg_end()); |
| 566 | RVal X = GetRVal(St, *(CE->arg_begin())); |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 567 | MakeNode(Dst, CE, *DI, SetRVal(St, CE, X)); |
Ted Kremenek | 21581c6 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 568 | continue; |
| 569 | } |
| 570 | |
| 571 | default: |
| 572 | invalidateArgs = true; |
| 573 | break; |
| 574 | } |
| 575 | } |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | if (invalidateArgs) { |
Ted Kremenek | b9a20e0 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 579 | // Invalidate all arguments passed in by reference (LVals). |
| 580 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 581 | I != E; ++I) { |
| 582 | RVal V = GetRVal(St, *I); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 583 | |
Ted Kremenek | b9a20e0 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 584 | if (isa<LVal>(V)) |
| 585 | St = SetRVal(St, cast<LVal>(V), UnknownVal()); |
Ted Kremenek | b451dd3 | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 588 | MakeNode(Dst, CE, *DI, St); |
Ted Kremenek | b9a20e0 | 2008-02-21 19:46:04 +0000 | [diff] [blame] | 589 | } |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 590 | else { |
| 591 | |
| 592 | // Check any arguments passed-by-value against being undefined. |
| 593 | |
| 594 | bool badArg = false; |
| 595 | |
| 596 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
| 597 | I != E; ++I) { |
| 598 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 599 | if (GetRVal(GetState(*DI), *I).isUndef()) { |
| 600 | NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI); |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 601 | |
| 602 | if (N) { |
| 603 | N->markAsSink(); |
| 604 | UndefArgs[N] = *I; |
| 605 | } |
| 606 | |
| 607 | badArg = true; |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (badArg) |
| 613 | continue; |
| 614 | |
| 615 | // Dispatch to the plug-in transfer function. |
Ted Kremenek | 3eea8dd | 2008-03-05 00:33:14 +0000 | [diff] [blame] | 616 | |
| 617 | unsigned size = Dst.size(); |
| 618 | |
| 619 | EvalCall(Dst, CE, cast<LVal>(L), *DI); |
| 620 | |
Ted Kremenek | a2822eb | 2008-03-05 22:49:16 +0000 | [diff] [blame] | 621 | if (!Builder->BuildSinks && Dst.size() == size) |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 622 | MakeNode(Dst, CE, *DI, St); |
Ted Kremenek | 769f348 | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 623 | } |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 627 | void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 629 | NodeSet S1; |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 630 | QualType T = CastE->getType(); |
| 631 | |
Ted Kremenek | 1d1b6c9 | 2008-03-04 22:16:08 +0000 | [diff] [blame] | 632 | if (T->isReferenceType()) |
| 633 | VisitLVal(Ex, Pred, S1); |
| 634 | else |
| 635 | Visit(Ex, Pred, S1); |
| 636 | |
Ted Kremenek | 5a9b6aa | 2008-02-19 18:47:04 +0000 | [diff] [blame] | 637 | // Check for redundant casts or casting to "void" |
| 638 | if (T->isVoidType() || |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 639 | Ex->getType() == T || |
| 640 | (T->isPointerType() && Ex->getType()->isFunctionType())) { |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 641 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 642 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) |
Ted Kremenek | 5f585b0 | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 643 | Dst.Add(*I1); |
| 644 | |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 648 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 649 | NodeTy* N = *I1; |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 650 | ValueState* St = GetState(N); |
Ted Kremenek | 1d1b6c9 | 2008-03-04 22:16:08 +0000 | [diff] [blame] | 651 | |
| 652 | RVal V = T->isReferenceType() ? GetLVal(St, Ex) : GetRVal(St, Ex); |
| 653 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 654 | MakeNode(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType()))); |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 655 | } |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 658 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 659 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 660 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 661 | ValueState* St = GetState(Pred); |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 662 | |
| 663 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 8c9bc34 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 664 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | e025800 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 665 | |
| 666 | // FIXME: Add support for local arrays. |
| 667 | if (VD->getType()->isArrayType()) |
| 668 | continue; |
| 669 | |
Ted Kremenek | 04ab4c1 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 670 | const Expr* Ex = VD->getInit(); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 671 | |
Ted Kremenek | 04ab4c1 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 672 | if (!VD->hasGlobalStorage() || VD->getStorageClass() == VarDecl::Static) { |
| 673 | |
| 674 | // In this context, Static => Local variable. |
| 675 | |
| 676 | assert (!VD->getStorageClass() == VarDecl::Static || |
| 677 | !isa<FileVarDecl>(VD)); |
| 678 | |
| 679 | // If there is no initializer, set the value of the |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 680 | // variable to "Undefined". |
Ted Kremenek | 04ab4c1 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 681 | // |
| 682 | // FIXME: static variables may have an initializer, but the second |
| 683 | // time a function is called those values may not be current. |
Ted Kremenek | 98d093b | 2008-03-04 20:40:11 +0000 | [diff] [blame] | 684 | |
| 685 | QualType T = VD->getType(); |
Ted Kremenek | 04ab4c1 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 686 | |
Ted Kremenek | a6cfcc9 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 687 | if ( VD->getStorageClass() == VarDecl::Static) { |
Ted Kremenek | a6cfcc9 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 688 | |
| 689 | // C99: 6.7.8 Initialization |
| 690 | // If an object that has static storage duration is not initialized |
| 691 | // explicitly, then: |
| 692 | // —if it has pointer type, it is initialized to a null pointer; |
| 693 | // —if it has arithmetic type, it is initialized to (positive or |
| 694 | // unsigned) zero; |
| 695 | |
Ted Kremenek | 98d093b | 2008-03-04 20:40:11 +0000 | [diff] [blame] | 696 | // FIXME: Handle structs. Now we treat their values as unknown. |
| 697 | |
Ted Kremenek | a6cfcc9 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 698 | if (T->isPointerType()) { |
| 699 | |
| 700 | St = SetRVal(St, lval::DeclVal(VD), |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 701 | lval::ConcreteInt(BasicVals.getValue(0, T))); |
Ted Kremenek | a6cfcc9 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 702 | } |
| 703 | else if (T->isIntegerType()) { |
| 704 | |
| 705 | St = SetRVal(St, lval::DeclVal(VD), |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 706 | nonlval::ConcreteInt(BasicVals.getValue(0, T))); |
Ted Kremenek | a6cfcc9 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Ted Kremenek | 98d093b | 2008-03-04 20:40:11 +0000 | [diff] [blame] | 709 | |
Ted Kremenek | a6cfcc9 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 710 | } |
Ted Kremenek | 98d093b | 2008-03-04 20:40:11 +0000 | [diff] [blame] | 711 | else { |
Ted Kremenek | a6cfcc9 | 2008-03-04 04:18:04 +0000 | [diff] [blame] | 712 | |
Ted Kremenek | 98d093b | 2008-03-04 20:40:11 +0000 | [diff] [blame] | 713 | // FIXME: Handle structs. Now we treat them as unknown. What |
| 714 | // we need to do is treat their members as unknown. |
| 715 | |
| 716 | if (T->isPointerType() || T->isIntegerType()) |
| 717 | St = SetRVal(St, lval::DeclVal(VD), |
| 718 | Ex ? GetRVal(St, Ex) : UndefinedVal()); |
| 719 | } |
Ted Kremenek | 04ab4c1 | 2008-02-27 19:21:33 +0000 | [diff] [blame] | 720 | } |
Ted Kremenek | 8c9bc34 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 721 | } |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 722 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 723 | MakeNode(Dst, DS, Pred, St); |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 724 | } |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 725 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 726 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 727 | void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R, |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 728 | NodeTy* Pred, NodeSet& Dst) { |
| 729 | |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 730 | assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex)); |
| 731 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 732 | ValueState* St = GetState(Pred); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 733 | RVal X = GetBlkExprRVal(St, Ex); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 734 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 735 | assert (X.isUndef()); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 736 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 737 | Expr* SE = (Expr*) cast<UndefinedVal>(X).getData(); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 738 | |
| 739 | assert (SE); |
| 740 | |
| 741 | X = GetBlkExprRVal(St, SE); |
Ted Kremenek | ad5d5c5 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 742 | |
| 743 | // Make sure that we invalidate the previous binding. |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 744 | MakeNode(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true)); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 747 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 748 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex, |
| 749 | NodeTy* Pred, |
| 750 | NodeSet& Dst) { |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 751 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 752 | QualType T = Ex->getArgumentType(); |
Ted Kremenek | c3b1283 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 753 | uint64_t amt; |
| 754 | |
| 755 | if (Ex->isSizeOf()) { |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 756 | |
Ted Kremenek | c3b1283 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 757 | // FIXME: Add support for VLAs. |
| 758 | if (!T.getTypePtr()->isConstantSizeType()) |
| 759 | return; |
| 760 | |
| 761 | amt = 1; // Handle sizeof(void) |
| 762 | |
| 763 | if (T != getContext().VoidTy) |
| 764 | amt = getContext().getTypeSize(T) / 8; |
| 765 | |
| 766 | } |
| 767 | else // Get alignment of the type. |
Ted Kremenek | 8eac9c0 | 2008-03-15 03:13:55 +0000 | [diff] [blame] | 768 | amt = getContext().getTypeAlign(T) / 8; |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 769 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 770 | MakeNode(Dst, Ex, Pred, |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 771 | SetRVal(GetState(Pred), Ex, |
Ted Kremenek | c3b1283 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 772 | NonLVal::MakeVal(BasicVals, amt, Ex->getType()))); |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 775 | void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, |
| 776 | NodeSet& Dst, bool GetLVal) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 777 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 778 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 779 | |
| 780 | NodeSet DstTmp; |
| 781 | |
Ted Kremenek | 56a8999 | 2008-02-26 03:44:25 +0000 | [diff] [blame] | 782 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 783 | DstTmp.Add(Pred); |
| 784 | else |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 785 | Visit(Ex, Pred, DstTmp); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 786 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 787 | for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 788 | |
| 789 | NodeTy* N = *I; |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 790 | ValueState* St = GetState(N); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 791 | |
| 792 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 793 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 794 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 795 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 796 | // Check for dereferences of undefined values. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 797 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 798 | if (V.isUndef()) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 799 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 800 | NodeTy* Succ = Builder->generateNode(U, St, N); |
| 801 | |
| 802 | if (Succ) { |
| 803 | Succ->markAsSink(); |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 804 | UndefDeref.insert(Succ); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | continue; |
| 808 | } |
| 809 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 810 | // Check for dereferences of unknown values. Treat as No-Ops. |
| 811 | |
| 812 | if (V.isUnknown()) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 813 | Dst.Add(N); |
| 814 | continue; |
| 815 | } |
| 816 | |
| 817 | // After a dereference, one of two possible situations arise: |
| 818 | // (1) A crash, because the pointer was NULL. |
| 819 | // (2) The pointer is not NULL, and the dereference works. |
| 820 | // |
| 821 | // We add these assumptions. |
| 822 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 823 | LVal LV = cast<LVal>(V); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 824 | bool isFeasibleNotNull; |
| 825 | |
| 826 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 827 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 828 | ValueState* StNotNull = Assume(St, LV, true, isFeasibleNotNull); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 829 | |
| 830 | if (isFeasibleNotNull) { |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 831 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 832 | if (GetLVal) MakeNode(Dst, U, N, SetRVal(StNotNull, U, LV)); |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 833 | else { |
| 834 | |
| 835 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 836 | // for the contents of values. We need a better approach. |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 838 | MakeNode(Dst, U, N, SetRVal(StNotNull, U, |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 839 | GetRVal(StNotNull, LV, U->getType()))); |
| 840 | } |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | bool isFeasibleNull; |
| 844 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 845 | // Now "assume" that the pointer is NULL. |
| 846 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 847 | ValueState* StNull = Assume(St, LV, false, isFeasibleNull); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 848 | |
| 849 | if (isFeasibleNull) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 850 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 851 | // We don't use "MakeNode" here because the node will be a sink |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 852 | // and we have no intention of processing it later. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 853 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 854 | NodeTy* NullNode = Builder->generateNode(U, StNull, N); |
| 855 | |
| 856 | if (NullNode) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 857 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 858 | NullNode->markAsSink(); |
| 859 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 860 | if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode); |
| 861 | else ExplicitNullDeref.insert(NullNode); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 867 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred, |
| 868 | NodeSet& Dst) { |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 869 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 870 | NodeSet S1; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 871 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 872 | assert (U->getOpcode() != UnaryOperator::Deref); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 873 | assert (U->getOpcode() != UnaryOperator::SizeOf); |
| 874 | assert (U->getOpcode() != UnaryOperator::AlignOf); |
| 875 | |
| 876 | bool use_GetLVal = false; |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 877 | |
| 878 | switch (U->getOpcode()) { |
| 879 | case UnaryOperator::PostInc: |
| 880 | case UnaryOperator::PostDec: |
| 881 | case UnaryOperator::PreInc: |
| 882 | case UnaryOperator::PreDec: |
| 883 | case UnaryOperator::AddrOf: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 884 | // Evalue subexpression as an LVal. |
| 885 | use_GetLVal = true; |
| 886 | VisitLVal(U->getSubExpr(), Pred, S1); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 887 | break; |
| 888 | |
| 889 | default: |
| 890 | Visit(U->getSubExpr(), Pred, S1); |
| 891 | break; |
| 892 | } |
| 893 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 894 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 895 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 896 | NodeTy* N1 = *I1; |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 897 | ValueState* St = GetState(N1); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 898 | |
| 899 | RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) : |
| 900 | GetRVal(St, U->getSubExpr()); |
| 901 | |
| 902 | if (SubV.isUnknown()) { |
| 903 | Dst.Add(N1); |
| 904 | continue; |
| 905 | } |
| 906 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 907 | if (SubV.isUndef()) { |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 908 | MakeNode(Dst, U, N1, SetRVal(St, U, SubV)); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 909 | continue; |
| 910 | } |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 911 | |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 912 | if (U->isIncrementDecrementOp()) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 913 | |
| 914 | // Handle ++ and -- (both pre- and post-increment). |
| 915 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 916 | LVal SubLV = cast<LVal>(SubV); |
| 917 | RVal V = GetRVal(St, SubLV, U->getType()); |
| 918 | |
Ted Kremenek | b8782e1 | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 919 | if (V.isUnknown()) { |
| 920 | Dst.Add(N1); |
| 921 | continue; |
| 922 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 923 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 924 | // Propagate undefined values. |
| 925 | if (V.isUndef()) { |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 926 | MakeNode(Dst, U, N1, SetRVal(St, U, V)); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 927 | continue; |
| 928 | } |
| 929 | |
Ted Kremenek | b1669d4 | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 930 | // Handle all other values. |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 931 | |
| 932 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 933 | : BinaryOperator::Sub; |
| 934 | |
Ted Kremenek | b1669d4 | 2008-02-21 19:29:23 +0000 | [diff] [blame] | 935 | RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U)); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 936 | |
| 937 | if (U->isPostfix()) |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 938 | St = SetRVal(SetRVal(St, U, V), SubLV, Result); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 939 | else |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 940 | St = SetRVal(SetRVal(St, U, Result), SubLV, Result); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 941 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 942 | MakeNode(Dst, U, N1, St); |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 943 | continue; |
| 944 | } |
| 945 | |
| 946 | // Handle all other unary operators. |
| 947 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 948 | switch (U->getOpcode()) { |
Ted Kremenek | 3c53607 | 2008-03-15 03:05:30 +0000 | [diff] [blame] | 949 | |
| 950 | case UnaryOperator::Extension: |
| 951 | St = SetRVal(St, U, SubV); |
| 952 | break; |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 953 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 954 | case UnaryOperator::Minus: |
| 955 | St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV))); |
Ted Kremenek | cacd736 | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 956 | break; |
Ted Kremenek | cacd736 | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 957 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 958 | case UnaryOperator::Not: |
| 959 | St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV))); |
Ted Kremenek | 0cd9635 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 960 | break; |
Ted Kremenek | 0cd9635 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 961 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 962 | case UnaryOperator::LNot: |
Ted Kremenek | 2cb4664 | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 963 | |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 964 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 965 | // |
| 966 | // Note: technically we do "E == 0", but this is the same in the |
| 967 | // transfer functions as "0 == E". |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 968 | |
| 969 | if (isa<LVal>(SubV)) { |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 970 | lval::ConcreteInt V(BasicVals.getZeroWithPtrWidth()); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 971 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V); |
| 972 | St = SetRVal(St, U, Result); |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 973 | } |
| 974 | else { |
Ted Kremenek | a089467 | 2008-02-22 00:42:36 +0000 | [diff] [blame] | 975 | Expr* Ex = U->getSubExpr(); |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 976 | nonlval::ConcreteInt V(BasicVals.getValue(0, Ex->getType())); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 977 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V); |
| 978 | St = SetRVal(St, U, Result); |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | break; |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 982 | |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 983 | case UnaryOperator::AddrOf: { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 984 | assert (isa<LVal>(SubV)); |
| 985 | St = SetRVal(St, U, SubV); |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 986 | break; |
| 987 | } |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 988 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 989 | default: ; |
| 990 | assert (false && "Not implemented."); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 993 | MakeNode(Dst, U, N1, St); |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 997 | void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred, |
| 998 | NodeSet& Dst) { |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 999 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1000 | QualType T = U->getSubExpr()->getType(); |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1001 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1002 | // FIXME: Add support for VLAs. |
| 1003 | if (!T.getTypePtr()->isConstantSizeType()) |
| 1004 | return; |
| 1005 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1006 | uint64_t size = getContext().getTypeSize(T) / 8; |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1007 | ValueState* St = GetState(Pred); |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1008 | St = SetRVal(St, U, NonLVal::MakeVal(BasicVals, size, U->getType())); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1009 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1010 | MakeNode(Dst, U, Pred, St); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | a129a2e | 2008-02-27 07:04:16 +0000 | [diff] [blame] | 1014 | |
| 1015 | if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) { |
| 1016 | Dst.Add(Pred); |
| 1017 | return; |
| 1018 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1019 | |
| 1020 | Ex = Ex->IgnoreParens(); |
| 1021 | |
Ted Kremenek | b5175bf | 2008-02-27 06:47:26 +0000 | [diff] [blame] | 1022 | if (isa<DeclRefExpr>(Ex)) { |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 1023 | Dst.Add(Pred); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1024 | return; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 1025 | } |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1026 | |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 1027 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1028 | if (U->getOpcode() == UnaryOperator::Deref) { |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 1029 | VisitDeref(U, Pred, Dst, true); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1030 | return; |
| 1031 | } |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1032 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1033 | Visit(Ex, Pred, Dst); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Ted Kremenek | 31803c3 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 1036 | void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) { |
| 1037 | VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst); |
| 1038 | } |
| 1039 | |
| 1040 | void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A, |
| 1041 | AsmStmt::outputs_iterator I, |
| 1042 | AsmStmt::outputs_iterator E, |
| 1043 | NodeTy* Pred, NodeSet& Dst) { |
| 1044 | if (I == E) { |
| 1045 | VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst); |
| 1046 | return; |
| 1047 | } |
| 1048 | |
| 1049 | NodeSet Tmp; |
| 1050 | VisitLVal(*I, Pred, Tmp); |
| 1051 | |
| 1052 | ++I; |
| 1053 | |
| 1054 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
| 1055 | VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst); |
| 1056 | } |
| 1057 | |
| 1058 | void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A, |
| 1059 | AsmStmt::inputs_iterator I, |
| 1060 | AsmStmt::inputs_iterator E, |
| 1061 | NodeTy* Pred, NodeSet& Dst) { |
| 1062 | if (I == E) { |
| 1063 | |
| 1064 | // We have processed both the inputs and the outputs. All of the outputs |
| 1065 | // should evaluate to LVals. Nuke all of their values. |
| 1066 | |
| 1067 | // FIXME: Some day in the future it would be nice to allow a "plug-in" |
| 1068 | // which interprets the inline asm and stores proper results in the |
| 1069 | // outputs. |
| 1070 | |
| 1071 | ValueState* St = GetState(Pred); |
| 1072 | |
| 1073 | for (AsmStmt::outputs_iterator OI = A->begin_outputs(), |
| 1074 | OE = A->end_outputs(); OI != OE; ++OI) { |
| 1075 | |
| 1076 | RVal X = GetLVal(St, *OI); |
| 1077 | |
| 1078 | assert (!isa<NonLVal>(X)); |
| 1079 | |
| 1080 | if (isa<LVal>(X)) |
| 1081 | St = SetRVal(St, cast<LVal>(X), UnknownVal()); |
| 1082 | } |
| 1083 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1084 | MakeNode(Dst, A, Pred, St); |
Ted Kremenek | 31803c3 | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 1085 | return; |
| 1086 | } |
| 1087 | |
| 1088 | NodeSet Tmp; |
| 1089 | Visit(*I, Pred, Tmp); |
| 1090 | |
| 1091 | ++I; |
| 1092 | |
| 1093 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
| 1094 | VisitAsmStmtHelperInputs(A, I, E, *NI, Dst); |
| 1095 | } |
| 1096 | |
| 1097 | |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1098 | void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred, |
| 1099 | NodeSet& Dst){ |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1100 | VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(), Pred, Dst); |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1103 | void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME, |
Ted Kremenek | 4b2bdd5 | 2008-03-25 02:10:28 +0000 | [diff] [blame] | 1104 | ObjCMessageExpr::arg_iterator AI, |
| 1105 | ObjCMessageExpr::arg_iterator AE, |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1106 | NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 4b2bdd5 | 2008-03-25 02:10:28 +0000 | [diff] [blame] | 1107 | if (AI == AE) { |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1108 | |
| 1109 | // Process the receiver. |
| 1110 | |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1111 | if (Expr* Receiver = ME->getReceiver()) { |
| 1112 | NodeSet Tmp; |
Ted Kremenek | 2380f37 | 2008-03-26 21:36:08 +0000 | [diff] [blame] | 1113 | Visit(Receiver, Pred, Tmp); |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1114 | |
| 1115 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
| 1116 | VisitObjCMessageExprDispatchHelper(ME, *NI, Dst); |
Ted Kremenek | 4b2bdd5 | 2008-03-25 02:10:28 +0000 | [diff] [blame] | 1117 | |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1118 | return; |
Ted Kremenek | 4b2bdd5 | 2008-03-25 02:10:28 +0000 | [diff] [blame] | 1119 | } |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1120 | |
| 1121 | VisitObjCMessageExprDispatchHelper(ME, Pred, Dst); |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1122 | return; |
| 1123 | } |
| 1124 | |
| 1125 | NodeSet Tmp; |
Ted Kremenek | 4b2bdd5 | 2008-03-25 02:10:28 +0000 | [diff] [blame] | 1126 | Visit(*AI, Pred, Tmp); |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1127 | |
Ted Kremenek | 4b2bdd5 | 2008-03-25 02:10:28 +0000 | [diff] [blame] | 1128 | ++AI; |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1129 | |
| 1130 | for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1131 | VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst); |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1134 | void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME, |
| 1135 | NodeTy* Pred, NodeSet& Dst) { |
| 1136 | |
| 1137 | |
| 1138 | // FIXME: More logic for the processing the method call. |
| 1139 | |
| 1140 | ValueState* St = GetState(Pred); |
| 1141 | |
| 1142 | if (Expr* Receiver = ME->getReceiver()) { |
| 1143 | |
Ted Kremenek | 9ba5a1f | 2008-03-26 22:21:58 +0000 | [diff] [blame] | 1144 | RVal L = GetRVal(St, Receiver); |
Ted Kremenek | 7150747 | 2008-03-25 16:07:41 +0000 | [diff] [blame] | 1145 | |
| 1146 | // Check for undefined control-flow or calls to NULL. |
| 1147 | |
| 1148 | if (L.isUndef()) { |
| 1149 | NodeTy* N = Builder->generateNode(ME, St, Pred); |
| 1150 | |
| 1151 | if (N) { |
| 1152 | N->markAsSink(); |
| 1153 | UndefReceivers.insert(N); |
| 1154 | } |
| 1155 | |
| 1156 | return; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | // Check for any arguments that are uninitialized/undefined. |
| 1161 | |
| 1162 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 1163 | I != E; ++I) { |
| 1164 | |
| 1165 | if (GetRVal(St, *I).isUndef()) { |
| 1166 | |
| 1167 | NodeTy* N = Builder->generateNode(ME, St, Pred); |
| 1168 | |
| 1169 | if (N) { |
| 1170 | N->markAsSink(); |
| 1171 | MsgExprUndefArgs[N] = *I; |
| 1172 | } |
| 1173 | |
| 1174 | return; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | // FIXME: Eventually we will properly handle the effects of a message |
| 1179 | // expr. For now invalidate all arguments passed in by references. |
| 1180 | |
| 1181 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 1182 | I != E; ++I) { |
| 1183 | |
| 1184 | RVal V = GetRVal(St, *I); |
| 1185 | |
| 1186 | if (isa<LVal>(V)) |
| 1187 | St = SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 1188 | } |
| 1189 | |
| 1190 | MakeNode(Dst, ME, Pred, St); |
| 1191 | } |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1192 | |
| 1193 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1194 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1195 | GRExprEngine::NodeTy* Pred, |
| 1196 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1197 | NodeSet S1; |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (B->isAssignmentOp()) |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1200 | VisitLVal(B->getLHS(), Pred, S1); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1201 | else |
| 1202 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1203 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1204 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1205 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1206 | NodeTy* N1 = *I1; |
Ted Kremenek | e860db8 | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 1207 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1208 | // 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] | 1209 | // In such cases, we want to (initially) treat the LHS as an LVal, |
| 1210 | // so we use GetLVal instead of GetRVal so that DeclRefExpr's are |
| 1211 | // evaluated to LValDecl's instead of to an NonLVal. |
| 1212 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1213 | RVal LeftV = B->isAssignmentOp() ? GetLVal(GetState(N1), B->getLHS()) |
| 1214 | : GetRVal(GetState(N1), B->getLHS()); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1215 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1216 | // Visit the RHS... |
| 1217 | |
| 1218 | NodeSet S2; |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1219 | Visit(B->getRHS(), N1, S2); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Process the binary operator. |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1222 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1223 | for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1225 | NodeTy* N2 = *I2; |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1226 | ValueState* St = GetState(N2); |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1227 | Expr* RHS = B->getRHS(); |
| 1228 | RVal RightV = GetRVal(St, RHS); |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1229 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1230 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 1231 | |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1232 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 1233 | && RHS->getType()->isIntegerType()) { |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1234 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1235 | // Check if the denominator is undefined. |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1236 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1237 | if (!RightV.isUnknown()) { |
| 1238 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1239 | if (RightV.isUndef()) { |
| 1240 | NodeTy* DivUndef = Builder->generateNode(B, St, N2); |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1241 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1242 | if (DivUndef) { |
| 1243 | DivUndef->markAsSink(); |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1244 | ExplicitBadDivides.insert(DivUndef); |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | continue; |
| 1248 | } |
| 1249 | |
| 1250 | // Check for divide/remainder-by-zero. |
| 1251 | // |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1252 | // First, "assume" that the denominator is 0 or undefined. |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1253 | |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1254 | bool isFeasibleZero = false; |
| 1255 | ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero); |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1256 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1257 | // Second, "assume" that the denominator cannot be 0. |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1258 | |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1259 | bool isFeasibleNotZero = false; |
| 1260 | St = Assume(St, RightV, true, isFeasibleNotZero); |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1261 | |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1262 | // Create the node for the divide-by-zero (if it occurred). |
| 1263 | |
| 1264 | if (isFeasibleZero) |
| 1265 | if (NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2)) { |
| 1266 | DivZeroNode->markAsSink(); |
| 1267 | |
| 1268 | if (isFeasibleNotZero) |
| 1269 | ImplicitBadDivides.insert(DivZeroNode); |
| 1270 | else |
| 1271 | ExplicitBadDivides.insert(DivZeroNode); |
| 1272 | |
| 1273 | } |
| 1274 | |
| 1275 | if (!isFeasibleNotZero) |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1276 | continue; |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1279 | // Fall-through. The logic below processes the divide. |
| 1280 | } |
| 1281 | |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1282 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1283 | if (Op <= BinaryOperator::Or) { |
| 1284 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1285 | // Process non-assignements except commas or short-circuited |
| 1286 | // logical expressions (LAnd and LOr). |
| 1287 | |
| 1288 | RVal Result = EvalBinOp(Op, LeftV, RightV); |
| 1289 | |
| 1290 | if (Result.isUnknown()) { |
| 1291 | Dst.Add(N2); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1292 | continue; |
| 1293 | } |
| 1294 | |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1295 | if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) { |
| 1296 | |
| 1297 | // The operands were not undefined, but the result is undefined. |
| 1298 | |
| 1299 | if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) { |
| 1300 | UndefNode->markAsSink(); |
| 1301 | UndefResults.insert(UndefNode); |
| 1302 | } |
| 1303 | |
| 1304 | continue; |
| 1305 | } |
| 1306 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1307 | MakeNode(Dst, B, N2, SetRVal(St, B, Result)); |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1308 | continue; |
| 1309 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Process assignments. |
| 1312 | |
| 1313 | switch (Op) { |
| 1314 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1315 | case BinaryOperator::Assign: { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Simple assignments. |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1318 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1319 | if (LeftV.isUndef()) { |
| 1320 | HandleUndefinedStore(B, N2); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1321 | continue; |
| 1322 | } |
| 1323 | |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 1324 | // EXPERIMENTAL: "Conjured" symbols. |
| 1325 | |
| 1326 | if (RightV.isUnknown()) { |
| 1327 | unsigned Count = Builder->getCurrentBlockCount(); |
| 1328 | SymbolID Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count); |
| 1329 | |
| 1330 | RightV = B->getRHS()->getType()->isPointerType() |
| 1331 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 1332 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 1333 | } |
| 1334 | |
| 1335 | // Even if the LHS evaluates to an unknown L-Value, the entire |
| 1336 | // expression still evaluates to the RHS. |
| 1337 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1338 | if (LeftV.isUnknown()) { |
| 1339 | St = SetRVal(St, B, RightV); |
| 1340 | break; |
| 1341 | } |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Simulate the effects of a "store": bind the value of the RHS |
| 1344 | // to the L-Value represented by the LHS. |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1345 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1346 | St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV); |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1347 | break; |
| 1348 | } |
| 1349 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1350 | // Compound assignment operators. |
Ted Kremenek | 19072e0 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 1351 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1352 | default: { |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1353 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1354 | assert (B->isCompoundAssignmentOp()); |
| 1355 | |
| 1356 | if (Op >= BinaryOperator::AndAssign) |
| 1357 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 1358 | else |
| 1359 | ((int&) Op) -= BinaryOperator::MulAssign; |
| 1360 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1361 | // Check if the LHS is undefined. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1362 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1363 | if (LeftV.isUndef()) { |
| 1364 | HandleUndefinedStore(B, N2); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1365 | continue; |
| 1366 | } |
| 1367 | |
| 1368 | if (LeftV.isUnknown()) { |
Ted Kremenek | 80cb2c8 | 2008-03-09 08:12:37 +0000 | [diff] [blame] | 1369 | assert (isa<UnknownVal>(GetRVal(St, B))); |
| 1370 | Dst.Add(N2); |
| 1371 | continue; |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1374 | // At this pointer we know that the LHS evaluates to an LVal |
Ted Kremenek | 80cb2c8 | 2008-03-09 08:12:37 +0000 | [diff] [blame] | 1375 | // that is neither "Unknown" or "Undefined." |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1376 | |
| 1377 | LVal LeftLV = cast<LVal>(LeftV); |
| 1378 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1379 | // Fetch the value of the LHS (the value of the variable, etc.). |
| 1380 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1381 | RVal V = GetRVal(GetState(N1), LeftLV, B->getLHS()->getType()); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1382 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1383 | // Propagate undefined value (left-side). We |
| 1384 | // propogate undefined values for the RHS below when |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1385 | // we also check for divide-by-zero. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1386 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1387 | if (V.isUndef()) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1388 | St = SetRVal(St, B, V); |
| 1389 | break; |
| 1390 | } |
| 1391 | |
| 1392 | // Propagate unknown values. |
| 1393 | |
Ted Kremenek | b8782e1 | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 1394 | if (V.isUnknown()) { |
Ted Kremenek | 80cb2c8 | 2008-03-09 08:12:37 +0000 | [diff] [blame] | 1395 | // The value bound to LeftV is unknown. Thus we just |
| 1396 | // propagate the current node (as "B" is already bound to nothing). |
| 1397 | assert (isa<UnknownVal>(GetRVal(St, B))); |
Ted Kremenek | b8782e1 | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 1398 | Dst.Add(N2); |
| 1399 | continue; |
| 1400 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (RightV.isUnknown()) { |
Ted Kremenek | 80cb2c8 | 2008-03-09 08:12:37 +0000 | [diff] [blame] | 1403 | assert (isa<UnknownVal>(GetRVal(St, B))); |
| 1404 | St = SetRVal(St, LeftLV, UnknownVal()); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1405 | break; |
| 1406 | } |
| 1407 | |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1408 | // At this point: |
| 1409 | // |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1410 | // The LHS is not Undef/Unknown. |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1411 | // The RHS is not Unknown. |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1412 | |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1413 | // Get the computation type. |
| 1414 | QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType(); |
| 1415 | |
| 1416 | // Perform promotions. |
| 1417 | V = EvalCast(V, CTy); |
Ted Kremenek | 666ab92 | 2008-02-21 18:46:24 +0000 | [diff] [blame] | 1418 | RightV = EvalCast(RightV, CTy); |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1419 | |
| 1420 | // Evaluate operands and promote to result type. |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1421 | |
Ted Kremenek | 2c36979 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 1422 | if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem) |
| 1423 | && RHS->getType()->isIntegerType()) { |
| 1424 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1425 | // Check if the denominator is undefined. |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1426 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1427 | if (RightV.isUndef()) { |
| 1428 | NodeTy* DivUndef = Builder->generateNode(B, St, N2); |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1429 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1430 | if (DivUndef) { |
| 1431 | DivUndef->markAsSink(); |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1432 | ExplicitBadDivides.insert(DivUndef); |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | continue; |
| 1436 | } |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1437 | |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1438 | // First, "assume" that the denominator is 0. |
| 1439 | |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1440 | bool isFeasibleZero = false; |
| 1441 | ValueState* ZeroSt = Assume(St, RightV, false, isFeasibleZero); |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1442 | |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1443 | // Second, "assume" that the denominator cannot be 0. |
| 1444 | |
| 1445 | bool isFeasibleNotZero = false; |
| 1446 | St = Assume(St, RightV, true, isFeasibleNotZero); |
| 1447 | |
| 1448 | // Create the node for the divide-by-zero error (if it occurred). |
| 1449 | |
| 1450 | if (isFeasibleZero) { |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1451 | NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2); |
| 1452 | |
| 1453 | if (DivZeroNode) { |
| 1454 | DivZeroNode->markAsSink(); |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1455 | |
| 1456 | if (isFeasibleNotZero) |
| 1457 | ImplicitBadDivides.insert(DivZeroNode); |
| 1458 | else |
| 1459 | ExplicitBadDivides.insert(DivZeroNode); |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1463 | if (!isFeasibleNotZero) |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1464 | continue; |
| 1465 | |
| 1466 | // Fall-through. The logic below processes the divide. |
| 1467 | } |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1468 | else { |
| 1469 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1470 | // Propagate undefined values (right-side). |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1471 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1472 | if (RightV.isUndef()) { |
Ted Kremenek | 5a60086 | 2008-02-26 22:27:51 +0000 | [diff] [blame] | 1473 | St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV); |
| 1474 | break; |
| 1475 | } |
| 1476 | |
| 1477 | } |
Ted Kremenek | 6ff5f7f | 2008-02-25 17:51:31 +0000 | [diff] [blame] | 1478 | |
Ted Kremenek | 9cfda3f | 2008-02-21 18:43:30 +0000 | [diff] [blame] | 1479 | RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType()); |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1480 | |
| 1481 | if (Result.isUndef()) { |
| 1482 | |
| 1483 | // The operands were not undefined, but the result is undefined. |
| 1484 | |
| 1485 | if (NodeTy* UndefNode = Builder->generateNode(B, St, N2)) { |
| 1486 | UndefNode->markAsSink(); |
| 1487 | UndefResults.insert(UndefNode); |
| 1488 | } |
| 1489 | |
| 1490 | continue; |
| 1491 | } |
| 1492 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1493 | St = SetRVal(SetRVal(St, B, Result), LeftLV, Result); |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 1494 | } |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1495 | } |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1496 | |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1497 | MakeNode(Dst, B, N2, St); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 1498 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1499 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1500 | } |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1501 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1502 | void GRExprEngine::HandleUndefinedStore(Stmt* S, NodeTy* Pred) { |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1503 | NodeTy* N = Builder->generateNode(S, GetState(Pred), Pred); |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1504 | N->markAsSink(); |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1505 | UndefStores.insert(N); |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1506 | } |
Ted Kremenek | be96245 | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1507 | |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1508 | void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1509 | |
| 1510 | // FIXME: add metadata to the CFG so that we can disable |
| 1511 | // this check when we KNOW that there is no block-level subexpression. |
| 1512 | // The motivation is that this check requires a hashtable lookup. |
| 1513 | |
| 1514 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 1515 | Dst.Add(Pred); |
| 1516 | return; |
| 1517 | } |
| 1518 | |
| 1519 | switch (S->getStmtClass()) { |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1520 | |
| 1521 | default: |
| 1522 | // Cases we intentionally have "default" handle: |
Ted Kremenek | 9b496f9 | 2008-02-26 19:17:09 +0000 | [diff] [blame] | 1523 | // AddrLabelExpr, IntegerLiteral, CharacterLiteral |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1524 | |
| 1525 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1526 | break; |
Ted Kremenek | 52b8d0e | 2008-03-17 21:31:48 +0000 | [diff] [blame] | 1527 | |
| 1528 | case Stmt::AsmStmtClass: |
| 1529 | VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst); |
| 1530 | break; |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1531 | |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1532 | case Stmt::BinaryOperatorClass: { |
| 1533 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1535 | if (B->isLogicalOp()) { |
| 1536 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1537 | break; |
| 1538 | } |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1539 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1540 | ValueState* St = GetState(Pred); |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1541 | MakeNode(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS()))); |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1542 | break; |
| 1543 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1544 | |
| 1545 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1546 | break; |
| 1547 | } |
Ted Kremenek | d9268e3 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1548 | |
| 1549 | case Stmt::CallExprClass: { |
| 1550 | CallExpr* C = cast<CallExpr>(S); |
| 1551 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
| 1552 | break; |
| 1553 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1554 | |
| 1555 | case Stmt::CastExprClass: { |
| 1556 | CastExpr* C = cast<CastExpr>(S); |
| 1557 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1558 | break; |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1559 | } |
Ted Kremenek | 9b496f9 | 2008-02-26 19:17:09 +0000 | [diff] [blame] | 1560 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1561 | // FIXME: ChooseExpr is really a constant. We need to fix |
| 1562 | // the CFG do not model them as explicit control-flow. |
| 1563 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1564 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 1565 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1566 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1567 | break; |
| 1568 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1569 | |
Ted Kremenek | 9914e9c | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1570 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1571 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1572 | break; |
| 1573 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1574 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 1575 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 1576 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1577 | break; |
| 1578 | } |
| 1579 | |
| 1580 | case Stmt::DeclRefExprClass: |
| 1581 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 1582 | break; |
| 1583 | |
| 1584 | case Stmt::DeclStmtClass: |
| 1585 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1586 | break; |
| 1587 | |
| 1588 | case Stmt::ImplicitCastExprClass: { |
| 1589 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1590 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1591 | break; |
| 1592 | } |
Ted Kremenek | c6b7a1e | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 1593 | |
| 1594 | case Stmt::ObjCMessageExprClass: { |
| 1595 | VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst); |
| 1596 | break; |
| 1597 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1598 | |
| 1599 | case Stmt::ParenExprClass: |
| 1600 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1601 | break; |
| 1602 | |
| 1603 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 1604 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 1605 | break; |
| 1606 | |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1607 | case Stmt::StmtExprClass: { |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1608 | StmtExpr* SE = cast<StmtExpr>(S); |
| 1609 | |
Ted Kremenek | 50ef5ff | 2008-03-10 04:11:42 +0000 | [diff] [blame] | 1610 | ValueState* St = GetState(Pred); |
Ted Kremenek | a0a7c41 | 2008-03-15 03:27:30 +0000 | [diff] [blame] | 1611 | |
| 1612 | // FIXME: Not certain if we can have empty StmtExprs. If so, we should |
| 1613 | // probably just remove these from the CFG. |
| 1614 | assert (!SE->getSubStmt()->body_empty()); |
| 1615 | |
| 1616 | if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) |
Ted Kremenek | f10f288 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 1617 | MakeNode(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr))); |
Ted Kremenek | a0a7c41 | 2008-03-15 03:27:30 +0000 | [diff] [blame] | 1618 | else |
| 1619 | Dst.Add(Pred); |
| 1620 | |
| 1621 | break; |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1624 | // FIXME: We may wish to always bind state to ReturnStmts so |
| 1625 | // that users can quickly query what was the state at the |
| 1626 | // exit points of a function. |
| 1627 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1628 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1629 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1630 | Visit(R, Pred, Dst); |
| 1631 | else |
| 1632 | Dst.Add(Pred); |
| 1633 | |
| 1634 | break; |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1635 | } |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1636 | |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1637 | case Stmt::UnaryOperatorClass: { |
| 1638 | UnaryOperator* U = cast<UnaryOperator>(S); |
| 1639 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1640 | switch (U->getOpcode()) { |
| 1641 | case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break; |
| 1642 | case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break; |
| 1643 | case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break; |
| 1644 | default: VisitUnaryOperator(U, Pred, Dst); break; |
| 1645 | } |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1646 | |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1647 | break; |
Ted Kremenek | b996ebc | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1648 | } |
Ted Kremenek | ab8ed95 | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1649 | } |
Ted Kremenek | be96245 | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1652 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1653 | // "Assume" logic. |
| 1654 | //===----------------------------------------------------------------------===// |
| 1655 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1656 | ValueState* GRExprEngine::Assume(ValueState* St, LVal Cond, |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1657 | bool Assumption, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1658 | bool& isFeasible) { |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1659 | switch (Cond.getSubKind()) { |
| 1660 | default: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1661 | assert (false && "'Assume' not implemented for this LVal."); |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1662 | return St; |
| 1663 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1664 | case lval::SymbolValKind: |
| 1665 | if (Assumption) |
| 1666 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1667 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1668 | else |
| 1669 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1670 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1671 | |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1672 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1673 | case lval::DeclValKind: |
Ted Kremenek | 3870619 | 2008-02-22 00:54:56 +0000 | [diff] [blame] | 1674 | case lval::FuncValKind: |
| 1675 | case lval::GotoLabelKind: |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1676 | isFeasible = Assumption; |
| 1677 | return St; |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1678 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1679 | case lval::ConcreteIntKind: { |
| 1680 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1681 | isFeasible = b ? Assumption : !Assumption; |
| 1682 | return St; |
| 1683 | } |
| 1684 | } |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1687 | ValueState* GRExprEngine::Assume(ValueState* St, NonLVal Cond, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1688 | bool Assumption, |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1689 | bool& isFeasible) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1690 | switch (Cond.getSubKind()) { |
| 1691 | default: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1692 | assert (false && "'Assume' not implemented for this NonLVal."); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1693 | return St; |
| 1694 | |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1695 | |
| 1696 | case nonlval::SymbolValKind: { |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1697 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1698 | SymbolID sym = SV.getSymbol(); |
| 1699 | |
| 1700 | if (Assumption) |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1701 | return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1702 | isFeasible); |
| 1703 | else |
Ted Kremenek | 8ad1987 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1704 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1705 | isFeasible); |
| 1706 | } |
| 1707 | |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1708 | case nonlval::SymIntConstraintValKind: |
| 1709 | return |
| 1710 | AssumeSymInt(St, Assumption, |
| 1711 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1712 | isFeasible); |
| 1713 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1714 | case nonlval::ConcreteIntKind: { |
| 1715 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1716 | isFeasible = b ? Assumption : !Assumption; |
| 1717 | return St; |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1722 | ValueState* |
| 1723 | GRExprEngine::AssumeSymNE(ValueState* St, SymbolID sym, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1724 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1725 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1726 | // First, determine if sym == X, where X != V. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1727 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1728 | isFeasible = *X != V; |
| 1729 | return St; |
| 1730 | } |
| 1731 | |
| 1732 | // Second, determine if sym != V. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1733 | if (St->isNotEqual(sym, V)) { |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1734 | isFeasible = true; |
| 1735 | return St; |
| 1736 | } |
| 1737 | |
| 1738 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1739 | // Make that assumption. |
| 1740 | |
| 1741 | isFeasible = true; |
| 1742 | return StateMgr.AddNE(St, sym, V); |
| 1743 | } |
| 1744 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1745 | ValueState* |
| 1746 | GRExprEngine::AssumeSymEQ(ValueState* St, SymbolID sym, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1747 | const llvm::APSInt& V, bool& isFeasible) { |
| 1748 | |
| 1749 | // First, determine if sym == X, where X != V. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1750 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1751 | isFeasible = *X == V; |
| 1752 | return St; |
| 1753 | } |
| 1754 | |
| 1755 | // Second, determine if sym != V. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1756 | if (St->isNotEqual(sym, V)) { |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1757 | isFeasible = false; |
| 1758 | return St; |
| 1759 | } |
| 1760 | |
| 1761 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1762 | // Make that assumption. |
| 1763 | |
| 1764 | isFeasible = true; |
| 1765 | return StateMgr.AddEQ(St, sym, V); |
| 1766 | } |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1767 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1768 | ValueState* |
| 1769 | GRExprEngine::AssumeSymInt(ValueState* St, bool Assumption, |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1770 | const SymIntConstraint& C, bool& isFeasible) { |
| 1771 | |
| 1772 | switch (C.getOpcode()) { |
| 1773 | default: |
| 1774 | // No logic yet for other operators. |
Ted Kremenek | d467651 | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 1775 | isFeasible = true; |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1776 | return St; |
| 1777 | |
| 1778 | case BinaryOperator::EQ: |
| 1779 | if (Assumption) |
| 1780 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1781 | else |
| 1782 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1783 | |
| 1784 | case BinaryOperator::NE: |
| 1785 | if (Assumption) |
| 1786 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1787 | else |
| 1788 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1789 | } |
| 1790 | } |
| 1791 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1792 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1793 | // Visualization. |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1794 | //===----------------------------------------------------------------------===// |
| 1795 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1796 | #ifndef NDEBUG |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1797 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 8b41e8c | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 1798 | static SourceManager* GraphPrintSourceManager; |
Ted Kremenek | 9f59792 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 1799 | static ValueState::CheckerStatePrinter* GraphCheckerStatePrinter; |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1800 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1801 | namespace llvm { |
| 1802 | template<> |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1803 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1804 | public DefaultDOTGraphTraits { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1805 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1806 | static void PrintVarBindings(std::ostream& Out, ValueState* St) { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1807 | |
| 1808 | Out << "Variables:\\l"; |
| 1809 | |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1810 | bool isFirst = true; |
| 1811 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1812 | 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] | 1813 | |
| 1814 | if (isFirst) |
| 1815 | isFirst = false; |
| 1816 | else |
| 1817 | Out << "\\l"; |
| 1818 | |
| 1819 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1820 | I.getData().print(Out); |
| 1821 | } |
| 1822 | |
| 1823 | } |
| 1824 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1825 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1826 | static void PrintSubExprBindings(std::ostream& Out, ValueState* St){ |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1827 | |
| 1828 | bool isFirst = true; |
| 1829 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1830 | 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] | 1831 | |
| 1832 | if (isFirst) { |
| 1833 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1834 | isFirst = false; |
| 1835 | } |
| 1836 | else |
| 1837 | Out << "\\l"; |
| 1838 | |
| 1839 | Out << " (" << (void*) I.getKey() << ") "; |
| 1840 | I.getKey()->printPretty(Out); |
| 1841 | Out << " : "; |
| 1842 | I.getData().print(Out); |
| 1843 | } |
| 1844 | } |
| 1845 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1846 | static void PrintBlkExprBindings(std::ostream& Out, ValueState* St){ |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1847 | |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1848 | bool isFirst = true; |
| 1849 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1850 | 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] | 1851 | if (isFirst) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1852 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1853 | isFirst = false; |
| 1854 | } |
| 1855 | else |
| 1856 | Out << "\\l"; |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1857 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1858 | Out << " (" << (void*) I.getKey() << ") "; |
| 1859 | I.getKey()->printPretty(Out); |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1860 | Out << " : "; |
| 1861 | I.getData().print(Out); |
| 1862 | } |
| 1863 | } |
| 1864 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1865 | static void PrintEQ(std::ostream& Out, ValueState* St) { |
| 1866 | ValueState::ConstEqTy CE = St->ConstEq; |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (CE.isEmpty()) |
| 1869 | return; |
| 1870 | |
| 1871 | Out << "\\l\\|'==' constraints:"; |
| 1872 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1873 | 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] | 1874 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1875 | } |
| 1876 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1877 | static void PrintNE(std::ostream& Out, ValueState* St) { |
| 1878 | ValueState::ConstNotEqTy NE = St->ConstNotEq; |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1879 | |
| 1880 | if (NE.isEmpty()) |
| 1881 | return; |
| 1882 | |
| 1883 | Out << "\\l\\|'!=' constraints:"; |
| 1884 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1885 | for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1886 | I != EI; ++I){ |
| 1887 | |
| 1888 | Out << "\\l $" << I.getKey() << " : "; |
| 1889 | bool isFirst = true; |
| 1890 | |
| 1891 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1892 | EJ=I.getData().end(); |
| 1893 | for ( ; J != EJ; ++J) { |
| 1894 | if (isFirst) isFirst = false; |
| 1895 | else Out << ", "; |
| 1896 | |
| 1897 | Out << (*J)->toString(); |
| 1898 | } |
| 1899 | } |
Ted Kremenek | a853de6 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1903 | |
| 1904 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1905 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1906 | GraphPrintCheckerState->isUndefDeref(N) || |
| 1907 | GraphPrintCheckerState->isUndefStore(N) || |
| 1908 | GraphPrintCheckerState->isUndefControlFlow(N) || |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1909 | GraphPrintCheckerState->isExplicitBadDivide(N) || |
| 1910 | GraphPrintCheckerState->isImplicitBadDivide(N) || |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1911 | GraphPrintCheckerState->isUndefResult(N) || |
Ted Kremenek | 9b31f5b | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 1912 | GraphPrintCheckerState->isBadCall(N) || |
| 1913 | GraphPrintCheckerState->isUndefArg(N)) |
Ted Kremenek | a853de6 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1914 | return "color=\"red\",style=\"filled\""; |
| 1915 | |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1916 | if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 1917 | return "color=\"blue\",style=\"filled\""; |
| 1918 | |
Ted Kremenek | a853de6 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1919 | return ""; |
| 1920 | } |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1921 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1922 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1923 | std::ostringstream Out; |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1924 | |
| 1925 | // Program Location. |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1926 | ProgramPoint Loc = N->getLocation(); |
| 1927 | |
| 1928 | switch (Loc.getKind()) { |
| 1929 | case ProgramPoint::BlockEntranceKind: |
| 1930 | Out << "Block Entrance: B" |
| 1931 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1932 | break; |
| 1933 | |
| 1934 | case ProgramPoint::BlockExitKind: |
| 1935 | assert (false); |
| 1936 | break; |
| 1937 | |
| 1938 | case ProgramPoint::PostStmtKind: { |
Ted Kremenek | 8b41e8c | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 1939 | const PostStmt& L = cast<PostStmt>(Loc); |
| 1940 | Stmt* S = L.getStmt(); |
| 1941 | SourceLocation SLoc = S->getLocStart(); |
| 1942 | |
| 1943 | Out << S->getStmtClassName() << ' ' << (void*) S << ' '; |
| 1944 | S->printPretty(Out); |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1945 | |
Ted Kremenek | f97c668 | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 1946 | if (SLoc.isFileID()) { |
| 1947 | Out << "\\lline=" |
| 1948 | << GraphPrintSourceManager->getLineNumber(SLoc) << " col=" |
| 1949 | << GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l"; |
| 1950 | } |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1951 | |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1952 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1953 | Out << "\\|Implicit-Null Dereference.\\l"; |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1954 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) |
Ted Kremenek | ae7bdc1 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1955 | Out << "\\|Explicit-Null Dereference.\\l"; |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1956 | else if (GraphPrintCheckerState->isUndefDeref(N)) |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1957 | Out << "\\|Dereference of undefialied value.\\l"; |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1958 | else if (GraphPrintCheckerState->isUndefStore(N)) |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 1959 | Out << "\\|Store to Undefined LVal."; |
Ted Kremenek | 75f32c6 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 1960 | else if (GraphPrintCheckerState->isExplicitBadDivide(N)) |
| 1961 | Out << "\\|Explicit divide-by zero or undefined value."; |
| 1962 | else if (GraphPrintCheckerState->isImplicitBadDivide(N)) |
| 1963 | Out << "\\|Implicit divide-by zero or undefined value."; |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1964 | else if (GraphPrintCheckerState->isUndefResult(N)) |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1965 | Out << "\\|Result of operation is undefined."; |
Ted Kremenek | c2d0720 | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 1966 | else if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 1967 | Out << "\\|Call to function marked \"noreturn\"."; |
Ted Kremenek | 43863eb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 1968 | else if (GraphPrintCheckerState->isBadCall(N)) |
| 1969 | Out << "\\|Call to NULL/Undefined."; |
Ted Kremenek | 9b31f5b | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 1970 | else if (GraphPrintCheckerState->isUndefArg(N)) |
| 1971 | Out << "\\|Argument in call is undefined"; |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1972 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1973 | break; |
| 1974 | } |
| 1975 | |
| 1976 | default: { |
| 1977 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1978 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1979 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1980 | |
| 1981 | if (Stmt* T = E.getSrc()->getTerminator()) { |
Ted Kremenek | 8b41e8c | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 1982 | |
| 1983 | SourceLocation SLoc = T->getLocStart(); |
| 1984 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1985 | Out << "\\|Terminator: "; |
Ted Kremenek | 8b41e8c | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 1986 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1987 | E.getSrc()->printTerminator(Out); |
| 1988 | |
Ted Kremenek | f97c668 | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 1989 | if (SLoc.isFileID()) { |
| 1990 | Out << "\\lline=" |
| 1991 | << GraphPrintSourceManager->getLineNumber(SLoc) << " col=" |
| 1992 | << GraphPrintSourceManager->getColumnNumber(SLoc); |
| 1993 | } |
Ted Kremenek | 8b41e8c | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 1994 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1995 | if (isa<SwitchStmt>(T)) { |
| 1996 | Stmt* Label = E.getDst()->getLabel(); |
| 1997 | |
| 1998 | if (Label) { |
| 1999 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 2000 | Out << "\\lcase "; |
| 2001 | C->getLHS()->printPretty(Out); |
| 2002 | |
| 2003 | if (Stmt* RHS = C->getRHS()) { |
| 2004 | Out << " .. "; |
| 2005 | RHS->printPretty(Out); |
| 2006 | } |
| 2007 | |
| 2008 | Out << ":"; |
| 2009 | } |
| 2010 | else { |
| 2011 | assert (isa<DefaultStmt>(Label)); |
| 2012 | Out << "\\ldefault:"; |
| 2013 | } |
| 2014 | } |
| 2015 | else |
| 2016 | Out << "\\l(implicit) default:"; |
| 2017 | } |
| 2018 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 2019 | // FIXME |
| 2020 | } |
| 2021 | else { |
| 2022 | Out << "\\lCondition: "; |
| 2023 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 2024 | Out << "true"; |
| 2025 | else |
| 2026 | Out << "false"; |
| 2027 | } |
| 2028 | |
| 2029 | Out << "\\l"; |
| 2030 | } |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2031 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 2032 | if (GraphPrintCheckerState->isUndefControlFlow(N)) { |
| 2033 | Out << "\\|Control-flow based on\\lUndefined value.\\l"; |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2034 | } |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 2038 | Out << "\\|StateID: " << (void*) N->getState() << "\\|"; |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 2039 | |
Ted Kremenek | 9f59792 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2040 | N->getState()->printDOT(Out, GraphCheckerStatePrinter); |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 2041 | |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 2042 | Out << "\\l"; |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2043 | return Out.str(); |
| 2044 | } |
| 2045 | }; |
| 2046 | } // end llvm namespace |
| 2047 | #endif |
| 2048 | |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2049 | #ifndef NDEBUG |
Ted Kremenek | 83f04aa | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2050 | |
| 2051 | template <typename ITERATOR> |
| 2052 | GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; } |
| 2053 | |
| 2054 | template <> |
| 2055 | GRExprEngine::NodeTy* |
| 2056 | GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator> |
| 2057 | (llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) { |
| 2058 | return I->first; |
| 2059 | } |
| 2060 | |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2061 | template <typename ITERATOR> |
| 2062 | static void AddSources(llvm::SmallVector<GRExprEngine::NodeTy*, 10>& Sources, |
Ted Kremenek | 83f04aa | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2063 | ITERATOR I, ITERATOR E) { |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2064 | |
Ted Kremenek | 83f04aa | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2065 | llvm::SmallPtrSet<void*,10> CachedSources; |
| 2066 | |
| 2067 | for ( ; I != E; ++I ) { |
| 2068 | GRExprEngine::NodeTy* N = GetGraphNode(I); |
| 2069 | void* p = N->getLocation().getRawData(); |
| 2070 | |
| 2071 | if (CachedSources.count(p)) |
| 2072 | continue; |
| 2073 | |
| 2074 | CachedSources.insert(p); |
| 2075 | |
| 2076 | Sources.push_back(N); |
| 2077 | } |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2078 | } |
| 2079 | #endif |
| 2080 | |
| 2081 | void GRExprEngine::ViewGraph(bool trim) { |
Ted Kremenek | e44a830 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2082 | #ifndef NDEBUG |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2083 | if (trim) { |
Ted Kremenek | 83f04aa | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2084 | llvm::SmallVector<NodeTy*, 10> Src; |
| 2085 | AddSources(Src, null_derefs_begin(), null_derefs_end()); |
| 2086 | AddSources(Src, undef_derefs_begin(), undef_derefs_end()); |
| 2087 | AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end()); |
| 2088 | AddSources(Src, undef_results_begin(), undef_results_end()); |
| 2089 | AddSources(Src, bad_calls_begin(), bad_calls_end()); |
| 2090 | AddSources(Src, undef_arg_begin(), undef_arg_end()); |
Ted Kremenek | 2f0c0e1 | 2008-03-14 18:14:50 +0000 | [diff] [blame] | 2091 | AddSources(Src, undef_branches_begin(), undef_branches_end()); |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2092 | |
Ted Kremenek | 83f04aa | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 2093 | ViewGraph(&Src[0], &Src[0]+Src.size()); |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2094 | } |
Ted Kremenek | e44a830 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2095 | else { |
| 2096 | GraphPrintCheckerState = this; |
| 2097 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | 9f59792 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2098 | GraphCheckerStatePrinter = TF->getCheckerStatePrinter(); |
Ted Kremenek | e44a830 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2099 | |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2100 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | e44a830 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2101 | |
| 2102 | GraphPrintCheckerState = NULL; |
| 2103 | GraphPrintSourceManager = NULL; |
Ted Kremenek | 9f59792 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2104 | GraphCheckerStatePrinter = NULL; |
Ted Kremenek | e44a830 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2105 | } |
| 2106 | #endif |
| 2107 | } |
| 2108 | |
| 2109 | void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) { |
| 2110 | #ifndef NDEBUG |
| 2111 | GraphPrintCheckerState = this; |
| 2112 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | 9f59792 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2113 | GraphCheckerStatePrinter = TF->getCheckerStatePrinter(); |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2114 | |
Ted Kremenek | e44a830 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 2115 | GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End); |
| 2116 | |
| 2117 | if (!TrimmedG) |
| 2118 | llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n"; |
| 2119 | else { |
| 2120 | llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine"); |
| 2121 | delete TrimmedG; |
| 2122 | } |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 2123 | |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2124 | GraphPrintCheckerState = NULL; |
Ted Kremenek | 8b41e8c | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2125 | GraphPrintSourceManager = NULL; |
Ted Kremenek | 9f59792 | 2008-03-11 19:02:40 +0000 | [diff] [blame] | 2126 | GraphCheckerStatePrinter = NULL; |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 2127 | #endif |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 2128 | } |