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