Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/DeclBase.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DeclBase.cpp - Declaration AST Node Implementation -----------------===//
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 implements the Decl and DeclContext classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/DeclBase.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/ASTLambda.h"
16
#include "clang/AST/ASTMutationListener.h"
17
#include "clang/AST/Attr.h"
18
#include "clang/AST/AttrIterator.h"
19
#include "clang/AST/Decl.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclContextInternals.h"
22
#include "clang/AST/DeclFriend.h"
23
#include "clang/AST/DeclObjC.h"
24
#include "clang/AST/DeclOpenMP.h"
25
#include "clang/AST/DeclTemplate.h"
26
#include "clang/AST/DependentDiagnostic.h"
27
#include "clang/AST/ExternalASTSource.h"
28
#include "clang/AST/Stmt.h"
29
#include "clang/AST/Type.h"
30
#include "clang/Basic/IdentifierTable.h"
31
#include "clang/Basic/LLVM.h"
32
#include "clang/Basic/LangOptions.h"
33
#include "clang/Basic/ObjCRuntime.h"
34
#include "clang/Basic/PartialDiagnostic.h"
35
#include "clang/Basic/SourceLocation.h"
36
#include "clang/Basic/TargetInfo.h"
37
#include "llvm/ADT/ArrayRef.h"
38
#include "llvm/ADT/PointerIntPair.h"
39
#include "llvm/ADT/SmallVector.h"
40
#include "llvm/ADT/StringRef.h"
41
#include "llvm/Support/Casting.h"
42
#include "llvm/Support/ErrorHandling.h"
43
#include "llvm/Support/MathExtras.h"
44
#include "llvm/Support/VersionTuple.h"
45
#include "llvm/Support/raw_ostream.h"
46
#include <algorithm>
47
#include <cassert>
48
#include <cstddef>
49
#include <string>
50
#include <tuple>
51
#include <utility>
52
53
using namespace clang;
54
55
//===----------------------------------------------------------------------===//
56
//  Statistics
57
//===----------------------------------------------------------------------===//
58
59
#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
60
#define ABSTRACT_DECL(DECL)
61
#include "clang/AST/DeclNodes.inc"
62
63
0
void Decl::updateOutOfDate(IdentifierInfo &II) const {
64
0
  getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
65
0
}
66
67
#define DECL(DERIVED, BASE)                                                    \
68
  static_assert(alignof(Decl) >= alignof(DERIVED##Decl),                       \
69
                "Alignment sufficient after objects prepended to " #DERIVED);
70
#define ABSTRACT_DECL(DECL)
71
#include "clang/AST/DeclNodes.inc"
72
73
void *Decl::operator new(std::size_t Size, const ASTContext &Context,
74
7.54M
                         unsigned ID, std::size_t Extra) {
75
  // Allocate an extra 8 bytes worth of storage, which ensures that the
76
  // resulting pointer will still be 8-byte aligned.
77
7.54M
  static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
78
7.54M
                "Decl won't be misaligned");
79
7.54M
  void *Start = Context.Allocate(Size + Extra + 8);
80
7.54M
  void *Result = (char*)Start + 8;
81
82
7.54M
  unsigned *PrefixPtr = (unsigned *)Result - 2;
83
84
  // Zero out the first 4 bytes; this is used to store the owning module ID.
85
7.54M
  PrefixPtr[0] = 0;
86
87
  // Store the global declaration ID in the second 4 bytes.
88
7.54M
  PrefixPtr[1] = ID;
89
90
7.54M
  return Result;
91
7.54M
}
92
93
void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
94
145M
                         DeclContext *Parent, std::size_t Extra) {
95
145M
  assert(!Parent || &Parent->getParentASTContext() == &Ctx);
96
  // With local visibility enabled, we track the owning module even for local
97
  // declarations. We create the TU decl early and may not yet know what the
98
  // LangOpts are, so conservatively allocate the storage.
99
145M
  if (Ctx.getLangOpts().trackLocalOwningModule() || 
!Parent141M
) {
100
    // Ensure required alignment of the resulting object by adding extra
101
    // padding at the start if required.
102
4.89M
    size_t ExtraAlign =
103
4.89M
        llvm::offsetToAlignment(sizeof(Module *), llvm::Align(alignof(Decl)));
104
4.89M
    auto *Buffer = reinterpret_cast<char *>(
105
4.89M
        ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
106
4.89M
    Buffer += ExtraAlign;
107
4.89M
    auto *ParentModule =
108
4.89M
        Parent ? 
cast<Decl>(Parent)->getOwningModule()4.62M
:
nullptr268k
;
109
4.89M
    return new (Buffer) Module*(ParentModule) + 1;
110
4.89M
  }
111
141M
  return ::operator new(Size + Extra, Ctx);
112
145M
}
113
114
1.75M
Module *Decl::getOwningModuleSlow() const {
115
1.75M
  assert(isFromASTFile() && "Not from AST file?");
116
0
  return getASTContext().getExternalSource()->getModule(getOwningModuleID());
117
1.75M
}
118
119
7.19M
bool Decl::hasLocalOwningModuleStorage() const {
120
7.19M
  return getASTContext().getLangOpts().trackLocalOwningModule();
121
7.19M
}
122
123
37.7k
const char *Decl::getDeclKindName() const {
124
37.7k
  switch (DeclKind) {
125
0
  default: llvm_unreachable("Declaration not in DeclNodes.inc!");
126
37.7k
#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
127
0
#define ABSTRACT_DECL(DECL)
128
37.7k
#include 
"clang/AST/DeclNodes.inc"0
129
37.7k
  }
130
37.7k
}
131
132
255k
void Decl::setInvalidDecl(bool Invalid) {
133
255k
  InvalidDecl = Invalid;
134
255k
  assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
135
255k
  if (!Invalid) {
136
212k
    return;
137
212k
  }
138
139
43.0k
  if (!isa<ParmVarDecl>(this)) {
140
    // Defensive maneuver for ill-formed code: we're likely not to make it to
141
    // a point where we set the access specifier, so default it to "public"
142
    // to avoid triggering asserts elsewhere in the front end.
143
39.9k
    setAccess(AS_public);
144
39.9k
  }
145
146
  // Marking a DecompositionDecl as invalid implies all the child BindingDecl's
147
  // are invalid too.
148
43.0k
  if (auto *DD = dyn_cast<DecompositionDecl>(this)) {
149
110
    for (auto *Binding : DD->bindings()) {
150
110
      Binding->setInvalidDecl();
151
110
    }
152
78
  }
153
43.0k
}
154
155
120
const char *DeclContext::getDeclKindName() const {
156
120
  switch (getDeclKind()) {
157
120
#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
158
0
#define ABSTRACT_DECL(DECL)
159
120
#include 
"clang/AST/DeclNodes.inc"0
160
120
  }
161
0
  llvm_unreachable("Declaration context not in DeclNodes.inc!");
162
0
}
163
164
bool Decl::StatisticsEnabled = false;
165
4
void Decl::EnableStatistics() {
166
4
  StatisticsEnabled = true;
167
4
}
168
169
4
void Decl::PrintStats() {
170
4
  llvm::errs() << "\n*** Decl Stats:\n";
171
172
4
  int totalDecls = 0;
173
332
#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
174
4
#define ABSTRACT_DECL(DECL)
175
4
#include "clang/AST/DeclNodes.inc"
176
4
  llvm::errs() << "  " << totalDecls << " decls total.\n";
177
178
4
  int totalBytes = 0;
179
4
#define DECL(DERIVED, BASE)                                             \
180
332
  if (n##DERIVED##s > 0) {                                              \
181
26
    totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl));         \
182
26
    llvm::errs() << "    " << n##DERIVED##s << " " #DERIVED " decls, "  \
183
26
                 << sizeof(DERIVED##Decl) << " each ("                  \
184
26
                 << n##DERIVED##s * sizeof(DERIVED##Decl)               \
185
26
                 << " bytes)\n";                                        \
186
26
  }
187
4
#define ABSTRACT_DECL(DECL)
188
4
#include "clang/AST/DeclNodes.inc"
189
190
4
  llvm::errs() << "Total bytes = " << totalBytes << "\n";
191
4
}
192
193
78
void Decl::add(Kind k) {
194
78
  switch (k) {
195
78
#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
196
0
#define ABSTRACT_DECL(DECL)
197
78
#include 
"clang/AST/DeclNodes.inc"0
198
78
  }
199
78
}
200
201
42.9M
bool Decl::isTemplateParameterPack() const {
202
42.9M
  if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(this))
203
26.6M
    return TTP->isParameterPack();
204
16.3M
  if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(this))
205
5.74M
    return NTTP->isParameterPack();
206
10.5M
  if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(this))
207
97.6k
    return TTP->isParameterPack();
208
10.4M
  return false;
