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 | |
| 259 | /// VisitDeclStmt - Transfer function logic for DeclStmts. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 260 | void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst); |
| 261 | |
| 262 | /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose |
| 263 | void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 264 | NodeTy* Pred, NodeSet& Dst); |
| 265 | |
| 266 | /// VisitLogicalExpr - Transfer function logic for '&&', '||' |
| 267 | void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 268 | }; |
| 269 | } // end anonymous namespace |
| 270 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 272 | GRConstants::StateTy |
| 273 | GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) { |
| 274 | |
| 275 | if (!StateCleaned) { |
| 276 | St = RemoveDeadBindings(CurrentStmt, St); |
| 277 | StateCleaned = true; |
| 278 | } |
| 279 | |
| 280 | bool isBlkExpr = false; |
| 281 | |
| 282 | if (S == CurrentStmt) { |
| 283 | isBlkExpr = getCFG().isBlkExpr(S); |
| 284 | |
| 285 | if (!isBlkExpr) |
| 286 | return St; |
| 287 | } |
| 288 | |
| 289 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 290 | } |
| 291 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 292 | const GRConstants::StateTy::BufferTy& |
| 293 | GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB, |
| 294 | StateTy::BufferTy& RetBuf) { |
| 295 | |
| 296 | assert (RetBuf.empty()); |
| 297 | |
| 298 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 299 | RetBuf.push_back(SetValue(St, S, *I)); |
| 300 | |
| 301 | return RetBuf; |
| 302 | } |
| 303 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 304 | GRConstants::StateTy |
| 305 | GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
| 306 | |
| 307 | if (!LV.isValid()) |
| 308 | return St; |
| 309 | |
| 310 | if (!StateCleaned) { |
| 311 | St = RemoveDeadBindings(CurrentStmt, St); |
| 312 | StateCleaned = true; |
| 313 | } |
| 314 | |
| 315 | return StateMgr.SetValue(St, LV, V); |
| 316 | } |
| 317 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 318 | void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 319 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 320 | |
| 321 | StateTy PrevState = builder.getState(); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 323 | // Remove old bindings for subexpressions. |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 324 | 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] | 325 | if (I.getKey().isSubExpr()) |
| 326 | PrevState = StateMgr.Remove(PrevState, I.getKey()); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 327 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 328 | // Remove terminator-specific bindings. |
| 329 | switch (Term->getStmtClass()) { |
| 330 | default: break; |
| 331 | |
| 332 | case Stmt::BinaryOperatorClass: { // '&&', '||' |
| 333 | BinaryOperator* B = cast<BinaryOperator>(Term); |
| 334 | // FIXME: Liveness analysis should probably remove these automatically. |
| 335 | // Verify later when we converge to an 'optimization' stage. |
| 336 | PrevState = StateMgr.Remove(PrevState, B->getRHS()); |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 341 | ConditionalOperator* C = cast<ConditionalOperator>(Term); |
| 342 | // FIXME: Liveness analysis should probably remove these automatically. |
| 343 | // Verify later when we converge to an 'optimization' stage. |
| 344 | if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L); |
| 345 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 350 | ChooseExpr* C = cast<ChooseExpr>(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, C->getRHS()); |
| 354 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 359 | RValue V = GetValue(PrevState, Condition); |
| 360 | |
| 361 | switch (V.getBaseKind()) { |
| 362 | default: |
| 363 | break; |
| 364 | |
| 365 | case RValue::InvalidKind: |
| 366 | builder.generateNode(PrevState, true); |
| 367 | builder.generateNode(PrevState, false); |
| 368 | return; |
| 369 | |
| 370 | case RValue::UninitializedKind: { |
| 371 | NodeTy* N = builder.generateNode(PrevState, true); |
| 372 | |
| 373 | if (N) { |
| 374 | N->markAsSink(); |
| 375 | UninitBranches.insert(N); |
| 376 | } |
| 377 | |
| 378 | builder.markInfeasible(false); |
| 379 | return; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Process the true branch. |
| 384 | bool isFeasible = true; |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 385 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 386 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 387 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 388 | if (isFeasible) |
| 389 | builder.generateNode(St, true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 390 | else { |
| 391 | builder.markInfeasible(true); |
| 392 | isFeasible = true; |
| 393 | } |
| 394 | |
| 395 | // Process the false branch. |
| 396 | St = Assume(PrevState, V, false, isFeasible); |
| 397 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 398 | if (isFeasible) |
| 399 | builder.generateNode(St, false); |
| 400 | else |
| 401 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 404 | |
| 405 | void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
| 406 | NodeSet& Dst) { |
| 407 | |
| 408 | bool hasR2; |
| 409 | StateTy PrevState = Pred->getState(); |
| 410 | |
| 411 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 412 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 413 | |
| 414 | if (isa<InvalidValue>(R1) && |
| 415 | (isa<InvalidValue>(R2) || |
| 416 | isa<UninitializedValue>(R2))) { |
| 417 | |
| 418 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 419 | return; |
| 420 | } |
| 421 | else if (isa<UninitializedValue>(R1)) { |
| 422 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 427 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 428 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 429 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 430 | if (hasR2 == false) { |
| 431 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 432 | return; |
| 433 | } |
| 434 | } |
| 435 | else { |
| 436 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 437 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 438 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 439 | if (hasR2 == false) { |
| 440 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 441 | return; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 446 | // R2 == false. |
| 447 | |
| 448 | bool isFeasible; |
| 449 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 450 | |
| 451 | if (isFeasible) |
| 452 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 453 | |
| 454 | St = Assume(PrevState, R2, false, isFeasible); |
| 455 | |
| 456 | if (isFeasible) |
| 457 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 462 | void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 463 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 464 | |
| 465 | StmtEntryNode = builder.getLastNode(); |
| 466 | CurrentStmt = S; |
| 467 | NodeSet Dst; |
| 468 | StateCleaned = false; |
| 469 | |
| 470 | Visit(S, StmtEntryNode, Dst); |
| 471 | |
| 472 | // If no nodes were generated, generate a new node that has all the |
| 473 | // dead mappings removed. |
| 474 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 475 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 476 | builder.generateNode(S, St, StmtEntryNode); |
| 477 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 479 | CurrentStmt = NULL; |
| 480 | StmtEntryNode = NULL; |
| 481 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 484 | GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 485 | // Note: in the code below, we can assign a new map to M since the |
| 486 | // iterators are iterating over the tree of the *original* map. |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 487 | StateTy::vb_iterator I = M.begin(), E = M.end(); |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 488 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 490 | for (; I!=E && !I.getKey().isSymbol(); ++I) { |
| 491 | // Remove old bindings for subexpressions and "dead" |
| 492 | // block-level expressions. |
| 493 | if (I.getKey().isSubExpr() || |
| 494 | I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){ |
| 495 | M = StateMgr.Remove(M, I.getKey()); |
| 496 | } |
| 497 | else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls. |
| 498 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 499 | if (!Liveness.isLive(Loc, V)) |
| 500 | M = StateMgr.Remove(M, I.getKey()); |
| 501 | } |
| 502 | } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 503 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 504 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 507 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 508 | |
| 509 | // If the state hasn't changed, don't generate a new node. |
| 510 | if (St == Pred->getState()) |
| 511 | return; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 512 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 513 | Dst.Add(Builder->generateNode(S, St, Pred)); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 514 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 516 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
| 517 | const StateTy::BufferTy& SB) { |
| 518 | |
| 519 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 520 | Nodify(Dst, S, Pred, *I); |
| 521 | } |
| 522 | |
| 523 | void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 524 | |
| 525 | QualType T = CastE->getType(); |
| 526 | |
| 527 | // Check for redundant casts. |
| 528 | if (E->getType() == T) { |
| 529 | Dst.Add(Pred); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | NodeSet S1; |
| 534 | Visit(E, Pred, S1); |
| 535 | |
| 536 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 537 | NodeTy* N = *I1; |
| 538 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 539 | const RValue& V = GetValue(St, E); |
| 540 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 541 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 545 | GRConstants::NodeSet& Dst) { |
| 546 | |
| 547 | StateTy St = Pred->getState(); |
| 548 | |
| 549 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 550 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 551 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 552 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 553 | E ? GetValue(St, E) : UninitializedValue()); |
| 554 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 555 | |
| 556 | Nodify(Dst, DS, Pred, St); |
| 557 | |
| 558 | if (Dst.empty()) |
| 559 | Dst.Add(Pred); |
| 560 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 561 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 562 | |
| 563 | void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 564 | NodeTy* Pred, NodeSet& Dst) { |
| 565 | |
| 566 | StateTy St = Pred->getState(); |
| 567 | |
| 568 | RValue R = GetValue(St, LHS); |
| 569 | if (isa<InvalidValue>(R)) R = GetValue(St, RHS); |
| 570 | |
| 571 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 572 | } |
| 573 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 574 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 575 | GRConstants::NodeTy* Pred, |
| 576 | GRConstants::NodeSet& Dst) { |
| 577 | NodeSet S1; |
| 578 | Visit(U->getSubExpr(), Pred, S1); |
| 579 | |
| 580 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 581 | NodeTy* N1 = *I1; |
| 582 | StateTy St = N1->getState(); |
| 583 | |
| 584 | switch (U->getOpcode()) { |
| 585 | case UnaryOperator::PostInc: { |
| 586 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 587 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 0ff9a4d | 2008-02-05 00:43:43 +0000 | [diff] [blame] | 588 | NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U)); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 589 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | case UnaryOperator::PostDec: { |
| 594 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 595 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 0ff9a4d | 2008-02-05 00:43:43 +0000 | [diff] [blame] | 596 | NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U)); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 597 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | case UnaryOperator::PreInc: { |
| 602 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 603 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 0ff9a4d | 2008-02-05 00:43:43 +0000 | [diff] [blame] | 604 | NonLValue Result = R1.Add(ValMgr, GetRValueConstant(1U, U)); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 605 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | case UnaryOperator::PreDec: { |
| 610 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 611 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 0ff9a4d | 2008-02-05 00:43:43 +0000 | [diff] [blame] | 612 | NonLValue Result = R1.Sub(ValMgr, GetRValueConstant(1U, U)); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 613 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 614 | break; |
| 615 | } |
| 616 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 617 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 618 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
| 619 | Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 623 | case UnaryOperator::Not: { |
| 624 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
| 625 | Nodify(Dst, U, N1, SetValue(St, U, R1.BitwiseComplement(ValMgr))); |
| 626 | break; |
| 627 | } |
| 628 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame^] | 629 | case UnaryOperator::LNot: { |
| 630 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 631 | // |
| 632 | // Note: technically we do "E == 0", but this is the same in the |
| 633 | // transfer functions as "0 == E". |
| 634 | |
| 635 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 636 | |
| 637 | if (isa<LValue>(V1)) { |
| 638 | lval::ConcreteInt V2(ValMgr.getValue(0, U->getSubExpr()->getType())); |
| 639 | Nodify(Dst, U, N1, SetValue(St, U, |
| 640 | cast<LValue>(V1).EQ(ValMgr, V2))); |
| 641 | } |
| 642 | else { |
| 643 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 644 | Nodify(Dst, U, N1, SetValue(St, U, |
| 645 | cast<NonLValue>(V1).EQ(ValMgr, V2))); |
| 646 | } |
| 647 | |
| 648 | break; |
| 649 | } |
| 650 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 651 | case UnaryOperator::AddrOf: { |
| 652 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 653 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | case UnaryOperator::Deref: { |
| 658 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 659 | Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1))); |
| 660 | break; |
| 661 | } |
| 662 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 663 | default: ; |
| 664 | assert (false && "Not implemented."); |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 669 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 670 | GRConstants::NodeTy* Pred, |
| 671 | GRConstants::NodeSet& Dst) { |
| 672 | NodeSet S1; |
| 673 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 674 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 675 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 676 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 677 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 678 | // When getting the value for the LHS, check if we are in an assignment. |
| 679 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 680 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 681 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 682 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 683 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 684 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 685 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 686 | NodeSet S2; |
| 687 | Visit(B->getRHS(), N1, S2); |
| 688 | |
| 689 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
| 690 | NodeTy* N2 = *I2; |
| 691 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 692 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 693 | |
| 694 | switch (B->getOpcode()) { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 695 | default: |
| 696 | Dst.Add(N2); |
| 697 | break; |
| 698 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 699 | // Arithmetic operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 700 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 701 | case BinaryOperator::Add: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 702 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 703 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 704 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 705 | Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | |
| 709 | case BinaryOperator::Sub: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 710 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 711 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 712 | Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 716 | case BinaryOperator::Mul: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 717 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 718 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 719 | Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2))); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 720 | break; |
| 721 | } |
| 722 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 723 | case BinaryOperator::Div: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 724 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 725 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 726 | Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2))); |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 727 | break; |
| 728 | } |
| 729 | |
Ted Kremenek | cce207d | 2008-01-28 22:26:15 +0000 | [diff] [blame] | 730 | case BinaryOperator::Rem: { |
| 731 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 732 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 733 | Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2))); |
| 734 | break; |
| 735 | } |
| 736 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 737 | // Assignment operators. |
| 738 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 739 | case BinaryOperator::Assign: { |
| 740 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 3434b08 | 2008-02-06 04:41:14 +0000 | [diff] [blame] | 741 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 742 | break; |
| 743 | } |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 744 | |
| 745 | case BinaryOperator::AddAssign: { |
| 746 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 747 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 748 | NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 749 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 750 | break; |
| 751 | } |
| 752 | |
| 753 | case BinaryOperator::SubAssign: { |
| 754 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 755 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 756 | NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 757 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 758 | break; |
| 759 | } |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 760 | |
| 761 | case BinaryOperator::MulAssign: { |
| 762 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 763 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 764 | NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 765 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 766 | break; |
| 767 | } |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 768 | |
| 769 | case BinaryOperator::DivAssign: { |
| 770 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 771 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 772 | NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 773 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 774 | break; |
| 775 | } |
Ted Kremenek | 10099a6 | 2008-01-28 22:28:54 +0000 | [diff] [blame] | 776 | |
| 777 | case BinaryOperator::RemAssign: { |
| 778 | const LValue& L1 = cast<LValue>(V1); |
| 779 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 780 | NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2)); |
| 781 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 782 | break; |
| 783 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 784 | |
| 785 | // Equality operators. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 786 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 787 | case BinaryOperator::EQ: |
| 788 | // FIXME: should we allow XX.EQ() to return a set of values, |
| 789 | // allowing state bifurcation? In such cases, they will also |
| 790 | // modify the state (meaning that a new state will be returned |
| 791 | // as well). |
| 792 | assert (B->getType() == getContext().IntTy); |
| 793 | |
| 794 | if (isa<LValue>(V1)) { |
| 795 | const LValue& L1 = cast<LValue>(V1); |
| 796 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 797 | Nodify(Dst, B, N2, SetValue(St, B, L1.EQ(ValMgr, L2))); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 798 | } |
| 799 | else { |
| 800 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 801 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 802 | Nodify(Dst, B, N2, SetValue(St, B, R1.EQ(ValMgr, R2))); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 805 | break; |
| 806 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 807 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 808 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 809 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 810 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 811 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 812 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 813 | GRConstants::NodeSet& Dst) { |
| 814 | |
| 815 | // FIXME: add metadata to the CFG so that we can disable |
| 816 | // this check when we KNOW that there is no block-level subexpression. |
| 817 | // The motivation is that this check requires a hashtable lookup. |
| 818 | |
| 819 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 820 | Dst.Add(Pred); |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | switch (S->getStmtClass()) { |
| 825 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 826 | |
| 827 | if (cast<BinaryOperator>(S)->isLogicalOp()) { |
| 828 | VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst); |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | // Fall-through. |
| 833 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 834 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 835 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 836 | break; |
| 837 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 838 | case Stmt::UnaryOperatorClass: |
| 839 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
| 840 | break; |
| 841 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 842 | case Stmt::ParenExprClass: |
| 843 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 844 | break; |
| 845 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 846 | case Stmt::ImplicitCastExprClass: { |
| 847 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 848 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | case Stmt::CastExprClass: { |
| 853 | CastExpr* C = cast<CastExpr>(S); |
| 854 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 855 | break; |
| 856 | } |
| 857 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 858 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 859 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 860 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 865 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 866 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 867 | break; |
| 868 | } |
| 869 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 870 | case Stmt::DeclStmtClass: |
| 871 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 872 | break; |
| 873 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 874 | default: |
| 875 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 876 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 877 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 880 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 881 | // "Assume" logic. |
| 882 | //===----------------------------------------------------------------------===// |
| 883 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 884 | GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, |
| 885 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 886 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 887 | |
| 888 | switch (Cond.getSubKind()) { |
| 889 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 890 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 891 | return St; |
| 892 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 893 | case lval::SymbolValKind: |
| 894 | if (Assumption) |
| 895 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 896 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 897 | else |
| 898 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 899 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 900 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 901 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 902 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 903 | isFeasible = Assumption; |
| 904 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 905 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 906 | case lval::ConcreteIntKind: { |
| 907 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 908 | isFeasible = b ? Assumption : !Assumption; |
| 909 | return St; |
| 910 | } |
| 911 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 914 | GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, |
| 915 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 916 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 917 | |
| 918 | switch (Cond.getSubKind()) { |
| 919 | default: |
| 920 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 921 | return St; |
| 922 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 923 | |
| 924 | case nonlval::SymbolValKind: { |
| 925 | lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond); |
| 926 | SymbolID sym = SV.getSymbol(); |
| 927 | |
| 928 | if (Assumption) |
| 929 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 930 | isFeasible); |
| 931 | else |
| 932 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 933 | isFeasible); |
| 934 | } |
| 935 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 936 | case nonlval::SymIntConstraintValKind: |
| 937 | return |
| 938 | AssumeSymInt(St, Assumption, |
| 939 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 940 | isFeasible); |
| 941 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 942 | case nonlval::ConcreteIntKind: { |
| 943 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 944 | isFeasible = b ? Assumption : !Assumption; |
| 945 | return St; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 950 | GRConstants::StateTy |
| 951 | GRConstants::AssumeSymNE(StateTy St, SymbolID sym, |
| 952 | const llvm::APSInt& V, bool& isFeasible) { |
| 953 | |
| 954 | // First, determine if sym == X, where X != V. |
| 955 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 956 | isFeasible = *X != V; |
| 957 | return St; |
| 958 | } |
| 959 | |
| 960 | // Second, determine if sym != V. |
| 961 | if (St.isNotEqual(sym, V)) { |
| 962 | isFeasible = true; |
| 963 | return St; |
| 964 | } |
| 965 | |
| 966 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 967 | // Make that assumption. |
| 968 | |
| 969 | isFeasible = true; |
| 970 | return StateMgr.AddNE(St, sym, V); |
| 971 | } |
| 972 | |
| 973 | GRConstants::StateTy |
| 974 | GRConstants::AssumeSymEQ(StateTy St, SymbolID sym, |
| 975 | const llvm::APSInt& V, bool& isFeasible) { |
| 976 | |
| 977 | // First, determine if sym == X, where X != V. |
| 978 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 979 | isFeasible = *X == V; |
| 980 | return St; |
| 981 | } |
| 982 | |
| 983 | // Second, determine if sym != V. |
| 984 | if (St.isNotEqual(sym, V)) { |
| 985 | isFeasible = false; |
| 986 | return St; |
| 987 | } |
| 988 | |
| 989 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 990 | // Make that assumption. |
| 991 | |
| 992 | isFeasible = true; |
| 993 | return StateMgr.AddEQ(St, sym, V); |
| 994 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 995 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 996 | GRConstants::StateTy |
| 997 | GRConstants::AssumeSymInt(StateTy St, bool Assumption, |
| 998 | const SymIntConstraint& C, bool& isFeasible) { |
| 999 | |
| 1000 | switch (C.getOpcode()) { |
| 1001 | default: |
| 1002 | // No logic yet for other operators. |
| 1003 | return St; |
| 1004 | |
| 1005 | case BinaryOperator::EQ: |
| 1006 | if (Assumption) |
| 1007 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1008 | else |
| 1009 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1010 | |
| 1011 | case BinaryOperator::NE: |
| 1012 | if (Assumption) |
| 1013 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1014 | else |
| 1015 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1016 | } |
| 1017 | } |
| 1018 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1019 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1020 | // Driver. |
| 1021 | //===----------------------------------------------------------------------===// |
| 1022 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1023 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1024 | static GRConstants* GraphPrintCheckerState; |
| 1025 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1026 | namespace llvm { |
| 1027 | template<> |
| 1028 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 1029 | public DefaultDOTGraphTraits { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1030 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1031 | static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1032 | switch (kind) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1033 | case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break; |
| 1034 | case VarBindKey::IsDecl: Out << "Variables:\\l"; break; |
| 1035 | case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 1036 | default: assert (false && "Unknown VarBindKey type."); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1040 | static void PrintKind(std::ostream& Out, GRConstants::StateTy M, |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1041 | VarBindKey::Kind kind, bool isFirstGroup = false) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1042 | bool isFirst = true; |
| 1043 | |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 1044 | 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] | 1045 | if (I.getKey().getKind() != kind) |
| 1046 | continue; |
| 1047 | |
| 1048 | if (isFirst) { |
| 1049 | if (!isFirstGroup) Out << "\\l\\l"; |
| 1050 | PrintKindLabel(Out, kind); |
| 1051 | isFirst = false; |
| 1052 | } |
| 1053 | else |
| 1054 | Out << "\\l"; |
| 1055 | |
| 1056 | Out << ' '; |
| 1057 | |
| 1058 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) |
| 1059 | Out << V->getName(); |
| 1060 | else { |
| 1061 | Stmt* E = cast<Stmt>(I.getKey()); |
| 1062 | Out << " (" << (void*) E << ") "; |
| 1063 | E->printPretty(Out); |
| 1064 | } |
| 1065 | |
| 1066 | Out << " : "; |
| 1067 | I.getData().print(Out); |
| 1068 | } |
| 1069 | } |
| 1070 | |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1071 | static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) { |
| 1072 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1073 | |
| 1074 | if (CE.isEmpty()) |
| 1075 | return; |
| 1076 | |
| 1077 | Out << "\\l\\|'==' constraints:"; |
| 1078 | |
| 1079 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1080 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1081 | } |
| 1082 | |
| 1083 | static void PrintNE(std::ostream& Out, GRConstants::StateTy St) { |
| 1084 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1085 | |
| 1086 | if (NE.isEmpty()) |
| 1087 | return; |
| 1088 | |
| 1089 | Out << "\\l\\|'!=' constraints:"; |
| 1090 | |
| 1091 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1092 | I != EI; ++I){ |
| 1093 | |
| 1094 | Out << "\\l $" << I.getKey() << " : "; |
| 1095 | bool isFirst = true; |
| 1096 | |
| 1097 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1098 | EJ=I.getData().end(); |
| 1099 | for ( ; J != EJ; ++J) { |
| 1100 | if (isFirst) isFirst = false; |
| 1101 | else Out << ", "; |
| 1102 | |
| 1103 | Out << (*J)->toString(); |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1108 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 1109 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1112 | ProgramPoint Loc = N->getLocation(); |
| 1113 | |
| 1114 | switch (Loc.getKind()) { |
| 1115 | case ProgramPoint::BlockEntranceKind: |
| 1116 | Out << "Block Entrance: B" |
| 1117 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1118 | break; |
| 1119 | |
| 1120 | case ProgramPoint::BlockExitKind: |
| 1121 | assert (false); |
| 1122 | break; |
| 1123 | |
| 1124 | case ProgramPoint::PostStmtKind: { |
| 1125 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1126 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1127 | << (void*) L.getStmt() << ' '; |
| 1128 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1129 | L.getStmt()->printPretty(Out); |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | default: { |
| 1134 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1135 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1136 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1139 | Out << "\\|Terminator: "; |
| 1140 | E.getSrc()->printTerminator(Out); |
| 1141 | |
| 1142 | if (isa<SwitchStmt>(T)) { |
| 1143 | // FIXME |
| 1144 | } |
| 1145 | else { |
| 1146 | Out << "\\lCondition: "; |
| 1147 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1148 | Out << "true"; |
| 1149 | else |
| 1150 | Out << "false"; |
| 1151 | } |
| 1152 | |
| 1153 | Out << "\\l"; |
| 1154 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1155 | |
| 1156 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1157 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1158 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1162 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1163 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1164 | PrintKind(Out, N->getState(), VarBindKey::IsDecl, true); |
| 1165 | PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr); |
| 1166 | PrintKind(Out, N->getState(), VarBindKey::IsSubExpr); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1167 | |
| 1168 | PrintEQ(Out, N->getState()); |
| 1169 | PrintNE(Out, N->getState()); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1170 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1171 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1172 | return Out.str(); |
| 1173 | } |
| 1174 | }; |
| 1175 | } // end llvm namespace |
| 1176 | #endif |
| 1177 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1178 | namespace clang { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 1179 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) { |
| 1180 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1181 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1182 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1183 | GraphPrintCheckerState = &Engine.getCheckerState(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1184 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1185 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1186 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1187 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1188 | } // end clang namespace |