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 | |
| 335 | if (!LV.isValid()) |
| 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 | |
| 393 | case RValue::InvalidKind: |
| 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 | |
| 442 | if (isa<InvalidValue>(R1) && |
| 443 | (isa<InvalidValue>(R2) || |
| 444 | isa<UninitializedValue>(R2))) { |
| 445 | |
| 446 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 447 | return; |
| 448 | } |
| 449 | else if (isa<UninitializedValue>(R1)) { |
| 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 | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 523 | for (StateTy::vb_iterator I = M.begin(), E = M.end(); |
| 524 | I!=E && !I.getKey().isSymbol(); ++I) { |
| 525 | |
| 526 | // Remove old bindings for subexpressions. |
| 527 | if (I.getKey().isSubExpr()) { |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 528 | M = StateMgr.Remove(M, I.getKey()); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 529 | continue; |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 530 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 531 | |
| 532 | if (I.getKey().isBlkExpr()) { |
| 533 | if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) { |
| 534 | if (isa<lval::DeclVal>(I.getData())) { |
| 535 | lval::DeclVal LV = cast<lval::DeclVal>(I.getData()); |
| 536 | WList.push_back(LV.getDecl()); |
| 537 | } |
| 538 | } |
| 539 | else |
| 540 | M = StateMgr.Remove(M, I.getKey()); |
| 541 | |
| 542 | continue; |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 543 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 544 | |
| 545 | assert (I.getKey().isDecl()); |
| 546 | |
| 547 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 548 | if (Liveness.isLive(Loc, V)) |
| 549 | WList.push_back(V); |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 550 | } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 551 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 552 | llvm::SmallPtrSet<ValueDecl*, 10> Marked; |
| 553 | |
| 554 | while (!WList.empty()) { |
| 555 | ValueDecl* V = WList.back(); |
| 556 | WList.pop_back(); |
| 557 | |
| 558 | if (Marked.count(V)) |
| 559 | continue; |
| 560 | |
| 561 | Marked.insert(V); |
| 562 | |
| 563 | if (V->getType()->isPointerType()) { |
| 564 | const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V))); |
| 565 | |
| 566 | if (!isa<lval::DeclVal>(LV)) |
| 567 | continue; |
| 568 | |
| 569 | const lval::DeclVal& LVD = cast<lval::DeclVal>(LV); |
| 570 | WList.push_back(LVD.getDecl()); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I) |
| 575 | if (I.getKey().isDecl()) |
| 576 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 577 | if (!Marked.count(V)) |
| 578 | M = StateMgr.Remove(M, V); |
| 579 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 580 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 583 | GRConstants::NodeTy* |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame^] | 584 | GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 585 | |
| 586 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame^] | 587 | if (St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 588 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 589 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 590 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 591 | Dst.Add(N); |
| 592 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 593 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 594 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 595 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
| 596 | const StateTy::BufferTy& SB) { |
| 597 | |
| 598 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 599 | Nodify(Dst, S, Pred, *I); |
| 600 | } |
| 601 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 602 | void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) { |
| 603 | if (D != CurrentStmt) { |
| 604 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | // If we are here, we are loading the value of the decl and binding |
| 609 | // it to the block-level expression. |
| 610 | |
| 611 | StateTy St = Pred->getState(); |
| 612 | |
| 613 | Nodify(Dst, D, Pred, |
| 614 | SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl())))); |
| 615 | } |
| 616 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 617 | void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 618 | |
| 619 | QualType T = CastE->getType(); |
| 620 | |
| 621 | // Check for redundant casts. |
| 622 | if (E->getType() == T) { |
| 623 | Dst.Add(Pred); |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | NodeSet S1; |
| 628 | Visit(E, Pred, S1); |
| 629 | |
| 630 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 631 | NodeTy* N = *I1; |
| 632 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 633 | const RValue& V = GetValue(St, E); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 634 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 635 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 639 | GRConstants::NodeSet& Dst) { |
| 640 | |
| 641 | StateTy St = Pred->getState(); |
| 642 | |
| 643 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 644 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 645 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 646 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 647 | E ? GetValue(St, E) : UninitializedValue()); |
| 648 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 649 | |
| 650 | Nodify(Dst, DS, Pred, St); |
| 651 | |
| 652 | if (Dst.empty()) |
| 653 | Dst.Add(Pred); |
| 654 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 655 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 656 | |
| 657 | void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 658 | NodeTy* Pred, NodeSet& Dst) { |
| 659 | |
| 660 | StateTy St = Pred->getState(); |
| 661 | |
| 662 | RValue R = GetValue(St, LHS); |
| 663 | if (isa<InvalidValue>(R)) R = GetValue(St, RHS); |
| 664 | |
| 665 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 666 | } |
| 667 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 668 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 669 | GRConstants::NodeTy* Pred, |
| 670 | GRConstants::NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 671 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 672 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 673 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 674 | |
| 675 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 676 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 677 | // probably just handle AddrOf and Deref in their own methods to make |
| 678 | // this cleaner. |
| 679 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 680 | isa<DeclRefExpr>(U->getSubExpr())) |
| 681 | S1.Add(Pred); |
| 682 | else |
| 683 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 684 | |
| 685 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 686 | NodeTy* N1 = *I1; |
| 687 | StateTy St = N1->getState(); |
| 688 | |
| 689 | switch (U->getOpcode()) { |
| 690 | case UnaryOperator::PostInc: { |
| 691 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 692 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 693 | |
| 694 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 695 | GetRValueConstant(1U, U)); |
| 696 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 697 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | case UnaryOperator::PostDec: { |
| 702 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 703 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 704 | |
| 705 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 706 | GetRValueConstant(1U, U)); |
| 707 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 708 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | case UnaryOperator::PreInc: { |
| 713 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 714 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 715 | |
| 716 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 717 | GetRValueConstant(1U, U)); |
| 718 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 719 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 720 | break; |
| 721 | } |
| 722 | |
| 723 | case UnaryOperator::PreDec: { |
| 724 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 725 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 726 | |
| 727 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 728 | GetRValueConstant(1U, U)); |
| 729 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 730 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 731 | break; |
| 732 | } |
| 733 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 734 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 735 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 736 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 737 | break; |
| 738 | } |
| 739 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 740 | case UnaryOperator::Not: { |
| 741 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 742 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 743 | break; |
| 744 | } |
| 745 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 746 | case UnaryOperator::LNot: { |
| 747 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 748 | // |
| 749 | // Note: technically we do "E == 0", but this is the same in the |
| 750 | // transfer functions as "0 == E". |
| 751 | |
| 752 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 753 | |
| 754 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 755 | const LValue& L1 = cast<LValue>(V1); |
| 756 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 757 | Nodify(Dst, U, N1, |
| 758 | SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 759 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 760 | } |
| 761 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 762 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 763 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 764 | Nodify(Dst, U, N1, |
| 765 | SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 766 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | break; |
| 770 | } |
| 771 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 772 | case UnaryOperator::AddrOf: { |
| 773 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 774 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | case UnaryOperator::Deref: { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 779 | // FIXME: Stop when dereferencing an uninitialized value. |
| 780 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 781 | |
| 782 | const RValue& V = GetValue(St, U->getSubExpr()); |
| 783 | const LValue& L1 = cast<LValue>(V); |
| 784 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 785 | // After a dereference, one of two possible situations arise: |
| 786 | // (1) A crash, because the pointer was NULL. |
| 787 | // (2) The pointer is not NULL, and the dereference works. |
| 788 | // |
| 789 | // We add these assumptions. |
| 790 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 791 | bool isFeasibleNotNull; |
| 792 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 793 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 794 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 795 | |
| 796 | if (isFeasibleNotNull) { |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 797 | QualType T = U->getType(); |
| 798 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 799 | GetValue(StNotNull, L1, &T))); |
| 800 | } |
| 801 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 802 | bool isFeasibleNull; |
| 803 | |
| 804 | // "Assume" that the pointer is NULL. |
| 805 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 806 | |
| 807 | if (isFeasibleNull) { |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame^] | 808 | // We don't use "Nodify" here because the node will be a sink |
| 809 | // and we have no intention of processing it later. |
| 810 | NodeTy* NullNode = Builder->generateNode(U, StNull, N1); |
| 811 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 812 | if (NullNode) { |
| 813 | NullNode->markAsSink(); |
| 814 | |
| 815 | if (isFeasibleNotNull) |
| 816 | ImplicitNullDeref.insert(NullNode); |
| 817 | else |
| 818 | ExplicitNullDeref.insert(NullNode); |
| 819 | } |
| 820 | } |
| 821 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 825 | default: ; |
| 826 | assert (false && "Not implemented."); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 831 | void GRConstants::VisitAssignmentLHS(Expr* E, GRConstants::NodeTy* Pred, |
| 832 | GRConstants::NodeSet& Dst) { |
| 833 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 834 | if (isa<DeclRefExpr>(E)) { |
| 835 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 836 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 837 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 838 | |
| 839 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 840 | if (U->getOpcode() == UnaryOperator::Deref) { |
| 841 | Visit(U->getSubExpr(), Pred, Dst); |
| 842 | return; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | Visit(E, Pred, Dst); |
| 847 | } |
| 848 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 849 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 850 | GRConstants::NodeTy* Pred, |
| 851 | GRConstants::NodeSet& Dst) { |
| 852 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 853 | |
| 854 | if (B->isAssignmentOp()) |
| 855 | VisitAssignmentLHS(B->getLHS(), Pred, S1); |
| 856 | else |
| 857 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 858 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 859 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 860 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 861 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 862 | // When getting the value for the LHS, check if we are in an assignment. |
| 863 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 864 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 865 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 866 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 867 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 868 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 869 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 870 | NodeSet S2; |
| 871 | Visit(B->getRHS(), N1, S2); |
| 872 | |
| 873 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 874 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 875 | NodeTy* N2 = *I2; |
| 876 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 877 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 878 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 879 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 880 | |
| 881 | if (Op <= BinaryOperator::Or) { |
| 882 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 883 | if (isa<InvalidValue>(V1) || isa<UninitializedValue>(V1)) { |
| 884 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 885 | continue; |
| 886 | } |
| 887 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 888 | if (isa<LValue>(V1)) { |
| 889 | // FIXME: Add support for RHS being a non-lvalue. |
| 890 | const LValue& L1 = cast<LValue>(V1); |
| 891 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 892 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 893 | Nodify(Dst, B, N2, SetValue(St, B, L1.EvalBinaryOp(ValMgr, Op, L2))); |
| 894 | } |
| 895 | else { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 896 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 897 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 898 | |
| 899 | Nodify(Dst, B, N2, SetValue(St, B, R1.EvalBinaryOp(ValMgr, Op, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 900 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 901 | |
| 902 | continue; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 903 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | switch (Op) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 907 | case BinaryOperator::Assign: { |
| 908 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | 3434b08 | 2008-02-06 04:41:14 +0000 | [diff] [blame] | 909 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 910 | break; |
| 911 | } |
| 912 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 913 | default: { // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 914 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 915 | assert (B->isCompoundAssignmentOp()); |
| 916 | |
| 917 | const LValue& L1 = cast<LValue>(V1); |
| 918 | RValue Result = cast<NonLValue>(InvalidValue()); |
| 919 | |
| 920 | Op = (BinaryOperator::Opcode) |
| 921 | (((unsigned) Op) - ((unsigned) BinaryOperator::MulAssign)); |
| 922 | |
| 923 | if (isa<LValue>(V2)) { |
| 924 | // FIXME: Add support for Non-LValues on RHS. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 925 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 926 | Result = L1.EvalBinaryOp(ValMgr, Op, L2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 927 | } |
| 928 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 929 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 930 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 931 | Result = R1.EvalBinaryOp(ValMgr, Op, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 934 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 935 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 936 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 937 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 938 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 939 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 940 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 941 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 942 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 943 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 944 | GRConstants::NodeSet& Dst) { |
| 945 | |
| 946 | // FIXME: add metadata to the CFG so that we can disable |
| 947 | // this check when we KNOW that there is no block-level subexpression. |
| 948 | // The motivation is that this check requires a hashtable lookup. |
| 949 | |
| 950 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 951 | Dst.Add(Pred); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | switch (S->getStmtClass()) { |
| 956 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 957 | |
| 958 | if (cast<BinaryOperator>(S)->isLogicalOp()) { |
| 959 | VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst); |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | // Fall-through. |
| 964 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 965 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 966 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 967 | break; |
| 968 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 969 | case Stmt::UnaryOperatorClass: |
| 970 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
| 971 | break; |
| 972 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 973 | case Stmt::ParenExprClass: |
| 974 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 975 | break; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 976 | |
| 977 | case Stmt::DeclRefExprClass: |
| 978 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 979 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 980 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 981 | case Stmt::ImplicitCastExprClass: { |
| 982 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 983 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 984 | break; |
| 985 | } |
| 986 | |
| 987 | case Stmt::CastExprClass: { |
| 988 | CastExpr* C = cast<CastExpr>(S); |
| 989 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 990 | break; |
| 991 | } |
| 992 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 993 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 994 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 995 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 996 | break; |
| 997 | } |
| 998 | |
| 999 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 1000 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1001 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1002 | break; |
| 1003 | } |
| 1004 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1005 | case Stmt::ReturnStmtClass: |
| 1006 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1007 | Visit(R, Pred, Dst); |
| 1008 | else |
| 1009 | Dst.Add(Pred); |
| 1010 | |
| 1011 | break; |
| 1012 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1013 | case Stmt::DeclStmtClass: |
| 1014 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1015 | break; |
| 1016 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1017 | default: |
| 1018 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1019 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1020 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1023 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1024 | // "Assume" logic. |
| 1025 | //===----------------------------------------------------------------------===// |
| 1026 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1027 | GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, |
| 1028 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1029 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1030 | |
| 1031 | switch (Cond.getSubKind()) { |
| 1032 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1033 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1034 | return St; |
| 1035 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1036 | case lval::SymbolValKind: |
| 1037 | if (Assumption) |
| 1038 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1039 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1040 | else |
| 1041 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1042 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1043 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1044 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1045 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1046 | isFeasible = Assumption; |
| 1047 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1048 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1049 | case lval::ConcreteIntKind: { |
| 1050 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1051 | isFeasible = b ? Assumption : !Assumption; |
| 1052 | return St; |
| 1053 | } |
| 1054 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1057 | GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, |
| 1058 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1059 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1060 | |
| 1061 | switch (Cond.getSubKind()) { |
| 1062 | default: |
| 1063 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 1064 | return St; |
| 1065 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1066 | |
| 1067 | case nonlval::SymbolValKind: { |
| 1068 | lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond); |
| 1069 | SymbolID sym = SV.getSymbol(); |
| 1070 | |
| 1071 | if (Assumption) |
| 1072 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1073 | isFeasible); |
| 1074 | else |
| 1075 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1076 | isFeasible); |
| 1077 | } |
| 1078 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1079 | case nonlval::SymIntConstraintValKind: |
| 1080 | return |
| 1081 | AssumeSymInt(St, Assumption, |
| 1082 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1083 | isFeasible); |
| 1084 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1085 | case nonlval::ConcreteIntKind: { |
| 1086 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1087 | isFeasible = b ? Assumption : !Assumption; |
| 1088 | return St; |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1093 | GRConstants::StateTy |
| 1094 | GRConstants::AssumeSymNE(StateTy St, SymbolID sym, |
| 1095 | const llvm::APSInt& V, bool& isFeasible) { |
| 1096 | |
| 1097 | // First, determine if sym == X, where X != V. |
| 1098 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1099 | isFeasible = *X != V; |
| 1100 | return St; |
| 1101 | } |
| 1102 | |
| 1103 | // Second, determine if sym != V. |
| 1104 | if (St.isNotEqual(sym, V)) { |
| 1105 | isFeasible = true; |
| 1106 | return St; |
| 1107 | } |
| 1108 | |
| 1109 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1110 | // Make that assumption. |
| 1111 | |
| 1112 | isFeasible = true; |
| 1113 | return StateMgr.AddNE(St, sym, V); |
| 1114 | } |
| 1115 | |
| 1116 | GRConstants::StateTy |
| 1117 | GRConstants::AssumeSymEQ(StateTy St, SymbolID sym, |
| 1118 | const llvm::APSInt& V, bool& isFeasible) { |
| 1119 | |
| 1120 | // First, determine if sym == X, where X != V. |
| 1121 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1122 | isFeasible = *X == V; |
| 1123 | return St; |
| 1124 | } |
| 1125 | |
| 1126 | // Second, determine if sym != V. |
| 1127 | if (St.isNotEqual(sym, V)) { |
| 1128 | isFeasible = false; |
| 1129 | return St; |
| 1130 | } |
| 1131 | |
| 1132 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1133 | // Make that assumption. |
| 1134 | |
| 1135 | isFeasible = true; |
| 1136 | return StateMgr.AddEQ(St, sym, V); |
| 1137 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1138 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1139 | GRConstants::StateTy |
| 1140 | GRConstants::AssumeSymInt(StateTy St, bool Assumption, |
| 1141 | const SymIntConstraint& C, bool& isFeasible) { |
| 1142 | |
| 1143 | switch (C.getOpcode()) { |
| 1144 | default: |
| 1145 | // No logic yet for other operators. |
| 1146 | return St; |
| 1147 | |
| 1148 | case BinaryOperator::EQ: |
| 1149 | if (Assumption) |
| 1150 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1151 | else |
| 1152 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1153 | |
| 1154 | case BinaryOperator::NE: |
| 1155 | if (Assumption) |
| 1156 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1157 | else |
| 1158 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1159 | } |
| 1160 | } |
| 1161 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1162 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1163 | // Driver. |
| 1164 | //===----------------------------------------------------------------------===// |
| 1165 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1166 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1167 | static GRConstants* GraphPrintCheckerState; |
| 1168 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1169 | namespace llvm { |
| 1170 | template<> |
| 1171 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 1172 | public DefaultDOTGraphTraits { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1173 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1174 | static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1175 | switch (kind) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1176 | case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break; |
| 1177 | case VarBindKey::IsDecl: Out << "Variables:\\l"; break; |
| 1178 | case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 1179 | default: assert (false && "Unknown VarBindKey type."); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1183 | static void PrintKind(std::ostream& Out, GRConstants::StateTy M, |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1184 | VarBindKey::Kind kind, bool isFirstGroup = false) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1185 | bool isFirst = true; |
| 1186 | |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 1187 | 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] | 1188 | if (I.getKey().getKind() != kind) |
| 1189 | continue; |
| 1190 | |
| 1191 | if (isFirst) { |
| 1192 | if (!isFirstGroup) Out << "\\l\\l"; |
| 1193 | PrintKindLabel(Out, kind); |
| 1194 | isFirst = false; |
| 1195 | } |
| 1196 | else |
| 1197 | Out << "\\l"; |
| 1198 | |
| 1199 | Out << ' '; |
| 1200 | |
| 1201 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) |
| 1202 | Out << V->getName(); |
| 1203 | else { |
| 1204 | Stmt* E = cast<Stmt>(I.getKey()); |
| 1205 | Out << " (" << (void*) E << ") "; |
| 1206 | E->printPretty(Out); |
| 1207 | } |
| 1208 | |
| 1209 | Out << " : "; |
| 1210 | I.getData().print(Out); |
| 1211 | } |
| 1212 | } |
| 1213 | |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1214 | static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) { |
| 1215 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1216 | |
| 1217 | if (CE.isEmpty()) |
| 1218 | return; |
| 1219 | |
| 1220 | Out << "\\l\\|'==' constraints:"; |
| 1221 | |
| 1222 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1223 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1224 | } |
| 1225 | |
| 1226 | static void PrintNE(std::ostream& Out, GRConstants::StateTy St) { |
| 1227 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1228 | |
| 1229 | if (NE.isEmpty()) |
| 1230 | return; |
| 1231 | |
| 1232 | Out << "\\l\\|'!=' constraints:"; |
| 1233 | |
| 1234 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1235 | I != EI; ++I){ |
| 1236 | |
| 1237 | Out << "\\l $" << I.getKey() << " : "; |
| 1238 | bool isFirst = true; |
| 1239 | |
| 1240 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1241 | EJ=I.getData().end(); |
| 1242 | for ( ; J != EJ; ++J) { |
| 1243 | if (isFirst) isFirst = false; |
| 1244 | else Out << ", "; |
| 1245 | |
| 1246 | Out << (*J)->toString(); |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1251 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 1252 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1253 | |
| 1254 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1255 | ProgramPoint Loc = N->getLocation(); |
| 1256 | |
| 1257 | switch (Loc.getKind()) { |
| 1258 | case ProgramPoint::BlockEntranceKind: |
| 1259 | Out << "Block Entrance: B" |
| 1260 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1261 | break; |
| 1262 | |
| 1263 | case ProgramPoint::BlockExitKind: |
| 1264 | assert (false); |
| 1265 | break; |
| 1266 | |
| 1267 | case ProgramPoint::PostStmtKind: { |
| 1268 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1269 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1270 | << (void*) L.getStmt() << ' '; |
| 1271 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1272 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1273 | |
| 1274 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1275 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1276 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1277 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1278 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1279 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1280 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | } |
| 1283 | |
| 1284 | default: { |
| 1285 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1286 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1287 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1288 | |
| 1289 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1290 | Out << "\\|Terminator: "; |
| 1291 | E.getSrc()->printTerminator(Out); |
| 1292 | |
| 1293 | if (isa<SwitchStmt>(T)) { |
| 1294 | // FIXME |
| 1295 | } |
| 1296 | else { |
| 1297 | Out << "\\lCondition: "; |
| 1298 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1299 | Out << "true"; |
| 1300 | else |
| 1301 | Out << "false"; |
| 1302 | } |
| 1303 | |
| 1304 | Out << "\\l"; |
| 1305 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1306 | |
| 1307 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1308 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1309 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1313 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1314 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1315 | PrintKind(Out, N->getState(), VarBindKey::IsDecl, true); |
| 1316 | PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr); |
| 1317 | PrintKind(Out, N->getState(), VarBindKey::IsSubExpr); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1318 | |
| 1319 | PrintEQ(Out, N->getState()); |
| 1320 | PrintNE(Out, N->getState()); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1321 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1322 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1323 | return Out.str(); |
| 1324 | } |
| 1325 | }; |
| 1326 | } // end llvm namespace |
| 1327 | #endif |
| 1328 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1329 | namespace clang { |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1330 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx, |
| 1331 | Diagnostic& Diag) { |
| 1332 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 1333 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1334 | Engine.ExecuteWorkList(); |
| 1335 | |
| 1336 | // Look for explicit-Null dereferences and warn about them. |
| 1337 | GRConstants* CheckerState = &Engine.getCheckerState(); |
| 1338 | |
| 1339 | for (GRConstants::null_iterator I=CheckerState->null_begin(), |
| 1340 | E=CheckerState->null_end(); I!=E; ++I) { |
| 1341 | |
| 1342 | const PostStmt& L = cast<PostStmt>((*I)->getLocation()); |
| 1343 | Expr* E = cast<Expr>(L.getStmt()); |
| 1344 | |
| 1345 | Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()), |
| 1346 | diag::chkr_null_deref_after_check); |
| 1347 | } |
| 1348 | |
| 1349 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1350 | #ifndef NDEBUG |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame] | 1351 | GraphPrintCheckerState = CheckerState; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1352 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1353 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1354 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1355 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1356 | } // end clang namespace |