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