209
10.5M
}
210
211
25.8M
bool Decl::isParameterPack() const {
212
25.8M
  if (const auto *Var = dyn_cast<VarDecl>(this))
213
17.8M
    return Var->isParameterPack();
214
215
7.95M
  return isTemplateParameterPack();
216
25.8M
}
217
218
78.2M
FunctionDecl *Decl::getAsFunction() {
219
78.2M
  if (auto *FD = dyn_cast<FunctionDecl>(this))
220
73.1M
    return FD;
221
5.08M
  if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
222
3.68M
    return FTD->getTemplatedDecl();
223
1.40M
  return nullptr;
224
5.08M
}
225
226
4.23M
bool Decl::isTemplateDecl() const {
227
4.23M
  return isa<TemplateDecl>(this);
228
4.23M
}
229
230
12.0M
TemplateDecl *Decl::getDescribedTemplate() const {
231
12.0M
  if (auto *FD = dyn_cast<FunctionDecl>(this))
232
2.02M
    return FD->getDescribedFunctionTemplate();
233
10.0M
  if (auto *RD = dyn_cast<CXXRecordDecl>(this))
234
677k
    return RD->getDescribedClassTemplate();
235
9.38M
  if (auto *VD = dyn_cast<VarDecl>(this))
236
1.66M
    return VD->getDescribedVarTemplate();
237
7.72M
  if (auto *AD = dyn_cast<TypeAliasDecl>(this))
238
2.43k
    return AD->getDescribedAliasTemplate();
239
240
7.71M
  return nullptr;
241
7.72M
}
242
243
3.97M
const TemplateParameterList *Decl::getDescribedTemplateParams() const {
244
3.97M
  if (auto *TD = getDescribedTemplate())
245
319k
    return TD->getTemplateParameters();
246
3.65M
  if (auto *CTPSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(this))
247
76.5k
    return CTPSD->getTemplateParameters();
248
3.57M
  if (auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl>(this))
249
13
    return VTPSD->getTemplateParameters();
250
3.57M
  return nullptr;
251
3.57M
}
252
253
29.1M
bool Decl::isTemplated() const {
254
  // A declaration is templated if it is a template or a template pattern, or
255
  // is within (lexcially for a friend, semantically otherwise) a dependent
256
  // context.
257
  // FIXME: Should local extern declarations be treated like friends?
258
29.1M
  if (auto *AsDC = dyn_cast<DeclContext>(this))
259
24.9M
    return AsDC->isDependentContext();
260
4.22M
  auto *DC = getFriendObjectKind() ? 
getLexicalDeclContext()0
: getDeclContext();
261
4.22M
  return DC->isDependentContext() || 
isTemplateDecl()4.15M
||
262
4.22M
         
getDescribedTemplateParams()3.55M
;
263
29.1M
}
264
265
24.0k
unsigned Decl::getTemplateDepth() const {
266
24.0k
  if (auto *DC = dyn_cast<DeclContext>(this))
267
24.0k
    if (DC->isFileContext())
268
107
      return 0;
269
270
23.9k
  if (auto *TPL = getDescribedTemplateParams())
271
23.8k
    return TPL->getDepth() + 1;
272
273
  // If this is a dependent lambda, there might be an enclosing variable
274
  // template. In this case, the next step is not the parent DeclContext (or
275
  // even a DeclContext at all).
276
114
  auto *RD = dyn_cast<CXXRecordDecl>(this);
277
114
  if (RD && 
RD->isDependentLambda()111
)
278
0
    if (Decl *Context = RD->getLambdaContextDecl())
279
0
      return Context->getTemplateDepth();
280
281
114
  const DeclContext *DC =
282
114
      getFriendObjectKind() ? 
getLexicalDeclContext()0
: getDeclContext();
283
114
  return cast<Decl>(DC)->getTemplateDepth();
284
114
}
285
286
1.78M
const DeclContext *Decl::getParentFunctionOrMethod() const {
287
1.78M
  for (const DeclContext *DC = getDeclContext();
288
1.82M
       DC && 
!DC->isTranslationUnit()1.81M
&&
!DC->isNamespace()1.71M
;
289
1.78M
       
DC = DC->getParent()44.1k
)
290
78.7k
    if (DC->isFunctionOrMethod())
291
34.6k
      return DC;
292
293
1.74M
  return nullptr;
294
1.78M
}
295
296
//===----------------------------------------------------------------------===//
297
// PrettyStackTraceDecl Implementation
298
//===----------------------------------------------------------------------===//
299
300
0
void PrettyStackTraceDecl::print(raw_ostream &OS) const {
301
0
  SourceLocation TheLoc = Loc;
302
0
  if (TheLoc.isInvalid() && TheDecl)
303
0
    TheLoc = TheDecl->getLocation();
304
305
0
  if (TheLoc.isValid()) {
306
0
    TheLoc.print(OS, SM);
307
0
    OS << ": ";
308
0
  }
309
310
0
  OS << Message;
311
312
0
  if (const auto *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
313
0
    OS << " '";
314
0
    DN->printQualifiedName(OS);
315
0
    OS << '\'';
316
0
  }
317
0
  OS << '\n';
318
0
}
319
320
//===----------------------------------------------------------------------===//
321
// Decl Implementation
322
//===----------------------------------------------------------------------===//
323
324
// Out-of-line virtual method providing a home for Decl.
325
9.88k
Decl::~Decl() = default;
326
327
96.5M
void Decl::setDeclContext(DeclContext *DC) {
328
96.5M
  DeclCtx = DC;
329
96.5M
}
330
331
34.8M
void Decl::setLexicalDeclContext(DeclContext *DC) {
332
34.8M
  if (DC == getLexicalDeclContext())
333
34.4M
    return;
334
335
469k
  if (isInSemaDC()) {
336
455k
    setDeclContextsImpl(getDeclContext(), DC, getASTContext());
337
455k
  } else {
338
14.1k
    getMultipleDC()->LexicalDC = DC;
339
14.1k
  }
340
341
  // FIXME: We shouldn't be changing the lexical context of declarations
342
  // imported from AST files.
343
469k
  if (!isFromASTFile()) {
344
469k
    setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC));
345
469k
    if (hasOwningModule())
346
36.8k
      setLocalOwningModule(cast<Decl>(DC)->getOwningModule());
347
469k
  }
348
349
469k
  assert(
350
469k
      (getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported ||
351
469k
       getOwningModule()) &&
352
469k
      "hidden declaration has no owning module");
353
469k
}
354
355
void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
356
7.79M
                               ASTContext &Ctx) {
357
7.79M
  if (SemaDC == LexicalDC) {
358
7.08M
    DeclCtx = SemaDC;
359
7.08M
  } else {
360
710k
    auto *MDC = new (Ctx) Decl::MultipleDC();
361
710k
    MDC->SemanticDC = SemaDC;
362
710k
    MDC->LexicalDC = LexicalDC;
363
710k
    DeclCtx = MDC;
364
710k
  }
365
7.79M
}
366
367
128k
bool Decl::isInLocalScopeForInstantiation() const {
368
128k
  const DeclContext *LDC = getLexicalDeclContext();
369
128k
  if (!LDC->isDependentContext())
370
7.51k
    return false;
371
243k
  
while (121k
true) {
372
243k
    if (LDC->isFunctionOrMethod())
373
25
      return true;
374
243k
    if (!isa<TagDecl>(LDC))
375
121k
      return false;
376
122k
    if (const auto *CRD = dyn_cast<CXXRecordDecl>(LDC))
377
122k
      if (CRD->isLambda())
378
26
        return true;
379
122k
    LDC = LDC->getLexicalParent();
380
122k
  }
381
0
  return false;
382
121k
}
383
384
7.86M
bool Decl::isInAnonymousNamespace() const {
385
21.3M
  for (const DeclContext *DC = getDeclContext(); DC; 
DC = DC->getParent()13.5M
) {
386
13.5M
    if (const auto *ND = dyn_cast<NamespaceDecl>(DC))
387
4.10M
      if (ND->isAnonymousNamespace())
388
4.83k
        return true;
389
13.5M
  }
390
391
7.86M
  return false;
392
7.86M
}
393
394
2.81M
bool Decl::isInStdNamespace() const {
395
2.81M
  const DeclContext *DC = getDeclContext();
396
2.81M
  return DC && DC->isStdNamespace();
397
2.81M
}
398
399
3.06G
TranslationUnitDecl *Decl::getTranslationUnitDecl() {
400
3.06G
  if (auto *TUD = dyn_cast<TranslationUnitDecl>(this))
401
261M
    return TUD;
402
403
2.80G
  DeclContext *DC = getDeclContext();
404
2.80G
  assert(DC && "This decl is not contained in a translation unit!");
405
406
4.88G
  while (!DC->isTranslationUnit()) {
407
2.07G
    DC = DC->getParent();
408
2.07G
    assert(DC && "This decl is not contained in a translation unit!");
409
2.07G
  }
410
411
2.80G
  return cast<TranslationUnitDecl>(DC);
412
3.06G
}
413
414
3.06G
ASTContext &Decl::getASTContext() const {
415
3.06G
  return getTranslationUnitDecl()->getASTContext();
416
3.06G
}
417
418
/// Helper to get the language options from the ASTContext.
419
/// Defined out of line to avoid depending on ASTContext.h.
420
1.35M
const LangOptions &Decl::getLangOpts() const {
421
1.35M
  return getASTContext().getLangOpts();
422
1.35M
}
423
424
47.2M
ASTMutationListener *Decl::getASTMutationListener() const {
425
47.2M
  return getASTContext().getASTMutationListener();
426
47.2M
}
427
428
3.98M
unsigned Decl::getMaxAlignment() const {
429
3.98M
  if (!hasAttrs())
430
3.68M
    return 0;
431
432
309k
  unsigned Align = 0;
433
309k
  const AttrVec &V = getAttrs();
434
309k
  ASTContext &Ctx = getASTContext();
435
309k
  specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
436
323k
  for (; I != E; 
++I14.3k
) {
437
14.3k
    if (!I->isAlignmentErrorDependent())
438
14.3k
      Align = std::max(Align, I->getAlignment(Ctx));
439
14.3k
  }
440
309k
  return Align;
441
3.98M
}
442
443
86.3M
bool Decl::isUsed(bool CheckUsedAttr) const {
444
86.3M
  const Decl *CanonD = getCanonicalDecl();
445
86.3M
  if (CanonD->Used)
446
9.36M
    return true;
447
448
  // Check for used attribute.
449
  // Ask the most recent decl, since attributes accumulate in the redecl chain.
450
76.9M
  if (CheckUsedAttr && 
getMostRecentDecl()->hasAttr<UsedAttr>()50.2M
)
451
862
    return true;
452
453
  // The information may have not been deserialized yet. Force deserialization
454
  // to complete the needed information.
455
76.9M
  return getMostRecentDecl()->getCanonicalDecl()->Used;
456
76.9M
}
457
458
11.8M
void Decl::markUsed(ASTContext &C) {
459
11.8M
  if (isUsed(false))
460
3.54M
    return;
461
462
8.32M
  if (C.getASTMutationListener())
463
301k
    C.getASTMutationListener()->DeclarationMarkedUsed(this);
464
465
8.32M
  setIsUsed();
466
8.32M
}
467
468
122M
bool Decl::isReferenced() const {
469
122M
  if (Referenced)
470
24.0M
    return true;
471
472
  // Check redeclarations.
473
98.0M
  for (const auto *I : redecls())
474
118M
    if (I->Referenced)
475
6.39k
      return true;
476
477
98.0M
  return false;
478
98.0M
}
479
480
30.8k
ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
481
30.8k
  const Decl *Definition = nullptr;
482
30.8k
  if (auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
483
1.49k
    Definition = ID->getDefinition();
484
29.3k
  } else if (auto *PD = dyn_cast<ObjCProtocolDecl>(this)) {
485
181
    Definition = PD->getDefinition();
486
29.1k
  } else if (auto *TD = dyn_cast<TagDecl>(this)) {
487
14.5k
    Definition = TD->getDefinition();
488
14.5k
  }
489
30.8k
  if (!Definition)
490
14.8k
    Definition = this;
491
492
30.8k
  if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
493
71
    return attr;
494
30.7k
  if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
495
30.7k
    return dcd->getAttr<ExternalSourceSymbolAttr>();
496
30.7k
  }
497
498
0
  return nullptr;
