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