Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [diff] [blame^] | 1 | //== AnalysisContext.cpp - Analysis context for Path Sens analysis -*- 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 | // This file defines AnalysisContext, a class that manages the analysis context |
| 11 | // data for path sensitive analysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/CFG.h" |
| 16 | #include "clang/Analysis/AnalysisContext.h" |
| 17 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
| 21 | #include "clang/AST/ParentMap.h" |
| 22 | #include "clang/AST/StmtVisitor.h" |
| 23 | #include "clang/Analysis/Support/BumpVector.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | void AnalysisContextManager::clear() { |
| 29 | for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I) |
| 30 | delete I->second; |
| 31 | Contexts.clear(); |
| 32 | } |
| 33 | |
| 34 | Stmt *AnalysisContext::getBody() { |
| 35 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 36 | return FD->getBody(); |
| 37 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 38 | return MD->getBody(); |
| 39 | else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 40 | return BD->getBody(); |
| 41 | else if (const FunctionTemplateDecl *FunTmpl |
| 42 | = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 43 | return FunTmpl->getTemplatedDecl()->getBody(); |
| 44 | |
| 45 | llvm_unreachable("unknown code decl"); |
| 46 | } |
| 47 | |
| 48 | const ImplicitParamDecl *AnalysisContext::getSelfDecl() const { |
| 49 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 50 | return MD->getSelfDecl(); |
| 51 | |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | CFG *AnalysisContext::getCFG() { |
| 56 | if (!cfg) |
| 57 | cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), AddEHEdges); |
| 58 | return cfg; |
| 59 | } |
| 60 | |
| 61 | ParentMap &AnalysisContext::getParentMap() { |
| 62 | if (!PM) |
| 63 | PM = new ParentMap(getBody()); |
| 64 | return *PM; |
| 65 | } |
| 66 | |
| 67 | LiveVariables *AnalysisContext::getLiveVariables() { |
| 68 | if (!liveness) { |
| 69 | CFG *c = getCFG(); |
| 70 | if (!c) |
| 71 | return 0; |
| 72 | |
| 73 | liveness = new LiveVariables(*this); |
| 74 | liveness->runOnCFG(*c); |
| 75 | liveness->runOnAllBlocks(*c, 0, true); |
| 76 | } |
| 77 | |
| 78 | return liveness; |
| 79 | } |
| 80 | |
| 81 | AnalysisContext *AnalysisContextManager::getContext(const Decl *D) { |
| 82 | AnalysisContext *&AC = Contexts[D]; |
| 83 | if (!AC) |
| 84 | AC = new AnalysisContext(D); |
| 85 | |
| 86 | return AC; |
| 87 | } |
| 88 | |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | // FoldingSet profiling. |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
| 93 | void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID, |
| 94 | ContextKind ck, |
| 95 | AnalysisContext *ctx, |
| 96 | const LocationContext *parent, |
| 97 | const void* data) { |
| 98 | ID.AddInteger(ck); |
| 99 | ID.AddPointer(ctx); |
| 100 | ID.AddPointer(parent); |
| 101 | ID.AddPointer(data); |
| 102 | } |
| 103 | |
| 104 | void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) { |
| 105 | Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index); |
| 106 | } |
| 107 | |
| 108 | void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) { |
| 109 | Profile(ID, getAnalysisContext(), getParent(), Enter); |
| 110 | } |
| 111 | |
| 112 | void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) { |
| 113 | Profile(ID, getAnalysisContext(), getParent(), BD); |
| 114 | } |
| 115 | |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | // LocationContext creation. |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | |
| 120 | template <typename LOC, typename DATA> |
| 121 | const LOC* |
| 122 | LocationContextManager::getLocationContext(AnalysisContext *ctx, |
| 123 | const LocationContext *parent, |
| 124 | const DATA *d) { |
| 125 | llvm::FoldingSetNodeID ID; |
| 126 | LOC::Profile(ID, ctx, parent, d); |
| 127 | void *InsertPos; |
| 128 | |
| 129 | LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 130 | |
| 131 | if (!L) { |
| 132 | L = new LOC(ctx, parent, d); |
| 133 | Contexts.InsertNode(L, InsertPos); |
| 134 | } |
| 135 | return L; |
| 136 | } |
| 137 | |
| 138 | const StackFrameContext* |
| 139 | LocationContextManager::getStackFrame(AnalysisContext *ctx, |
| 140 | const LocationContext *parent, |
| 141 | const Stmt *s, const CFGBlock *blk, |
| 142 | unsigned idx) { |
| 143 | llvm::FoldingSetNodeID ID; |
| 144 | StackFrameContext::Profile(ID, ctx, parent, s, blk, idx); |
| 145 | void *InsertPos; |
| 146 | StackFrameContext *L = |
| 147 | cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 148 | if (!L) { |
| 149 | L = new StackFrameContext(ctx, parent, s, blk, idx); |
| 150 | Contexts.InsertNode(L, InsertPos); |
| 151 | } |
| 152 | return L; |
| 153 | } |
| 154 | |
| 155 | const ScopeContext * |
| 156 | LocationContextManager::getScope(AnalysisContext *ctx, |
| 157 | const LocationContext *parent, |
| 158 | const Stmt *s) { |
| 159 | return getLocationContext<ScopeContext, Stmt>(ctx, parent, s); |
| 160 | } |
| 161 | |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | // LocationContext methods. |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | |
| 166 | const StackFrameContext *LocationContext::getCurrentStackFrame() const { |
| 167 | const LocationContext *LC = this; |
| 168 | while (LC) { |
| 169 | if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) |
| 170 | return SFC; |
| 171 | LC = LC->getParent(); |
| 172 | } |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | const StackFrameContext * |
| 177 | LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const { |
| 178 | const LocationContext *LC = this; |
| 179 | while (LC) { |
| 180 | if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) { |
| 181 | if (cast<DeclContext>(SFC->getDecl()) == DC) |
| 182 | return SFC; |
| 183 | } |
| 184 | LC = LC->getParent(); |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | //===----------------------------------------------------------------------===// |
| 190 | // Lazily generated map to query the external variables referenced by a Block. |
| 191 | //===----------------------------------------------------------------------===// |
| 192 | |
| 193 | namespace { |
| 194 | class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{ |
| 195 | BumpVector<const VarDecl*> &BEVals; |
| 196 | BumpVectorContext &BC; |
| 197 | llvm::DenseMap<const VarDecl*, unsigned> Visited; |
| 198 | public: |
| 199 | FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals, |
| 200 | BumpVectorContext &bc) |
| 201 | : BEVals(bevals), BC(bc) {} |
| 202 | |
| 203 | void VisitStmt(Stmt *S) { |
| 204 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I) |
| 205 | if (Stmt *child = *I) |
| 206 | Visit(child); |
| 207 | } |
| 208 | |
| 209 | void VisitDeclRefExpr(const DeclRefExpr *DR) { |
| 210 | // Non-local variables are also directly modified. |
| 211 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 212 | if (!VD->hasLocalStorage()) { |
| 213 | unsigned &flag = Visited[VD]; |
| 214 | if (!flag) { |
| 215 | flag = 1; |
| 216 | BEVals.push_back(VD, BC); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) { |
| 222 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 223 | unsigned &flag = Visited[VD]; |
| 224 | if (!flag) { |
| 225 | flag = 1; |
| 226 | BEVals.push_back(VD, BC); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | }; |
| 231 | } // end anonymous namespace |
| 232 | |
| 233 | typedef BumpVector<const VarDecl*> DeclVec; |
| 234 | |
| 235 | static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD, |
| 236 | void *&Vec, |
| 237 | llvm::BumpPtrAllocator &A) { |
| 238 | if (Vec) |
| 239 | return (DeclVec*) Vec; |
| 240 | |
| 241 | BumpVectorContext BC(A); |
| 242 | DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>(); |
| 243 | new (BV) DeclVec(BC, 10); |
| 244 | |
| 245 | // Find the referenced variables. |
| 246 | FindBlockDeclRefExprsVals F(*BV, BC); |
| 247 | F.Visit(BD->getBody()); |
| 248 | |
| 249 | Vec = BV; |
| 250 | return BV; |
| 251 | } |
| 252 | |
| 253 | std::pair<AnalysisContext::referenced_decls_iterator, |
| 254 | AnalysisContext::referenced_decls_iterator> |
| 255 | AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) { |
| 256 | if (!ReferencedBlockVars) |
| 257 | ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>(); |
| 258 | |
| 259 | DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A); |
| 260 | return std::make_pair(V->begin(), V->end()); |
| 261 | } |
| 262 | |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | // Cleanup. |
| 265 | //===----------------------------------------------------------------------===// |
| 266 | |
| 267 | AnalysisContext::~AnalysisContext() { |
| 268 | delete cfg; |
| 269 | delete liveness; |
| 270 | delete PM; |
| 271 | delete ReferencedBlockVars; |
| 272 | } |
| 273 | |
| 274 | AnalysisContextManager::~AnalysisContextManager() { |
| 275 | for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I) |
| 276 | delete I->second; |
| 277 | } |
| 278 | |
| 279 | LocationContext::~LocationContext() {} |
| 280 | |
| 281 | LocationContextManager::~LocationContextManager() { |
| 282 | clear(); |
| 283 | } |
| 284 | |
| 285 | void LocationContextManager::clear() { |
| 286 | for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(), |
| 287 | E = Contexts.end(); I != E; ) { |
| 288 | LocationContext *LC = &*I; |
| 289 | ++I; |
| 290 | delete LC; |
| 291 | } |
| 292 | |
| 293 | Contexts.clear(); |
| 294 | } |
| 295 | |