499
30.7k
}
500
501
27.5M
bool Decl::hasDefiningAttr() const {
502
27.5M
  return hasAttr<AliasAttr>() || 
hasAttr<IFuncAttr>()27.5M
||
503
27.5M
         
hasAttr<LoaderUninitializedAttr>()27.5M
;
504
27.5M
}
505
506
149
const Attr *Decl::getDefiningAttr() const {
507
149
  if (auto *AA = getAttr<AliasAttr>())
508
134
    return AA;
509
15
  if (auto *IFA = getAttr<IFuncAttr>())
510
15
    return IFA;
511
0
  if (auto *NZA = getAttr<LoaderUninitializedAttr>())
512
0
    return NZA;
513
0
  return nullptr;
514
0
}
515
516
static StringRef getRealizedPlatform(const AvailabilityAttr *A,
517
4.23M
                                     const ASTContext &Context) {
518
  // Check if this is an App Extension "platform", and if so chop off
519
  // the suffix for matching with the actual platform.
520
4.23M
  StringRef RealizedPlatform = A->getPlatform()->getName();
521
4.23M
  if (!Context.getLangOpts().AppExt)
522
4.23M
    return RealizedPlatform;
523
75
  size_t suffix = RealizedPlatform.rfind("_app_extension");
524
75
  if (suffix != StringRef::npos)
525
55
    return RealizedPlatform.slice(0, suffix);
526
20
  return RealizedPlatform;
527
75
}
528
529
/// Determine the availability of the given declaration based on
530
/// the target platform.
531
///
532
/// When it returns an availability result other than \c AR_Available,
533
/// if the \p Message parameter is non-NULL, it will be set to a
534
/// string describing why the entity is unavailable.
535
///
536
/// FIXME: Make these strings localizable, since they end up in
537
/// diagnostics.
538
static AvailabilityResult CheckAvailability(ASTContext &Context,
539
                                            const AvailabilityAttr *A,
540
                                            std::string *Message,
541
4.23M
                                            VersionTuple EnclosingVersion) {
542
4.23M
  if (EnclosingVersion.empty())
543
4.23M
    EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
544
545
4.23M
  if (EnclosingVersion.empty())
546
35
    return AR_Available;
547
548
4.23M
  StringRef ActualPlatform = A->getPlatform()->getName();
549
4.23M
  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
550
551
  // Match the platform name.
552
4.23M
  if (getRealizedPlatform(A, Context) != TargetPlatform)
553
2.76M
    return AR_Available;
554
555
1.46M
  StringRef PrettyPlatformName
556
1.46M
    = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
557
558
1.46M
  if (PrettyPlatformName.empty())
559
0
    PrettyPlatformName = ActualPlatform;
560
561
1.46M
  std::string HintMessage;
562
1.46M
  if (!A->getMessage().empty()) {
563
288k
    HintMessage = " - ";
564
288k
    HintMessage += A->getMessage();
565
288k
  }
566
567
  // Make sure that this declaration has not been marked 'unavailable'.
568
1.46M
  if (A->getUnavailable()) {
569
123k
    if (Message) {
570
368
      Message->clear();
571
368
      llvm::raw_string_ostream Out(*Message);
572
368
      Out << "not available on " << PrettyPlatformName
573
368
          << HintMessage;
574
368
    }
575
576
123k
    return AR_Unavailable;
577
123k
  }
578
579
  // Make sure that this declaration has already been introduced.
580
1.34M
  if (!A->getIntroduced().empty() &&
581
1.34M
      
EnclosingVersion < A->getIntroduced()1.33M
) {
582
1.22k
    if (Message) {
583
425
      Message->clear();
584
425
      llvm::raw_string_ostream Out(*Message);
585
425
      VersionTuple VTI(A->getIntroduced());
586
425
      Out << "introduced in " << PrettyPlatformName << ' '
587
425
          << VTI << HintMessage;
588
425
    }
589
590
1.22k
    return A->getStrict() ? 
AR_Unavailable8
:
AR_NotYetIntroduced1.22k
;
591
1.22k
  }
592
593
  // Make sure that this declaration hasn't been obsoleted.
594
1.34M
  if (!A->getObsoleted().empty() && 
EnclosingVersion >= A->getObsoleted()49
) {
595
47
    if (Message) {
596
28
      Message->clear();
597
28
      llvm::raw_string_ostream Out(*Message);
598
28
      VersionTuple VTO(A->getObsoleted());
599
28
      Out << "obsoleted in " << PrettyPlatformName << ' '
600
28
          << VTO << HintMessage;
601
28
    }
602
603
47
    return AR_Unavailable;
604
47
  }
605
606
  // Make sure that this declaration hasn't been deprecated.
607
1.34M
  if (!A->getDeprecated().empty() && 
EnclosingVersion >= A->getDeprecated()763k
) {
608
751k
    if (Message) {
609
259k
      Message->clear();
610
259k
      llvm::raw_string_ostream Out(*Message);
611
259k
      VersionTuple VTD(A->getDeprecated());
612
259k
      Out << "first deprecated in " << PrettyPlatformName << ' '
613
259k
          << VTD << HintMessage;
614
259k
    }
615
616
751k
    return AR_Deprecated;
617
751k
  }
618
619
588k
  return AR_Available;
620
1.34M
}
621
622
AvailabilityResult Decl::getAvailability(std::string *Message,
623
                                         VersionTuple EnclosingVersion,
624
139M
                                         StringRef *RealizedPlatform) const {
625
139M
  if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
626
52.2k
    return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion,
627
52.2k
                                                    RealizedPlatform);
628
629
139M
  AvailabilityResult Result = AR_Available;
630
139M
  std::string ResultMessage;
631
632
139M
  for (const auto *A : attrs()) {
633
20.0M
    if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
634
3.26k
      if (Result >= AR_Deprecated)
635
55
        continue;
636
637
3.20k
      if (Message)
638
2.14k
        ResultMessage = std::string(Deprecated->getMessage());
639
640
3.20k
      Result = AR_Deprecated;
641
3.20k
      continue;
642
3.26k
    }
643
644
20.0M
    if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
645
60.8k
      if (Message)
646
435
        *Message = std::string(Unavailable->getMessage());
647
60.8k
      return AR_Unavailable;
648
60.8k
    }
649
650
20.0M
    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
651
4.20M
      AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
652
4.20M
                                                Message, EnclosingVersion);
653
654
4.20M
      if (AR == AR_Unavailable) {
655
123k
        if (RealizedPlatform)
656
5
          *RealizedPlatform = Availability->getPlatform()->getName();
657
123k
        return AR_Unavailable;
658
123k
      }
659
660
4.07M
      if (AR > Result) {
661
752k
        Result = AR;
662
752k
        if (Message)
663
259k
          ResultMessage.swap(*Message);
664
752k
      }
665
4.07M
      continue;
666
4.20M
    }
667
20.0M
  }
668
669
139M
  if (Message)
670
137M
    Message->swap(ResultMessage);
671
139M
  return Result;
672
139M
}
673
674
5
VersionTuple Decl::getVersionIntroduced() const {
675
5
  const ASTContext &Context = getASTContext();
676
5
  StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
677
5
  for (const auto *A : attrs()) {
678
5
    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
679
5
      if (getRealizedPlatform(Availability, Context) != TargetPlatform)
680
0
        continue;
681
5
      if (!Availability->getIntroduced().empty())
682
5
        return Availability->getIntroduced();
683
5
    }
684
5
  }
685
0
  return {};
686
5
}
687
688
18.3M
bool Decl::canBeWeakImported(bool &IsDefinition) const {
689
18.3M
  IsDefinition = false;
690
691
  // Variables, if they aren't definitions.
692
18.3M
  if (const auto *Var = dyn_cast<VarDecl>(this)) {
693
17.9M
    if (Var->isThisDeclarationADefinition()) {
694
13.4M
      IsDefinition = true;
695
13.4M
      return false;
696
13.4M
    }
697
4.45M
    return true;
698
17.9M
  }
699
  // Functions, if they aren't definitions.
700
452k
  if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
701
421k
    if (FD->hasBody()) {
702
290k
      IsDefinition = true;
703
290k
      return false;
704
290k
    }
705
130k
    return true;
706
707
421k
  }
708
  // Objective-C classes, if this is the non-fragile runtime.
709
30.6k
  if (isa<ObjCInterfaceDecl>(this) &&
710
30.6k
             
getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()22.9k
) {
711
22.7k
    return true;
712
22.7k
  }
713
  // Nothing else.
714
7.87k
  return false;
715
30.6k
}
716
717
18.3M
bool Decl::isWeakImported() const {
718
18.3M
  bool IsDefinition;
719
18.3M
  if (!canBeWeakImported(IsDefinition))
720
13.7M
    return false;
721
722
4.60M
  for (const auto *A : getMostRecentDecl()->attrs()) {
723
102k
    if (isa<WeakImportAttr>(A))
724
165
      return true;
725
726
102k
    if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
727
31.5k
      if (CheckAvailability(getASTContext(), Availability, nullptr,
728
31.5k
                            VersionTuple()) == AR_NotYetIntroduced)
729
43
        return true;
730
31.5k
    }
731
102k
  }
732
733
4.60M
  return false;
734
4.60M
}
735
736
153M
unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
737
153M
  switch (DeclKind) {
738
28.6M
    case Function:
739
28.6M
    case CXXDeductionGuide:
740
31.1M
    case CXXMethod:
741
32.1M
    case CXXConstructor:
742
32.1M
    case ConstructorUsingShadow:
743
32.3M
    case CXXDestructor:
744
32.4M
    case CXXConversion:
745
36.2M
    case EnumConstant:
746
40.3M
    case Var:
747
44.9M
    case ImplicitParam:
748
126M
    case ParmVar:
749
127M
    case ObjCMethod:
750
128M
    case ObjCProperty:
751
128M
    case MSProperty:
752
128M
      return IDNS_Ordinary;
753
3.61k
    case Label:
754
3.61k
      return IDNS_Label;
755
9.41k
    case IndirectField:
756
9.41k
      return IDNS_Ordinary | IDNS_Member;
757
758
780
    case Binding:
759
536k
    case NonTypeTemplateParm:
760
539k
    case VarTemplate:
761
540k
    case Concept:
762
      // These (C++-only) declarations are found by redeclaration lookup for
763
      // tag types, so we include them in the tag namespace.
764
540k
      return IDNS_Ordinary | IDNS_Tag;
765
766
246
    case ObjCCompatibleAlias:
767
282k
    case ObjCInterface:
768
282k
      return IDNS_Ordinary | IDNS_Type;
769
770
3.77M
    case Typedef:
771
3.99M
    case TypeAlias:
772
7.95M
    case TemplateTypeParm:
773
8.02M
    case ObjCTypeParam:
774
8.02M
      return IDNS_Ordinary | IDNS_Type;
775
776
1.10k
    case UnresolvedUsingTypename:
777
1.10k
      return IDNS_Ordinary | IDNS_Type | IDNS_Using;
778
779
350k
    case UsingShadow:
780
350k
      return 0; // we'll actually overwrite this later
781
782
693
    case UnresolvedUsingValue:
783
693
      return IDNS_Ordinary | IDNS_Using;
784
785
225k
    case Using:
786
225k
    case UsingPack:
787
225k
    case UsingEnum:
788
225k
      return IDNS_Using;
789
790
25.8k
    case ObjCProtocol:
791
25.8k
      return IDNS_ObjCProtocol;
792
793
4.07M
    case Field:
794
4.07M
    case ObjCAtDefsField:
795
4.21M
    case ObjCIvar:
796
4.21M
      return IDNS_Member;
797
798
540k
    case Record:
799
3.78M
    case CXXRecord:
800
4.33M
    case Enum:
801
4.33M
      return IDNS_Tag | IDNS_Type;
802
803
1.23M
    case Namespace:
804
1.23M
    case NamespaceAlias:
805
1.23M
      return IDNS_Namespace;
806
807
1.17M
    case FunctionTemplate:
808
1.17M
      return IDNS_Ordinary;
809
810
559k
    case ClassTemplate:
811
599k
    case TemplateTemplateParm:
812
689k
    case TypeAliasTemplate:
813
689k
      return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
814
815
1.44k
    case UnresolvedUsingIfExists:
816
1.44k
      return IDNS_Type | IDNS_Ordinary;
817
818
906
    case OMPDeclareReduction:
819
906
      return IDNS_OMPReduction;
820
821
418
    case OMPDeclareMapper:
822
418
      return IDNS_OMPMapper;
823
824
    // Never have names.
825
101k
    case Friend:
826
101k
    case FriendTemplate:
827
511k
    case AccessSpec:
828
885k
    case LinkageSpec:
829
885k
    case Export:
830
885k
    case FileScopeAsm:
831
1.00M
    case StaticAssert:
832
1.00M
    case ObjCPropertyImpl:
833
1.00M
    case PragmaComment:
834
1.00M
    case PragmaDetectMismatch:
835
1.00M
    case Block:
836
1.60M
    case Captured:
837
1.70M
    case TranslationUnit:
838
1.76M
    case ExternCContext:
839
1.76M
    case Decomposition:
840
1.76M
    case MSGuid:
841
1.76M
    case TemplateParamObject:
842
843
1.77M
    case UsingDirective:
844
1.77M
    case BuiltinTemplate:
845
3.73M
    case ClassTemplateSpecialization:
846
3.92M
    case ClassTemplatePartialSpecialization:
847
3.92M
    case ClassScopeFunctionSpecialization:
848
3.92M
    case VarTemplateSpecialization:
849
3.92M
    case VarTemplatePartialSpecialization:
850
3.93M
    case ObjCImplementation:
851
3.99M
    case ObjCCategory:
852
3.99M
    case ObjCCategoryImpl:
853
4.09M
    case Import:
854
4.09M
    case OMPThreadPrivate:
855
4.09M
    case OMPAllocate:
856
4.09M
    case OMPRequires:
857
4.16M
    case OMPCapturedExpr:
858
4.17M
    case Empty:
859
4.17M
    case LifetimeExtendedTemporary:
860
4.17M
    case RequiresExprBody:
861
      // Never looked up by name.
862
4.17M
      return 0;
863
153M
  }
