Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1 | //===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==// |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 4af8431 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | d27f816 | 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 | // |
| 10 | // Constant Propagation via Graph Reachability |
| 11 | // |
| 12 | // This files defines a simple analysis that performs path-sensitive |
| 13 | // constant propagation within a function. An example use of this analysis |
| 14 | // is to perform simple checks for NULL dereferences. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 18 | #include "RValues.h" |
| 19 | #include "ValueState.h" |
| 20 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 22 | #include "clang/AST/Expr.h" |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 25 | |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/DataTypes.h" |
| 28 | #include "llvm/ADT/APSInt.h" |
| 29 | #include "llvm/ADT/FoldingSet.h" |
| 30 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Allocator.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Streams.h" |
| 36 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 37 | #include <functional> |
| 38 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 39 | #ifndef NDEBUG |
| 40 | #include "llvm/Support/GraphWriter.h" |
| 41 | #include <sstream> |
| 42 | #endif |
| 43 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 44 | using namespace clang; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 45 | using llvm::dyn_cast; |
| 46 | using llvm::cast; |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 47 | using llvm::APSInt; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 48 | |
| 49 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 50 | // The Checker. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 51 | // |
| 52 | // FIXME: This checker logic should be eventually broken into two components. |
| 53 | // The first is the "meta"-level checking logic; the code that |
| 54 | // does the Stmt visitation, fetching values from the map, etc. |
| 55 | // The second part does the actual state manipulation. This way we |
| 56 | // get more of a separate of concerns of these two pieces, with the |
| 57 | // latter potentially being refactored back into the main checking |
| 58 | // logic. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | namespace { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 63 | class VISIBILITY_HIDDEN GRConstants { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 64 | |
| 65 | public: |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 66 | typedef ValueStateManager::StateTy StateTy; |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 67 | typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder; |
| 68 | typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder; |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 69 | typedef ExplodedGraph<GRConstants> GraphTy; |
| 70 | typedef GraphTy::NodeTy NodeTy; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 71 | |
| 72 | class NodeSet { |
| 73 | typedef llvm::SmallVector<NodeTy*,3> ImplTy; |
| 74 | ImplTy Impl; |
| 75 | public: |
| 76 | |
| 77 | NodeSet() {} |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 78 | NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 80 | void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 81 | |
| 82 | typedef ImplTy::iterator iterator; |
| 83 | typedef ImplTy::const_iterator const_iterator; |
| 84 | |
| 85 | unsigned size() const { return Impl.size(); } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 86 | bool empty() const { return Impl.empty(); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 87 | |
| 88 | iterator begin() { return Impl.begin(); } |
| 89 | iterator end() { return Impl.end(); } |
| 90 | |
| 91 | const_iterator begin() const { return Impl.begin(); } |
| 92 | const_iterator end() const { return Impl.end(); } |
| 93 | }; |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 95 | protected: |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 96 | /// G - the simulation graph. |
| 97 | GraphTy& G; |
| 98 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 99 | /// Liveness - live-variables information the ValueDecl* and block-level |
| 100 | /// Expr* in the CFG. Used to prune out dead state. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 101 | LiveVariables Liveness; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 103 | /// Builder - The current GRStmtNodeBuilder which is used when building the nodes |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 104 | /// for a given statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 105 | StmtNodeBuilder* Builder; |
| 106 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 107 | /// StateMgr - Object that manages the data for all created states. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 108 | ValueStateManager StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 110 | /// ValueMgr - Object that manages the data for all created RValues. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 111 | ValueManager& ValMgr; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 113 | /// SymMgr - Object that manages the symbol information. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 114 | SymbolManager& SymMgr; |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 116 | /// StmtEntryNode - The immediate predecessor node. |
| 117 | NodeTy* StmtEntryNode; |
| 118 | |
| 119 | /// CurrentStmt - The current block-level statement. |
| 120 | Stmt* CurrentStmt; |
| 121 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 122 | /// UninitBranches - Nodes in the ExplodedGraph that result from |
| 123 | /// taking a branch based on an uninitialized value. |
| 124 | typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy; |
| 125 | UninitBranchesTy UninitBranches; |
| 126 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 127 | /// ImplicitNullDeref - Nodes in the ExplodedGraph that result from |
| 128 | /// taking a dereference on a symbolic pointer that may be NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 129 | typedef llvm::SmallPtrSet<NodeTy*,5> NullDerefTy; |
| 130 | NullDerefTy ImplicitNullDeref; |
| 131 | NullDerefTy ExplicitNullDeref; |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 132 | |
| 133 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 134 | bool StateCleaned; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 136 | ASTContext& getContext() const { return G.getContext(); } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 138 | public: |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 139 | GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()), |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 140 | Builder(NULL), |
Ted Kremenek | 768ad16 | 2008-02-05 05:15:51 +0000 | [diff] [blame] | 141 | StateMgr(G.getContext(), G.getAllocator()), |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 142 | ValMgr(StateMgr.getValueManager()), |
| 143 | SymMgr(StateMgr.getSymbolManager()), |
| 144 | StmtEntryNode(NULL), CurrentStmt(NULL) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 146 | // Compute liveness information. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 147 | Liveness.runOnCFG(G.getCFG()); |
| 148 | Liveness.runOnAllBlocks(G.getCFG(), NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 149 | } |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 150 | |
| 151 | /// getCFG - Returns the CFG associated with this analysis. |
| 152 | CFG& getCFG() { return G.getCFG(); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 153 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 154 | /// getInitialState - Return the initial state used for the root vertex |
| 155 | /// in the ExplodedGraph. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 156 | StateTy getInitialState() { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 157 | StateTy St = StateMgr.getInitialState(); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 158 | |
| 159 | // Iterate the parameters. |
| 160 | FunctionDecl& F = G.getFunctionDecl(); |
| 161 | |
| 162 | for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end(); |
Ted Kremenek | 4150abf | 2008-01-31 00:09:56 +0000 | [diff] [blame] | 163 | I!=E; ++I) |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 164 | St = SetValue(St, lval::DeclVal(*I), RValue::GetSymbolValue(SymMgr, *I)); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 166 | return St; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 167 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 168 | |
| 169 | bool isUninitControlFlow(const NodeTy* N) const { |
| 170 | return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0; |
| 171 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 172 | |
| 173 | bool isImplicitNullDeref(const NodeTy* N) const { |
| 174 | return N->isSink() && ImplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0; |
| 175 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 176 | |
| 177 | bool isExplicitNullDeref(const NodeTy* N) const { |
| 178 | return N->isSink() && ExplicitNullDeref.count(const_cast<NodeTy*>(N)) != 0; |
| 179 | } |
| 180 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 182 | /// ProcessStmt - Called by GREngine. Used to generate new successor |
| 183 | /// nodes by processing the 'effects' of a block-level statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 184 | void ProcessStmt(Stmt* S, StmtNodeBuilder& builder); |
| 185 | |
| 186 | /// ProcessBranch - Called by GREngine. Used to generate successor |
| 187 | /// nodes by processing the 'effects' of a branch condition. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 188 | void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 189 | |
| 190 | /// RemoveDeadBindings - Return a new state that is the same as 'M' except |
| 191 | /// that all subexpression mappings are removed and that any |
| 192 | /// block-level expressions that are not live at 'S' also have their |
| 193 | /// mappings removed. |
| 194 | StateTy RemoveDeadBindings(Stmt* S, StateTy M); |
| 195 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 196 | StateTy SetValue(StateTy St, Stmt* S, const RValue& V); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 198 | StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 199 | return SetValue(St, const_cast<Stmt*>(S), V); |
| 200 | } |
| 201 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 202 | /// SetValue - This version of SetValue is used to batch process a set |
| 203 | /// of different possible RValues and return a set of different states. |
| 204 | const StateTy::BufferTy& SetValue(StateTy St, Stmt* S, |
| 205 | const RValue::BufferTy& V, |
| 206 | StateTy::BufferTy& RetBuf); |
| 207 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 208 | StateTy SetValue(StateTy St, const LValue& LV, const RValue& V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 210 | inline RValue GetValue(const StateTy& St, Stmt* S) { |
| 211 | return StateMgr.GetValue(St, S); |
| 212 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 213 | |
| 214 | inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) { |
| 215 | return StateMgr.GetValue(St, S, &hasVal); |
| 216 | } |
| 217 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 218 | inline RValue GetValue(const StateTy& St, const Stmt* S) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 219 | return GetValue(St, const_cast<Stmt*>(S)); |
| 220 | } |
| 221 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 222 | inline RValue GetValue(const StateTy& St, const LValue& LV, |
| 223 | QualType* T = NULL) { |
| 224 | |
| 225 | return StateMgr.GetValue(St, LV, T); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | inline LValue GetLValue(const StateTy& St, Stmt* S) { |
| 229 | return StateMgr.GetLValue(St, S); |
| 230 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 231 | |
| 232 | inline NonLValue GetRValueConstant(uint64_t X, Expr* E) { |
| 233 | return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart()); |
| 234 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 235 | |
| 236 | /// Assume - Create new state by assuming that a given expression |
| 237 | /// is true or false. |
| 238 | inline StateTy Assume(StateTy St, RValue Cond, bool Assumption, |
| 239 | bool& isFeasible) { |
| 240 | if (isa<LValue>(Cond)) |
| 241 | return Assume(St, cast<LValue>(Cond), Assumption, isFeasible); |
| 242 | else |
| 243 | return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible); |
| 244 | } |
| 245 | |
| 246 | StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible); |
| 247 | StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 249 | StateTy AssumeSymNE(StateTy St, SymbolID sym, const llvm::APSInt& V, |
| 250 | bool& isFeasible); |
| 251 | |
| 252 | StateTy AssumeSymEQ(StateTy St, SymbolID sym, const llvm::APSInt& V, |
| 253 | bool& isFeasible); |
| 254 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 255 | StateTy AssumeSymInt(StateTy St, bool Assumption, const SymIntConstraint& C, |
| 256 | bool& isFeasible); |
| 257 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 258 | NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St, |
| 259 | bool AlwaysMakeNode = false); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 261 | /// Nodify - This version of Nodify is used to batch process a set of states. |
| 262 | /// The states are not guaranteed to be unique. |
| 263 | void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB); |
| 264 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 265 | /// Visit - Transfer function logic for all statements. Dispatches to |
| 266 | /// other functions that handle specific kinds of statements. |
| 267 | void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 268 | |
| 269 | /// VisitCast - Transfer function logic for all casts (implicit and explicit). |
| 270 | void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 272 | /// VisitUnaryOperator - Transfer function logic for unary operators. |
| 273 | void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 274 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 275 | /// VisitBinaryOperator - Transfer function logic for binary operators. |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 276 | void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 277 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 278 | void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 279 | |
| 280 | /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs. |
| 281 | void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst); |
| 282 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 283 | /// VisitDeclStmt - Transfer function logic for DeclStmts. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 284 | void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst); |
| 285 | |
| 286 | /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose |
| 287 | void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 288 | NodeTy* Pred, NodeSet& Dst); |
| 289 | |
| 290 | /// VisitLogicalExpr - Transfer function logic for '&&', '||' |
| 291 | void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 292 | }; |
| 293 | } // end anonymous namespace |
| 294 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 296 | GRConstants::StateTy |
| 297 | GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 299 | if (!StateCleaned) { |
| 300 | St = RemoveDeadBindings(CurrentStmt, St); |
| 301 | StateCleaned = true; |
| 302 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 304 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 306 | if (S == CurrentStmt) { |
| 307 | isBlkExpr = getCFG().isBlkExpr(S); |
| 308 | |
| 309 | if (!isBlkExpr) |
| 310 | return St; |
| 311 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 313 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 314 | } |
| 315 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 316 | const GRConstants::StateTy::BufferTy& |
| 317 | GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB, |
| 318 | StateTy::BufferTy& RetBuf) { |
| 319 | |
| 320 | assert (RetBuf.empty()); |
| 321 | |
| 322 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 323 | RetBuf.push_back(SetValue(St, S, *I)); |
| 324 | |
| 325 | return RetBuf; |
| 326 | } |
| 327 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 328 | GRConstants::StateTy |
| 329 | GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
| 330 | |
| 331 | if (!LV.isValid()) |
| 332 | return St; |
| 333 | |
| 334 | if (!StateCleaned) { |
| 335 | St = RemoveDeadBindings(CurrentStmt, St); |
| 336 | StateCleaned = true; |
| 337 | } |
| 338 | |
| 339 | return StateMgr.SetValue(St, LV, V); |
| 340 | } |
| 341 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 342 | void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 343 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 344 | |
| 345 | StateTy PrevState = builder.getState(); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 347 | // Remove old bindings for subexpressions. |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 348 | for (StateTy::vb_iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I) |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 349 | if (I.getKey().isSubExpr()) |
| 350 | PrevState = StateMgr.Remove(PrevState, I.getKey()); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 351 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 352 | // Remove terminator-specific bindings. |
| 353 | switch (Term->getStmtClass()) { |
| 354 | default: break; |
| 355 | |
| 356 | case Stmt::BinaryOperatorClass: { // '&&', '||' |
| 357 | BinaryOperator* B = cast<BinaryOperator>(Term); |
| 358 | // FIXME: Liveness analysis should probably remove these automatically. |
| 359 | // Verify later when we converge to an 'optimization' stage. |
| 360 | PrevState = StateMgr.Remove(PrevState, B->getRHS()); |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 365 | ConditionalOperator* C = cast<ConditionalOperator>(Term); |
| 366 | // FIXME: Liveness analysis should probably remove these automatically. |
| 367 | // Verify later when we converge to an 'optimization' stage. |
| 368 | if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L); |
| 369 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 374 | ChooseExpr* C = cast<ChooseExpr>(Term); |
| 375 | // FIXME: Liveness analysis should probably remove these automatically. |
| 376 | // Verify later when we converge to an 'optimization' stage. |
| 377 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 378 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 383 | RValue V = GetValue(PrevState, Condition); |
| 384 | |
| 385 | switch (V.getBaseKind()) { |
| 386 | default: |
| 387 | break; |
| 388 | |
| 389 | case RValue::InvalidKind: |
| 390 | builder.generateNode(PrevState, true); |
| 391 | builder.generateNode(PrevState, false); |
| 392 | return; |
| 393 | |
| 394 | case RValue::UninitializedKind: { |
| 395 | NodeTy* N = builder.generateNode(PrevState, true); |
| 396 | |
| 397 | if (N) { |
| 398 | N->markAsSink(); |
| 399 | UninitBranches.insert(N); |
| 400 | } |
| 401 | |
| 402 | builder.markInfeasible(false); |
| 403 | return; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // Process the true branch. |
| 408 | bool isFeasible = true; |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 410 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 411 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 412 | if (isFeasible) |
| 413 | builder.generateNode(St, true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 414 | else { |
| 415 | builder.markInfeasible(true); |
| 416 | isFeasible = true; |
| 417 | } |
| 418 | |
| 419 | // Process the false branch. |
| 420 | St = Assume(PrevState, V, false, isFeasible); |
| 421 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 422 | if (isFeasible) |
| 423 | builder.generateNode(St, false); |
| 424 | else |
| 425 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 428 | |
| 429 | void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
| 430 | NodeSet& Dst) { |
| 431 | |
| 432 | bool hasR2; |
| 433 | StateTy PrevState = Pred->getState(); |
| 434 | |
| 435 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 436 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 437 | |
| 438 | if (isa<InvalidValue>(R1) && |
| 439 | (isa<InvalidValue>(R2) || |
| 440 | isa<UninitializedValue>(R2))) { |
| 441 | |
| 442 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 443 | return; |
| 444 | } |
| 445 | else if (isa<UninitializedValue>(R1)) { |
| 446 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 451 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 452 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 453 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 454 | if (hasR2 == false) { |
| 455 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 456 | return; |
| 457 | } |
| 458 | } |
| 459 | else { |
| 460 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 461 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 462 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 463 | if (hasR2 == false) { |
| 464 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 465 | return; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 470 | // R2 == false. |
| 471 | |
| 472 | bool isFeasible; |
| 473 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 474 | |
| 475 | if (isFeasible) |
| 476 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 477 | |
| 478 | St = Assume(PrevState, R2, false, isFeasible); |
| 479 | |
| 480 | if (isFeasible) |
| 481 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 486 | void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 487 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 488 | |
| 489 | StmtEntryNode = builder.getLastNode(); |
| 490 | CurrentStmt = S; |
| 491 | NodeSet Dst; |
| 492 | StateCleaned = false; |
| 493 | |
| 494 | Visit(S, StmtEntryNode, Dst); |
| 495 | |
| 496 | // If no nodes were generated, generate a new node that has all the |
| 497 | // dead mappings removed. |
| 498 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 499 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 500 | builder.generateNode(S, St, StmtEntryNode); |
| 501 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 503 | CurrentStmt = NULL; |
| 504 | StmtEntryNode = NULL; |
| 505 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 508 | GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 509 | |
| 510 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 511 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 512 | // tells us are live. We then see what Decls they may reference, and keep |
| 513 | // those around. This code more than likely can be made faster, and the |
| 514 | // frequency of which this method is called should be experimented with |
| 515 | // for optimum performance. |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 517 | llvm::SmallVector<ValueDecl*, 10> WList; |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 518 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 519 | for (StateTy::vb_iterator I = M.begin(), E = M.end(); |
| 520 | I!=E && !I.getKey().isSymbol(); ++I) { |
| 521 | |
| 522 | // Remove old bindings for subexpressions. |
| 523 | if (I.getKey().isSubExpr()) { |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 524 | M = StateMgr.Remove(M, I.getKey()); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 525 | continue; |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 526 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 527 | |
| 528 | if (I.getKey().isBlkExpr()) { |
| 529 | if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) { |
| 530 | if (isa<lval::DeclVal>(I.getData())) { |
| 531 | lval::DeclVal LV = cast<lval::DeclVal>(I.getData()); |
| 532 | WList.push_back(LV.getDecl()); |
| 533 | } |
| 534 | } |
| 535 | else |
| 536 | M = StateMgr.Remove(M, I.getKey()); |
| 537 | |
| 538 | continue; |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 539 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 540 | |
| 541 | assert (I.getKey().isDecl()); |
| 542 | |
| 543 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 544 | if (Liveness.isLive(Loc, V)) |
| 545 | WList.push_back(V); |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 546 | } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 547 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 548 | llvm::SmallPtrSet<ValueDecl*, 10> Marked; |
| 549 | |
| 550 | while (!WList.empty()) { |
| 551 | ValueDecl* V = WList.back(); |
| 552 | WList.pop_back(); |
| 553 | |
| 554 | if (Marked.count(V)) |
| 555 | continue; |
| 556 | |
| 557 | Marked.insert(V); |
| 558 | |
| 559 | if (V->getType()->isPointerType()) { |
| 560 | const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V))); |
| 561 | |
| 562 | if (!isa<lval::DeclVal>(LV)) |
| 563 | continue; |
| 564 | |
| 565 | const lval::DeclVal& LVD = cast<lval::DeclVal>(LV); |
| 566 | WList.push_back(LVD.getDecl()); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I) |
| 571 | if (I.getKey().isDecl()) |
| 572 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 573 | if (!Marked.count(V)) |
| 574 | M = StateMgr.Remove(M, V); |
| 575 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 576 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 579 | GRConstants::NodeTy* |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 580 | GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St, |
| 581 | bool AlwaysMakeNode) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 582 | |
| 583 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 584 | if (!AlwaysMakeNode && St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 585 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 586 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 587 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 588 | Dst.Add(N); |
| 589 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 590 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 591 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 592 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
| 593 | const StateTy::BufferTy& SB) { |
| 594 | |
| 595 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 596 | Nodify(Dst, S, Pred, *I); |
| 597 | } |
| 598 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 599 | void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) { |
| 600 | if (D != CurrentStmt) { |
| 601 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | // If we are here, we are loading the value of the decl and binding |
| 606 | // it to the block-level expression. |
| 607 | |
| 608 | StateTy St = Pred->getState(); |
| 609 | |
| 610 | Nodify(Dst, D, Pred, |
| 611 | SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl())))); |
| 612 | } |
| 613 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 614 | void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 615 | |
| 616 | QualType T = CastE->getType(); |
| 617 | |
| 618 | // Check for redundant casts. |
| 619 | if (E->getType() == T) { |
| 620 | Dst.Add(Pred); |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | NodeSet S1; |
| 625 | Visit(E, Pred, S1); |
| 626 | |
| 627 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 628 | NodeTy* N = *I1; |
| 629 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 630 | const RValue& V = GetValue(St, E); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 631 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 632 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 636 | GRConstants::NodeSet& Dst) { |
| 637 | |
| 638 | StateTy St = Pred->getState(); |
| 639 | |
| 640 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 641 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 642 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 643 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 644 | E ? GetValue(St, E) : UninitializedValue()); |
| 645 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 646 | |
| 647 | Nodify(Dst, DS, Pred, St); |
| 648 | |
| 649 | if (Dst.empty()) |
| 650 | Dst.Add(Pred); |
| 651 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 652 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 653 | |
| 654 | void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 655 | NodeTy* Pred, NodeSet& Dst) { |
| 656 | |
| 657 | StateTy St = Pred->getState(); |
| 658 | |
| 659 | RValue R = GetValue(St, LHS); |
| 660 | if (isa<InvalidValue>(R)) R = GetValue(St, RHS); |
| 661 | |
| 662 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 663 | } |
| 664 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 665 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 666 | GRConstants::NodeTy* Pred, |
| 667 | GRConstants::NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 668 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 669 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 670 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 671 | |
| 672 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 673 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 674 | // probably just handle AddrOf and Deref in their own methods to make |
| 675 | // this cleaner. |
| 676 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 677 | isa<DeclRefExpr>(U->getSubExpr())) |
| 678 | S1.Add(Pred); |
| 679 | else |
| 680 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 681 | |
| 682 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 683 | NodeTy* N1 = *I1; |
| 684 | StateTy St = N1->getState(); |
| 685 | |
| 686 | switch (U->getOpcode()) { |
| 687 | case UnaryOperator::PostInc: { |
| 688 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 689 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 690 | |
| 691 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 692 | GetRValueConstant(1U, U)); |
| 693 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 694 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | case UnaryOperator::PostDec: { |
| 699 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 700 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 701 | |
| 702 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 703 | GetRValueConstant(1U, U)); |
| 704 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 705 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 706 | break; |
| 707 | } |
| 708 | |
| 709 | case UnaryOperator::PreInc: { |
| 710 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 711 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 712 | |
| 713 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 714 | GetRValueConstant(1U, U)); |
| 715 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 716 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | case UnaryOperator::PreDec: { |
| 721 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 722 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 723 | |
| 724 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 725 | GetRValueConstant(1U, U)); |
| 726 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 727 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 728 | break; |
| 729 | } |
| 730 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 731 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 732 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 733 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 734 | break; |
| 735 | } |
| 736 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 737 | case UnaryOperator::Not: { |
| 738 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 739 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 740 | break; |
| 741 | } |
| 742 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 743 | case UnaryOperator::LNot: { |
| 744 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 745 | // |
| 746 | // Note: technically we do "E == 0", but this is the same in the |
| 747 | // transfer functions as "0 == E". |
| 748 | |
| 749 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 750 | |
| 751 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 752 | const LValue& L1 = cast<LValue>(V1); |
| 753 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 754 | Nodify(Dst, U, N1, |
| 755 | SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 756 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 757 | } |
| 758 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 759 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 760 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 761 | Nodify(Dst, U, N1, |
| 762 | SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 763 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | break; |
| 767 | } |
| 768 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 769 | case UnaryOperator::AddrOf: { |
| 770 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 771 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | case UnaryOperator::Deref: { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 776 | // FIXME: Stop when dereferencing an uninitialized value. |
| 777 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 778 | |
| 779 | const RValue& V = GetValue(St, U->getSubExpr()); |
| 780 | const LValue& L1 = cast<LValue>(V); |
| 781 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 782 | // After a dereference, one of two possible situations arise: |
| 783 | // (1) A crash, because the pointer was NULL. |
| 784 | // (2) The pointer is not NULL, and the dereference works. |
| 785 | // |
| 786 | // We add these assumptions. |
| 787 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 788 | bool isFeasibleNotNull; |
| 789 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 790 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 791 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 792 | |
| 793 | if (isFeasibleNotNull) { |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 794 | QualType T = U->getType(); |
| 795 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 796 | GetValue(StNotNull, L1, &T))); |
| 797 | } |
| 798 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 799 | bool isFeasibleNull; |
| 800 | |
| 801 | // "Assume" that the pointer is NULL. |
| 802 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 803 | |
| 804 | if (isFeasibleNull) { |
| 805 | NodeTy* NullNode = Nodify(Dst, U, N1, StNull, true); |
| 806 | if (NullNode) { |
| 807 | NullNode->markAsSink(); |
| 808 | |
| 809 | if (isFeasibleNotNull) |
| 810 | ImplicitNullDeref.insert(NullNode); |
| 811 | else |
| 812 | ExplicitNullDeref.insert(NullNode); |
| 813 | } |
| 814 | } |
| 815 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 816 | break; |
| 817 | } |
| 818 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 819 | default: ; |
| 820 | assert (false && "Not implemented."); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 825 | void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred, |
| 826 | GRConstants::NodeSet& Dst) { |
| 827 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 828 | if (isa<DeclRefExpr>(E)) { |
| 829 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 830 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 831 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 832 | |
| 833 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 834 | if (U->getOpcode() == UnaryOperator::Deref) { |
| 835 | Visit(U->getSubExpr(), Pred, Dst); |
| 836 | return; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | Visit(E, Pred, Dst); |
| 841 | } |
| 842 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 843 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 844 | GRConstants::NodeTy* Pred, |
| 845 | GRConstants::NodeSet& Dst) { |
| 846 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 847 | |
| 848 | if (B->isAssignmentOp()) |
| 849 | VisitAssignmentLHS(B->getLHS(), Pred, S1); |
| 850 | else |
| 851 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 852 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 853 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 854 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 855 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 856 | // When getting the value for the LHS, check if we are in an assignment. |
| 857 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 858 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 859 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 860 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 861 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 862 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 863 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 864 | NodeSet S2; |
| 865 | Visit(B->getRHS(), N1, S2); |
| 866 | |
| 867 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 868 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 869 | NodeTy* N2 = *I2; |
| 870 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 871 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 872 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 873 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 874 | |
| 875 | if (Op <= BinaryOperator::Or) { |
| 876 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 877 | if (isa<InvalidValue>(V1) || isa<UninitializedValue>(V1)) { |
| 878 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 879 | continue; |
| 880 | } |
| 881 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 882 | if (isa<LValue>(V1)) { |
| 883 | // FIXME: Add support for RHS being a non-lvalue. |
| 884 | const LValue& L1 = cast<LValue>(V1); |
| 885 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 886 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 887 | Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2))); |
| 888 | } |
| 889 | else { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 890 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 891 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 892 | |
| 893 | Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 894 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 895 | |
| 896 | continue; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 897 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | switch (Op) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 901 | case BinaryOperator::Assign: { |
| 902 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 3434b08 | 2008-02-06 04:41:14 +0000 | [diff] [blame] | 903 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 907 | default: { // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 908 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 909 | assert (B->isCompoundAssignmentOp()); |
| 910 | |
| 911 | const LValue& L1 = cast<LValue>(V1); |
| 912 | RValue Result = cast<NonLValue>(InvalidValue()); |
| 913 | |
| 914 | Op = (BinaryOperator::Opcode) |
| 915 | (((unsigned) Op) - ((unsigned) BinaryOperator::MulAssign)); |
| 916 | |
| 917 | if (isa<LValue>(V2)) { |
| 918 | // FIXME: Add support for Non-LValues on RHS. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 919 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 920 | Result = L1.EvalBinaryOp(ValMgr, Op, L2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 921 | } |
| 922 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 923 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 924 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 925 | Result = R1.EvalBinaryOp(ValMgr, Op, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 928 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 929 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 930 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 931 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 932 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 933 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 934 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 935 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 936 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 937 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 938 | GRConstants::NodeSet& Dst) { |
| 939 | |
| 940 | // FIXME: add metadata to the CFG so that we can disable |
| 941 | // this check when we KNOW that there is no block-level subexpression. |
| 942 | // The motivation is that this check requires a hashtable lookup. |
| 943 | |
| 944 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 945 | Dst.Add(Pred); |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | switch (S->getStmtClass()) { |
| 950 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 951 | |
| 952 | if (cast<BinaryOperator>(S)->isLogicalOp()) { |
| 953 | VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst); |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | // Fall-through. |
| 958 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 959 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 960 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 961 | break; |
| 962 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 963 | case Stmt::UnaryOperatorClass: |
| 964 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
| 965 | break; |
| 966 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 967 | case Stmt::ParenExprClass: |
| 968 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 969 | break; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 970 | |
| 971 | case Stmt::DeclRefExprClass: |
| 972 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 973 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 974 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 975 | case Stmt::ImplicitCastExprClass: { |
| 976 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 977 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | case Stmt::CastExprClass: { |
| 982 | CastExpr* C = cast<CastExpr>(S); |
| 983 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 984 | break; |
| 985 | } |
| 986 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 987 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 988 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 989 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 990 | break; |
| 991 | } |
| 992 | |
| 993 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 994 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 995 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 996 | break; |
| 997 | } |
| 998 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 999 | case Stmt::ReturnStmtClass: |
| 1000 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1001 | Visit(R, Pred, Dst); |
| 1002 | else |
| 1003 | Dst.Add(Pred); |
| 1004 | |
| 1005 | break; |
| 1006 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1007 | case Stmt::DeclStmtClass: |
| 1008 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1009 | break; |
| 1010 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1011 | default: |
| 1012 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1013 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1014 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1017 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1018 | // "Assume" logic. |
| 1019 | //===----------------------------------------------------------------------===// |
| 1020 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1021 | GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, |
| 1022 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1023 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1024 | |
| 1025 | switch (Cond.getSubKind()) { |
| 1026 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1027 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1028 | return St; |
| 1029 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1030 | case lval::SymbolValKind: |
| 1031 | if (Assumption) |
| 1032 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1033 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1034 | else |
| 1035 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1036 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1037 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1038 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1039 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1040 | isFeasible = Assumption; |
| 1041 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1042 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1043 | case lval::ConcreteIntKind: { |
| 1044 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1045 | isFeasible = b ? Assumption : !Assumption; |
| 1046 | return St; |
| 1047 | } |
| 1048 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1051 | GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, |
| 1052 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1053 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1054 | |
| 1055 | switch (Cond.getSubKind()) { |
| 1056 | default: |
| 1057 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 1058 | return St; |
| 1059 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1060 | |
| 1061 | case nonlval::SymbolValKind: { |
| 1062 | lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond); |
| 1063 | SymbolID sym = SV.getSymbol(); |
| 1064 | |
| 1065 | if (Assumption) |
| 1066 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1067 | isFeasible); |
| 1068 | else |
| 1069 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1070 | isFeasible); |
| 1071 | } |
| 1072 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1073 | case nonlval::SymIntConstraintValKind: |
| 1074 | return |
| 1075 | AssumeSymInt(St, Assumption, |
| 1076 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1077 | isFeasible); |
| 1078 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1079 | case nonlval::ConcreteIntKind: { |
| 1080 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1081 | isFeasible = b ? Assumption : !Assumption; |
| 1082 | return St; |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1087 | GRConstants::StateTy |
| 1088 | GRConstants::AssumeSymNE(StateTy St, SymbolID sym, |
| 1089 | const llvm::APSInt& V, bool& isFeasible) { |
| 1090 | |
| 1091 | // First, determine if sym == X, where X != V. |
| 1092 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1093 | isFeasible = *X != V; |
| 1094 | return St; |
| 1095 | } |
| 1096 | |
| 1097 | // Second, determine if sym != V. |
| 1098 | if (St.isNotEqual(sym, V)) { |
| 1099 | isFeasible = true; |
| 1100 | return St; |
| 1101 | } |
| 1102 | |
| 1103 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1104 | // Make that assumption. |
| 1105 | |
| 1106 | isFeasible = true; |
| 1107 | return StateMgr.AddNE(St, sym, V); |
| 1108 | } |
| 1109 | |
| 1110 | GRConstants::StateTy |
| 1111 | GRConstants::AssumeSymEQ(StateTy St, SymbolID sym, |
| 1112 | const llvm::APSInt& V, bool& isFeasible) { |
| 1113 | |
| 1114 | // First, determine if sym == X, where X != V. |
| 1115 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1116 | isFeasible = *X == V; |
| 1117 | return St; |
| 1118 | } |
| 1119 | |
| 1120 | // Second, determine if sym != V. |
| 1121 | if (St.isNotEqual(sym, V)) { |
| 1122 | isFeasible = false; |
| 1123 | return St; |
| 1124 | } |
| 1125 | |
| 1126 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1127 | // Make that assumption. |
| 1128 | |
| 1129 | isFeasible = true; |
| 1130 | return StateMgr.AddEQ(St, sym, V); |
| 1131 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1132 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1133 | GRConstants::StateTy |
| 1134 | GRConstants::AssumeSymInt(StateTy St, bool Assumption, |
| 1135 | const SymIntConstraint& C, bool& isFeasible) { |
| 1136 | |
| 1137 | switch (C.getOpcode()) { |
| 1138 | default: |
| 1139 | // No logic yet for other operators. |
| 1140 | return St; |
| 1141 | |
| 1142 | case BinaryOperator::EQ: |
| 1143 | if (Assumption) |
| 1144 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1145 | else |
| 1146 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1147 | |
| 1148 | case BinaryOperator::NE: |
| 1149 | if (Assumption) |
| 1150 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1151 | else |
| 1152 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1153 | } |
| 1154 | } |
| 1155 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1156 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1157 | // Driver. |
| 1158 | //===----------------------------------------------------------------------===// |
| 1159 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1160 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1161 | static GRConstants* GraphPrintCheckerState; |
| 1162 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1163 | namespace llvm { |
| 1164 | template<> |
| 1165 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 1166 | public DefaultDOTGraphTraits { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1167 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1168 | static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1169 | switch (kind) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1170 | case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break; |
| 1171 | case VarBindKey::IsDecl: Out << "Variables:\\l"; break; |
| 1172 | case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 1173 | default: assert (false && "Unknown VarBindKey type."); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1177 | static void PrintKind(std::ostream& Out, GRConstants::StateTy M, |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1178 | VarBindKey::Kind kind, bool isFirstGroup = false) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1179 | bool isFirst = true; |
| 1180 | |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 1181 | for (GRConstants::StateTy::vb_iterator I=M.begin(), E=M.end();I!=E;++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1182 | if (I.getKey().getKind() != kind) |
| 1183 | continue; |
| 1184 | |
| 1185 | if (isFirst) { |
| 1186 | if (!isFirstGroup) Out << "\\l\\l"; |
| 1187 | PrintKindLabel(Out, kind); |
| 1188 | isFirst = false; |
| 1189 | } |
| 1190 | else |
| 1191 | Out << "\\l"; |
| 1192 | |
| 1193 | Out << ' '; |
| 1194 | |
| 1195 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) |
| 1196 | Out << V->getName(); |
| 1197 | else { |
| 1198 | Stmt* E = cast<Stmt>(I.getKey()); |
| 1199 | Out << " (" << (void*) E << ") "; |
| 1200 | E->printPretty(Out); |
| 1201 | } |
| 1202 | |
| 1203 | Out << " : "; |
| 1204 | I.getData().print(Out); |
| 1205 | } |
| 1206 | } |
| 1207 | |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1208 | static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) { |
| 1209 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1210 | |
| 1211 | if (CE.isEmpty()) |
| 1212 | return; |
| 1213 | |
| 1214 | Out << "\\l\\|'==' constraints:"; |
| 1215 | |
| 1216 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1217 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1218 | } |
| 1219 | |
| 1220 | static void PrintNE(std::ostream& Out, GRConstants::StateTy St) { |
| 1221 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1222 | |
| 1223 | if (NE.isEmpty()) |
| 1224 | return; |
| 1225 | |
| 1226 | Out << "\\l\\|'!=' constraints:"; |
| 1227 | |
| 1228 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1229 | I != EI; ++I){ |
| 1230 | |
| 1231 | Out << "\\l $" << I.getKey() << " : "; |
| 1232 | bool isFirst = true; |
| 1233 | |
| 1234 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1235 | EJ=I.getData().end(); |
| 1236 | for ( ; J != EJ; ++J) { |
| 1237 | if (isFirst) isFirst = false; |
| 1238 | else Out << ", "; |
| 1239 | |
| 1240 | Out << (*J)->toString(); |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1245 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 1246 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1249 | ProgramPoint Loc = N->getLocation(); |
| 1250 | |
| 1251 | switch (Loc.getKind()) { |
| 1252 | case ProgramPoint::BlockEntranceKind: |
| 1253 | Out << "Block Entrance: B" |
| 1254 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1255 | break; |
| 1256 | |
| 1257 | case ProgramPoint::BlockExitKind: |
| 1258 | assert (false); |
| 1259 | break; |
| 1260 | |
| 1261 | case ProgramPoint::PostStmtKind: { |
| 1262 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1263 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1264 | << (void*) L.getStmt() << ' '; |
| 1265 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1266 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1267 | |
| 1268 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1269 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1270 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame^] | 1271 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1272 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1273 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1274 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1275 | break; |
| 1276 | } |
| 1277 | |
| 1278 | default: { |
| 1279 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1280 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1281 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1282 | |
| 1283 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1284 | Out << "\\|Terminator: "; |
| 1285 | E.getSrc()->printTerminator(Out); |
| 1286 | |
| 1287 | if (isa<SwitchStmt>(T)) { |
| 1288 | // FIXME |
| 1289 | } |
| 1290 | else { |
| 1291 | Out << "\\lCondition: "; |
| 1292 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1293 | Out << "true"; |
| 1294 | else |
| 1295 | Out << "false"; |
| 1296 | } |
| 1297 | |
| 1298 | Out << "\\l"; |
| 1299 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1300 | |
| 1301 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1302 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1303 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1307 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1308 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1309 | PrintKind(Out, N->getState(), VarBindKey::IsDecl, true); |
| 1310 | PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr); |
| 1311 | PrintKind(Out, N->getState(), VarBindKey::IsSubExpr); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1312 | |
| 1313 | PrintEQ(Out, N->getState()); |
| 1314 | PrintNE(Out, N->getState()); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1315 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1316 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1317 | return Out.str(); |
| 1318 | } |
| 1319 | }; |
| 1320 | } // end llvm namespace |
| 1321 | #endif |
| 1322 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1323 | namespace clang { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 1324 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) { |
| 1325 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1326 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1327 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1328 | GraphPrintCheckerState = &Engine.getCheckerState(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1329 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1330 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1331 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1332 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1333 | } // end clang namespace |