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