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