]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Analysis/AnalysisDeclContext.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Analysis / AnalysisDeclContext.cpp
1 //===- AnalysisDeclContext.cpp - Analysis context for Path Sens analysis --===//
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
11 // context data for path sensitive analysis.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/Analysis/AnalysisDeclContext.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/LambdaCapture.h"
24 #include "clang/AST/ParentMap.h"
25 #include "clang/AST/PrettyPrinter.h"
26 #include "clang/AST/Stmt.h"
27 #include "clang/AST/StmtCXX.h"
28 #include "clang/AST/StmtVisitor.h"
29 #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
30 #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
31 #include "clang/Analysis/BodyFarm.h"
32 #include "clang/Analysis/CFG.h"
33 #include "clang/Analysis/CFGStmtMap.h"
34 #include "clang/Analysis/Support/BumpVector.h"
35 #include "clang/Basic/LLVM.h"
36 #include "clang/Basic/SourceLocation.h"
37 #include "clang/Basic/SourceManager.h"
38 #include "llvm/ADT/DenseMap.h"
39 #include "llvm/ADT/FoldingSet.h"
40 #include "llvm/ADT/STLExtras.h"
41 #include "llvm/ADT/SmallPtrSet.h"
42 #include "llvm/ADT/iterator_range.h"
43 #include "llvm/Support/Allocator.h"
44 #include "llvm/Support/Casting.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Support/SaveAndRestore.h"
48 #include "llvm/Support/raw_ostream.h"
49 #include <cassert>
50 #include <memory>
51
52 using namespace clang;
53
54 using ManagedAnalysisMap = llvm::DenseMap<const void *, ManagedAnalysis *>;
55
56 AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
57                                          const Decl *d,
58                                          const CFG::BuildOptions &buildOptions)
59     : Manager(Mgr), D(d), cfgBuildOptions(buildOptions) {
60   cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
61 }
62
63 AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
64                                          const Decl *d)
65     : Manager(Mgr), D(d) {
66   cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
67 }
68
69 AnalysisDeclContextManager::AnalysisDeclContextManager(
70     ASTContext &ASTCtx, bool useUnoptimizedCFG, bool addImplicitDtors,
71     bool addInitializers, bool addTemporaryDtors, bool addLifetime,
72     bool addLoopExit, bool addScopes, bool synthesizeBodies,
73     bool addStaticInitBranch, bool addCXXNewAllocator,
74     bool addRichCXXConstructors, bool markElidedCXXConstructors,
75     CodeInjector *injector)
76     : Injector(injector), FunctionBodyFarm(ASTCtx, injector),
77       SynthesizeBodies(synthesizeBodies) {
78   cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
79   cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
80   cfgBuildOptions.AddInitializers = addInitializers;
81   cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
82   cfgBuildOptions.AddLifetime = addLifetime;
83   cfgBuildOptions.AddLoopExit = addLoopExit;
84   cfgBuildOptions.AddScopes = addScopes;
85   cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
86   cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
87   cfgBuildOptions.AddRichCXXConstructors = addRichCXXConstructors;
88   cfgBuildOptions.MarkElidedCXXConstructors = markElidedCXXConstructors;
89 }
90
91 void AnalysisDeclContextManager::clear() { Contexts.clear(); }
92
93 Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const {
94   IsAutosynthesized = false;
95   if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
96     Stmt *Body = FD->getBody();
97     if (auto *CoroBody = dyn_cast_or_null<CoroutineBodyStmt>(Body))
98       Body = CoroBody->getBody();
99     if (Manager && Manager->synthesizeBodies()) {
100       Stmt *SynthesizedBody = Manager->getBodyFarm().getBody(FD);
101       if (SynthesizedBody) {
102         Body = SynthesizedBody;
103         IsAutosynthesized = true;
104       }
105     }
106     return Body;
107   }
108   else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
109     Stmt *Body = MD->getBody();
110     if (Manager && Manager->synthesizeBodies()) {
111       Stmt *SynthesizedBody = Manager->getBodyFarm().getBody(MD);
112       if (SynthesizedBody) {
113         Body = SynthesizedBody;
114         IsAutosynthesized = true;
115       }
116     }
117     return Body;
118   } else if (const auto *BD = dyn_cast<BlockDecl>(D))
119     return BD->getBody();
120   else if (const auto *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
121     return FunTmpl->getTemplatedDecl()->getBody();
122
123   llvm_unreachable("unknown code decl");
124 }
125
126 Stmt *AnalysisDeclContext::getBody() const {
127   bool Tmp;
128   return getBody(Tmp);
129 }
130
131 bool AnalysisDeclContext::isBodyAutosynthesized() const {
132   bool Tmp;
133   getBody(Tmp);
134   return Tmp;
135 }
136
137 bool AnalysisDeclContext::isBodyAutosynthesizedFromModelFile() const {
138   bool Tmp;
139   Stmt *Body = getBody(Tmp);
140   return Tmp && Body->getLocStart().isValid();
141 }
142
143 /// Returns true if \param VD is an Objective-C implicit 'self' parameter.
144 static bool isSelfDecl(const VarDecl *VD) {
145   return isa<ImplicitParamDecl>(VD) && VD->getName() == "self";
146 }
147
148 const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
149   if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
150     return MD->getSelfDecl();
151   if (const auto *BD = dyn_cast<BlockDecl>(D)) {
152     // See if 'self' was captured by the block.
153     for (const auto &I : BD->captures()) {
154       const VarDecl *VD = I.getVariable();
155       if (isSelfDecl(VD))
156         return dyn_cast<ImplicitParamDecl>(VD);
157     }
158   }
159
160   auto *CXXMethod = dyn_cast<CXXMethodDecl>(D);
161   if (!CXXMethod)
162     return nullptr;
163
164   const CXXRecordDecl *parent = CXXMethod->getParent();
165   if (!parent->isLambda())
166     return nullptr;
167
168   for (const auto &LC : parent->captures()) {
169     if (!LC.capturesVariable())
170       continue;
171
172     VarDecl *VD = LC.getCapturedVar();
173     if (isSelfDecl(VD))
174       return dyn_cast<ImplicitParamDecl>(VD);
175   }
176
177   return nullptr;
178 }
179
180 void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
181   if (!forcedBlkExprs)
182     forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
183   // Default construct an entry for 'stmt'.
184   if (const auto *e = dyn_cast<Expr>(stmt))
185     stmt = e->IgnoreParens();
186   (void) (*forcedBlkExprs)[stmt];
187 }
188
189 const CFGBlock *
190 AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
191   assert(forcedBlkExprs);
192   if (const auto *e = dyn_cast<Expr>(stmt))
193     stmt = e->IgnoreParens();
194   CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
195     forcedBlkExprs->find(stmt);
196   assert(itr != forcedBlkExprs->end());
197   return itr->second;
198 }
199
200 /// Add each synthetic statement in the CFG to the parent map, using the
201 /// source statement's parent.
202 static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) {
203   if (!TheCFG)
204     return;
205
206   for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(),
207                                     E = TheCFG->synthetic_stmt_end();
208        I != E; ++I) {
209     PM.setParent(I->first, PM.getParent(I->second));
210   }
211 }
212
213 CFG *AnalysisDeclContext::getCFG() {
214   if (!cfgBuildOptions.PruneTriviallyFalseEdges)
215     return getUnoptimizedCFG();
216
217   if (!builtCFG) {
218     cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
219     // Even when the cfg is not successfully built, we don't
220     // want to try building it again.
221     builtCFG = true;
222
223     if (PM)
224       addParentsForSyntheticStmts(cfg.get(), *PM);
225
226     // The Observer should only observe one build of the CFG.
227     getCFGBuildOptions().Observer = nullptr;
228   }
229   return cfg.get();
230 }
231
232 CFG *AnalysisDeclContext::getUnoptimizedCFG() {
233   if (!builtCompleteCFG) {
234     SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
235                                   false);
236     completeCFG =
237         CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
238     // Even when the cfg is not successfully built, we don't
239     // want to try building it again.
240     builtCompleteCFG = true;
241
242     if (PM)
243       addParentsForSyntheticStmts(completeCFG.get(), *PM);
244
245     // The Observer should only observe one build of the CFG.
246     getCFGBuildOptions().Observer = nullptr;
247   }
248   return completeCFG.get();
249 }
250
251 CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
252   if (cfgStmtMap)
253     return cfgStmtMap.get();
254
255   if (CFG *c = getCFG()) {
256     cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
257     return cfgStmtMap.get();
258   }
259
260   return nullptr;
261 }
262
263 CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
264   if (CFA)
265     return CFA.get();
266
267   if (CFG *c = getCFG()) {
268     CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
269     return CFA.get();
270   }
271
272   return nullptr;
273 }
274
275 void AnalysisDeclContext::dumpCFG(bool ShowColors) {
276   getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
277 }
278
279 ParentMap &AnalysisDeclContext::getParentMap() {
280   if (!PM) {
281     PM.reset(new ParentMap(getBody()));
282     if (const auto *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
283       for (const auto *I : C->inits()) {
284         PM->addStmt(I->getInit());
285       }
286     }
287     if (builtCFG)
288       addParentsForSyntheticStmts(getCFG(), *PM);
289     if (builtCompleteCFG)
290       addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
291   }
292   return *PM;
293 }
294
295 PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
296   if (!PCA)
297     PCA.reset(new PseudoConstantAnalysis(getBody()));
298   return PCA.get();
299 }
300
301 AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
302   if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
303     // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
304     // that has the body.
305     FD->hasBody(FD);
306     D = FD;
307   }
308
309   std::unique_ptr<AnalysisDeclContext> &AC = Contexts[D];
310   if (!AC)
311     AC = llvm::make_unique<AnalysisDeclContext>(this, D, cfgBuildOptions);
312   return AC.get();
313 }
314
315 BodyFarm &AnalysisDeclContextManager::getBodyFarm() { return FunctionBodyFarm; }
316
317 const StackFrameContext *
318 AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
319                                const CFGBlock *Blk, unsigned Idx) {
320   return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
321 }
322
323 const BlockInvocationContext *
324 AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
325                                                const BlockDecl *BD,
326                                                const void *ContextData) {
327   return getLocationContextManager().getBlockInvocationContext(this, parent,
328                                                                BD, ContextData);
329 }
330
331 bool AnalysisDeclContext::isInStdNamespace(const Decl *D) {
332   const DeclContext *DC = D->getDeclContext()->getEnclosingNamespaceContext();
333   const auto *ND = dyn_cast<NamespaceDecl>(DC);
334   if (!ND)
335     return false;
336
337   while (const DeclContext *Parent = ND->getParent()) {
338     if (!isa<NamespaceDecl>(Parent))
339       break;
340     ND = cast<NamespaceDecl>(Parent);
341   }
342
343   return ND->isStdNamespace();
344 }
345
346 LocationContextManager &AnalysisDeclContext::getLocationContextManager() {
347   assert(Manager &&
348          "Cannot create LocationContexts without an AnalysisDeclContextManager!");
349   return Manager->getLocationContextManager();
350 }
351
352 //===----------------------------------------------------------------------===//
353 // FoldingSet profiling.
354 //===----------------------------------------------------------------------===//
355
356 void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
357                                     ContextKind ck,
358                                     AnalysisDeclContext *ctx,
359                                     const LocationContext *parent,
360                                     const void *data) {
361   ID.AddInteger(ck);
362   ID.AddPointer(ctx);
363   ID.AddPointer(parent);
364   ID.AddPointer(data);
365 }
366
367 void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
368   Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
369 }
370
371 void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
372   Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
373 }
374
375 void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
376   Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
377 }
378
379 //===----------------------------------------------------------------------===//
380 // LocationContext creation.
381 //===----------------------------------------------------------------------===//
382
383 template <typename LOC, typename DATA>
384 const LOC*
385 LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
386                                            const LocationContext *parent,
387                                            const DATA *d) {
388   llvm::FoldingSetNodeID ID;
389   LOC::Profile(ID, ctx, parent, d);
390   void *InsertPos;
391
392   LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
393
394   if (!L) {
395     L = new LOC(ctx, parent, d);
396     Contexts.InsertNode(L, InsertPos);
397   }
398   return L;
399 }
400
401 const StackFrameContext*
402 LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
403                                       const LocationContext *parent,
404                                       const Stmt *s,
405                                       const CFGBlock *blk, unsigned idx) {
406   llvm::FoldingSetNodeID ID;
407   StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
408   void *InsertPos;
409   auto *L =
410    cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
411   if (!L) {
412     L = new StackFrameContext(ctx, parent, s, blk, idx);
413     Contexts.InsertNode(L, InsertPos);
414   }
415   return L;
416 }
417
418 const ScopeContext *
419 LocationContextManager::getScope(AnalysisDeclContext *ctx,
420                                  const LocationContext *parent,
421                                  const Stmt *s) {
422   return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
423 }
424
425 const BlockInvocationContext *
426 LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
427                                                   const LocationContext *parent,
428                                                   const BlockDecl *BD,
429                                                   const void *ContextData) {
430   llvm::FoldingSetNodeID ID;
431   BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
432   void *InsertPos;
433   auto *L =
434     cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
435                                                                     InsertPos));
436   if (!L) {
437     L = new BlockInvocationContext(ctx, parent, BD, ContextData);
438     Contexts.InsertNode(L, InsertPos);
439   }
440   return L;
441 }
442
443 //===----------------------------------------------------------------------===//
444 // LocationContext methods.
445 //===----------------------------------------------------------------------===//
446
447 const StackFrameContext *LocationContext::getStackFrame() const {
448   const LocationContext *LC = this;
449   while (LC) {
450     if (const auto *SFC = dyn_cast<StackFrameContext>(LC))
451       return SFC;
452     LC = LC->getParent();
453   }
454   return nullptr;
455 }
456
457 bool LocationContext::inTopFrame() const {
458   return getStackFrame()->inTopFrame();
459 }
460
461 bool LocationContext::isParentOf(const LocationContext *LC) const {
462   do {
463     const LocationContext *Parent = LC->getParent();
464     if (Parent == this)
465       return true;
466     else
467       LC = Parent;
468   } while (LC);
469
470   return false;
471 }
472
473 static void printLocation(raw_ostream &OS, const SourceManager &SM,
474                           SourceLocation SLoc) {
475   if (SLoc.isFileID() && SM.isInMainFile(SLoc))
476     OS << "line " << SM.getExpansionLineNumber(SLoc);
477   else
478     SLoc.print(OS, SM);
479 }
480
481 void LocationContext::dumpStack(
482     raw_ostream &OS, StringRef Indent, const char *NL, const char *Sep,
483     std::function<void(const LocationContext *)> printMoreInfoPerContext) const {
484   ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
485   PrintingPolicy PP(Ctx.getLangOpts());
486   PP.TerseOutput = 1;
487
488   const SourceManager &SM =
489       getAnalysisDeclContext()->getASTContext().getSourceManager();
490
491   unsigned Frame = 0;
492   for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
493     switch (LCtx->getKind()) {
494     case StackFrame:
495       OS << Indent << '#' << Frame << ' ';
496       ++Frame;
497       if (const auto *D = dyn_cast<NamedDecl>(LCtx->getDecl()))
498         OS << "Calling " << D->getQualifiedNameAsString();
499       else
500         OS << "Calling anonymous code";
501       if (const Stmt *S = cast<StackFrameContext>(LCtx)->getCallSite()) {
502         OS << " at ";
503         printLocation(OS, SM, S->getLocStart());
504       }
505       break;
506     case Scope:
507       OS << "Entering scope";
508       break;
509     case Block:
510       OS << "Invoking block";
511       if (const Decl *D = cast<BlockInvocationContext>(LCtx)->getDecl()) {
512         OS << " defined at ";
513         printLocation(OS, SM, D->getLocStart());
514       }
515       break;
516     }
517     OS << NL;
518
519     printMoreInfoPerContext(LCtx);
520   }
521 }
522
523 LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
524   dumpStack(llvm::errs());
525 }
526
527 //===----------------------------------------------------------------------===//
528 // Lazily generated map to query the external variables referenced by a Block.
529 //===----------------------------------------------------------------------===//
530
531 namespace {
532
533 class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
534   BumpVector<const VarDecl *> &BEVals;
535   BumpVectorContext &BC;
536   llvm::SmallPtrSet<const VarDecl *, 4> Visited;
537   llvm::SmallPtrSet<const DeclContext *, 4> IgnoredContexts;
538
539 public:
540   FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
541                             BumpVectorContext &bc)
542       : BEVals(bevals), BC(bc) {}
543
544   void VisitStmt(Stmt *S) {
545     for (auto *Child : S->children())
546       if (Child)
547         Visit(Child);
548   }
549
550   void VisitDeclRefExpr(DeclRefExpr *DR) {
551     // Non-local variables are also directly modified.
552     if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
553       if (!VD->hasLocalStorage()) {
554         if (Visited.insert(VD).second)
555           BEVals.push_back(VD, BC);
556       }
557     }
558   }
559
560   void VisitBlockExpr(BlockExpr *BR) {
561     // Blocks containing blocks can transitively capture more variables.
562     IgnoredContexts.insert(BR->getBlockDecl());
563     Visit(BR->getBlockDecl()->getBody());
564   }
565
566   void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
567     for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
568          et = PE->semantics_end(); it != et; ++it) {
569       Expr *Semantic = *it;
570       if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
571         Semantic = OVE->getSourceExpr();
572       Visit(Semantic);
573     }
574   }
575 };
576
577 } // namespace
578
579 using DeclVec = BumpVector<const VarDecl *>;
580
581 static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
582                                               void *&Vec,
583                                               llvm::BumpPtrAllocator &A) {
584   if (Vec)
585     return (DeclVec*) Vec;
586
587   BumpVectorContext BC(A);
588   DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
589   new (BV) DeclVec(BC, 10);
590
591   // Go through the capture list.
592   for (const auto &CI : BD->captures()) {
593     BV->push_back(CI.getVariable(), BC);
594   }
595
596   // Find the referenced global/static variables.
597   FindBlockDeclRefExprsVals F(*BV, BC);
598   F.Visit(BD->getBody());
599
600   Vec = BV;
601   return BV;
602 }
603
604 llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
605 AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
606   if (!ReferencedBlockVars)
607     ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
608
609   const DeclVec *V =
610       LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
611   return llvm::make_range(V->begin(), V->end());
612 }
613
614 ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
615   if (!ManagedAnalyses)
616     ManagedAnalyses = new ManagedAnalysisMap();
617   ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
618   return (*M)[tag];
619 }
620
621 //===----------------------------------------------------------------------===//
622 // Cleanup.
623 //===----------------------------------------------------------------------===//
624
625 ManagedAnalysis::~ManagedAnalysis() = default;
626
627 AnalysisDeclContext::~AnalysisDeclContext() {
628   delete forcedBlkExprs;
629   delete ReferencedBlockVars;
630   // Release the managed analyses.
631   if (ManagedAnalyses) {
632     ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
633     llvm::DeleteContainerSeconds(*M);
634     delete M;
635   }
636 }
637
638 LocationContext::~LocationContext() = default;
639
640 LocationContextManager::~LocationContextManager() {
641   clear();
642 }
643
644 void LocationContextManager::clear() {
645   for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
646        E = Contexts.end(); I != E; ) {
647     LocationContext *LC = &*I;
648     ++I;
649     delete LC;
650   }
651   Contexts.clear();
652 }