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