1 //== AnalysisDeclContext.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 AnalysisDeclContext, a class that manages the analysis context
11 // data for path sensitive analysis.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/ParentMap.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "clang/Analysis/Analyses/LiveVariables.h"
22 #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
23 #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
24 #include "clang/Analysis/AnalysisContext.h"
25 #include "clang/Analysis/CFG.h"
26 #include "clang/Analysis/CFGStmtMap.h"
27 #include "clang/Analysis/Support/BumpVector.h"
28 #include "llvm/Support/SaveAndRestore.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/Support/ErrorHandling.h"
31 
32 #include "BodyFarm.h"
33 
34 using namespace clang;
35 
36 typedef llvm::DenseMap<const void *, ManagedAnalysis *> ManagedAnalysisMap;
37 
38 AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
39                                          const Decl *d,
40                                          const CFG::BuildOptions &buildOptions)
41   : Manager(Mgr),
42     D(d),
43     cfgBuildOptions(buildOptions),
44     forcedBlkExprs(0),
45     builtCFG(false),
46     builtCompleteCFG(false),
47     ReferencedBlockVars(0),
48     ManagedAnalyses(0)
49 {
50   cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
51 }
52 
53 AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
54                                          const Decl *d)
55 : Manager(Mgr),
56   D(d),
57   forcedBlkExprs(0),
58   builtCFG(false),
59   builtCompleteCFG(false),
60   ReferencedBlockVars(0),
61   ManagedAnalyses(0)
62 {
63   cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
64 }
65 
66 AnalysisDeclContextManager::AnalysisDeclContextManager(bool useUnoptimizedCFG,
67                                                        bool addImplicitDtors,
68                                                        bool addInitializers,
69                                                        bool addTemporaryDtors,
70                                                        bool synthesizeBodies)
71   : SynthesizeBodies(synthesizeBodies)
72 {
73   cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
74   cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
75   cfgBuildOptions.AddInitializers = addInitializers;
76   cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
77 }
78 
79 void AnalysisDeclContextManager::clear() {
80   for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
81     delete I->second;
82   Contexts.clear();
83 }
84 
85 static BodyFarm &getBodyFarm(ASTContext &C) {
86   static BodyFarm *BF = new BodyFarm(C);
87   return *BF;
88 }
89 
90 Stmt *AnalysisDeclContext::getBody() const {
91   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
92     Stmt *Body = FD->getBody();
93     if (!Body && Manager && Manager->synthesizeBodies())
94       return getBodyFarm(getASTContext()).getBody(FD);
95     return Body;
96   }
97   else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
98     return MD->getBody();
99   else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
100     return BD->getBody();
101   else if (const FunctionTemplateDecl *FunTmpl
102            = dyn_cast_or_null<FunctionTemplateDecl>(D))
103     return FunTmpl->getTemplatedDecl()->getBody();
104 
105   llvm_unreachable("unknown code decl");
106 }
107 
108 const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
109   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
110     return MD->getSelfDecl();
111   if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
112     // See if 'self' was captured by the block.
113     for (BlockDecl::capture_const_iterator it = BD->capture_begin(),
114          et = BD->capture_end(); it != et; ++it) {
115       const VarDecl *VD = it->getVariable();
116       if (VD->getName() == "self")
117         return dyn_cast<ImplicitParamDecl>(VD);
118     }
119   }
120 
121   return NULL;
122 }
123 
124 void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
125   if (!forcedBlkExprs)
126     forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
127   // Default construct an entry for 'stmt'.
128   if (const Expr *e = dyn_cast<Expr>(stmt))
129     stmt = e->IgnoreParens();
130   (void) (*forcedBlkExprs)[stmt];
131 }
132 
133 const CFGBlock *
134 AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
135   assert(forcedBlkExprs);
136   if (const Expr *e = dyn_cast<Expr>(stmt))
137     stmt = e->IgnoreParens();
138   CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
139     forcedBlkExprs->find(stmt);
140   assert(itr != forcedBlkExprs->end());
141   return itr->second;
142 }
143 
144 CFG *AnalysisDeclContext::getCFG() {
145   if (!cfgBuildOptions.PruneTriviallyFalseEdges)
146     return getUnoptimizedCFG();
147 
148   if (!builtCFG) {
149     cfg.reset(CFG::buildCFG(D, getBody(),
150                             &D->getASTContext(), cfgBuildOptions));
151     // Even when the cfg is not successfully built, we don't
152     // want to try building it again.
153     builtCFG = true;
154   }
155   return cfg.get();
156 }
157 
158 CFG *AnalysisDeclContext::getUnoptimizedCFG() {
159   if (!builtCompleteCFG) {
160     SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
161                                   false);
162     completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(),
163                                     cfgBuildOptions));
164     // Even when the cfg is not successfully built, we don't
165     // want to try building it again.
166     builtCompleteCFG = true;
167   }
168   return completeCFG.get();
169 }
170 
171 CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
172   if (cfgStmtMap)
173     return cfgStmtMap.get();
174 
175   if (CFG *c = getCFG()) {
176     cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
177     return cfgStmtMap.get();
178   }
179 
180   return 0;
181 }
182 
183 CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
184   if (CFA)
185     return CFA.get();
186 
187   if (CFG *c = getCFG()) {
188     CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
189     return CFA.get();
190   }
191 
192   return 0;
193 }
194 
195 void AnalysisDeclContext::dumpCFG(bool ShowColors) {
196     getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
197 }
198 
199 ParentMap &AnalysisDeclContext::getParentMap() {
200   if (!PM) {
201     PM.reset(new ParentMap(getBody()));
202     if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
203       for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
204                                                    E = C->init_end();
205            I != E; ++I) {
206         PM->addStmt((*I)->getInit());
207       }
208     }
209   }
210   return *PM;
211 }
212 
213 PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
214   if (!PCA)
215     PCA.reset(new PseudoConstantAnalysis(getBody()));
216   return PCA.get();
217 }
218 
219 AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
220   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
221     // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
222     // that has the body.
223     FD->hasBody(FD);
224     D = FD;
225   }
226 
227   AnalysisDeclContext *&AC = Contexts[D];
228   if (!AC)
229     AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
230   return AC;
231 }
232 
233 const StackFrameContext *
234 AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
235                                const CFGBlock *Blk, unsigned Idx) {
236   return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
237 }
238 
239 const BlockInvocationContext *
240 AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
241                                                const clang::BlockDecl *BD,
242                                                const void *ContextData) {
243   return getLocationContextManager().getBlockInvocationContext(this, parent,
244                                                                BD, ContextData);
245 }
246 
247 LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
248   assert(Manager &&
249          "Cannot create LocationContexts without an AnalysisDeclContextManager!");
250   return Manager->getLocationContextManager();
251 }
252 
253 //===----------------------------------------------------------------------===//
254 // FoldingSet profiling.
255 //===----------------------------------------------------------------------===//
256 
257 void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
258                                     ContextKind ck,
259                                     AnalysisDeclContext *ctx,
260                                     const LocationContext *parent,
261                                     const void *data) {
262   ID.AddInteger(ck);
263   ID.AddPointer(ctx);
264   ID.AddPointer(parent);
265   ID.AddPointer(data);
266 }
267 
268 void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
269   Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
270 }
271 
272 void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
273   Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
274 }
275 
276 void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
277   Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
278 }
279 
280 //===----------------------------------------------------------------------===//
281 // LocationContext creation.
282 //===----------------------------------------------------------------------===//
283 
284 template <typename LOC, typename DATA>
285 const LOC*
286 LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
287                                            const LocationContext *parent,
288                                            const DATA *d) {
289   llvm::FoldingSetNodeID ID;
290   LOC::Profile(ID, ctx, parent, d);
291   void *InsertPos;
292 
293   LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
294 
295   if (!L) {
296     L = new LOC(ctx, parent, d);
297     Contexts.InsertNode(L, InsertPos);
298   }
299   return L;
300 }
301 
302 const StackFrameContext*
303 LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
304                                       const LocationContext *parent,
305                                       const Stmt *s,
306                                       const CFGBlock *blk, unsigned idx) {
307   llvm::FoldingSetNodeID ID;
308   StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
309   void *InsertPos;
310   StackFrameContext *L =
311    cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
312   if (!L) {
313     L = new StackFrameContext(ctx, parent, s, blk, idx);
314     Contexts.InsertNode(L, InsertPos);
315   }
316   return L;
317 }
318 
319 const ScopeContext *
320 LocationContextManager::getScope(AnalysisDeclContext *ctx,
321                                  const LocationContext *parent,
322                                  const Stmt *s) {
323   return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
324 }
325 
326 const BlockInvocationContext *
327 LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
328                                                   const LocationContext *parent,
329                                                   const BlockDecl *BD,
330                                                   const void *ContextData) {
331   llvm::FoldingSetNodeID ID;
332   BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
333   void *InsertPos;
334   BlockInvocationContext *L =
335     cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
336                                                                     InsertPos));
337   if (!L) {
338     L = new BlockInvocationContext(ctx, parent, BD, ContextData);
339     Contexts.InsertNode(L, InsertPos);
340   }
341   return L;
342 }
343 
344 //===----------------------------------------------------------------------===//
345 // LocationContext methods.
346 //===----------------------------------------------------------------------===//
347 
348 const StackFrameContext *LocationContext::getCurrentStackFrame() const {
349   const LocationContext *LC = this;
350   while (LC) {
351     if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
352       return SFC;
353     LC = LC->getParent();
354   }
355   return NULL;
356 }
357 
358 bool LocationContext::isParentOf(const LocationContext *LC) const {
359   do {
360     const LocationContext *Parent = LC->getParent();
361     if (Parent == this)
362       return true;
363     else
364       LC = Parent;
365   } while (LC);
366 
367   return false;
368 }
369 
370 //===----------------------------------------------------------------------===//
371 // Lazily generated map to query the external variables referenced by a Block.
372 //===----------------------------------------------------------------------===//
373 
374 namespace {
375 class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
376   BumpVector<const VarDecl*> &BEVals;
377   BumpVectorContext &BC;
378   llvm::SmallPtrSet<const VarDecl*, 4> Visited;
379   llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
380 public:
381   FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
382                             BumpVectorContext &bc)
383   : BEVals(bevals), BC(bc) {}
384 
385   bool IsTrackedDecl(const VarDecl *VD) {
386     const DeclContext *DC = VD->getDeclContext();
387     return IgnoredContexts.count(DC) == 0;
388   }
389 
390   void VisitStmt(Stmt *S) {
391     for (Stmt::child_range I = S->children(); I; ++I)
392       if (Stmt *child = *I)
393         Visit(child);
394   }
395 
396   void VisitDeclRefExpr(DeclRefExpr *DR) {
397     // Non-local variables are also directly modified.
398     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
399       if (!VD->hasLocalStorage()) {
400         if (Visited.insert(VD))
401           BEVals.push_back(VD, BC);
402       } else if (DR->refersToEnclosingLocal()) {
403         if (Visited.insert(VD) && IsTrackedDecl(VD))
404           BEVals.push_back(VD, BC);
405       }
406     }
407   }
408 
409   void VisitBlockExpr(BlockExpr *BR) {
410     // Blocks containing blocks can transitively capture more variables.
411     IgnoredContexts.insert(BR->getBlockDecl());
412     Visit(BR->getBlockDecl()->getBody());
413   }
414 
415   void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
416     for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
417          et = PE->semantics_end(); it != et; ++it) {
418       Expr *Semantic = *it;
419       if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
420         Semantic = OVE->getSourceExpr();
421       Visit(Semantic);
422     }
423   }
424 };
425 } // end anonymous namespace
426 
427 typedef BumpVector<const VarDecl*> DeclVec;
428 
429 static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
430                                               void *&Vec,
431                                               llvm::BumpPtrAllocator &A) {
432   if (Vec)
433     return (DeclVec*) Vec;
434 
435   BumpVectorContext BC(A);
436   DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
437   new (BV) DeclVec(BC, 10);
438 
439   // Find the referenced variables.
440   FindBlockDeclRefExprsVals F(*BV, BC);
441   F.Visit(BD->getBody());
442 
443   Vec = BV;
444   return BV;
445 }
446 
447 std::pair<AnalysisDeclContext::referenced_decls_iterator,
448           AnalysisDeclContext::referenced_decls_iterator>
449 AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
450   if (!ReferencedBlockVars)
451     ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
452 
453   DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
454   return std::make_pair(V->begin(), V->end());
455 }
456 
457 ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
458   if (!ManagedAnalyses)
459     ManagedAnalyses = new ManagedAnalysisMap();
460   ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
461   return (*M)[tag];
462 }
463 
464 //===----------------------------------------------------------------------===//
465 // Cleanup.
466 //===----------------------------------------------------------------------===//
467 
468 ManagedAnalysis::~ManagedAnalysis() {}
469 
470 AnalysisDeclContext::~AnalysisDeclContext() {
471   delete forcedBlkExprs;
472   delete ReferencedBlockVars;
473   // Release the managed analyses.
474   if (ManagedAnalyses) {
475     ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
476     for (ManagedAnalysisMap::iterator I = M->begin(), E = M->end(); I!=E; ++I)
477       delete I->second;
478     delete M;
479   }
480 }
481 
482 AnalysisDeclContextManager::~AnalysisDeclContextManager() {
483   for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
484     delete I->second;
485 }
486 
487 LocationContext::~LocationContext() {}
488 
489 LocationContextManager::~LocationContextManager() {
490   clear();
491 }
492 
493 void LocationContextManager::clear() {
494   for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
495        E = Contexts.end(); I != E; ) {
496     LocationContext *LC = &*I;
497     ++I;
498     delete LC;
499   }
500 
501   Contexts.clear();
502 }
503 
504