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 | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 25 | |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/DataTypes.h" |
| 28 | #include "llvm/ADT/APSInt.h" |
| 29 | #include "llvm/ADT/FoldingSet.h" |
| 30 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Allocator.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Streams.h" |
| 36 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 37 | #include <functional> |
| 38 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 39 | #ifndef NDEBUG |
| 40 | #include "llvm/Support/GraphWriter.h" |
| 41 | #include <sstream> |
| 42 | #endif |
| 43 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 44 | using namespace clang; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 45 | using llvm::dyn_cast; |
| 46 | using llvm::cast; |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 47 | using llvm::APSInt; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 48 | |
| 49 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 50 | // The Checker. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 51 | // |
| 52 | // FIXME: This checker logic should be eventually broken into two components. |
| 53 | // The first is the "meta"-level checking logic; the code that |
| 54 | // does the Stmt visitation, fetching values from the map, etc. |
| 55 | // The second part does the actual state manipulation. This way we |
| 56 | // get more of a separate of concerns of these two pieces, with the |
| 57 | // latter potentially being refactored back into the main checking |
| 58 | // logic. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | namespace { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 63 | class VISIBILITY_HIDDEN GRConstants { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 64 | |
| 65 | public: |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 66 | typedef ValueStateManager::StateTy StateTy; |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 67 | typedef GRStmtNodeBuilder<GRConstants> StmtNodeBuilder; |
| 68 | typedef GRBranchNodeBuilder<GRConstants> BranchNodeBuilder; |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 69 | typedef ExplodedGraph<GRConstants> GraphTy; |
| 70 | typedef GraphTy::NodeTy NodeTy; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 71 | |
| 72 | class NodeSet { |
| 73 | typedef llvm::SmallVector<NodeTy*,3> ImplTy; |
| 74 | ImplTy Impl; |
| 75 | public: |
| 76 | |
| 77 | NodeSet() {} |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 78 | NodeSet(NodeTy* N) { assert (N && !N->isSink()); Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 80 | void Add(NodeTy* N) { if (N && !N->isSink()) Impl.push_back(N); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 81 | |
| 82 | typedef ImplTy::iterator iterator; |
| 83 | typedef ImplTy::const_iterator const_iterator; |
| 84 | |
| 85 | unsigned size() const { return Impl.size(); } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 86 | bool empty() const { return Impl.empty(); } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 87 | |
| 88 | iterator begin() { return Impl.begin(); } |
| 89 | iterator end() { return Impl.end(); } |
| 90 | |
| 91 | const_iterator begin() const { return Impl.begin(); } |
| 92 | const_iterator end() const { return Impl.end(); } |
| 93 | }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 94 | |
| 95 | protected: |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 96 | /// G - the simulation graph. |
| 97 | GraphTy& G; |
| 98 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 99 | /// Liveness - live-variables information the ValueDecl* and block-level |
| 100 | /// Expr* in the CFG. Used to prune out dead state. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 101 | LiveVariables Liveness; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 103 | /// Builder - The current GRStmtNodeBuilder which is used when building the nodes |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 104 | /// for a given statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 105 | StmtNodeBuilder* Builder; |
| 106 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 107 | /// StateMgr - Object that manages the data for all created states. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 108 | ValueStateManager StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 110 | /// ValueMgr - Object that manages the data for all created RValues. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 111 | ValueManager& ValMgr; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 113 | /// SymMgr - Object that manages the symbol information. |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 114 | SymbolManager& SymMgr; |
Ted Kremenek | 68fd257 | 2008-01-29 17:27:31 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 116 | /// StmtEntryNode - The immediate predecessor node. |
| 117 | NodeTy* StmtEntryNode; |
| 118 | |
| 119 | /// CurrentStmt - The current block-level statement. |
| 120 | Stmt* CurrentStmt; |
| 121 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 122 | /// UninitBranches - Nodes in the ExplodedGraph that result from |
| 123 | /// taking a branch based on an uninitialized value. |
| 124 | typedef llvm::SmallPtrSet<NodeTy*,5> UninitBranchesTy; |
| 125 | UninitBranchesTy UninitBranches; |
| 126 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 127 | bool StateCleaned; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 129 | ASTContext& getContext() const { return G.getContext(); } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 131 | public: |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 132 | GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()), |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 133 | Builder(NULL), |
| 134 | StateMgr(G.getContext()), |
| 135 | ValMgr(StateMgr.getValueManager()), |
| 136 | SymMgr(StateMgr.getSymbolManager()), |
| 137 | StmtEntryNode(NULL), CurrentStmt(NULL) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 139 | // Compute liveness information. |
Ted Kremenek | bffaa83 | 2008-01-29 05:13:23 +0000 | [diff] [blame] | 140 | Liveness.runOnCFG(G.getCFG()); |
| 141 | Liveness.runOnAllBlocks(G.getCFG(), NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 142 | } |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 143 | |
| 144 | /// getCFG - Returns the CFG associated with this analysis. |
| 145 | CFG& getCFG() { return G.getCFG(); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 147 | /// getInitialState - Return the initial state used for the root vertex |
| 148 | /// in the ExplodedGraph. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 149 | StateTy getInitialState() { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 150 | StateTy St = StateMgr.getInitialState(); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 151 | |
| 152 | // Iterate the parameters. |
| 153 | FunctionDecl& F = G.getFunctionDecl(); |
| 154 | |
| 155 | for (FunctionDecl::param_iterator I=F.param_begin(), E=F.param_end(); |
Ted Kremenek | 4150abf | 2008-01-31 00:09:56 +0000 | [diff] [blame] | 156 | I!=E; ++I) |
| 157 | St = SetValue(St, LValueDecl(*I), RValue::GetSymbolValue(SymMgr, *I)); |
Ted Kremenek | ff6e3c5 | 2008-01-29 00:43:03 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 159 | return St; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 161 | |
| 162 | bool isUninitControlFlow(const NodeTy* N) const { |
| 163 | return N->isSink() && UninitBranches.count(const_cast<NodeTy*>(N)) != 0; |
| 164 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 166 | /// ProcessStmt - Called by GREngine. Used to generate new successor |
| 167 | /// nodes by processing the 'effects' of a block-level statement. |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 168 | void ProcessStmt(Stmt* S, StmtNodeBuilder& builder); |
| 169 | |
| 170 | /// ProcessBranch - Called by GREngine. Used to generate successor |
| 171 | /// nodes by processing the 'effects' of a branch condition. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 172 | void ProcessBranch(Expr* Condition, Stmt* Term, BranchNodeBuilder& builder); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 173 | |
| 174 | /// RemoveDeadBindings - Return a new state that is the same as 'M' except |
| 175 | /// that all subexpression mappings are removed and that any |
| 176 | /// block-level expressions that are not live at 'S' also have their |
| 177 | /// mappings removed. |
| 178 | StateTy RemoveDeadBindings(Stmt* S, StateTy M); |
| 179 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 180 | StateTy SetValue(StateTy St, Stmt* S, const RValue& V); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 182 | StateTy SetValue(StateTy St, const Stmt* S, const RValue& V) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 183 | return SetValue(St, const_cast<Stmt*>(S), V); |
| 184 | } |
| 185 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 186 | StateTy SetValue(StateTy St, const LValue& LV, const RValue& V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 188 | inline RValue GetValue(const StateTy& St, Stmt* S) { |
| 189 | return StateMgr.GetValue(St, S); |
| 190 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 191 | |
| 192 | inline RValue GetValue(const StateTy& St, Stmt* S, bool& hasVal) { |
| 193 | return StateMgr.GetValue(St, S, &hasVal); |
| 194 | } |
| 195 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 196 | inline RValue GetValue(const StateTy& St, const Stmt* S) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 197 | return GetValue(St, const_cast<Stmt*>(S)); |
| 198 | } |
| 199 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 200 | inline RValue GetValue(const StateTy& St, const LValue& LV) { |
| 201 | return StateMgr.GetValue(St, LV); |
| 202 | } |
| 203 | |
| 204 | inline LValue GetLValue(const StateTy& St, Stmt* S) { |
| 205 | return StateMgr.GetLValue(St, S); |
| 206 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 207 | |
| 208 | inline NonLValue GetRValueConstant(uint64_t X, Expr* E) { |
| 209 | return NonLValue::GetValue(ValMgr, X, E->getType(), E->getLocStart()); |
| 210 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 211 | |
| 212 | /// Assume - Create new state by assuming that a given expression |
| 213 | /// is true or false. |
| 214 | inline StateTy Assume(StateTy St, RValue Cond, bool Assumption, |
| 215 | bool& isFeasible) { |
| 216 | if (isa<LValue>(Cond)) |
| 217 | return Assume(St, cast<LValue>(Cond), Assumption, isFeasible); |
| 218 | else |
| 219 | return Assume(St, cast<NonLValue>(Cond), Assumption, isFeasible); |
| 220 | } |
| 221 | |
| 222 | StateTy Assume(StateTy St, LValue Cond, bool Assumption, bool& isFeasible); |
| 223 | StateTy Assume(StateTy St, NonLValue Cond, bool Assumption, bool& isFeasible); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 225 | void Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 227 | /// Visit - Transfer function logic for all statements. Dispatches to |
| 228 | /// other functions that handle specific kinds of statements. |
| 229 | void Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 230 | |
| 231 | /// VisitCast - Transfer function logic for all casts (implicit and explicit). |
| 232 | void VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 234 | /// VisitUnaryOperator - Transfer function logic for unary operators. |
| 235 | void VisitUnaryOperator(UnaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 236 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 237 | /// VisitBinaryOperator - Transfer function logic for binary operators. |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 238 | void VisitBinaryOperator(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
| 239 | |
| 240 | /// VisitDeclStmt - Transfer function logic for DeclStmts. |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 241 | void VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst); |
| 242 | |
| 243 | /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose |
| 244 | void VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 245 | NodeTy* Pred, NodeSet& Dst); |
| 246 | |
| 247 | /// VisitLogicalExpr - Transfer function logic for '&&', '||' |
| 248 | void VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, NodeSet& Dst); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 249 | }; |
| 250 | } // end anonymous namespace |
| 251 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 253 | GRConstants::StateTy |
| 254 | GRConstants::SetValue(StateTy St, Stmt* S, const RValue& V) { |
| 255 | |
| 256 | if (!StateCleaned) { |
| 257 | St = RemoveDeadBindings(CurrentStmt, St); |
| 258 | StateCleaned = true; |
| 259 | } |
| 260 | |
| 261 | bool isBlkExpr = false; |
| 262 | |
| 263 | if (S == CurrentStmt) { |
| 264 | isBlkExpr = getCFG().isBlkExpr(S); |
| 265 | |
| 266 | if (!isBlkExpr) |
| 267 | return St; |
| 268 | } |
| 269 | |
| 270 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 271 | } |
| 272 | |
| 273 | GRConstants::StateTy |
| 274 | GRConstants::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
| 275 | |
| 276 | if (!LV.isValid()) |
| 277 | return St; |
| 278 | |
| 279 | if (!StateCleaned) { |
| 280 | St = RemoveDeadBindings(CurrentStmt, St); |
| 281 | StateCleaned = true; |
| 282 | } |
| 283 | |
| 284 | return StateMgr.SetValue(St, LV, V); |
| 285 | } |
| 286 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 287 | void GRConstants::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 288 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 289 | |
| 290 | StateTy PrevState = builder.getState(); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 292 | // Remove old bindings for subexpressions. |
| 293 | for (StateTy::iterator I=PrevState.begin(), E=PrevState.end(); I!=E; ++I) |
| 294 | if (I.getKey().isSubExpr()) |
| 295 | PrevState = StateMgr.Remove(PrevState, I.getKey()); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 297 | // Remove terminator-specific bindings. |
| 298 | switch (Term->getStmtClass()) { |
| 299 | default: break; |
| 300 | |
| 301 | case Stmt::BinaryOperatorClass: { // '&&', '||' |
| 302 | BinaryOperator* B = cast<BinaryOperator>(Term); |
| 303 | // FIXME: Liveness analysis should probably remove these automatically. |
| 304 | // Verify later when we converge to an 'optimization' stage. |
| 305 | PrevState = StateMgr.Remove(PrevState, B->getRHS()); |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 310 | ConditionalOperator* C = cast<ConditionalOperator>(Term); |
| 311 | // FIXME: Liveness analysis should probably remove these automatically. |
| 312 | // Verify later when we converge to an 'optimization' stage. |
| 313 | if (Expr* L = C->getLHS()) PrevState = StateMgr.Remove(PrevState, L); |
| 314 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 319 | ChooseExpr* C = cast<ChooseExpr>(Term); |
| 320 | // FIXME: Liveness analysis should probably remove these automatically. |
| 321 | // Verify later when we converge to an 'optimization' stage. |
| 322 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 323 | PrevState = StateMgr.Remove(PrevState, C->getRHS()); |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 328 | RValue V = GetValue(PrevState, Condition); |
| 329 | |
| 330 | switch (V.getBaseKind()) { |
| 331 | default: |
| 332 | break; |
| 333 | |
| 334 | case RValue::InvalidKind: |
| 335 | builder.generateNode(PrevState, true); |
| 336 | builder.generateNode(PrevState, false); |
| 337 | return; |
| 338 | |
| 339 | case RValue::UninitializedKind: { |
| 340 | NodeTy* N = builder.generateNode(PrevState, true); |
| 341 | |
| 342 | if (N) { |
| 343 | N->markAsSink(); |
| 344 | UninitBranches.insert(N); |
| 345 | } |
| 346 | |
| 347 | builder.markInfeasible(false); |
| 348 | return; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // Process the true branch. |
| 353 | bool isFeasible = true; |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 354 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 355 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 356 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 357 | if (isFeasible) |
| 358 | builder.generateNode(St, true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 359 | else { |
| 360 | builder.markInfeasible(true); |
| 361 | isFeasible = true; |
| 362 | } |
| 363 | |
| 364 | // Process the false branch. |
| 365 | St = Assume(PrevState, V, false, isFeasible); |
| 366 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 367 | if (isFeasible) |
| 368 | builder.generateNode(St, false); |
| 369 | else |
| 370 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 373 | |
| 374 | void GRConstants::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
| 375 | NodeSet& Dst) { |
| 376 | |
| 377 | bool hasR2; |
| 378 | StateTy PrevState = Pred->getState(); |
| 379 | |
| 380 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 381 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
| 382 | |
| 383 | if (isa<InvalidValue>(R1) && |
| 384 | (isa<InvalidValue>(R2) || |
| 385 | isa<UninitializedValue>(R2))) { |
| 386 | |
| 387 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 388 | return; |
| 389 | } |
| 390 | else if (isa<UninitializedValue>(R1)) { |
| 391 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 396 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 397 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 398 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 399 | if (hasR2 == false) { |
| 400 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 401 | return; |
| 402 | } |
| 403 | } |
| 404 | else { |
| 405 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 406 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 407 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 408 | if (hasR2 == false) { |
| 409 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 415 | // R2 == false. |
| 416 | |
| 417 | bool isFeasible; |
| 418 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 419 | |
| 420 | if (isFeasible) |
| 421 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 422 | |
| 423 | St = Assume(PrevState, R2, false, isFeasible); |
| 424 | |
| 425 | if (isFeasible) |
| 426 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 431 | void GRConstants::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 432 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 433 | |
| 434 | StmtEntryNode = builder.getLastNode(); |
| 435 | CurrentStmt = S; |
| 436 | NodeSet Dst; |
| 437 | StateCleaned = false; |
| 438 | |
| 439 | Visit(S, StmtEntryNode, Dst); |
| 440 | |
| 441 | // If no nodes were generated, generate a new node that has all the |
| 442 | // dead mappings removed. |
| 443 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 444 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 445 | builder.generateNode(S, St, StmtEntryNode); |
| 446 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 448 | CurrentStmt = NULL; |
| 449 | StmtEntryNode = NULL; |
| 450 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 453 | GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 454 | // Note: in the code below, we can assign a new map to M since the |
| 455 | // iterators are iterating over the tree of the *original* map. |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 456 | StateTy::iterator I = M.begin(), E = M.end(); |
| 457 | |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 458 | |
Ted Kremenek | 65cac13 | 2008-01-29 05:25:31 +0000 | [diff] [blame] | 459 | for (; I!=E && !I.getKey().isSymbol(); ++I) { |
| 460 | // Remove old bindings for subexpressions and "dead" |
| 461 | // block-level expressions. |
| 462 | if (I.getKey().isSubExpr() || |
| 463 | I.getKey().isBlkExpr() && !Liveness.isLive(Loc,cast<Stmt>(I.getKey()))){ |
| 464 | M = StateMgr.Remove(M, I.getKey()); |
| 465 | } |
| 466 | else if (I.getKey().isDecl()) { // Remove bindings for "dead" decls. |
| 467 | if (VarDecl* V = dyn_cast<VarDecl>(cast<ValueDecl>(I.getKey()))) |
| 468 | if (!Liveness.isLive(Loc, V)) |
| 469 | M = StateMgr.Remove(M, I.getKey()); |
| 470 | } |
| 471 | } |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 473 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 476 | void GRConstants::Nodify(NodeSet& Dst, Stmt* S, GRConstants::NodeTy* Pred, |
| 477 | GRConstants::StateTy St) { |
| 478 | |
| 479 | // If the state hasn't changed, don't generate a new node. |
| 480 | if (St == Pred->getState()) |
| 481 | return; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 482 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 483 | Dst.Add(Builder->generateNode(S, St, Pred)); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 484 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 486 | void GRConstants::VisitCast(Expr* CastE, Expr* E, GRConstants::NodeTy* Pred, |
| 487 | GRConstants::NodeSet& Dst) { |
| 488 | |
| 489 | QualType T = CastE->getType(); |
| 490 | |
| 491 | // Check for redundant casts. |
| 492 | if (E->getType() == T) { |
| 493 | Dst.Add(Pred); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | NodeSet S1; |
| 498 | Visit(E, Pred, S1); |
| 499 | |
| 500 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 501 | NodeTy* N = *I1; |
| 502 | StateTy St = N->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 503 | const RValue& V = GetValue(St, E); |
| 504 | Nodify(Dst, CastE, N, SetValue(St, CastE, V.Cast(ValMgr, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 505 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void GRConstants::VisitDeclStmt(DeclStmt* DS, GRConstants::NodeTy* Pred, |
| 509 | GRConstants::NodeSet& Dst) { |
| 510 | |
| 511 | StateTy St = Pred->getState(); |
| 512 | |
| 513 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 514 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 515 | const Expr* E = VD->getInit(); |
| 516 | St = SetValue(St, LValueDecl(VD), |
| 517 | E ? GetValue(St, E) : UninitializedValue()); |
| 518 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 519 | |
| 520 | Nodify(Dst, DS, Pred, St); |
| 521 | |
| 522 | if (Dst.empty()) |
| 523 | Dst.Add(Pred); |
| 524 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 526 | |
| 527 | void GRConstants::VisitGuardedExpr(Stmt* S, Stmt* LHS, Stmt* RHS, |
| 528 | NodeTy* Pred, NodeSet& Dst) { |
| 529 | |
| 530 | StateTy St = Pred->getState(); |
| 531 | |
| 532 | RValue R = GetValue(St, LHS); |
| 533 | if (isa<InvalidValue>(R)) R = GetValue(St, RHS); |
| 534 | |
| 535 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 536 | } |
| 537 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 538 | void GRConstants::VisitUnaryOperator(UnaryOperator* U, |
| 539 | GRConstants::NodeTy* Pred, |
| 540 | GRConstants::NodeSet& Dst) { |
| 541 | NodeSet S1; |
| 542 | Visit(U->getSubExpr(), Pred, S1); |
| 543 | |
| 544 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 545 | NodeTy* N1 = *I1; |
| 546 | StateTy St = N1->getState(); |
| 547 | |
| 548 | switch (U->getOpcode()) { |
| 549 | case UnaryOperator::PostInc: { |
| 550 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 551 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 552 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 553 | U->getLocStart()); |
Ted Kremenek | e0cf9c8 | 2008-01-24 19:00:57 +0000 | [diff] [blame] | 554 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 555 | NonLValue Result = R1.Add(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 556 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | case UnaryOperator::PostDec: { |
| 561 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 562 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 563 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 564 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 566 | NonLValue Result = R1.Sub(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 567 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | case UnaryOperator::PreInc: { |
| 572 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 573 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 574 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 575 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 576 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 577 | NonLValue Result = R1.Add(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 578 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | case UnaryOperator::PreDec: { |
| 583 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 584 | NonLValue R1 = cast<NonLValue>(GetValue(St, L1)); |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 585 | NonLValue R2 = NonLValue::GetValue(ValMgr, 1U, U->getType(), |
| 586 | U->getLocStart()); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 588 | NonLValue Result = R1.Sub(ValMgr, R2); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 589 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 590 | break; |
| 591 | } |
| 592 | |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 593 | case UnaryOperator::Minus: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 594 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
| 595 | Nodify(Dst, U, N1, SetValue(St, U, R1.UnaryMinus(ValMgr, U))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 596 | break; |
| 597 | } |
| 598 | |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 599 | case UnaryOperator::Not: { |
| 600 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
| 601 | Nodify(Dst, U, N1, SetValue(St, U, R1.BitwiseComplement(ValMgr))); |
| 602 | break; |
| 603 | } |
| 604 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 605 | case UnaryOperator::AddrOf: { |
| 606 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 607 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | case UnaryOperator::Deref: { |
| 612 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 613 | Nodify(Dst, U, N1, SetValue(St, U, GetValue(St, L1))); |
| 614 | break; |
| 615 | } |
| 616 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 617 | default: ; |
| 618 | assert (false && "Not implemented."); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 623 | void GRConstants::VisitBinaryOperator(BinaryOperator* B, |
| 624 | GRConstants::NodeTy* Pred, |
| 625 | GRConstants::NodeSet& Dst) { |
| 626 | NodeSet S1; |
| 627 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 629 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 630 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 631 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 632 | // When getting the value for the LHS, check if we are in an assignment. |
| 633 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 634 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 635 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 636 | const RValue& V1 = |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 637 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 638 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 640 | NodeSet S2; |
| 641 | Visit(B->getRHS(), N1, S2); |
| 642 | |
| 643 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
| 644 | NodeTy* N2 = *I2; |
| 645 | StateTy St = N2->getState(); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 646 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 647 | |
| 648 | switch (B->getOpcode()) { |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 649 | default: |
| 650 | Dst.Add(N2); |
| 651 | break; |
| 652 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 653 | // Arithmetic operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 654 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 655 | case BinaryOperator::Add: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 656 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 657 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 658 | |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 659 | Nodify(Dst, B, N2, SetValue(St, B, R1.Add(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | |
| 663 | case BinaryOperator::Sub: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 664 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 665 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 666 | Nodify(Dst, B, N2, SetValue(St, B, R1.Sub(ValMgr, R2))); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 670 | case BinaryOperator::Mul: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 671 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 672 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 673 | Nodify(Dst, B, N2, SetValue(St, B, R1.Mul(ValMgr, R2))); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 674 | break; |
| 675 | } |
| 676 | |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 677 | case BinaryOperator::Div: { |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 678 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 679 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 680 | Nodify(Dst, B, N2, SetValue(St, B, R1.Div(ValMgr, R2))); |
Ted Kremenek | 5ee4ff8 | 2008-01-25 22:55:56 +0000 | [diff] [blame] | 681 | break; |
| 682 | } |
| 683 | |
Ted Kremenek | cce207d | 2008-01-28 22:26:15 +0000 | [diff] [blame] | 684 | case BinaryOperator::Rem: { |
| 685 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 686 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 687 | Nodify(Dst, B, N2, SetValue(St, B, R1.Rem(ValMgr, R2))); |
| 688 | break; |
| 689 | } |
| 690 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 691 | // Assignment operators. |
| 692 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 693 | case BinaryOperator::Assign: { |
| 694 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 695 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 696 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, R2), L1, R2)); |
| 697 | break; |
| 698 | } |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 699 | |
| 700 | case BinaryOperator::AddAssign: { |
| 701 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 702 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 703 | NonLValue Result = R1.Add(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 704 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | case BinaryOperator::SubAssign: { |
| 709 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 710 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 711 | NonLValue Result = R1.Sub(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 712 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 713 | break; |
| 714 | } |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 715 | |
| 716 | case BinaryOperator::MulAssign: { |
| 717 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 718 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 719 | NonLValue Result = R1.Mul(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 2eafd0e | 2008-01-23 23:42:27 +0000 | [diff] [blame] | 720 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 721 | break; |
| 722 | } |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 723 | |
| 724 | case BinaryOperator::DivAssign: { |
| 725 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bd03f1d | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 726 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 727 | NonLValue Result = R1.Div(ValMgr, cast<NonLValue>(V2)); |
Ted Kremenek | 5c1e262 | 2008-01-25 23:45:34 +0000 | [diff] [blame] | 728 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 729 | break; |
| 730 | } |
Ted Kremenek | 10099a6 | 2008-01-28 22:28:54 +0000 | [diff] [blame] | 731 | |
| 732 | case BinaryOperator::RemAssign: { |
| 733 | const LValue& L1 = cast<LValue>(V1); |
| 734 | NonLValue R1 = cast<NonLValue>(GetValue(N1->getState(), L1)); |
| 735 | NonLValue Result = R1.Rem(ValMgr, cast<NonLValue>(V2)); |
| 736 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
| 737 | break; |
| 738 | } |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 739 | |
| 740 | // Equality operators. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 741 | |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 742 | case BinaryOperator::EQ: |
| 743 | // FIXME: should we allow XX.EQ() to return a set of values, |
| 744 | // allowing state bifurcation? In such cases, they will also |
| 745 | // modify the state (meaning that a new state will be returned |
| 746 | // as well). |
| 747 | assert (B->getType() == getContext().IntTy); |
| 748 | |
| 749 | if (isa<LValue>(V1)) { |
| 750 | const LValue& L1 = cast<LValue>(V1); |
| 751 | const LValue& L2 = cast<LValue>(V2); |
| 752 | St = SetValue(St, B, L1.EQ(ValMgr, L2)); |
| 753 | } |
| 754 | else { |
| 755 | const NonLValue& R1 = cast<NonLValue>(V1); |
| 756 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 757 | St = SetValue(St, B, R1.EQ(ValMgr, R2)); |
| 758 | } |
| 759 | |
| 760 | Nodify(Dst, B, N2, St); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 761 | break; |
| 762 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 763 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 764 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 765 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 766 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 767 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 768 | void GRConstants::Visit(Stmt* S, GRConstants::NodeTy* Pred, |
| 769 | GRConstants::NodeSet& Dst) { |
| 770 | |
| 771 | // FIXME: add metadata to the CFG so that we can disable |
| 772 | // this check when we KNOW that there is no block-level subexpression. |
| 773 | // The motivation is that this check requires a hashtable lookup. |
| 774 | |
| 775 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 776 | Dst.Add(Pred); |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | switch (S->getStmtClass()) { |
| 781 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 782 | |
| 783 | if (cast<BinaryOperator>(S)->isLogicalOp()) { |
| 784 | VisitLogicalExpr(cast<BinaryOperator>(S), Pred, Dst); |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | // Fall-through. |
| 789 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 790 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 791 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 792 | break; |
| 793 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 794 | case Stmt::UnaryOperatorClass: |
| 795 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
| 796 | break; |
| 797 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 798 | case Stmt::ParenExprClass: |
| 799 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 800 | break; |
| 801 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 802 | case Stmt::ImplicitCastExprClass: { |
| 803 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 804 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | case Stmt::CastExprClass: { |
| 809 | CastExpr* C = cast<CastExpr>(S); |
| 810 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 811 | break; |
| 812 | } |
| 813 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame^] | 814 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 815 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 816 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 817 | break; |
| 818 | } |
| 819 | |
| 820 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 821 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 822 | VisitGuardedExpr(S, C->getLHS(), C->getRHS(), Pred, Dst); |
| 823 | break; |
| 824 | } |
| 825 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 826 | case Stmt::DeclStmtClass: |
| 827 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 828 | break; |
| 829 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 830 | default: |
| 831 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 832 | break; |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 833 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 836 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 837 | // "Assume" logic. |
| 838 | //===----------------------------------------------------------------------===// |
| 839 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 840 | GRConstants::StateTy GRConstants::Assume(StateTy St, LValue Cond, bool Assumption, |
| 841 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 842 | |
| 843 | switch (Cond.getSubKind()) { |
| 844 | default: |
| 845 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 846 | return St; |
| 847 | |
| 848 | case LValueDeclKind: |
| 849 | isFeasible = Assumption; |
| 850 | return St; |
| 851 | |
| 852 | case ConcreteIntLValueKind: { |
| 853 | bool b = cast<ConcreteIntLValue>(Cond).getValue() != 0; |
| 854 | isFeasible = b ? Assumption : !Assumption; |
| 855 | return St; |
| 856 | } |
| 857 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 860 | GRConstants::StateTy GRConstants::Assume(StateTy St, NonLValue Cond, bool Assumption, |
| 861 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 862 | |
| 863 | switch (Cond.getSubKind()) { |
| 864 | default: |
| 865 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 866 | return St; |
| 867 | |
| 868 | case ConcreteIntKind: { |
| 869 | bool b = cast<ConcreteInt>(Cond).getValue() != 0; |
| 870 | isFeasible = b ? Assumption : !Assumption; |
| 871 | return St; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | |
| 877 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 878 | // Driver. |
| 879 | //===----------------------------------------------------------------------===// |
| 880 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 881 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 882 | static GRConstants* GraphPrintCheckerState; |
| 883 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 884 | namespace llvm { |
| 885 | template<> |
| 886 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 887 | public DefaultDOTGraphTraits { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 888 | |
| 889 | static void PrintKindLabel(std::ostream& Out, ValueKey::Kind kind) { |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 890 | switch (kind) { |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 891 | case ValueKey::IsSubExpr: Out << "Sub-Expressions:\\l"; break; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 892 | case ValueKey::IsDecl: Out << "Variables:\\l"; break; |
| 893 | case ValueKey::IsBlkExpr: Out << "Block-level Expressions:\\l"; break; |
| 894 | default: assert (false && "Unknown ValueKey type."); |
| 895 | } |
| 896 | } |
| 897 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 898 | static void PrintKind(std::ostream& Out, GRConstants::StateTy M, |
| 899 | ValueKey::Kind kind, bool isFirstGroup = false) { |
| 900 | bool isFirst = true; |
| 901 | |
| 902 | for (GRConstants::StateTy::iterator I=M.begin(), E=M.end();I!=E;++I) { |
| 903 | if (I.getKey().getKind() != kind) |
| 904 | continue; |
| 905 | |
| 906 | if (isFirst) { |
| 907 | if (!isFirstGroup) Out << "\\l\\l"; |
| 908 | PrintKindLabel(Out, kind); |
| 909 | isFirst = false; |
| 910 | } |
| 911 | else |
| 912 | Out << "\\l"; |
| 913 | |
| 914 | Out << ' '; |
| 915 | |
| 916 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) |
| 917 | Out << V->getName(); |
| 918 | else { |
| 919 | Stmt* E = cast<Stmt>(I.getKey()); |
| 920 | Out << " (" << (void*) E << ") "; |
| 921 | E->printPretty(Out); |
| 922 | } |
| 923 | |
| 924 | Out << " : "; |
| 925 | I.getData().print(Out); |
| 926 | } |
| 927 | } |
| 928 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 929 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 930 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 931 | |
| 932 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 933 | ProgramPoint Loc = N->getLocation(); |
| 934 | |
| 935 | switch (Loc.getKind()) { |
| 936 | case ProgramPoint::BlockEntranceKind: |
| 937 | Out << "Block Entrance: B" |
| 938 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 939 | break; |
| 940 | |
| 941 | case ProgramPoint::BlockExitKind: |
| 942 | assert (false); |
| 943 | break; |
| 944 | |
| 945 | case ProgramPoint::PostStmtKind: { |
| 946 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 947 | Out << L.getStmt()->getStmtClassName() << ':' |
| 948 | << (void*) L.getStmt() << ' '; |
| 949 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 950 | L.getStmt()->printPretty(Out); |
| 951 | break; |
| 952 | } |
| 953 | |
| 954 | default: { |
| 955 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 956 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 957 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 958 | |
| 959 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 960 | Out << "\\|Terminator: "; |
| 961 | E.getSrc()->printTerminator(Out); |
| 962 | |
| 963 | if (isa<SwitchStmt>(T)) { |
| 964 | // FIXME |
| 965 | } |
| 966 | else { |
| 967 | Out << "\\lCondition: "; |
| 968 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 969 | Out << "true"; |
| 970 | else |
| 971 | Out << "false"; |
| 972 | } |
| 973 | |
| 974 | Out << "\\l"; |
| 975 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 976 | |
| 977 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 978 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 979 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 983 | Out << "\\|StateID: " << (void*) N->getState().getRoot() << "\\|"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 984 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 985 | PrintKind(Out, N->getState(), ValueKey::IsDecl, true); |
| 986 | PrintKind(Out, N->getState(), ValueKey::IsBlkExpr); |
Ted Kremenek | 565256e | 2008-01-24 22:44:24 +0000 | [diff] [blame] | 987 | PrintKind(Out, N->getState(), ValueKey::IsSubExpr); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 988 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 989 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 990 | return Out.str(); |
| 991 | } |
| 992 | }; |
| 993 | } // end llvm namespace |
| 994 | #endif |
| 995 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 996 | namespace clang { |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 997 | void RunGRConstants(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx) { |
| 998 | GREngine<GRConstants> Engine(cfg, FD, Ctx); |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 999 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1000 | #ifndef NDEBUG |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1001 | GraphPrintCheckerState = &Engine.getCheckerState(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1002 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1003 | GraphPrintCheckerState = NULL; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1004 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1005 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1006 | } // end clang namespace |