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