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 | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 263 | NodeTy* Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St, |
| 264 | bool AlwaysMakeNode = false); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 266 | /// Nodify - This version of Nodify is used to batch process a set of states. |
| 267 | /// The states are not guaranteed to be unique. |
| 268 | void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, const StateTy::BufferTy& SB); |
| 269 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 270 | /// Visit - Transfer function logic for all statements. Dispatches to |
| 271 | /// other functions that handle specific kinds of statements. |
| 272 | void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 273 | |
| 274 | /// VisitCast - Transfer function logic for all casts (implicit and explicit). |
| 275 | void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 277 | /// VisitUnaryOperator - Transfer function logic for unary operators. |
| 278 | void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 279 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 280 | /// VisitBinaryOperator - Transfer function logic for binary operators. |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 281 | void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 282 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 283 | void VisitAssignmentLHS(Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 284 | |
| 285 | /// VisitDeclRefExpr - Transfer function logic for DeclRefExprs. |
| 286 | void VisitDeclRefExpr(DeclRefExpr* DR, NodeTy* Pred, NodeSet& Dst); |
| 287 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 288 | /// VisitDeclStmt - Transfer function logic for DeclStmts. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 289 | void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst); |
| 290 | |
| 291 | /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose |
| 292 | void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 293 | NodeTy* Pred, NodeSet& Dst); |
| 294 | |
| 295 | /// VisitLogicalExpr - Transfer function logic for '&&', '||' |
| 296 | void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 297 | }; |
| 298 | } // end anonymous namespace |
| 299 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 301 | GRConstants::StateTy |
| 302 | GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 304 | if (!StateCleaned) { |
| 305 | St = RemoveDeadBindings(CurrentStmt, St); |
| 306 | StateCleaned = true; |
| 307 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 309 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 311 | if (S == CurrentStmt) { |
| 312 | isBlkExpr = getCFG().isBlkExpr(S); |
| 313 | |
| 314 | if (!isBlkExpr) |
| 315 | return St; |
| 316 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 318 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 319 | } |
| 320 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 321 | const GRConstants::StateTy::BufferTy& |
| 322 | GRConstants::SetValue(StateTy St, Stmt* S, const RValue::BufferTy& RB, |
| 323 | StateTy::BufferTy& RetBuf) { |
| 324 | |
| 325 | assert (RetBuf.empty()); |
| 326 | |
| 327 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 328 | RetBuf.push_back(SetValue(St, S, *I)); |
| 329 | |
| 330 | return RetBuf; |
| 331 | } |
| 332 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 333 | GRConstants::StateTy |
| 334 | GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
| 335 | |
| 336 | if (!LV.isValid()) |
| 337 | return St; |
| 338 | |
| 339 | if (!StateCleaned) { |
| 340 | St = RemoveDeadBindings(CurrentStmt, St); |
| 341 | StateCleaned = true; |
| 342 | } |
| 343 | |
| 344 | return StateMgr.SetValue(St, LV, V); |
| 345 | } |
| 346 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 347 | void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 348 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 349 | |
| 350 | StateTy PrevState = builder.getState(); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 351 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 352 | // Remove old bindings for subexpressions. |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 353 | 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] | 354 | if (I.getKey().isSubExpr()) |
| 355 | PrevState = StateMgr.Remove(PrevState, I.getKey()); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 357 | // Remove terminator-specific bindings. |
| 358 | switch (Term->getStmtClass()) { |
| 359 | default: break; |
| 360 | |
| 361 | case Stmt::BinaryOperatorClass: { // '&&', '||' |
| 362 | BinaryOperator* B = cast<BinaryOperator>(Term); |
| 363 | // FIXME: Liveness analysis should probably remove these automatically. |
| 364 | // Verify later when we converge to an 'optimization' stage. |
| 365 | PrevState = StateMgr.Remove(PrevState, B->getRHS()); |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 370 | ConditionalOperator* C = cast<ConditionalOperator>(Term); |
| 371 | // FIXME: Liveness analysis should probably remove these automatically. |
| 372 | // Verify later when we converge to an 'optimization' stage. |
| 373 | if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L); |
| 374 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 379 | ChooseExpr* C = cast<ChooseExpr>(Term); |
| 380 | // FIXME: Liveness analysis should probably remove these automatically. |
| 381 | // Verify later when we converge to an 'optimization' stage. |
| 382 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 383 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 388 | RValue V = GetValue(PrevState, Condition); |
| 389 | |
| 390 | switch (V.getBaseKind()) { |
| 391 | default: |
| 392 | break; |
| 393 | |
| 394 | case RValue::InvalidKind: |
| 395 | builder.generateNode(PrevState, true); |
| 396 | builder.generateNode(PrevState, false); |
| 397 | return; |
| 398 | |
| 399 | case RValue::UninitializedKind: { |
| 400 | NodeTy* N = builder.generateNode(PrevState, true); |
| 401 | |
| 402 | if (N) { |
| 403 | N->markAsSink(); |
| 404 | UninitBranches.insert(N); |
| 405 | } |
| 406 | |
| 407 | builder.markInfeasible(false); |
| 408 | return; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // Process the true branch. |
| 413 | bool isFeasible = true; |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 414 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 415 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 416 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 417 | if (isFeasible) |
| 418 | builder.generateNode(St, true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 419 | else { |
| 420 | builder.markInfeasible(true); |
| 421 | isFeasible = true; |
| 422 | } |
| 423 | |
| 424 | // Process the false branch. |
| 425 | St = Assume(PrevState, V, false, isFeasible); |
| 426 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 427 | if (isFeasible) |
| 428 | builder.generateNode(St, false); |
| 429 | else |
| 430 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 433 | |
| 434 | void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
| 435 | NodeSet& Dst) { |
| 436 | |
| 437 | bool hasR2; |
| 438 | StateTy PrevState = Pred->getState(); |
| 439 | |
| 440 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 441 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 442 | |
| 443 | if (isa<InvalidValue>(R1) && |
| 444 | (isa<InvalidValue>(R2) || |
| 445 | isa<UninitializedValue>(R2))) { |
| 446 | |
| 447 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 448 | return; |
| 449 | } |
| 450 | else if (isa<UninitializedValue>(R1)) { |
| 451 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 456 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 457 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 458 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 459 | if (hasR2 == false) { |
| 460 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 461 | return; |
| 462 | } |
| 463 | } |
| 464 | else { |
| 465 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 466 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 467 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 468 | if (hasR2 == false) { |
| 469 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 470 | return; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 475 | // R2 == false. |
| 476 | |
| 477 | bool isFeasible; |
| 478 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 479 | |
| 480 | if (isFeasible) |
| 481 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 482 | |
| 483 | St = Assume(PrevState, R2, false, isFeasible); |
| 484 | |
| 485 | if (isFeasible) |
| 486 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 487 | } |
| 488 | |
| 489 | |
| 490 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 491 | void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 492 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 493 | |
| 494 | StmtEntryNode = builder.getLastNode(); |
| 495 | CurrentStmt = S; |
| 496 | NodeSet Dst; |
| 497 | StateCleaned = false; |
| 498 | |
| 499 | Visit(S, StmtEntryNode, Dst); |
| 500 | |
| 501 | // If no nodes were generated, generate a new node that has all the |
| 502 | // dead mappings removed. |
| 503 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 504 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 505 | builder.generateNode(S, St, StmtEntryNode); |
| 506 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 507 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 508 | CurrentStmt = NULL; |
| 509 | StmtEntryNode = NULL; |
| 510 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 513 | GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 514 | |
| 515 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 516 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 517 | // tells us are live. We then see what Decls they may reference, and keep |
| 518 | // those around. This code more than likely can be made faster, and the |
| 519 | // frequency of which this method is called should be experimented with |
| 520 | // for optimum performance. |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 521 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 522 | llvm::SmallVector<ValueDecl*, 10> WList; |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 523 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 524 | for (StateTy::vb_iterator I = M.begin(), E = M.end(); |
| 525 | I!=E && !I.getKey().isSymbol(); ++I) { |
| 526 | |
| 527 | // Remove old bindings for subexpressions. |
| 528 | if (I.getKey().isSubExpr()) { |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 529 | M = StateMgr.Remove(M, I.getKey()); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 530 | continue; |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 531 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 532 | |
| 533 | if (I.getKey().isBlkExpr()) { |
| 534 | if (Liveness.isLive(Loc, cast<Stmt>(I.getKey()))) { |
| 535 | if (isa<lval::DeclVal>(I.getData())) { |
| 536 | lval::DeclVal LV = cast<lval::DeclVal>(I.getData()); |
| 537 | WList.push_back(LV.getDecl()); |
| 538 | } |
| 539 | } |
| 540 | else |
| 541 | M = StateMgr.Remove(M, I.getKey()); |
| 542 | |
| 543 | continue; |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 544 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 545 | |
| 546 | assert (I.getKey().isDecl()); |
| 547 | |
| 548 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 549 | if (Liveness.isLive(Loc, V)) |
| 550 | WList.push_back(V); |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 551 | } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 552 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 553 | llvm::SmallPtrSet<ValueDecl*, 10> Marked; |
| 554 | |
| 555 | while (!WList.empty()) { |
| 556 | ValueDecl* V = WList.back(); |
| 557 | WList.pop_back(); |
| 558 | |
| 559 | if (Marked.count(V)) |
| 560 | continue; |
| 561 | |
| 562 | Marked.insert(V); |
| 563 | |
| 564 | if (V->getType()->isPointerType()) { |
| 565 | const LValue& LV = cast<LValue>(GetValue(M, lval::DeclVal(V))); |
| 566 | |
| 567 | if (!isa<lval::DeclVal>(LV)) |
| 568 | continue; |
| 569 | |
| 570 | const lval::DeclVal& LVD = cast<lval::DeclVal>(LV); |
| 571 | WList.push_back(LVD.getDecl()); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | for (StateTy::vb_iterator I = M.begin(), E = M.end(); I!=E ; ++I) |
| 576 | if (I.getKey().isDecl()) |
| 577 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 578 | if (!Marked.count(V)) |
| 579 | M = StateMgr.Remove(M, V); |
| 580 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 581 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 584 | GRConstants::NodeTy* |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 585 | GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St, |
| 586 | bool AlwaysMakeNode) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 587 | |
| 588 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 589 | if (!AlwaysMakeNode && St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 590 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 591 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 592 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 593 | Dst.Add(N); |
| 594 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 595 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 597 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
| 598 | const StateTy::BufferTy& SB) { |
| 599 | |
| 600 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 601 | Nodify(Dst, S, Pred, *I); |
| 602 | } |
| 603 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 604 | void GRConstants::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst) { |
| 605 | if (D != CurrentStmt) { |
| 606 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | // If we are here, we are loading the value of the decl and binding |
| 611 | // it to the block-level expression. |
| 612 | |
| 613 | StateTy St = Pred->getState(); |
| 614 | |
| 615 | Nodify(Dst, D, Pred, |
| 616 | SetValue(St, D, GetValue(St, lval::DeclVal(D->getDecl())))); |
| 617 | } |
| 618 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 619 | void GRConstants::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 620 | |
| 621 | QualType T = CastE->getType(); |
| 622 | |
| 623 | // Check for redundant casts. |
| 624 | if (E->getType() == T) { |
| 625 | Dst.Add(Pred); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | NodeSet S1; |
| 630 | Visit(E, Pred, S1); |
| 631 | |
| 632 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 633 | NodeTy* N = *I1; |
| 634 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 635 | const RValue& V = GetValue(St, E); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 636 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.EvalCast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 637 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 641 | GRConstants::NodeSet& Dst) { |
| 642 | |
| 643 | StateTy St = Pred->getState(); |
| 644 | |
| 645 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 646 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 647 | const Expr* E = VD->getInit(); |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 648 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 649 | E ? GetValue(St, E) : UninitializedValue()); |
| 650 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 651 | |
| 652 | Nodify(Dst, DS, Pred, St); |
| 653 | |
| 654 | if (Dst.empty()) |
| 655 | Dst.Add(Pred); |
| 656 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 657 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 658 | |
| 659 | void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 660 | NodeTy* Pred, NodeSet& Dst) { |
| 661 | |
| 662 | StateTy St = Pred->getState(); |
| 663 | |
| 664 | RValue R = GetValue(St, LHS); |
| 665 | if (isa<InvalidValue>(R)) R = GetValue(St, RHS); |
| 666 | |
| 667 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 668 | } |
| 669 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 670 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 671 | GRConstants::NodeTy* Pred, |
| 672 | GRConstants::NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 673 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 674 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 675 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 676 | |
| 677 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 678 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 679 | // probably just handle AddrOf and Deref in their own methods to make |
| 680 | // this cleaner. |
| 681 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 682 | isa<DeclRefExpr>(U->getSubExpr())) |
| 683 | S1.Add(Pred); |
| 684 | else |
| 685 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 686 | |
| 687 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 688 | NodeTy* N1 = *I1; |
| 689 | StateTy St = N1->getState(); |
| 690 | |
| 691 | switch (U->getOpcode()) { |
| 692 | case UnaryOperator::PostInc: { |
| 693 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 694 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 695 | |
| 696 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 697 | GetRValueConstant(1U, U)); |
| 698 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 699 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | case UnaryOperator::PostDec: { |
| 704 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 705 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 706 | |
| 707 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 708 | GetRValueConstant(1U, U)); |
| 709 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 710 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | case UnaryOperator::PreInc: { |
| 715 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 716 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 717 | |
| 718 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Add, |
| 719 | GetRValueConstant(1U, U)); |
| 720 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 721 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | case UnaryOperator::PreDec: { |
| 726 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 727 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 728 | |
| 729 | NonLValue Result = R1.EvalBinaryOp(ValMgr, BinaryOperator::Sub, |
| 730 | GetRValueConstant(1U, U)); |
| 731 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 732 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 733 | break; |
| 734 | } |
| 735 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 736 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 737 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 738 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 739 | break; |
| 740 | } |
| 741 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 742 | case UnaryOperator::Not: { |
| 743 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 744 | Nodify(Dst, U, N1, SetValue(St, U, R1.EvalComplement(ValMgr))); |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 748 | case UnaryOperator::LNot: { |
| 749 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 750 | // |
| 751 | // Note: technically we do "E == 0", but this is the same in the |
| 752 | // transfer functions as "0 == E". |
| 753 | |
| 754 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 755 | |
| 756 | if (isa<LValue>(V1)) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 757 | const LValue& L1 = cast<LValue>(V1); |
| 758 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 759 | Nodify(Dst, U, N1, |
| 760 | SetValue(St, U, L1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 761 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 762 | } |
| 763 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 764 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 765 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 766 | Nodify(Dst, U, N1, |
| 767 | SetValue(St, U, R1.EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 768 | V2))); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | break; |
| 772 | } |
| 773 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 774 | case UnaryOperator::AddrOf: { |
| 775 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 776 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | case UnaryOperator::Deref: { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 781 | // FIXME: Stop when dereferencing an uninitialized value. |
| 782 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 783 | |
| 784 | const RValue& V = GetValue(St, U->getSubExpr()); |
| 785 | const LValue& L1 = cast<LValue>(V); |
| 786 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 787 | // After a dereference, one of two possible situations arise: |
| 788 | // (1) A crash, because the pointer was NULL. |
| 789 | // (2) The pointer is not NULL, and the dereference works. |
| 790 | // |
| 791 | // We add these assumptions. |
| 792 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 793 | bool isFeasibleNotNull; |
| 794 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 795 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 796 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 797 | |
| 798 | if (isFeasibleNotNull) { |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 799 | QualType T = U->getType(); |
| 800 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 801 | GetValue(StNotNull, L1, &T))); |
| 802 | } |
| 803 | |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 804 | bool isFeasibleNull; |
| 805 | |
| 806 | // "Assume" that the pointer is NULL. |
| 807 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 808 | |
| 809 | if (isFeasibleNull) { |
| 810 | NodeTy* NullNode = Nodify(Dst, U, N1, StNull, true); |
| 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 | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 882 | if (isa<InvalidValue>(V1) || isa<UninitializedValue>(V1)) { |
| 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); |
| 917 | RValue Result = cast<NonLValue>(InvalidValue()); |
| 918 | |
| 919 | Op = (BinaryOperator::Opcode) |
| 920 | (((unsigned) Op) - ((unsigned) BinaryOperator::MulAssign)); |
| 921 | |
| 922 | if (isa<LValue>(V2)) { |
| 923 | // FIXME: Add support for Non-LValues on RHS. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 924 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 925 | Result = L1.EvalBinaryOp(ValMgr, Op, L2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 926 | } |
| 927 | else { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 928 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 929 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 930 | Result = R1.EvalBinaryOp(ValMgr, Op, R2); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 933 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 934 | break; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 935 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 936 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 937 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 938 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 939 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 940 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 941 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 942 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 943 | GRConstants::NodeSet& Dst) { |
| 944 | |
| 945 | // FIXME: add metadata to the CFG so that we can disable |
| 946 | // this check when we KNOW that there is no block-level subexpression. |
| 947 | // The motivation is that this check requires a hashtable lookup. |
| 948 | |
| 949 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 950 | Dst.Add(Pred); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | switch (S->getStmtClass()) { |
| 955 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 956 | |
| 957 | if (cast<BinaryOperator>(S)->isLogicalOp()) { |
| 958 | VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst); |
| 959 | break; |
| 960 | } |
| 961 | |
| 962 | // Fall-through. |
| 963 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 964 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 965 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 966 | break; |
| 967 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 968 | case Stmt::UnaryOperatorClass: |
| 969 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
| 970 | break; |
| 971 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 972 | case Stmt::ParenExprClass: |
| 973 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 974 | break; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 975 | |
| 976 | case Stmt::DeclRefExprClass: |
| 977 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 978 | break; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 979 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 980 | case Stmt::ImplicitCastExprClass: { |
| 981 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 982 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 983 | break; |
| 984 | } |
| 985 | |
| 986 | case Stmt::CastExprClass: { |
| 987 | CastExpr* C = cast<CastExpr>(S); |
| 988 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 989 | break; |
| 990 | } |
| 991 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 992 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 993 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 994 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 995 | break; |
| 996 | } |
| 997 | |
| 998 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 999 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1000 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1001 | break; |
| 1002 | } |
| 1003 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1004 | case Stmt::ReturnStmtClass: |
| 1005 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1006 | Visit(R, Pred, Dst); |
| 1007 | else |
| 1008 | Dst.Add(Pred); |
| 1009 | |
| 1010 | break; |
| 1011 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1012 | case Stmt::DeclStmtClass: |
| 1013 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1014 | break; |
| 1015 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1016 | default: |
| 1017 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 1018 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1019 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1022 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1023 | // "Assume" logic. |
| 1024 | //===----------------------------------------------------------------------===// |
| 1025 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1026 | GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, |
| 1027 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1028 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1029 | |
| 1030 | switch (Cond.getSubKind()) { |
| 1031 | default: |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1032 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1033 | return St; |
| 1034 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1035 | case lval::SymbolValKind: |
| 1036 | if (Assumption) |
| 1037 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1038 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1039 | else |
| 1040 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1041 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1042 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1043 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1044 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1045 | isFeasible = Assumption; |
| 1046 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1047 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1048 | case lval::ConcreteIntKind: { |
| 1049 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1050 | isFeasible = b ? Assumption : !Assumption; |
| 1051 | return St; |
| 1052 | } |
| 1053 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1056 | GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, |
| 1057 | bool Assumption, |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1058 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1059 | |
| 1060 | switch (Cond.getSubKind()) { |
| 1061 | default: |
| 1062 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 1063 | return St; |
| 1064 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1065 | |
| 1066 | case nonlval::SymbolValKind: { |
| 1067 | lval::SymbolVal& SV = cast<lval::SymbolVal>(Cond); |
| 1068 | SymbolID sym = SV.getSymbol(); |
| 1069 | |
| 1070 | if (Assumption) |
| 1071 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1072 | isFeasible); |
| 1073 | else |
| 1074 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1075 | isFeasible); |
| 1076 | } |
| 1077 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1078 | case nonlval::SymIntConstraintValKind: |
| 1079 | return |
| 1080 | AssumeSymInt(St, Assumption, |
| 1081 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1082 | isFeasible); |
| 1083 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1084 | case nonlval::ConcreteIntKind: { |
| 1085 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1086 | isFeasible = b ? Assumption : !Assumption; |
| 1087 | return St; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1092 | GRConstants::StateTy |
| 1093 | GRConstants::AssumeSymNE(StateTy St, SymbolID sym, |
| 1094 | const llvm::APSInt& V, bool& isFeasible) { |
| 1095 | |
| 1096 | // First, determine if sym == X, where X != V. |
| 1097 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1098 | isFeasible = *X != V; |
| 1099 | return St; |
| 1100 | } |
| 1101 | |
| 1102 | // Second, determine if sym != V. |
| 1103 | if (St.isNotEqual(sym, V)) { |
| 1104 | isFeasible = true; |
| 1105 | return St; |
| 1106 | } |
| 1107 | |
| 1108 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1109 | // Make that assumption. |
| 1110 | |
| 1111 | isFeasible = true; |
| 1112 | return StateMgr.AddNE(St, sym, V); |
| 1113 | } |
| 1114 | |
| 1115 | GRConstants::StateTy |
| 1116 | GRConstants::AssumeSymEQ(StateTy St, SymbolID sym, |
| 1117 | const llvm::APSInt& V, bool& isFeasible) { |
| 1118 | |
| 1119 | // First, determine if sym == X, where X != V. |
| 1120 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1121 | isFeasible = *X == V; |
| 1122 | return St; |
| 1123 | } |
| 1124 | |
| 1125 | // Second, determine if sym != V. |
| 1126 | if (St.isNotEqual(sym, V)) { |
| 1127 | isFeasible = false; |
| 1128 | return St; |
| 1129 | } |
| 1130 | |
| 1131 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1132 | // Make that assumption. |
| 1133 | |
| 1134 | isFeasible = true; |
| 1135 | return StateMgr.AddEQ(St, sym, V); |
| 1136 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1137 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1138 | GRConstants::StateTy |
| 1139 | GRConstants::AssumeSymInt(StateTy St, bool Assumption, |
| 1140 | const SymIntConstraint& C, bool& isFeasible) { |
| 1141 | |
| 1142 | switch (C.getOpcode()) { |
| 1143 | default: |
| 1144 | // No logic yet for other operators. |
| 1145 | return St; |
| 1146 | |
| 1147 | case BinaryOperator::EQ: |
| 1148 | if (Assumption) |
| 1149 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1150 | else |
| 1151 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1152 | |
| 1153 | case BinaryOperator::NE: |
| 1154 | if (Assumption) |
| 1155 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1156 | else |
| 1157 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1158 | } |
| 1159 | } |
| 1160 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1161 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1162 | // Driver. |
| 1163 | //===----------------------------------------------------------------------===// |
| 1164 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1165 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1166 | static GRConstants* GraphPrintCheckerState; |
| 1167 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1168 | namespace llvm { |
| 1169 | template<> |
| 1170 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 1171 | public DefaultDOTGraphTraits { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1172 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1173 | static void PrintKindLabel(std::ostream& Out, VarBindKey::Kind kind) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1174 | switch (kind) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1175 | case VarBindKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break; |
| 1176 | case VarBindKey::IsDecl: Out << "Variables:\\l"; break; |
| 1177 | case VarBindKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 1178 | default: assert (false && "Unknown VarBindKey type."); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1182 | static void PrintKind(std::ostream& Out, GRConstants::StateTy M, |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1183 | VarBindKey::Kind kind, bool isFirstGroup = false) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1184 | bool isFirst = true; |
| 1185 | |
Ted Kremenek | b80cbfe | 2008-02-05 18:19:15 +0000 | [diff] [blame] | 1186 | 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] | 1187 | if (I.getKey().getKind() != kind) |
| 1188 | continue; |
| 1189 | |
| 1190 | if (isFirst) { |
| 1191 | if (!isFirstGroup) Out << "\\l\\l"; |
| 1192 | PrintKindLabel(Out, kind); |
| 1193 | isFirst = false; |
| 1194 | } |
| 1195 | else |
| 1196 | Out << "\\l"; |
| 1197 | |
| 1198 | Out << ' '; |
| 1199 | |
| 1200 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) |
| 1201 | Out << V->getName(); |
| 1202 | else { |
| 1203 | Stmt* E = cast<Stmt>(I.getKey()); |
| 1204 | Out << " (" << (void*) E << ") "; |
| 1205 | E->printPretty(Out); |
| 1206 | } |
| 1207 | |
| 1208 | Out << " : "; |
| 1209 | I.getData().print(Out); |
| 1210 | } |
| 1211 | } |
| 1212 | |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1213 | static void PrintEQ(std::ostream& Out, GRConstants::StateTy St) { |
| 1214 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1215 | |
| 1216 | if (CE.isEmpty()) |
| 1217 | return; |
| 1218 | |
| 1219 | Out << "\\l\\|'==' constraints:"; |
| 1220 | |
| 1221 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1222 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1223 | } |
| 1224 | |
| 1225 | static void PrintNE(std::ostream& Out, GRConstants::StateTy St) { |
| 1226 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1227 | |
| 1228 | if (NE.isEmpty()) |
| 1229 | return; |
| 1230 | |
| 1231 | Out << "\\l\\|'!=' constraints:"; |
| 1232 | |
| 1233 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1234 | I != EI; ++I){ |
| 1235 | |
| 1236 | Out << "\\l $" << I.getKey() << " : "; |
| 1237 | bool isFirst = true; |
| 1238 | |
| 1239 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1240 | EJ=I.getData().end(); |
| 1241 | for ( ; J != EJ; ++J) { |
| 1242 | if (isFirst) isFirst = false; |
| 1243 | else Out << ", "; |
| 1244 | |
| 1245 | Out << (*J)->toString(); |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1250 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 1251 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1254 | ProgramPoint Loc = N->getLocation(); |
| 1255 | |
| 1256 | switch (Loc.getKind()) { |
| 1257 | case ProgramPoint::BlockEntranceKind: |
| 1258 | Out << "Block Entrance: B" |
| 1259 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1260 | break; |
| 1261 | |
| 1262 | case ProgramPoint::BlockExitKind: |
| 1263 | assert (false); |
| 1264 | break; |
| 1265 | |
| 1266 | case ProgramPoint::PostStmtKind: { |
| 1267 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1268 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1269 | << (void*) L.getStmt() << ' '; |
| 1270 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1271 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1274 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1275 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1276 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1277 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1278 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1279 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | } |
| 1282 | |
| 1283 | default: { |
| 1284 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1285 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1286 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1289 | Out << "\\|Terminator: "; |
| 1290 | E.getSrc()->printTerminator(Out); |
| 1291 | |
| 1292 | if (isa<SwitchStmt>(T)) { |
| 1293 | // FIXME |
| 1294 | } |
| 1295 | else { |
| 1296 | Out << "\\lCondition: "; |
| 1297 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1298 | Out << "true"; |
| 1299 | else |
| 1300 | Out << "false"; |
| 1301 | } |
| 1302 | |
| 1303 | Out << "\\l"; |
| 1304 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1307 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1308 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1312 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1313 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1314 | PrintKind(Out, N->getState(), VarBindKey::IsDecl, true); |
| 1315 | PrintKind(Out, N->getState(), VarBindKey::IsBlkExpr); |
| 1316 | PrintKind(Out, N->getState(), VarBindKey::IsSubExpr); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1317 | |
| 1318 | PrintEQ(Out, N->getState()); |
| 1319 | PrintNE(Out, N->getState()); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1320 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1321 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1322 | return Out.str(); |
| 1323 | } |
| 1324 | }; |
| 1325 | } // end llvm namespace |
| 1326 | #endif |
| 1327 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1328 | namespace clang { |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame^] | 1329 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx, |
| 1330 | Diagnostic& Diag) { |
| 1331 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 1332 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame^] | 1333 | Engine.ExecuteWorkList(); |
| 1334 | |
| 1335 | // Look for explicit-Null dereferences and warn about them. |
| 1336 | GRConstants* CheckerState = &Engine.getCheckerState(); |
| 1337 | |
| 1338 | for (GRConstants::null_iterator I=CheckerState->null_begin(), |
| 1339 | E=CheckerState->null_end(); I!=E; ++I) { |
| 1340 | |
| 1341 | const PostStmt& L = cast<PostStmt>((*I)->getLocation()); |
| 1342 | Expr* E = cast<Expr>(L.getStmt()); |
| 1343 | |
| 1344 | Diag.Report(FullSourceLoc(E->getExprLoc(), Ctx.getSourceManager()), |
| 1345 | diag::chkr_null_deref_after_check); |
| 1346 | } |
| 1347 | |
| 1348 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1349 | #ifndef NDEBUG |
Ted Kremenek | 19227e3 | 2008-02-07 06:33:19 +0000 | [diff] [blame^] | 1350 | GraphPrintCheckerState = CheckerState; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1351 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1352 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1353 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1354 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1355 | } // end clang namespace |