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