864
865
0
  llvm_unreachable("Invalid DeclKind!");
866
0
}
867
868
34.2M
void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
869
34.2M
  assert(!HasAttrs && "Decl already contains attrs.");
870
871
0
  AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
872
34.2M
  assert(AttrBlank.empty() && "HasAttrs was wrong?");
873
874
0
  AttrBlank = attrs;
875
34.2M
  HasAttrs = true;
876
34.2M
}
877
878
4.48k
void Decl::dropAttrs() {
879
4.48k
  if (!HasAttrs) 
return0
;
880
881
4.48k
  HasAttrs = false;
882
4.48k
  getASTContext().eraseDeclAttrs(this);
883
4.48k
}
884
885
119M
void Decl::addAttr(Attr *A) {
886
119M
  if (!hasAttrs()) {
887
33.2M
    setAttrs(AttrVec(1, A));
888
33.2M
    return;
889
33.2M
  }
890
891
85.9M
  AttrVec &Attrs = getAttrs();
892
85.9M
  if (!A->isInherited()) {
893
85.6M
    Attrs.push_back(A);
894
85.6M
    return;
895
85.6M
  }
896
897
  // Attribute inheritance is processed after attribute parsing. To keep the
898
  // order as in the source code, add inherited attributes before non-inherited
899
  // ones.
900
298k
  auto I = Attrs.begin(), E = Attrs.end();
901
458k
  for (; I != E; 
++I160k
) {
902
183k
    if (!(*I)->isInherited())
903
23.1k
      break;
904
183k
  }
905
298k
  Attrs.insert(I, A);
906
298k
}
907
908
1.98G
const AttrVec &Decl::getAttrs() const {
909
1.98G
  assert(HasAttrs && "No attrs to get!");
910
0
  return getASTContext().getDeclAttrs(this);
911
1.98G
}
912
913
3.72G
Decl *Decl::castFromDeclContext (const DeclContext *D) {
914
3.72G
  Decl::Kind DK = D->getDeclKind();
915
3.72G
  switch(DK) {
916
0
#define DECL(NAME, BASE)
917
0
#define DECL_CONTEXT(NAME) \
918
2.80G
    case Decl::NAME:       \
919
2.80G
      return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
920
0
#define DECL_CONTEXT_BASE(NAME)
921
0
#include "clang/AST/DeclNodes.inc"
922
919M
    default:
923
919M
#define DECL(NAME, BASE)
924
919M
#define DECL_CONTEXT_BASE(NAME)                  \
925
1.55G
      if (DK >= first##NAME && 
DK <= last919M
##NAME) \
926
1.55G
        
return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D))919M
;
927
919M
#include "clang/AST/DeclNodes.inc"
928
18.4E
      llvm_unreachable("a decl that inherits DeclContext isn't handled");
929
3.72G
  }
930
3.72G
}
931
932
56.2M
DeclContext *Decl::castToDeclContext(const Decl *D) {
933
56.2M
  Decl::Kind DK = D->getKind();
934
56.2M
  switch(DK) {
935
0
#define DECL(NAME, BASE)
936
0
#define DECL_CONTEXT(NAME) \
937
6.42M
    case Decl::NAME:       \
938
6.42M
      return static_cast<NAME##Decl *>(const_cast<Decl *>(D));
939
0
#define DECL_CONTEXT_BASE(NAME)
940
0
#include "clang/AST/DeclNodes.inc"
941
49.8M
    default:
942
49.8M
#define DECL(NAME, BASE)
943
49.8M
#define DECL_CONTEXT_BASE(NAME)                                   \
944
75.1M
      if (DK >= first##NAME && 
DK <= last49.8M
##NAME) \
945
75.1M
        
return static_cast<NAME##Decl *>(const_cast<Decl *>(D))49.8M
;
946
49.8M
#include "clang/AST/DeclNodes.inc"
947
56.2M
      
llvm_unreachable1
("a decl that inherits DeclContext isn't handled");
948
56.2M
  }
949
56.2M
}
950
951
26.5k
SourceLocation Decl::getBodyRBrace() const {
952
  // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
953
  // FunctionDecl stores EndRangeLoc for this purpose.
954
26.5k
  if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
955
183
    const FunctionDecl *Definition;
956
183
    if (FD->hasBody(Definition))
957
183
      return Definition->getSourceRange().getEnd();
958
0
    return {};
959
183
  }
960
961
26.4k
  if (Stmt *Body = getBody())
962
26.4k
    return Body->getSourceRange().getEnd();
963
964
0
  return {};
965
26.4k
}
966
967
339M
bool Decl::AccessDeclContextCheck() const {
968
339M
#ifndef NDEBUG
969
  // Suppress this check if any of the following hold:
970
  // 1. this is the translation unit (and thus has no parent)
971
  // 2. this is a template parameter (and thus doesn't belong to its context)
972
  // 3. this is a non-type template parameter
973
  // 4. the context is not a record
974
  // 5. it's invalid
975
  // 6. it's a C++0x static_assert.
976
  // 7. it's a block literal declaration
977
  // 8. it's a temporary with lifetime extended due to being default value.
978
339M
  if (
isa<TranslationUnitDecl>(this)339M
|| isa<TemplateTypeParmDecl>(this) ||
979
339M
      
isa<NonTypeTemplateParmDecl>(this)331M
||
!getDeclContext()330M
||
980
339M
      
!isa<CXXRecordDecl>(getDeclContext())330M
||
isInvalidDecl()42.8M
||
981
339M
      
isa<StaticAssertDecl>(this)42.8M
||
isa<BlockDecl>(this)42.8M
||
982
      // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
983
      // as DeclContext (?).
984
339M
      
isa<ParmVarDecl>(this)42.8M
||
985
      // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
986
      // AS_none as access specifier.
987
339M
      
isa<CXXRecordDecl>(this)42.8M
||
988
339M
      
isa<ClassScopeFunctionSpecializationDecl>(this)38.9M
||
989
339M
      
isa<LifetimeExtendedTemporaryDecl>(this)38.9M
)
990
300M
    return true;
991
992
38.9M
  assert(Access != AS_none &&
993
38.9M
         "Access specifier is AS_none inside a record decl");
994
0
#endif
995
0
  return true;
996
339M
}
997
998
303k
static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
999
87.8k
static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
1000
1001
0
int64_t Decl::getID() const {
1002
0
  return getASTContext().getAllocator().identifyKnownAlignedObject<Decl>(this);
1003
0
}
1004
1005
23.4M
const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
1006
23.4M
  QualType Ty;
1007
23.4M
  if (const auto *D = dyn_cast<ValueDecl>(this))
1008
23.4M
    Ty = D->getType();
1009
35.0k
  else if (const auto *D = dyn_cast<TypedefNameDecl>(this))
1010
1.87k
    Ty = D->getUnderlyingType();
1011
33.1k
  else
1012
33.1k
    return nullptr;
1013
1014
23.4M
  if (Ty->isFunctionPointerType())
1015
362
    Ty = Ty->castAs<PointerType>()->getPointeeType();
1016
23.4M
  else if (Ty->isFunctionReferenceType())
1017
4
    Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1018
23.4M
  else if (BlocksToo && 
Ty->isBlockPointerType()1.08M
)
1019
1.91k
    Ty = Ty->castAs<BlockPointerType>()->getPointeeType();
1020
1021
23.4M
  return Ty->getAs<FunctionType>();
