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