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