1022
23.4M
}
1023
1024
/// Starting at a given context (a Decl or DeclContext), look for a
1025
/// code context that is not a closure (a lambda, block, etc.).
1026
391k
template <class T> static Decl *getNonClosureContext(T *D) {
1027
391k
  if (getKind(D) == Decl::CXXMethod) {
1028
58.3k
    auto *MD = cast<CXXMethodDecl>(D);
1029
58.3k
    if (MD->getOverloadedOperator() == OO_Call &&
1030
58.3k
        
MD->getParent()->isLambda()5.17k
)
1031
3.12k
      return getNonClosureContext(MD->getParent()->getParent());
1032
55.2k
    return MD;
1033
58.3k
  }
1034
333k
  if (auto *FD = dyn_cast<FunctionDecl>(D))
1035
253k
    return FD;
1036
79.3k
  if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
1037
6.50k
    return MD;
1038
72.8k
  if (auto *BD = dyn_cast<BlockDecl>(D))
1039
1.55k
    return getNonClosureContext(BD->getParent());
1040
71.3k
  if (auto *CD = dyn_cast<CapturedDecl>(D))
1041
64.7k
    return getNonClosureContext(CD->getParent());
1042
6.54k
  return nullptr;
1043
71.3k
}
DeclBase.cpp:clang::Decl* getNonClosureContext<clang::Decl>(clang::Decl*)
Line
Count
Source
1026
303k
template <class T> static Decl *getNonClosureContext(T *D) {
1027
303k
  if (getKind(D) == Decl::CXXMethod) {
1028
53.5k
    auto *MD = cast<CXXMethodDecl>(D);
1029
53.5k
    if (MD->getOverloadedOperator() == OO_Call &&
1030
53.5k
        
MD->getParent()->isLambda()3.69k
)
1031
1.64k
      return getNonClosureContext(MD->getParent()->getParent());
1032
51.9k
    return MD;
1033
53.5k
  }
1034
249k
  if (auto *FD = dyn_cast<FunctionDecl>(D))
1035
214k
    return FD;
1036
35.3k
  if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
1037
2.58k
    return MD;
1038
32.7k
  if (auto *BD = dyn_cast<BlockDecl>(D))
1039
1.16k
    return getNonClosureContext(BD->getParent());
1040
31.5k
  if (auto *CD = dyn_cast<CapturedDecl>(D))
1041
25.2k
    return getNonClosureContext(CD->getParent());
1042
6.37k
  return nullptr;
1043
31.5k
}
DeclBase.cpp:clang::Decl* getNonClosureContext<clang::DeclContext>(clang::DeclContext*)
Line
Count
Source
1026
87.8k
template <class T> static Decl *getNonClosureContext(T *D) {
1027
87.8k
  if (getKind(D) == Decl::CXXMethod) {
1028
4.75k
    auto *MD = cast<CXXMethodDecl>(D);
1029
4.75k
    if (MD->getOverloadedOperator() == OO_Call &&
1030
4.75k
        
MD->getParent()->isLambda()1.48k
)
1031
1.47k
      return getNonClosureContext(MD->getParent()->getParent());
1032
3.28k
    return MD;
1033
4.75k
  }
1034
83.1k
  if (auto *FD = dyn_cast<FunctionDecl>(D))
1035
39.0k
    return FD;
1036
44.0k
  if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
1037
3.92k
    return MD;
1038
40.1k
  if (auto *BD = dyn_cast<BlockDecl>(D))
1039
392
    return getNonClosureContext(BD->getParent());
1040
39.7k
  if (auto *CD = dyn_cast<CapturedDecl>(D))
1041
39.5k
    return getNonClosureContext(CD->getParent());
1042
164
  return nullptr;
1043
39.7k
}
1044
1045
303k
Decl *Decl::getNonClosureContext() {
1046
303k
  return ::getNonClosureContext(this);
1047
303k
}
1048
1049
18.4k
Decl *DeclContext::getNonClosureAncestor() {
1050
18.4k
  return ::getNonClosureContext(this);
1051
18.4k
}
1052
1053
//===----------------------------------------------------------------------===//
1054
// DeclContext Implementation
1055
//===----------------------------------------------------------------------===//
1056
1057
42.8M
DeclContext::DeclContext(Decl::Kind K) {
1058
42.8M
  DeclContextBits.DeclKind = K;
1059
42.8M
  setHasExternalLexicalStorage(false);
1060
42.8M
  setHasExternalVisibleStorage(false);
1061
42.8M
  setNeedToReconcileExternalVisibleStorage(false);
1062
42.8M
  setHasLazyLocalLexicalLookups(false);
1063
42.8M
  setHasLazyExternalLexicalLookups(false);
1064
42.8M
  setUseQualifiedLookup(false);
1065
42.8M
}
1066
1067
111M
bool DeclContext::classof(const Decl *D) {
1068
111M
  switch (D->getKind()) {
1069
0
#define DECL(NAME, BASE)
1070
62.8M
#define DECL_CONTEXT(NAME) case Decl::NAME:
1071
0
#define DECL_CONTEXT_BASE(NAME)
1072
11.7M
#include 
"clang/AST/DeclNodes.inc"0
1073
11.7M
      return true;
1074
99.7M
    default:
1075
99.7M
#define DECL(NAME, BASE)
1076
99.7M
#define DECL_CONTEXT_BASE(NAME)                 \
1077
164M
      if (D->getKind() >= Decl::first##NAME &&  \
1078
164M
          
D->getKind() <= Decl::last119M
##NAME) \
1079
164M
        
return true84.6M
;
1080
99.7M
#include "clang/AST/DeclNodes.inc"
1081
15.0M
      return false;
1082
111M
  }
1083
111M
}
1084
1085
0
DeclContext::~DeclContext() = default;
1086
1087
/// Find the parent context of this context that will be
1088
/// used for unqualified name lookup.
1089
///
1090
/// Generally, the parent lookup context is the semantic context. However, for
1091
/// a friend function the parent lookup context is the lexical context, which
1092
/// is the class in which the friend is declared.
1093
68.6M
DeclContext *DeclContext::getLookupParent() {
1094
  // FIXME: Find a better way to identify friends.
1095
68.6M
  if (isa<FunctionDecl>(this))
1096
8.95M
    if (getParent()->getRedeclContext()->isFileContext() &&
1097
8.95M
        
getLexicalParent()->getRedeclContext()->isRecord()5.11M
)
1098
19.1k
      return getLexicalParent();
1099
1100
  // A lookup within the call operator of a lambda never looks in the lambda
1101
  // class; instead, skip to the context in which that closure type is
1102
  // declared.
1103
68.6M
  if (isLambdaCallOperator(this))
1104
20.9k
    return getParent()->getParent();
1105
1106
68.6M
  return getParent();
1107
68.6M
}
1108
1109
263
const BlockDecl *DeclContext::getInnermostBlockDecl() const {
1110
263
  const DeclContext *Ctx = this;
1111
1112
743
  do {
1113
743
    if (Ctx->isClosure())
1114
25
      return cast<BlockDecl>(Ctx);
1115
718
    Ctx = Ctx->getParent();
1116
718
  } while (Ctx);
1117
1118
238
  return nullptr;
1119
263
}
1120
1121
57.0M
bool DeclContext::isInlineNamespace() const {
1122
57.0M
  return isNamespace() &&
1123
57.0M
         
cast<NamespaceDecl>(this)->isInline()6.96M
;
1124
57.0M
}
1125
1126
3.48M
bool DeclContext::isStdNamespace() const {
1127
3.48M
  if (!isNamespace())
1128
2.10M
    return false;
1129
1130
1.38M
  const auto *ND = cast<NamespaceDecl>(this);
1131
1.38M
  if (ND->isInline()) {
1132
615k
    return ND->getParent()->isStdNamespace();
1133
615k
  }
1134
1135
765k
  if (!getParent()->getRedeclContext()->isTranslationUnit())
1136
40.3k
    return false;
1137
1138
725k
  const IdentifierInfo *II = ND->getIdentifier();
1139
725k
  return II && 
II->isStr("std")724k
;
1140
765k
}
1141
1142
384M
bool DeclContext::isDependentContext() const {
1143
384M
  if (isFileContext())
1144
200M
    return false;
1145
1146
184M
  if (isa<ClassTemplatePartialSpecializationDecl>(this))
1147
6.02M
    return true;
1148
1149
178M
  if (const auto *Record = dyn_cast<CXXRecordDecl>(this)) {
1150
62.2M
    if (Record->getDescribedClassTemplate())
1151
20.0M
      return true;
1152
1153
42.1M
    if (Record->isDependentLambda())
1154
23.2k
      return true;
1155
42.1M
  }
1156
1157
158M
  if (const auto *Function = dyn_cast<FunctionDecl>(this)) {
1158
61.6M
    if (Function->getDescribedFunctionTemplate())
1159
5.65M
      return true;
1160
1161
    // Friend function declarations are dependent if their *lexical*
1162
    // context is dependent.
1163
56.0M
    if (cast<Decl>(this)->getFriendObjectKind())
1164
82.3k
      return getLexicalParent()->isDependentContext();
1165
56.0M
  }
1166
1167
  // FIXME: A variable template is a dependent context, but is not a
1168
  // DeclContext. A context within it (such as a lambda-expression)
1169
  // should be considered dependent.
1170
1171
152M
  return getParent() && getParent()->isDependentContext();
1172
158M
}
1173
1174
1.56G
bool DeclContext::isTransparentContext() const {
1175
1.56G
  if (getDeclKind() == Decl::Enum)
1176
8.45M
    return !cast<EnumDecl>(this)->isScoped();
1177
1178
1.55G
  return getDeclKind() == Decl::LinkageSpec || 
getDeclKind() == Decl::Export1.24G
;
1179
1.56G
}
1180
1181
static bool isLinkageSpecContext(const DeclContext *DC,
1182
35.5M
                                 LinkageSpecDecl::LanguageIDs ID) {
1183
62.2M
  while (DC->getDeclKind() != Decl::TranslationUnit) {
1184
48.7M
    if (DC->getDeclKind() == Decl::LinkageSpec)
1185
21.9M
      return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
1186
26.7M
    DC = DC->getLexicalParent();
1187
26.7M
  }
1188
13.5M
  return false;
1189
35.5M
}
1190
1191
35.4M
bool DeclContext::isExternCContext() const {
1192
35.4M
  return isLinkageSpecContext(this, LinkageSpecDecl::lang_c);
1193
35.4M
}
1194
1195
34
const LinkageSpecDecl *DeclContext::getExternCContext() const {
1196
34
  const DeclContext *DC = this;
1197
38
  while (DC->getDeclKind() != Decl::TranslationUnit) {
1198
38
    if (DC->getDeclKind() == Decl::LinkageSpec &&
1199
38
        
cast<LinkageSpecDecl>(DC)->getLanguage() == LinkageSpecDecl::lang_c34
)
1200
34
      return cast<LinkageSpecDecl>(DC);
1201
4
    DC = DC->getLexicalParent();
1202
4
  }
1203
0
  return nullptr;
1204
34
}
1205
1206
21.4k
bool DeclContext::isExternCXXContext() const {
1207
21.4k
  return isLinkageSpecContext(this, LinkageSpecDecl::lang_cxx);
1208
21.4k
}
1209
1210
28.2M
bool DeclContext::Encloses(const DeclContext *DC) const {
1211
28.2M
  if (getPrimaryContext() != this)
1212
9.47M
    return getPrimaryContext()->Encloses(DC);
1213
1214
46.1M
  
for (; 18.7M
DC;
DC = DC->getParent()27.4M
)
1215
37.0M
    if (!isa<LinkageSpecDecl>(DC) && 
!isa<ExportDecl>(DC)37.0M
&&
1216
37.0M
        
DC->getPrimaryContext() == this37.0M
)
1217
9.58M
      return true;
1218
9.14M
  return false;
1219
18.7M
}
1220
1221
4.45M
DeclContext *DeclContext::getNonTransparentContext() {
1222
4.45M
  DeclContext *DC = this;
1223
4.73M
  while (DC->isTransparentContext()) {
1224
272k
    DC = DC->getParent();
1225
272k
    assert(DC && "All transparent contexts should have a parent!");
1226
272k
  }
1227
4.45M
  return DC;
1228
4.45M
}
1229
1230
1.47G
DeclContext *DeclContext::getPrimaryContext() {
1231
1.47G
  switch (getDeclKind()) {
1232
30.5M
  case Decl::ExternCContext:
1233
116M
  case Decl::LinkageSpec:
1234
116M
  case Decl::Export:
1235
117M
  case Decl::Block:
1236
126M
  case Decl::Captured:
1237
126M
  case Decl::OMPDeclareReduction:
1238
126M
  case Decl::OMPDeclareMapper:
1239
126M
  case Decl::RequiresExprBody:
1240
    // There is only one DeclContext for these entities.
1241
126M
    return this;
1242
1243
745M
  case Decl::TranslationUnit:
1244
745M
    return static_cast<TranslationUnitDecl *>(this)->getFirstDecl();
1245
305M
  case Decl::Namespace:
1246
    // The original namespace is our primary context.
1247
305M
    return static_cast<NamespaceDecl *>(this)->getOriginalNamespace();
1248
1249
370k
  case Decl::ObjCMethod:
1250
370k
    return this;
1251
1252
7.12M
  case Decl::ObjCInterface:
1253
7.12M
    if (auto *OID = dyn_cast<ObjCInterfaceDecl>(this))
1254
7.12M
      if (auto *Def = OID->getDefinition())
1255
7.11M
        return Def;
1256
1.00k
    return this;
1257
1258
2.65M
  case Decl::ObjCProtocol:
1259
2.65M
    if (auto *OPD = dyn_cast<ObjCProtocolDecl>(this))
1260
2.65M
      if (auto *Def = OPD->getDefinition())
1261
2.65M
        return Def;
1262
138
    return this;
1263
1264
9.24M
  case Decl::ObjCCategory:
1265
9.24M
    return this;
1266
1267
98.3k
  case Decl::ObjCImplementation:
1268
105k
  case Decl::ObjCCategoryImpl:
1269
105k
    return this;
1270
1271
281M
  default:
1272
281M
    if (getDeclKind() >= Decl::firstTag && getDeclKind() <= Decl::lastTag) {
1273
      // If this is a tag type that has a definition or is currently
1274
      // being defined, that definition is our primary context.
1275
240M
      auto *Tag = cast<TagDecl>(this);
1276
1277
240M
      if (TagDecl *Def = Tag->getDefinition())
1278
226M
        return Def;
1279
1280
13.3M
      if (const auto *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
1281
        // Note, TagType::getDecl returns the (partial) definition one exists.
1282
13.1M
        TagDecl *PossiblePartialDef = TagTy->getDecl();
1283
13.1M
        if (PossiblePartialDef->isBeingDefined())
1284
12.7M
          return PossiblePartialDef;
1285
13.1M
      } else {
1286
149k
        assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
1287
149k
      }
1288
1289
547k
      return Tag;
1290
13.3M
    }
1291
1292
41.1M
    assert(getDeclKind() >= Decl::firstFunction &&
1293
41.1M
           getDeclKind() <= Decl::lastFunction &&
1294
41.1M
          "Unknown DeclContext kind");
1295
0
    return this;
1296
1.47G
  }
1297
1.47G
}
1298
1299
template <typename T>
1300
81.1k
void collectAllContextsImpl(T *Self, SmallVectorImpl<DeclContext *> &Contexts) {
1301
164k
  for (T *D = Self->getMostRecentDecl(); D; 
D = D->getPreviousDecl()83.2k
)
1302
83.2k
    Contexts.push_back(D);
1303
1304
81.1k
  std::reverse(Contexts.begin(), Contexts.end());
1305
81.1k
}
void collectAllContextsImpl<clang::TranslationUnitDecl>(clang::TranslationUnitDecl*, llvm::SmallVectorImpl<clang::DeclContext*>&)
Line
Count
Source
1300
50.4k
void collectAllContextsImpl(T *Self, SmallVectorImpl<DeclContext *> &Contexts) {
1301
100k
  for (T *D = Self->getMostRecentDecl(); D; 
D = D->getPreviousDecl()50.4k
)
1302
50.4k
    Contexts.push_back(D);
1303
1304
50.4k
  std::reverse(Contexts.begin(), Contexts.end());
1305
50.4k
}
void collectAllContextsImpl<clang::NamespaceDecl>(clang::NamespaceDecl*, llvm::SmallVectorImpl<clang::DeclContext*>&)
Line
Count
Source
1300
30.6k
void collectAllContextsImpl(T *Self, SmallVectorImpl<DeclContext *> &Contexts) {
1301
63.5k
  for (T *D = Self->getMostRecentDecl(); D; 
D = D->getPreviousDecl()32.8k
)
1302
32.8k
    Contexts.push_back(D);
1303
1304
30.6k
  std::reverse(Contexts.begin(), Contexts.end());
1305
30.6k
}
1306
1307
1.85M
void DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts) {
1308
1.85M
  Contexts.clear();
1309
1310
1.85M
  Decl::Kind Kind = getDeclKind();
1311
1312
1.85M
  if (Kind == Decl::TranslationUnit)
1313
50.4k
    collectAllContextsImpl(static_cast<TranslationUnitDecl *>(this), Contexts);
1314
1.80M
  else if (Kind == Decl::Namespace)
1315
30.6k
    collectAllContextsImpl(static_cast<NamespaceDecl *>(this), Contexts);
1316
1.77M
  else
1317
1.77M
    Contexts.push_back(this);
1318
1.85M
}
1319
1320
std::pair<Decl *, Decl *>
1321
DeclContext::BuildDeclChain(ArrayRef<Decl *> Decls,
1322
98.0k
                            bool FieldsAlreadyLoaded) {
1323
  // Build up a chain of declarations via the Decl::NextInContextAndBits field.
1324
98.0k
  Decl *FirstNewDecl = nullptr;
1325
98.0k
  Decl *PrevDecl = nullptr;
1326
748k
  for (auto *D : Decls) {
1327
748k
    if (FieldsAlreadyLoaded && 
isa<FieldDecl>(D)70.0k
)
1328
0
      continue;
1329
1330
748k
    if (PrevDecl)
1331
650k
      PrevDecl->NextInContextAndBits.setPointer(D);
1332
98.0k
    else
1333
98.0k
      FirstNewDecl = D;
1334
1335
748k
    PrevDecl = D;
1336
748k
  }
1337
1338
98.0k
  return std::make_pair(FirstNewDecl, PrevDecl);
1339
98.0k
}
1340
1341
/// We have just acquired external visible storage, and we already have
1342
/// built a lookup map. For every name in the map, pull in the new names from
1343
/// the external storage.
1344
27.5k
void DeclContext::reconcileExternalVisibleStorage() const {
1345
27.5k
  assert(hasNeedToReconcileExternalVisibleStorage() && LookupPtr);
1346
0
  setNeedToReconcileExternalVisibleStorage(false);
1347
1348
27.5k
  for (auto &Lookup : *LookupPtr)
1349
1.10M
    Lookup.second.setHasExternalDecls();
1350
27.5k
}
1351
1352
/// Load the declarations within this lexical storage from an
1353
/// external source.
1354
/// \return \c true if any declarations were added.
1355
bool
1356
158k
DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1357
158k
  ExternalASTSource *Source = getParentASTContext().getExternalSource();
