Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1 | //= GRState.cpp - Path-Sensitive "State" for tracking values -----*- C++ -*--=// |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 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 | // |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 10 | // This file implements GRState and GRStateManager. |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/GRStateTrait.h" |
| 16 | #include "clang/Checker/PathSensitive/GRState.h" |
Jordy Rose | 52082af | 2010-08-15 22:19:33 +0000 | [diff] [blame] | 17 | #include "clang/Checker/PathSensitive/GRSubEngine.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 18 | #include "clang/Checker/PathSensitive/GRTransferFuncs.h" |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 20 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 23 | // Give the vtable for ConstraintManager somewhere to live. |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 24 | // FIXME: Move this elsewhere. |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 25 | ConstraintManager::~ConstraintManager() {} |
| 26 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 27 | GRStateManager::~GRStateManager() { |
| 28 | for (std::vector<GRState::Printer*>::iterator I=Printers.begin(), |
| 29 | E=Printers.end(); I!=E; ++I) |
| 30 | delete *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 32 | for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end(); |
| 33 | I!=E; ++I) |
| 34 | I->second.second(I->second.first); |
| 35 | } |
| 36 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 37 | const GRState* |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 38 | GRStateManager::RemoveDeadBindings(const GRState* state, |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 39 | const StackFrameContext *LCtx, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 40 | SymbolReaper& SymReaper) { |
| 41 | |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 42 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 43 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 44 | // tells us are live. We then see what Decls they may reference, and keep |
| 45 | // those around. This code more than likely can be made faster, and the |
| 46 | // frequency of which this method is called should be experimented with |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 47 | // for optimum performance. |
| 48 | llvm::SmallVector<const MemRegion*, 10> RegionRoots; |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 49 | GRState NewState = *state; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 50 | |
Jordy Rose | 7dadf79 | 2010-07-01 20:09:55 +0000 | [diff] [blame] | 51 | NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, SymReaper, |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 52 | state, RegionRoots); |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 54 | // Clean up the store. |
Zhongxing Xu | 5e4cb34 | 2010-08-15 12:45:09 +0000 | [diff] [blame] | 55 | NewState.St = StoreMgr->RemoveDeadBindings(NewState.St, LCtx, |
| 56 | SymReaper, RegionRoots); |
| 57 | state = getPersistentState(NewState); |
| 58 | return ConstraintMgr->RemoveDeadBindings(state, SymReaper); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 60 | |
Zhongxing Xu | 2ce43c8 | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 61 | const GRState *GRStateManager::MarshalState(const GRState *state, |
Zhongxing Xu | d5ef7b2 | 2010-07-23 05:55:01 +0000 | [diff] [blame] | 62 | const StackFrameContext *InitLoc) { |
Zhongxing Xu | 2ce43c8 | 2010-07-22 13:52:13 +0000 | [diff] [blame] | 63 | // make up an empty state for now. |
| 64 | GRState State(this, |
| 65 | EnvMgr.getInitialEnvironment(), |
| 66 | StoreMgr->getInitialStore(InitLoc), |
| 67 | GDMFactory.GetEmptyMap()); |
| 68 | |
| 69 | return getPersistentState(State); |
| 70 | } |
| 71 | |
Jordy Rose | 52082af | 2010-08-15 22:19:33 +0000 | [diff] [blame] | 72 | const GRState *GRState::bindCompoundLiteral(const CompoundLiteralExpr* CL, |
| 73 | const LocationContext *LC, |
| 74 | SVal V) const { |
| 75 | Store new_store = |
| 76 | getStateManager().StoreMgr->BindCompoundLiteral(St, CL, LC, V); |
| 77 | return makeWithStore(new_store); |
| 78 | } |
| 79 | |
| 80 | const GRState *GRState::bindDecl(const VarRegion* VR, SVal IVal) const { |
| 81 | Store new_store = getStateManager().StoreMgr->BindDecl(St, VR, IVal); |
| 82 | return makeWithStore(new_store); |
| 83 | } |
| 84 | |
| 85 | const GRState *GRState::bindDeclWithNoInit(const VarRegion* VR) const { |
| 86 | Store new_store = getStateManager().StoreMgr->BindDeclWithNoInit(St, VR); |
| 87 | return makeWithStore(new_store); |
| 88 | } |
| 89 | |
| 90 | const GRState *GRState::bindLoc(Loc LV, SVal V) const { |
| 91 | GRStateManager &Mgr = getStateManager(); |
| 92 | Store new_store = Mgr.StoreMgr->Bind(St, LV, V); |
| 93 | const GRState *new_state = makeWithStore(new_store); |
| 94 | |
| 95 | const MemRegion *MR = LV.getAsRegion(); |
| 96 | if (MR) |
| 97 | return Mgr.getOwningEngine().ProcessRegionChange(new_state, MR); |
| 98 | |
| 99 | return new_state; |
| 100 | } |
| 101 | |
| 102 | const GRState *GRState::bindDefault(SVal loc, SVal V) const { |
| 103 | GRStateManager &Mgr = getStateManager(); |
| 104 | const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion(); |
| 105 | Store new_store = Mgr.StoreMgr->BindDefault(St, R, V); |
| 106 | const GRState *new_state = makeWithStore(new_store); |
| 107 | return Mgr.getOwningEngine().ProcessRegionChange(new_state, R); |
| 108 | } |
| 109 | |
| 110 | const GRState *GRState::InvalidateRegions(const MemRegion * const *Begin, |
| 111 | const MemRegion * const *End, |
| 112 | const Expr *E, unsigned Count, |
| 113 | StoreManager::InvalidatedSymbols *IS, |
| 114 | bool invalidateGlobals) const { |
| 115 | GRStateManager &Mgr = getStateManager(); |
| 116 | GRSubEngine &Eng = Mgr.getOwningEngine(); |
| 117 | |
| 118 | if (Eng.WantsRegionChangeUpdate(this)) { |
| 119 | StoreManager::InvalidatedRegions Regions; |
| 120 | |
| 121 | Store new_store = Mgr.StoreMgr->InvalidateRegions(St, Begin, End, |
| 122 | E, Count, IS, |
| 123 | invalidateGlobals, |
| 124 | &Regions); |
| 125 | const GRState *new_state = makeWithStore(new_store); |
| 126 | |
| 127 | return Eng.ProcessRegionChanges(new_state, |
| 128 | &Regions.front(), |
| 129 | &Regions.back()+1); |
| 130 | } |
| 131 | |
| 132 | Store new_store = Mgr.StoreMgr->InvalidateRegions(St, Begin, End, |
| 133 | E, Count, IS, |
| 134 | invalidateGlobals, |
| 135 | NULL); |
| 136 | return makeWithStore(new_store); |
| 137 | } |
| 138 | |
Ted Kremenek | 76500d0 | 2009-06-23 20:38:51 +0000 | [diff] [blame] | 139 | const GRState *GRState::unbindLoc(Loc LV) const { |
Jordy Rose | a9c7621 | 2010-08-05 03:33:56 +0000 | [diff] [blame] | 140 | assert(!isa<loc::MemRegionVal>(LV) && "Use InvalidateRegion instead."); |
| 141 | |
Ted Kremenek | 76500d0 | 2009-06-23 20:38:51 +0000 | [diff] [blame] | 142 | Store OldStore = getStore(); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 143 | Store NewStore = getStateManager().StoreMgr->Remove(OldStore, LV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 145 | if (NewStore == OldStore) |
Ted Kremenek | 76500d0 | 2009-06-23 20:38:51 +0000 | [diff] [blame] | 146 | return this; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Jordy Rose | a9c7621 | 2010-08-05 03:33:56 +0000 | [diff] [blame] | 148 | return makeWithStore(NewStore); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Jordy Rose | 52082af | 2010-08-15 22:19:33 +0000 | [diff] [blame] | 151 | const GRState *GRState::EnterStackFrame(const StackFrameContext *frame) const { |
| 152 | Store new_store = getStateManager().StoreMgr->EnterStackFrame(this, frame); |
| 153 | return makeWithStore(new_store); |
| 154 | } |
| 155 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 156 | SVal GRState::getSValAsScalarOrLoc(const MemRegion *R) const { |
Ted Kremenek | 233e913 | 2009-06-24 22:15:30 +0000 | [diff] [blame] | 157 | // We only want to do fetches from regions that we can actually bind |
| 158 | // values. For example, SymbolicRegions of type 'id<...>' cannot |
| 159 | // have direct bindings (but their can be bindings on their subregions). |
| 160 | if (!R->isBoundable()) |
| 161 | return UnknownVal(); |
| 162 | |
| 163 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 164 | QualType T = TR->getValueType(); |
Ted Kremenek | 233e913 | 2009-06-24 22:15:30 +0000 | [diff] [blame] | 165 | if (Loc::IsLocType(T) || T->isIntegerType()) |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 166 | return getSVal(R); |
Ted Kremenek | 233e913 | 2009-06-24 22:15:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return UnknownVal(); |
| 170 | } |
| 171 | |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 172 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 173 | const GRState *GRState::BindExpr(const Stmt* Ex, SVal V, bool Invalidate) const{ |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 174 | Environment NewEnv = getStateManager().EnvMgr.BindExpr(Env, Ex, V, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | Invalidate); |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 176 | if (NewEnv == Env) |
| 177 | return this; |
Ted Kremenek | 6d2c657 | 2009-08-27 22:15:20 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 179 | GRState NewSt = *this; |
| 180 | NewSt.Env = NewEnv; |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 181 | return getStateManager().getPersistentState(NewSt); |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 184 | const GRState* GRStateManager::getInitialState(const LocationContext *InitLoc) { |
Ted Kremenek | 6d2c657 | 2009-08-27 22:15:20 +0000 | [diff] [blame] | 185 | GRState State(this, |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 186 | EnvMgr.getInitialEnvironment(), |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 187 | StoreMgr->getInitialStore(InitLoc), |
| 188 | GDMFactory.GetEmptyMap()); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 189 | |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 190 | return getPersistentState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 193 | const GRState* GRStateManager::getPersistentState(GRState& State) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 195 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | State.Profile(ID); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 197 | void* InsertPos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 199 | if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 200 | return I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 202 | GRState* I = (GRState*) Alloc.Allocate<GRState>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | new (I) GRState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 204 | StateSet.InsertNode(I, InsertPos); |
| 205 | return I; |
| 206 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 207 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 208 | const GRState* GRState::makeWithStore(Store store) const { |
| 209 | GRState NewSt = *this; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 210 | NewSt.St = store; |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 211 | return getStateManager().getPersistentState(NewSt); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 214 | //===----------------------------------------------------------------------===// |
| 215 | // State pretty-printing. |
| 216 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 217 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 218 | void GRState::print(llvm::raw_ostream& Out, CFG &C, const char* nl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | const char* sep) const { |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 220 | // Print the store. |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 221 | GRStateManager &Mgr = getStateManager(); |
| 222 | Mgr.getStoreManager().print(getStore(), Out, nl, sep); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 224 | // Print Subexpression bindings. |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 225 | bool isFirst = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
| 227 | for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) { |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 228 | if (C.isBlkExpr(I.getKey())) |
| 229 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 231 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 232 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 233 | isFirst = false; |
| 234 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 235 | else { Out << nl; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 237 | Out << " (" << (void*) I.getKey() << ") "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 238 | LangOptions LO; // FIXME. |
| 239 | I.getKey()->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | 6f9b3a4 | 2009-07-13 23:53:06 +0000 | [diff] [blame] | 240 | Out << " : " << I.getData(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 243 | // Print block-expression bindings. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 244 | isFirst = true; |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 245 | |
| 246 | for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) { |
| 247 | if (!C.isBlkExpr(I.getKey())) |
| 248 | continue; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 249 | |
| 250 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 251 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 252 | isFirst = false; |
| 253 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 254 | else { Out << nl; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 256 | Out << " (" << (void*) I.getKey() << ") "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 257 | LangOptions LO; // FIXME. |
| 258 | I.getKey()->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | 6f9b3a4 | 2009-07-13 23:53:06 +0000 | [diff] [blame] | 259 | Out << " : " << I.getData(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 262 | Mgr.getConstraintManager().print(this, Out, nl, sep); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 264 | // Print checker-specific data. |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 265 | for (std::vector<Printer*>::iterator I = Mgr.Printers.begin(), |
| 266 | E = Mgr.Printers.end(); I != E; ++I) { |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 267 | (*I)->Print(Out, this, nl, sep); |
| 268 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 269 | } |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 270 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 271 | void GRState::printDOT(llvm::raw_ostream& Out, CFG &C) const { |
| 272 | print(Out, C, "\\l", "\\|"); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 275 | void GRState::printStdErr(CFG &C) const { |
| 276 | print(llvm::errs(), C); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 279 | //===----------------------------------------------------------------------===// |
| 280 | // Generic Data Map. |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | |
| 283 | void* const* GRState::FindGDM(void* K) const { |
| 284 | return GDM.lookup(K); |
| 285 | } |
| 286 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 287 | void* |
| 288 | GRStateManager::FindGDMContext(void* K, |
| 289 | void* (*CreateContext)(llvm::BumpPtrAllocator&), |
| 290 | void (*DeleteContext)(void*)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 292 | std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; |
| 293 | if (!p.first) { |
| 294 | p.first = CreateContext(Alloc); |
| 295 | p.second = DeleteContext; |
| 296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 298 | return p.first; |
| 299 | } |
| 300 | |
Zhongxing Xu | 4230da6 | 2008-11-03 05:18:34 +0000 | [diff] [blame] | 301 | const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){ |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 302 | GRState::GenericDataMap M1 = St->getGDM(); |
| 303 | GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 305 | if (M1 == M2) |
| 306 | return St; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 308 | GRState NewSt = *St; |
| 309 | NewSt.GDM = M2; |
| 310 | return getPersistentState(NewSt); |
| 311 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 312 | |
Zhongxing Xu | 0541d10 | 2010-03-25 01:39:39 +0000 | [diff] [blame] | 313 | const GRState *GRStateManager::removeGDM(const GRState *state, void *Key) { |
| 314 | GRState::GenericDataMap OldM = state->getGDM(); |
| 315 | GRState::GenericDataMap NewM = GDMFactory.Remove(OldM, Key); |
| 316 | |
| 317 | if (NewM == OldM) |
| 318 | return state; |
| 319 | |
| 320 | GRState NewState = *state; |
| 321 | NewState.GDM = NewM; |
| 322 | return getPersistentState(NewState); |
| 323 | } |
| 324 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 325 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 326 | // Utility. |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 329 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 330 | class ScanReachableSymbols : public SubRegionMap::Visitor { |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 331 | typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy; |
| 332 | |
| 333 | VisitedRegionsTy visited; |
Ted Kremenek | 47fed90 | 2009-06-18 01:33:24 +0000 | [diff] [blame] | 334 | const GRState *state; |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 335 | SymbolVisitor &visitor; |
| 336 | llvm::OwningPtr<SubRegionMap> SRM; |
| 337 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Ted Kremenek | 47fed90 | 2009-06-18 01:33:24 +0000 | [diff] [blame] | 339 | ScanReachableSymbols(const GRState *st, SymbolVisitor& v) |
| 340 | : state(st), visitor(v) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 342 | bool scan(nonloc::CompoundVal val); |
| 343 | bool scan(SVal val); |
| 344 | bool scan(const MemRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 346 | // From SubRegionMap::Visitor. |
| 347 | bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) { |
| 348 | return scan(SubRegion); |
| 349 | } |
| 350 | }; |
| 351 | } |
| 352 | |
| 353 | bool ScanReachableSymbols::scan(nonloc::CompoundVal val) { |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 354 | for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 355 | if (!scan(*I)) |
| 356 | return false; |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 357 | |
| 358 | return true; |
| 359 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 361 | bool ScanReachableSymbols::scan(SVal val) { |
| 362 | if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val)) |
| 363 | return scan(X->getRegion()); |
Ted Kremenek | 380022d | 2009-03-30 18:45:36 +0000 | [diff] [blame] | 364 | |
Zhongxing Xu | 951b334 | 2010-01-11 07:40:00 +0000 | [diff] [blame] | 365 | if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val)) |
| 366 | return scan(X->getLoc()); |
| 367 | |
Ted Kremenek | 380022d | 2009-03-30 18:45:36 +0000 | [diff] [blame] | 368 | if (SymbolRef Sym = val.getAsSymbol()) |
| 369 | return visitor.VisitSymbol(Sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 371 | if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 372 | return scan(*X); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 374 | return true; |
| 375 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 377 | bool ScanReachableSymbols::scan(const MemRegion *R) { |
Ted Kremenek | 1cb151e | 2009-03-04 00:13:10 +0000 | [diff] [blame] | 378 | if (isa<MemSpaceRegion>(R) || visited.count(R)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 379 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 381 | visited.insert(R); |
| 382 | |
| 383 | // If this is a symbolic region, visit the symbol for the region. |
| 384 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) |
| 385 | if (!visitor.VisitSymbol(SR->getSymbol())) |
| 386 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 388 | // If this is a subregion, also visit the parent regions. |
| 389 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) |
Ted Kremenek | 6076e0a | 2009-03-03 18:15:30 +0000 | [diff] [blame] | 390 | if (!scan(SR->getSuperRegion())) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 391 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 393 | // Now look at the binding to this region (if any). |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 394 | if (!scan(state->getSValAsScalarOrLoc(R))) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 395 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 397 | // Now look at the subregions. |
| 398 | if (!SRM.get()) |
Zhongxing Xu | f5416bd | 2010-02-05 05:18:47 +0000 | [diff] [blame] | 399 | SRM.reset(state->getStateManager().getStoreManager(). |
| 400 | getSubRegionMap(state->getStore())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 402 | return SRM->iterSubRegions(R, *this); |
| 403 | } |
| 404 | |
Ted Kremenek | 47fed90 | 2009-06-18 01:33:24 +0000 | [diff] [blame] | 405 | bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const { |
| 406 | ScanReachableSymbols S(this, visitor); |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 407 | return S.scan(val); |
| 408 | } |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 410 | bool GRState::scanReachableSymbols(const SVal *I, const SVal *E, |
| 411 | SymbolVisitor &visitor) const { |
| 412 | ScanReachableSymbols S(this, visitor); |
| 413 | for ( ; I != E; ++I) { |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 414 | if (!S.scan(*I)) |
| 415 | return false; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 416 | } |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 417 | return true; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | bool GRState::scanReachableSymbols(const MemRegion * const *I, |
| 421 | const MemRegion * const *E, |
| 422 | SymbolVisitor &visitor) const { |
| 423 | ScanReachableSymbols S(this, visitor); |
| 424 | for ( ; I != E; ++I) { |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 425 | if (!S.scan(*I)) |
| 426 | return false; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 427 | } |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 428 | return true; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 429 | } |