Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 1 | //===-- GRConstants.cpp - Simple, Path-Sens. Constant Prop. ------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 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 | |
| 18 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | #include "clang/Analysis/Visitors/CFGStmtVisitor.h" |
| 22 | |
| 23 | #include "llvm/Support/Casting.h" |
| 24 | #include "llvm/Support/DataTypes.h" |
| 25 | #include "llvm/ADT/APSInt.h" |
| 26 | #include "llvm/ADT/FoldingSet.h" |
| 27 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
| 30 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 31 | #ifndef NDEBUG |
| 32 | #include "llvm/Support/GraphWriter.h" |
| 33 | #include <sstream> |
| 34 | #endif |
| 35 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | using llvm::APInt; |
| 38 | using llvm::APFloat; |
| 39 | using llvm::dyn_cast; |
| 40 | using llvm::cast; |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 43 | /// DSPtr - A variant smart pointer that wraps either a ValueDecl* or a |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 44 | /// Stmt*. Use cast<> or dyn_cast<> to get actual pointer type |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | namespace { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 47 | class VISIBILITY_HIDDEN DSPtr { |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 48 | uintptr_t Raw; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 49 | public: |
Ted Kremenek | e3d7c24 | 2008-01-16 23:35:31 +0000 | [diff] [blame] | 50 | enum VariantKind { IsSubExp=0x0, IsValueDecl=0x1, IsBlkLvl=0x2, Flags=0x3 }; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 51 | inline void* getPtr() const { return reinterpret_cast<void*>(Raw & ~Flags); } |
| 52 | inline VariantKind getKind() const { return (VariantKind) (Raw & Flags); } |
| 53 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 54 | DSPtr(ValueDecl* D) : Raw(reinterpret_cast<uintptr_t>(D) | IsValueDecl) {} |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 55 | DSPtr(Stmt* S, bool isBlkLvl) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 56 | : Raw(reinterpret_cast<uintptr_t>(S) | (isBlkLvl ? IsBlkLvl : IsSubExp)) {} |
| 57 | |
| 58 | bool isSubExpr() const { return getKind() == IsSubExp; } |
| 59 | |
| 60 | inline void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 9849185 | 2008-01-16 05:51:13 +0000 | [diff] [blame] | 61 | ID.AddPointer(getPtr()); |
| 62 | ID.AddInteger((unsigned) getKind()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 63 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 64 | inline bool operator==(const DSPtr& X) const { return Raw == X.Raw; } |
| 65 | inline bool operator!=(const DSPtr& X) const { return Raw != X.Raw; } |
Ted Kremenek | b3d2dca | 2008-01-16 23:33:44 +0000 | [diff] [blame] | 66 | inline bool operator<(const DSPtr& X) const { |
| 67 | VariantKind k = getKind(), Xk = X.getKind(); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 68 | return k == Xk ? getPtr() < X.getPtr() : ((unsigned) k) < ((unsigned) Xk); |
Ted Kremenek | b3d2dca | 2008-01-16 23:33:44 +0000 | [diff] [blame] | 69 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 70 | }; |
| 71 | } // end anonymous namespace |
| 72 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 73 | // Machinery to get cast<> and dyn_cast<> working with DSPtr. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 74 | namespace llvm { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 75 | template<> inline bool isa<ValueDecl,DSPtr>(const DSPtr& V) { |
| 76 | return V.getKind() == DSPtr::IsValueDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 77 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 78 | template<> inline bool isa<Stmt,DSPtr>(const DSPtr& V) { |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 79 | return ((unsigned) V.getKind()) != DSPtr::IsValueDecl; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 80 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 81 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<ValueDecl,DSPtr> { |
| 82 | typedef const ValueDecl* ret_type; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 83 | }; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 84 | template<> struct VISIBILITY_HIDDEN cast_retty_impl<Stmt,DSPtr> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 85 | typedef const Stmt* ret_type; |
| 86 | }; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 87 | template<> struct VISIBILITY_HIDDEN simplify_type<DSPtr> { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 88 | typedef void* SimpleType; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 89 | static inline SimpleType getSimplifiedValue(const DSPtr &V) { |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 90 | return V.getPtr(); |
| 91 | } |
| 92 | }; |
| 93 | } // end llvm namespace |
| 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | // DeclStmtMapTy - A ImmutableMap type from Decl*/Stmt* to integers. |
| 97 | // |
| 98 | // FIXME: We may eventually use APSInt, or a mixture of APSInt and |
| 99 | // integer primitives to do this right; this will handle both |
| 100 | // different bit-widths and allow us to detect integer overflows, etc. |
| 101 | // |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 104 | typedef llvm::ImmutableMap<DSPtr,uint64_t> DeclStmtMapTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 105 | |
| 106 | namespace clang { |
| 107 | template<> |
| 108 | struct VISIBILITY_HIDDEN GRTrait<DeclStmtMapTy> { |
| 109 | static inline void* toPtr(DeclStmtMapTy M) { |
| 110 | return reinterpret_cast<void*>(M.getRoot()); |
| 111 | } |
| 112 | static inline DeclStmtMapTy toState(void* P) { |
| 113 | return DeclStmtMapTy(static_cast<DeclStmtMapTy::TreeTy*>(P)); |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | // The Checker! |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
| 122 | namespace { |
| 123 | class VISIBILITY_HIDDEN ExprVariantTy { |
| 124 | const uint64_t val; |
| 125 | const bool isConstant; |
| 126 | public: |
| 127 | ExprVariantTy() : val(0), isConstant(false) {} |
| 128 | ExprVariantTy(uint64_t v) : val(v), isConstant(true) {} |
| 129 | |
| 130 | operator bool() const { return isConstant; } |
| 131 | uint64_t getVal() const { assert (isConstant); return val; } |
| 132 | |
| 133 | ExprVariantTy operator+(const ExprVariantTy& X) const { |
| 134 | if (!isConstant || !X.isConstant) return ExprVariantTy(); |
| 135 | else return ExprVariantTy(val+X.val); |
| 136 | } |
| 137 | |
| 138 | ExprVariantTy operator-(const ExprVariantTy& X) const { |
| 139 | if (!isConstant || !X.isConstant) return ExprVariantTy(); |
Ted Kremenek | 95b3f6f | 2008-01-16 22:20:36 +0000 | [diff] [blame] | 140 | else return ExprVariantTy(val-X.val); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 141 | } |
| 142 | }; |
| 143 | } // end anonymous namespace |
| 144 | |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | // The Checker! |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
| 149 | namespace { |
| 150 | class VISIBILITY_HIDDEN GRConstants : public CFGStmtVisitor<GRConstants> { |
| 151 | |
| 152 | public: |
| 153 | typedef DeclStmtMapTy StateTy; |
| 154 | typedef GRNodeBuilder<GRConstants> NodeBuilder; |
| 155 | typedef ExplodedNode<StateTy> NodeTy; |
| 156 | |
| 157 | protected: |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 158 | // Liveness - live-variables information the ValueDecl* and Expr* (block-level) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 159 | // in the CFG. Used to prune out dead state. |
| 160 | LiveVariables* Liveness; |
| 161 | |
| 162 | // Builder - The current GRNodeBuilder which is used when building the nodes |
| 163 | // for a given statement. |
| 164 | NodeBuilder* Builder; |
| 165 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 166 | DeclStmtMapTy::Factory StateMgr; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 167 | |
| 168 | // cfg - the current CFG. |
| 169 | CFG* cfg; |
| 170 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 171 | typedef llvm::SmallVector<NodeTy*,8> NodeSetTy; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 172 | NodeSetTy NodeSetA; |
| 173 | NodeSetTy NodeSetB; |
| 174 | NodeSetTy* Nodes; |
| 175 | NodeSetTy* OldNodes; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 176 | StateTy CurrentState; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 177 | NodeTy* InitialPred; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 179 | public: |
| 180 | GRConstants() : Liveness(NULL), Builder(NULL), cfg(NULL), |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 181 | Nodes(&NodeSetA), OldNodes(&NodeSetB), |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 182 | CurrentState(StateMgr.GetEmptyMap()), InitialPred(NULL) {} |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 183 | |
| 184 | ~GRConstants() { delete Liveness; } |
| 185 | |
| 186 | CFG& getCFG() { assert (cfg); return *cfg; } |
| 187 | |
| 188 | void Initialize(CFG& c) { |
| 189 | cfg = &c; |
| 190 | Liveness = new LiveVariables(c); |
| 191 | Liveness->runOnCFG(c); |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame^] | 192 | Liveness->runOnAllBlocks(c, NULL, true); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | StateTy getInitialState() { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 196 | return StateMgr.GetEmptyMap(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void ProcessStmt(Stmt* S, NodeBuilder& builder); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 200 | void SwitchNodeSets(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 201 | void DoStmt(Stmt* S); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 202 | |
| 203 | StateTy RemoveDescendantMappings(Stmt* S, StateTy M, unsigned Levels=2); |
| 204 | StateTy RemoveSubExprMappings(StateTy M); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 205 | |
| 206 | void AddBinding(Expr* E, ExprVariantTy V, bool isBlkLvl = false); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 207 | void AddBinding(ValueDecl* D, ExprVariantTy V); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 209 | ExprVariantTy GetBinding(Expr* E); |
| 210 | |
| 211 | void BlockStmt_VisitStmt(Stmt* S) { DoStmt(S); } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 212 | |
| 213 | void VisitAssign(BinaryOperator* O); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 214 | void VisitBinAdd(BinaryOperator* O); |
| 215 | void VisitBinSub(BinaryOperator* O); |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 216 | void VisitBinAssign(BinaryOperator* D); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 217 | }; |
| 218 | } // end anonymous namespace |
| 219 | |
| 220 | void GRConstants::ProcessStmt(Stmt* S, NodeBuilder& builder) { |
| 221 | Builder = &builder; |
| 222 | Nodes->clear(); |
| 223 | OldNodes->clear(); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 224 | InitialPred = Builder->getLastNode(); |
| 225 | assert (InitialPred); |
| 226 | OldNodes->push_back(InitialPred); |
| 227 | CurrentState = RemoveSubExprMappings(InitialPred->getState()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 228 | BlockStmt_Visit(S); |
| 229 | Builder = NULL; |
| 230 | } |
| 231 | |
| 232 | ExprVariantTy GRConstants::GetBinding(Expr* E) { |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 233 | DSPtr P(NULL); |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 234 | E = E->IgnoreParens(); |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | ca3e857 | 2008-01-16 22:28:08 +0000 | [diff] [blame] | 236 | switch (E->getStmtClass()) { |
| 237 | case Stmt::DeclRefExprClass: |
| 238 | P = DSPtr(cast<DeclRefExpr>(E)->getDecl()); |
| 239 | break; |
| 240 | |
| 241 | case Stmt::IntegerLiteralClass: |
| 242 | return cast<IntegerLiteral>(E)->getValue().getZExtValue(); |
| 243 | |
| 244 | default: |
| 245 | P = DSPtr(E, getCFG().isBlkExpr(E)); |
| 246 | break; |
| 247 | } |
Ted Kremenek | 0525a4f | 2008-01-16 19:47:19 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 249 | StateTy::iterator I = CurrentState.find(P); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 251 | if (I == CurrentState.end()) |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 252 | return ExprVariantTy(); |
| 253 | |
| 254 | return (*I).second; |
| 255 | } |
| 256 | |
| 257 | void GRConstants::AddBinding(Expr* E, ExprVariantTy V, bool isBlkLvl) { |
Ted Kremenek | 22f0d97 | 2008-01-16 19:28:16 +0000 | [diff] [blame] | 258 | if (V) |
| 259 | CurrentState = StateMgr.Add(CurrentState, DSPtr(E,isBlkLvl), V.getVal()); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 262 | void GRConstants::AddBinding(ValueDecl* D, ExprVariantTy V) { |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 263 | if (V) |
| 264 | CurrentState = StateMgr.Add(CurrentState, DSPtr(D), V.getVal()); |
| 265 | else |
| 266 | CurrentState = StateMgr.Remove(CurrentState, DSPtr(D)); |
| 267 | } |
| 268 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 269 | void GRConstants::SwitchNodeSets() { |
| 270 | NodeSetTy* Tmp = OldNodes; |
| 271 | OldNodes = Nodes; |
| 272 | Nodes = Tmp; |
| 273 | Nodes->clear(); |
| 274 | } |
| 275 | |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 276 | GRConstants::StateTy |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 277 | GRConstants::RemoveSubExprMappings(StateTy M) { |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 278 | for (StateTy::iterator I = M.begin(), E = M.end(); |
| 279 | I!=E && I.getKey().getKind() == DSPtr::IsSubExp; ++I) { |
| 280 | // Note: we can assign a new map to M since the iterators are |
| 281 | // iterating over the tree of the original map (aren't immutable maps |
| 282 | // nice?). |
| 283 | M = StateMgr.Remove(M, I.getKey()); |
| 284 | } |
| 285 | |
| 286 | return M; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
| 290 | GRConstants::StateTy |
| 291 | GRConstants::RemoveDescendantMappings(Stmt* S, GRConstants::StateTy State, |
| 292 | unsigned Levels) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 293 | typedef Stmt::child_iterator iterator; |
| 294 | |
| 295 | for (iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 296 | if (Stmt* C = *I) { |
| 297 | if (Levels == 1) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 298 | // Observe that this will only remove mappings to non-block level |
| 299 | // expressions. This is valid even if *CI is a block-level expression, |
| 300 | // since it simply won't be in the map in the first place. |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 301 | // Note: This should also work if 'C' is a block-level expression, |
| 302 | // although ideally we would want to skip processing C's children. |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 303 | State = StateMgr.Remove(State, DSPtr(C,false)); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 304 | } |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 305 | else { |
| 306 | if (ParenExpr* P = dyn_cast<ParenExpr>(C)) |
| 307 | State = RemoveDescendantMappings(P, State, Levels); |
| 308 | else |
| 309 | State = RemoveDescendantMappings(C, State, Levels-1); |
| 310 | } |
| 311 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 312 | |
| 313 | return State; |
| 314 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 315 | |
| 316 | void GRConstants::DoStmt(Stmt* S) { |
| 317 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 318 | if (*I) DoStmt(*I); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 320 | for (NodeSetTy::iterator I=OldNodes->begin(), E=OldNodes->end(); I!=E; ++I) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 321 | NodeTy* Pred = *I; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 322 | |
| 323 | if (Pred != InitialPred) |
| 324 | CurrentState = Pred->getState(); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 326 | StateTy OldState = CurrentState; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 327 | |
| 328 | if (Pred != InitialPred) |
| 329 | CurrentState = RemoveDescendantMappings(S, CurrentState); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 331 | Visit(S); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 333 | if (CurrentState != OldState) { |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 334 | NodeTy* N = Builder->generateNode(S, CurrentState, Pred); |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 335 | if (N) Nodes->push_back(N); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 336 | } |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 337 | else Nodes->push_back(Pred); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 338 | } |
Ted Kremenek | 3c6c672 | 2008-01-16 17:56:25 +0000 | [diff] [blame] | 339 | |
| 340 | SwitchNodeSets(); |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 343 | void GRConstants::VisitBinAdd(BinaryOperator* B) { |
| 344 | AddBinding(B, GetBinding(B->getLHS()) + GetBinding(B->getRHS())); |
| 345 | } |
| 346 | |
| 347 | void GRConstants::VisitBinSub(BinaryOperator* B) { |
| 348 | AddBinding(B, GetBinding(B->getLHS()) - GetBinding(B->getRHS())); |
| 349 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 351 | |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 352 | void GRConstants::VisitBinAssign(BinaryOperator* B) { |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame^] | 353 | if (DeclRefExpr* D = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParens())) { |
| 354 | ExprVariantTy V = GetBinding(B->getRHS()); |
| 355 | AddBinding(D->getDecl(), V); |
| 356 | AddBinding(B, V); |
| 357 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===// |
| 361 | // Driver. |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 364 | #ifndef NDEBUG |
| 365 | namespace llvm { |
| 366 | template<> |
| 367 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRConstants::NodeTy*> : |
| 368 | public DefaultDOTGraphTraits { |
| 369 | |
| 370 | static std::string getNodeLabel(const GRConstants::NodeTy* N, void*) { |
| 371 | std::ostringstream Out; |
| 372 | |
| 373 | Out << "Vertex: " << (void*) N << '\n'; |
| 374 | ProgramPoint Loc = N->getLocation(); |
| 375 | |
| 376 | switch (Loc.getKind()) { |
| 377 | case ProgramPoint::BlockEntranceKind: |
| 378 | Out << "Block Entrance: B" |
| 379 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 380 | break; |
| 381 | |
| 382 | case ProgramPoint::BlockExitKind: |
| 383 | assert (false); |
| 384 | break; |
| 385 | |
| 386 | case ProgramPoint::PostStmtKind: { |
| 387 | const PostStmt& L = cast<PostStmt>(Loc); |
| 388 | Out << "Stmt: " << (void*) L.getStmt() << '\n'; |
| 389 | L.getStmt()->printPretty(Out); |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | default: { |
| 394 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 395 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 396 | << E.getDst()->getBlockID() << ')'; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | Out << "\n{"; |
| 401 | |
| 402 | GRConstants::StateTy M = N->getState(); |
| 403 | bool isFirst = true; |
| 404 | |
| 405 | for (GRConstants::StateTy::iterator I=M.begin(), E=M.end(); I!=E; ++I) { |
| 406 | if (!isFirst) |
| 407 | Out << '\n'; |
| 408 | else |
| 409 | isFirst = false; |
| 410 | |
| 411 | if (ValueDecl* V = dyn_cast<ValueDecl>(I.getKey())) { |
| 412 | Out << "Decl: " << (void*) V << ", " << V->getName(); |
| 413 | } |
| 414 | else { |
| 415 | Stmt* E = cast<Stmt>(I.getKey()); |
| 416 | Out << "Stmt: " << (void*) E; |
| 417 | } |
| 418 | |
| 419 | Out << " => " << I.getData(); |
| 420 | } |
| 421 | |
| 422 | Out << " }"; |
| 423 | |
| 424 | return Out.str(); |
| 425 | } |
| 426 | }; |
| 427 | } // end llvm namespace |
| 428 | #endif |
| 429 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 430 | namespace clang { |
| 431 | void RunGRConstants(CFG& cfg) { |
| 432 | GREngine<GRConstants> Engine(cfg); |
| 433 | Engine.ExecuteWorkList(); |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 434 | #ifndef NDEBUG |
| 435 | llvm::ViewGraph(*Engine.getGraph().roots_begin(),"GRConstants"); |
| 436 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 437 | } |
| 438 | } |