1358
158k
  assert(hasExternalLexicalStorage() && Source && "No external storage?");
1359
1360
  // Notify that we have a DeclContext that is initializing.
1361
0
  ExternalASTSource::Deserializing ADeclContext(Source);
1362
1363
  // Load the external declarations, if any.
1364
158k
  SmallVector<Decl*, 64> Decls;
1365
158k
  setHasExternalLexicalStorage(false);
1366
158k
  Source->FindExternalLexicalDecls(this, Decls);
1367
1368
158k
  if (Decls.empty())
1369
70.7k
    return false;
1370
1371
  // We may have already loaded just the fields of this record, in which case
1372
  // we need to ignore them.
1373
87.9k
  bool FieldsAlreadyLoaded = false;
1374
87.9k
  if (const auto *RD = dyn_cast<RecordDecl>(this))
1375
83.8k
    FieldsAlreadyLoaded = RD->hasLoadedFieldsFromExternalStorage();
1376
1377
  // Splice the newly-read declarations into the beginning of the list
1378
  // of declarations.
1379
87.9k
  Decl *ExternalFirst, *ExternalLast;
1380
87.9k
  std::tie(ExternalFirst, ExternalLast) =
1381
87.9k
      BuildDeclChain(Decls, FieldsAlreadyLoaded);
1382
87.9k
  ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
1383
87.9k
  FirstDecl = ExternalFirst;
1384
87.9k
  if (!LastDecl)
1385
86.0k
    LastDecl = ExternalLast;
1386
87.9k
  return true;
1387
158k
}
1388
1389
DeclContext::lookup_result
1390
ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1391
398k
                                                    DeclarationName Name) {
1392
398k
  ASTContext &Context = DC->getParentASTContext();
1393
398k
  StoredDeclsMap *Map;
1394
398k
  if (!(Map = DC->LookupPtr))
1395
0
    Map = DC->CreateStoredDeclsMap(Context);
1396
398k
  if (DC->hasNeedToReconcileExternalVisibleStorage())
1397
0
    DC->reconcileExternalVisibleStorage();
1398
1399
398k
  (*Map)[Name].removeExternalDecls();
1400
1401
398k
  return DeclContext::lookup_result();
1402
398k
}
1403
1404
DeclContext::lookup_result
1405
ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
1406
                                                  DeclarationName Name,
1407
1.36M
                                                  ArrayRef<NamedDecl*> Decls) {
1408
1.36M
  ASTContext &Context = DC->getParentASTContext();
1409
1.36M
  StoredDeclsMap *Map;
1410
1.36M
  if (!(Map = DC->LookupPtr))
1411
19
    Map = DC->CreateStoredDeclsMap(Context);
1412
1.36M
  if (DC->hasNeedToReconcileExternalVisibleStorage())
1413
14.6k
    DC->reconcileExternalVisibleStorage();
1414
1415
1.36M
  StoredDeclsList &List = (*Map)[Name];
1416
1.36M
  List.replaceExternalDecls(Decls);
1417
1.36M
  return List.getLookupResult();
1418
1.36M
}
1419
1420
14.9M
DeclContext::decl_iterator DeclContext::decls_begin() const {
1421
14.9M
  if (hasExternalLexicalStorage())
1422
91.6k
    LoadLexicalDeclsFromExternalStorage();
1423
14.9M
  return decl_iterator(FirstDecl);
1424
14.9M
}
1425
1426
789k
bool DeclContext::decls_empty() const {
1427
789k
  if (hasExternalLexicalStorage())
1428
19
    LoadLexicalDeclsFromExternalStorage();
1429
1430
789k
  return !FirstDecl;
1431
789k
}
1432
1433
53.4k
bool DeclContext::containsDecl(Decl *D) const {
1434
53.4k
  return (D->getLexicalDeclContext() == this &&
1435
53.4k
          
(52.8k
D->NextInContextAndBits.getPointer()52.8k
||
D == LastDecl7.76k
));
1436
53.4k
}
1437
1438
7.42k
bool DeclContext::containsDeclAndLoad(Decl *D) const {
1439
7.42k
  if (hasExternalLexicalStorage())
1440
9
    LoadLexicalDeclsFromExternalStorage();
1441
7.42k
  return containsDecl(D);
1442
7.42k
}
1443
1444
/// shouldBeHidden - Determine whether a declaration which was declared
1445
/// within its semantic context should be invisible to qualified name lookup.
1446
59.1M
static bool shouldBeHidden(NamedDecl *D) {
1447
  // Skip unnamed declarations.
1448
59.1M
  if (!D->getDeclName())
1449
1.06M
    return true;
1450
1451
  // Skip entities that can't be found by name lookup into a particular
1452
  // context.
1453
58.1M
  if ((D->getIdentifierNamespace() == 0 && 
!isa<UsingDirectiveDecl>(D)333k
) ||
1454
58.1M
      
D->isTemplateParameter()57.7M
)
1455
329k
    return true;
1456
1457
  // Skip friends and local extern declarations unless they're the first
1458
  // declaration of the entity.
1459
57.7M
  if ((D->isLocalExternDecl() || 
D->getFriendObjectKind()57.7M
) &&
1460
57.7M
      
D != D->getCanonicalDecl()59.6k
)
1461
19.5k
    return true;
1462
1463
  // Skip template specializations.
1464
  // FIXME: This feels like a hack. Should DeclarationName support
1465
  // template-ids, or is there a better way to keep specializations
1466
  // from being visible?
1467
57.7M
  if (isa<ClassTemplateSpecializationDecl>(D))
1468
0
    return true;
1469
57.7M
  if (auto *FD = dyn_cast<FunctionDecl>(D))
1470
31.1M
    if (FD->isFunctionTemplateSpecialization())
1471
9.03k
      return true;
1472
1473
  // Hide destructors that are invalid. There should always be one destructor,
1474
  // but if it is an invalid decl, another one is created. We need to hide the
1475
  // invalid one from places that expect exactly one destructor, like the
1476
  // serialization code.
1477
57.7M
  if (isa<CXXDestructorDecl>(D) && 
D->isInvalidDecl()161k
)
1478
35
    return true;
1479
1480
57.7M
  return false;
1481
57.7M
}
1482
1483
44.1k
void DeclContext::removeDecl(Decl *D) {
1484
44.1k
  assert(D->getLexicalDeclContext() == this &&
1485
44.1k
         "decl being removed from non-lexical context");
1486
0
  assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
1487
44.1k
         "decl is not in decls list");
1488
1489
  // Remove D from the decl chain.  This is O(n) but hopefully rare.
1490
44.1k
  if (D == FirstDecl) {
1491
7.77k
    if (D == LastDecl)
1492
727
      FirstDecl = LastDecl = nullptr;
1493
7.04k
    else
1494
7.04k
      FirstDecl = D->NextInContextAndBits.getPointer();
1495
36.3k
  } else {
1496
2.10M
    for (Decl *I = FirstDecl; true; 
I = I->NextInContextAndBits.getPointer()2.06M
) {
1497
2.10M
      assert(I && "decl not found in linked list");
1498
2.10M
      if (I->NextInContextAndBits.getPointer() == D) {
1499
36.3k
        I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
1500
36.3k
        if (D == LastDecl) 
LastDecl = I1.86k
;
1501
36.3k
        break;
1502
36.3k
      }
1503
2.10M
    }
1504
36.3k
  }
1505
1506
  // Mark that D is no longer in the decl chain.
1507
44.1k
  D->NextInContextAndBits.setPointer(nullptr);
1508
1509
  // Remove D from the lookup table if necessary.
1510
44.1k
  if (isa<NamedDecl>(D)) {
1511
29.6k
    auto *ND = cast<NamedDecl>(D);
1512
1513
    // Do not try to remove the declaration if that is invisible to qualified
1514
    // lookup.  E.g. template specializations are skipped.
1515
29.6k
    if (shouldBeHidden(ND))
1516
471
      return;
1517
1518
    // Remove only decls that have a name
1519
29.1k
    if (!ND->getDeclName())
1520
0
      return;
1521
1522
29.1k
    auto *DC = D->getDeclContext();
1523
29.1k
    do {
1524
29.1k
      StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
1525
29.1k
      if (Map) {
1526
26.6k
        StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1527
26.6k
        assert(Pos != Map->end() && "no lookup entry for decl");
1528
0
        Pos->second.remove(ND);
1529
26.6k
      }
1530
29.1k
    } while (DC->isTransparentContext() && 
(DC = DC->getParent())0
);
1531
29.1k
  }
