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