Coverage Report

Created: 2021-01-19 06:58

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