1532
44.1k
}
1533
1534
62.7M
void DeclContext::addHiddenDecl(Decl *D) {
1535
62.7M
  assert(D->getLexicalDeclContext() == this &&
1536
62.7M
         "Decl inserted into wrong lexical context");
1537
0
  assert(!D->getNextDeclInContext() && D != LastDecl &&
1538
62.7M
         "Decl already inserted into a DeclContext");
1539
1540
62.7M
  if (FirstDecl) {
1541
54.9M
    LastDecl->NextInContextAndBits.setPointer(D);
1542
54.9M
    LastDecl = D;
1543
54.9M
  } else {
1544
7.78M
    FirstDecl = LastDecl = D;
1545
7.78M
  }
1546
1547
  // Notify a C++ record declaration that we've added a member, so it can
1548
  // update its class-specific state.
1549
62.7M
  if (auto *Record = dyn_cast<CXXRecordDecl>(this))
1550
8.69M
    Record->addedMember(D);
1551
1552
  // If this is a newly-created (not de-serialized) import declaration, wire
1553
  // it in to the list of local import declarations.
1554
62.7M
  if (!D->isFromASTFile()) {
1555
62.6M
    if (auto *Import = dyn_cast<ImportDecl>(D))
1556
101k
      D->getASTContext().addedLocalImportDecl(Import);
1557
62.6M
  }
1558
62.7M
}
1559
1560
59.9M
void DeclContext::addDecl(Decl *D) {
1561
59.9M
  addHiddenDecl(D);
1562
1563
59.9M
  if (auto *ND = dyn_cast<NamedDecl>(D))
1564
58.7M
    ND->getDeclContext()->getPrimaryContext()->
1565
58.7M
        makeDeclVisibleInContextWithFlags(ND, false, true);
1566
59.9M
}
1567
1568
1.98M
void DeclContext::addDeclInternal(Decl *D) {
1569
1.98M
  addHiddenDecl(D);
1570
1571
1.98M
  if (auto *ND = dyn_cast<NamedDecl>(D))
1572
1.89M
    ND->getDeclContext()->getPrimaryContext()->
1573
1.89M
        makeDeclVisibleInContextWithFlags(ND, true, true);
1574
1.98M
}
1575
1576
/// buildLookup - Build the lookup data structure with all of the
1577
/// declarations in this DeclContext (and any other contexts linked
1578
/// to it or transparent contexts nested within it) and return it.
1579
///
1580
/// Note that the produced map may miss out declarations from an
1581
/// external source. If it does, those entries will be marked with
1582
/// the 'hasExternalDecls' flag.
1583
23.4M
StoredDeclsMap *DeclContext::buildLookup() {
1584
23.4M
  assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1585
1586
23.4M
  if (!hasLazyLocalLexicalLookups() &&
1587
23.4M
      
!hasLazyExternalLexicalLookups()21.7M
)
1588
21.6M
    return LookupPtr;
1589
1590
1.79M
  SmallVector<DeclContext *, 2> Contexts;
1591
1.79M
  collectAllContexts(Contexts);
1592
1593
1.79M
  if (hasLazyExternalLexicalLookups()) {
1594
67.3k
    setHasLazyExternalLexicalLookups(false);
1595
67.3k
    for (auto *DC : Contexts) {
1596
67.3k
      if (DC->hasExternalLexicalStorage()) {
1597
67.1k
        bool LoadedDecls = DC->LoadLexicalDeclsFromExternalStorage();
1598
67.1k
        setHasLazyLocalLexicalLookups(
1599
67.1k
            hasLazyLocalLexicalLookups() | LoadedDecls );
1600
67.1k
      }
1601
67.3k
    }
1602
1603
67.3k
    if (!hasLazyLocalLexicalLookups())
1604
62.1k
      return LookupPtr;
1605
67.3k
  }
1606
1607
1.73M
  for (auto *DC : Contexts)
1608
1.73M
    buildLookupImpl(DC, hasExternalVisibleStorage());
1609
1610
  // We no longer have any lazy decls.
1611
1.73M
  setHasLazyLocalLexicalLookups(false);
1612
1.73M
  return LookupPtr;
1613
1.79M
}
1614
1615
/// buildLookupImpl - Build part of the lookup data structure for the
1616
/// declarations contained within DCtx, which will either be this
1617
/// DeclContext, a DeclContext linked to it, or a transparent context
1618
/// nested within it.
1619
1.81M
void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
1620
3.42M
  for (auto *D : DCtx->noload_decls()) {
1621
    // Insert this declaration into the lookup structure, but only if
1622
    // it's semantically within its decl context. Any other decls which
1623
    // should be found in this context are added eagerly.
1624
    //
1625
    // If it's from an AST file, don't add it now. It'll get handled by
1626
    // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1627
    // in C++, we do not track external visible decls for the TU, so in
1628
    // that case we need to collect them all here.
1629
3.42M
    if (auto *ND = dyn_cast<NamedDecl>(D))
1630
3.19M
      if (ND->getDeclContext() == DCtx && 
!shouldBeHidden(ND)3.19M
&&
1631
3.19M
          
(3.15M
!ND->isFromASTFile()3.15M
||
1632
3.15M
           
(202k
isTranslationUnit()202k
&&
1633
202k
            
!getParentASTContext().getLangOpts().CPlusPlus5.29k
)))
1634
2.95M
        makeDeclVisibleInContextImpl(ND, Internal);
1635
1636
    // If this declaration is itself a transparent declaration context
1637
    // or inline namespace, add the members of this declaration of that
1638
    // context (recursively).
1639
3.42M
    if (auto *InnerCtx = dyn_cast<DeclContext>(D))
1640
1.77M
      if (InnerCtx->isTransparentContext() || 
InnerCtx->isInlineNamespace()1.75M
)
1641
15.8k
        buildLookupImpl(InnerCtx, Internal);
1642
3.42M
  }
1643
1.81M
}
1644
1645
DeclContext::lookup_result
1646
286M
DeclContext::lookup(DeclarationName Name) const {
1647
  // For transparent DeclContext, we should lookup in their enclosing context.
1648
286M
  if (getDeclKind() == Decl::LinkageSpec || 
getDeclKind() == Decl::Export286M
)
1649
4
    return getParent()->lookup(Name);
1650
1651
286M
  const DeclContext *PrimaryContext = getPrimaryContext();
1652
286M
  if (PrimaryContext != this)
1653
43.6M
    return PrimaryContext->lookup(Name);
1654
1655
  // If we have an external source, ensure that any later redeclarations of this
1656
  // context have been loaded, since they may add names to the result of this
1657
  // lookup (or add external visible storage).
1658
243M
  ExternalASTSource *Source = getParentASTContext().getExternalSource();
1659
243M
  if (Source)
1660
17.3M
    (void)cast<Decl>(this)->getMostRecentDecl();
1661
1662
243M
  if (hasExternalVisibleStorage()) {
1663
7.54M
    assert(Source && "external visible storage but no external source?");
1664
1665
7.54M
    if (hasNeedToReconcileExternalVisibleStorage())
1666
12.9k
      reconcileExternalVisibleStorage();
1667
1668
7.54M
    StoredDeclsMap *Map = LookupPtr;
1669
1670
7.54M
    if (hasLazyLocalLexicalLookups() ||
1671
7.54M
        
hasLazyExternalLexicalLookups()7.54M
)
1672
      // FIXME: Make buildLookup const?
1673
512
      Map = const_cast<DeclContext*>(this)->buildLookup();
1674
1675
7.54M
    if (!Map)
1676
40.2k
      Map = CreateStoredDeclsMap(getParentASTContext());
1677
1678
    // If we have a lookup result with no external decls, we are done.
1679
7.54M
    std::pair<StoredDeclsMap::iterator, bool> R =
1680
7.54M
        Map->insert(std::make_pair(Name, StoredDeclsList()));
1681
7.54M
    if (!R.second && 
!R.first->second.hasExternalDecls()6.47M
)
1682
5.96M
      return R.first->second.getLookupResult();
1683
1684
1.58M
    if (Source->FindExternalVisibleDeclsByName(this, Name) || 
!R.second1.06M
) {
1685
619k
      if (StoredDeclsMap *Map = LookupPtr) {
1686
619k
        StoredDeclsMap::iterator I = Map->find(Name);
1687
619k
        if (I != Map->end())
1688
619k
          return I->second.getLookupResult();
1689
619k
      }
1690
619k
    }
1691
1692
965k
    return {};
1693
1.58M
  }
1694
1695
235M
  StoredDeclsMap *Map = LookupPtr;
1696
235M
  if (hasLazyLocalLexicalLookups() ||
1697
235M
      
hasLazyExternalLexicalLookups()233M
)
1698
1.72M
    Map = const_cast<DeclContext*>(this)->buildLookup();
1699
1700
235M
  if (!Map)
1701
19.5M
    return {};
1702
1703
216M
  StoredDeclsMap::iterator I = Map->find(Name);
1704
216M
  if (I == Map->end())
1705
142M
    return {};
1706
1707
73.2M
  return I->second.getLookupResult();
1708
216M
}
1709
1710
DeclContext::lookup_result
1711
3.65M
DeclContext::noload_lookup(DeclarationName Name) {
1712
3.65M
  assert(getDeclKind() != Decl::LinkageSpec &&
1713
3.65M
         getDeclKind() != Decl::Export &&
1714
3.65M
         "should not perform lookups into transparent contexts");
1715
1716
0
  DeclContext *PrimaryContext = getPrimaryContext();
1717
3.65M
  if (PrimaryContext != this)
1718
226
    return PrimaryContext->noload_lookup(Name);
1719
1720
3.65M
  loadLazyLocalLexicalLookups();
1721
3.65M
  StoredDeclsMap *Map = LookupPtr;
1722
3.65M
  if (!Map)
1723
272k
    return {};
1724
1725
3.38M
  StoredDeclsMap::iterator I = Map->find(Name);
1726
3.38M
  return I != Map->end() ? 
I->second.getLookupResult()2.00M
1727
3.38M
                         : 
lookup_result()1.37M
;
1728
3.65M
}
1729
1730
// If we have any lazy lexical declarations not in our lookup map, add them
1731
// now. Don't import any external declarations, not even if we know we have
1732
// some missing from the external visible lookups.
1733
3.65M
void DeclContext::loadLazyLocalLexicalLookups() {
1734
3.65M
  if (hasLazyLocalLexicalLookups()) {
1735
62.3k
    SmallVector<DeclContext *, 2> Contexts;
1736
62.3k
    collectAllContexts(Contexts);
1737
62.3k
    for (auto *Context : Contexts)
1738
62.3k
      buildLookupImpl(Context, hasExternalVisibleStorage());
1739
62.3k
    setHasLazyLocalLexicalLookups(false);
1740
62.3k
  }
1741
3.65M
}
1742
1743
void DeclContext::localUncachedLookup(DeclarationName Name,
1744
843k
                                      SmallVectorImpl<NamedDecl *> &Results) {
1745
843k
  Results.clear();
1746
1747
  // If there's no external storage, just perform a normal lookup and copy
1748
  // the results.
1749
843k
  if (!hasExternalVisibleStorage() && 
!hasExternalLexicalStorage()663k
&&
Name536k
) {
1750
535k
    lookup_result LookupResults = lookup(Name);
1751
535k
    Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1752
535k
    return;
1753
535k
  }
1754
1755
  // If we have a lookup table, check there first. Maybe we'll get lucky.
1756
  // FIXME: Should we be checking these flags on the primary context?
1757
307k
  if (Name && 
!hasLazyLocalLexicalLookups()306k
&&
1758
307k
      
!hasLazyExternalLexicalLookups()306k
) {
1759
191k
    if (StoredDeclsMap *Map = LookupPtr) {
1760
183k
      StoredDeclsMap::iterator Pos = Map->find(Name);
1761
183k
      if (Pos != Map->end()) {
1762
1.00k
        Results.insert(Results.end(),
1763
1.00k
                       Pos->second.getLookupResult().begin(),
1764
1.00k
                       Pos->second.getLookupResult().end());
1765
1.00k
        return;
1766
1.00k
      }
1767
183k
    }
1768
191k
  }
1769
1770
  // Slow case: grovel through the declarations in our chain looking for
1771
  // matches.
1772
  // FIXME: If we have lazy external declarations, this will not find them!
1773
  // FIXME: Should we CollectAllContexts and walk them all here?
1774
78.6M
  
for (Decl *D = FirstDecl; 306k
D;
D = D->getNextDeclInContext()78.3M
) {
1775
78.3M
    if (auto *ND = dyn_cast<NamedDecl>(D))
1776
78.0M
      if (ND->getDeclName() == Name)
1777
5.88k
        Results.push_back(ND);
1778
78.3M
  }
1779
306k
}
1780
1781
1.01G
DeclContext *DeclContext::getRedeclContext() {
1782
1.01G
  DeclContext *Ctx = this;
1783
1784
  // In C, a record type is the redeclaration context for its fields only. If
1785
  // we arrive at a record context after skipping anything else, we should skip
1786
  // the record as well. Currently, this means skipping enumerations because
1787
  // they're the only transparent context that can exist within a struct or
1788
  // union.
1789
1.01G
  bool SkipRecords = getDeclKind() == Decl::Kind::Enum &&
1790
1.01G
                     
!getParentASTContext().getLangOpts().CPlusPlus758k
;
1791
1792
  // Skip through contexts to get to the redeclaration context. Transparent
1793
  // contexts are always skipped.
1794
1.27G
  while ((SkipRecords && 
Ctx->isRecord()1.20M
) ||
Ctx->isTransparentContext()1.27G
)
1795
252M
    Ctx = Ctx->getParent();
1796
1.01G
  return Ctx;
1797
1.01G
}
1798
1799
86.8k
DeclContext *DeclContext::getEnclosingNamespaceContext() {
1800
86.8k
  DeclContext *Ctx = this;
1801
  // Skip through non-namespace, non-translation-unit contexts.
1802
136k
  while (!Ctx->isFileContext())
1803
49.3k
    Ctx = Ctx->getParent();
1804
86.8k
  return Ctx->getPrimaryContext();
1805
86.8k
}
1806
1807
18
RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1808
  // Loop until we find a non-record context.
1809
18
  RecordDecl *OutermostRD = nullptr;
1810
18
  DeclContext *DC = this;
1811
45
  while (DC->isRecord()) {
1812
27
    OutermostRD = cast<RecordDecl>(DC);
1813
27
    DC = DC->getLexicalParent();
1814
27
  }
1815
18
  return OutermostRD;
1816
18
}
1817
1818
860k
bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1819
  // For non-file contexts, this is equivalent to Equals.
1820
860k
  if (!isFileContext())
1821
793k
    return O->Equals(this);
1822
1823
67.2k
  
do 67.2k
{
1824
67.2k
    if (O->Equals(this))
1825
67.1k
      return true;
1826
1827
124
    const auto *NS = dyn_cast<NamespaceDecl>(O);
1828
124
    if (!NS || 
!NS->isInline()109
)
1829
53
      break;
1830
71
    O = NS->getParent();
1831
71
  } while (O);
1832
1833
53
  return false;
1834
67.2k
}
1835
1836
1.36M
void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1837
1.36M
  DeclContext *PrimaryDC = this->getPrimaryContext();
1838
1.36M
  DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1839
  // If the decl is being added outside of its semantic decl context, we
1840
  // need to ensure that we eagerly build the lookup information for it.
1841
1.36M
  PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
1842
1.36M
}
1843
1844
void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1845
76.7M
                                                    bool Recoverable) {
1846
76.7M
  assert(this == getPrimaryContext() && "expected a primary DC");
1847
1848
76.7M
  if (!isLookupContext()) {
1849
20.7M
    if (isTransparentContext())
1850
9.60M
      getParent()->getPrimaryContext()
1851
9.60M
        ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1852
20.7M
    return;
1853
20.7M
  }
1854
1855
  // Skip declarations which should be invisible to name lookup.
1856
55.9M
  if (shouldBeHidden(D))
1857
1.39M
    return;
1858
1859
  // If we already have a lookup data structure, perform the insertion into
1860
  // it. If we might have externally-stored decls with this name, look them
1861
  // up and perform the insertion. If this decl was declared outside its
1862
  // semantic context, buildLookup won't add it, so add it now.
1863
  //
1864
  // FIXME: As a performance hack, don't add such decls into the translation
1865
  // unit unless we're in C++, since qualified lookup into the TU is never
1866
  // performed.
1867
54.5M
  if (LookupPtr || 
hasExternalVisibleStorage()33.2M
||
1868
54.5M
      
(33.1M
(33.1M
!Recoverable33.1M
||
D->getDeclContext() != D->getLexicalDeclContext()33.1M
) &&
1869
33.1M
       
(25.2k
getParentASTContext().getLangOpts().CPlusPlus25.2k
||
1870
21.4M
        
!isTranslationUnit()6.18k
))) {
1871
    // If we have lazily omitted any decls, they might have the same name as
1872
    // the decl which we are adding, so build a full lookup table before adding
1873
    // this decl.
1874
21.4M
    buildLookup();
1875
21.4M
    makeDeclVisibleInContextImpl(D, Internal);
1876
33.1M
  } else {
1877
33.1M
    setHasLazyLocalLexicalLookups(true);
1878
33.1M
  }
1879
1880
  // If we are a transparent context or inline namespace, insert into our
1881
  // parent context, too. This operation is recursive.
1882
54.5M
  if (isTransparentContext() || 
isInlineNamespace()50.8M
)
1883
5.09M
    getParent()->getPrimaryContext()->
1884
5.09M
        makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1885
1886
54.5M
  auto *DCAsDecl = cast<Decl>(this);
1887
  // Notify that a decl was made visible unless we are a Tag being defined.
1888
54.5M
  if (!(isa<TagDecl>(DCAsDecl) && 
cast<TagDecl>(DCAsDecl)->isBeingDefined()13.7M
))
1889
42.3M
    if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1890
837k
      L->AddedVisibleDecl(this, D);
1891
54.5M
}
1892
1893
25.5M
void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1894
  // Find or create the stored declaration map.
1895
25.5M
  StoredDeclsMap *Map = LookupPtr;
1896
25.5M
  if (!Map) {
1897
1.94M
    ASTContext *C = &getParentASTContext();
1898
1.94M
    Map = CreateStoredDeclsMap(*C);
1899
1.94M
  }
1900
1901
  // If there is an external AST source, load any declarations it knows about
1902
  // with this declaration's name.
1903
  // If the lookup table contains an entry about this name it means that we
1904
  // have already checked the external source.
1905
25.5M
  if (!Internal)
1906
23.3M
    if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1907
2.20M
      if (hasExternalVisibleStorage() &&
1908
2.20M
          
Map->find(D->getDeclName()) == Map->end()695k
)
1909
162k
        Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1910
1911
  // Insert this declaration into the map.
1912
25.5M
  StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1913
1914
25.5M
  if (Internal) {
1915
    // If this is being added as part of loading an external declaration,
1916
    // this may not be the only external declaration with this name.
1917
    // In this case, we never try to replace an existing declaration; we'll
1918
    // handle that when we finalize the list of declarations for this name.
1919
2.20M
    DeclNameEntries.setHasExternalDecls();
1920
2.20M
    DeclNameEntries.prependDeclNoReplace(D);
1921
2.20M
    return;
1922
2.20M
  }
1923
1924
23.3M
  DeclNameEntries.addOrReplaceDecl(D);
1925
23.3M
}
1926
1927
17.9M
UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1928
17.9M
  return cast<UsingDirectiveDecl>(*I);
1929
17.9M
}
1930
1931
/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1932
/// this context.
1933
94.4M
DeclContext::udir_range DeclContext::using_directives() const {
1934
  // FIXME: Use something more efficient than normal lookup for using
1935
  // directives. In C++, using directives are looked up more than anything else.
1936
94.4M
  lookup_result Result = lookup(UsingDirectiveDecl::getName());
1937
94.4M
  return udir_range(Result.begin(), Result.end());
1938
94.4M
}
1939
1940
//===----------------------------------------------------------------------===//
1941
// Creation and Destruction of StoredDeclsMaps.                               //
1942
//===----------------------------------------------------------------------===//
1943
1944
1.98M
StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1945
1.98M
  assert(!LookupPtr && "context already has a decls map");
1946
0
  assert(getPrimaryContext() == this &&
1947
1.98M
         "creating decls map on non-primary context");
1948
1949
0
  StoredDeclsMap *M;
1950
1.98M
  bool Dependent = isDependentContext();
1951
1.98M
  if (Dependent)
1952
446k
    M = new DependentStoredDeclsMap();
1953
1.53M
  else
1954
1.53M
    M = new StoredDeclsMap();
1955
1.98M
  M->Previous = C.LastSDM;
1956
1.98M
  C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1957
1.98M
  LookupPtr = M;
1958
1.98M
  return M;
1959
1.98M
}
1960
1961
92.9k
void ASTContext::ReleaseDeclContextMaps() {
1962
  // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1963
  // pointer because the subclass doesn't add anything that needs to
1964
  // be deleted.
1965
92.9k
  StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1966
92.9k
  LastSDM.setPointer(nullptr);
1967
92.9k
}
1968
1969
92.9k
void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1970
2.07M
  while (Map) {
1971
    // Advance the iteration before we invalidate memory.
1972
1.98M
    llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1973
1974
1.98M
    if (Dependent)
1975
445k
      delete static_cast<DependentStoredDeclsMap*>(Map);
1976
1.53M
    else
1977
1.53M
      delete Map;
1978
1979
1.98M
    Map = Next.getPointer();
1980
1.98M
    Dependent = Next.getInt();
1981
1.98M
  }
1982
92.9k
}
1983
1984
DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1985
                                                 DeclContext *Parent,
1986
97
                                           const PartialDiagnostic &PDiag) {
1987
97
  assert(Parent->isDependentContext()
1988
97
         && "cannot iterate dependent diagnostics of non-dependent context");
1989
0
  Parent = Parent->getPrimaryContext();
1990
97
  if (!Parent->LookupPtr)
1991
87
    Parent->CreateStoredDeclsMap(C);
1992
1993
97
  auto *Map = static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
1994
1995
  // Allocate the copy of the PartialDiagnostic via the ASTContext's
1996
  // BumpPtrAllocator, rather than the ASTContext itself.
1997
97
  DiagnosticStorage *DiagStorage = nullptr;
1998
97
  if (PDiag.hasStorage())
1999
2
    DiagStorage = new (C) DiagnosticStorage;
2000
2001
97
  auto *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
2002
2003
  // TODO: Maybe we shouldn't reverse the order during insertion.
2004
97
  DD->NextDiagnostic = Map->FirstDiagnostic;
2005
97
  Map->FirstDiagnostic = DD;
2006
2007
97
  return DD;
2008
97
}