Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/NestedNameSpecifier.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- NestedNameSpecifier.cpp - C++ nested name specifiers ---------------===//
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 the NestedNameSpecifier class, which represents
10
//  a C++ nested-name-specifier.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/NestedNameSpecifier.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/Decl.h"
17
#include "clang/AST/DeclCXX.h"
18
#include "clang/AST/DeclTemplate.h"
19
#include "clang/AST/DependenceFlags.h"
20
#include "clang/AST/PrettyPrinter.h"
21
#include "clang/AST/TemplateName.h"
22
#include "clang/AST/Type.h"
23
#include "clang/AST/TypeLoc.h"
24
#include "clang/Basic/LLVM.h"
25
#include "clang/Basic/LangOptions.h"
26
#include "clang/Basic/SourceLocation.h"
27
#include "llvm/ADT/FoldingSet.h"
28
#include "llvm/ADT/SmallVector.h"
29
#include "llvm/Support/Casting.h"
30
#include "llvm/Support/Compiler.h"
31
#include "llvm/Support/ErrorHandling.h"
32
#include "llvm/Support/raw_ostream.h"
33
#include <algorithm>
34
#include <cassert>
35
#include <cstdlib>
36
#include <cstring>
37
38
using namespace clang;
39
40
NestedNameSpecifier *
41
NestedNameSpecifier::FindOrInsert(const ASTContext &Context,
42
13.8M
                                  const NestedNameSpecifier &Mockup) {
43
13.8M
  llvm::FoldingSetNodeID ID;
44
13.8M
  Mockup.Profile(ID);
45
46
13.8M
  void *InsertPos = nullptr;
47
13.8M
  NestedNameSpecifier *NNS
48
13.8M
    = Context.NestedNameSpecifiers.FindNodeOrInsertPos(ID, InsertPos);
49
13.8M
  if (!NNS) {
50
5.68M
    NNS =
51
5.68M
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier(Mockup);
52
5.68M
    Context.NestedNameSpecifiers.InsertNode(NNS, InsertPos);
53
5.68M
  }
54
55
13.8M
  return NNS;
56
13.8M
}
57
58
NestedNameSpecifier *
59
NestedNameSpecifier::Create(const ASTContext &Context,
60
174k
                            NestedNameSpecifier *Prefix, IdentifierInfo *II) {
61
174k
  assert(II && "Identifier cannot be NULL");
62
0
  assert((!Prefix || Prefix->isDependent()) && "Prefix must be dependent");
63
64
0
  NestedNameSpecifier Mockup;
65
174k
  Mockup.Prefix.setPointer(Prefix);
66
174k
  Mockup.Prefix.setInt(StoredIdentifier);
67
174k
  Mockup.Specifier = II;
68
174k
  return FindOrInsert(Context, Mockup);
69
174k
}
70
71
NestedNameSpecifier *
72
NestedNameSpecifier::Create(const ASTContext &Context,
73
                            NestedNameSpecifier *Prefix,
74
1.85M
                            const NamespaceDecl *NS) {
75
1.85M
  assert(NS && "Namespace cannot be NULL");
76
0
  assert((!Prefix ||
77
1.85M
          (Prefix->getAsType() == nullptr &&
78
1.85M
           Prefix->getAsIdentifier() == nullptr)) &&
79
1.85M
         "Broken nested name specifier");
80
0
  NestedNameSpecifier Mockup;
81
1.85M
  Mockup.Prefix.setPointer(Prefix);
82
1.85M
  Mockup.Prefix.setInt(StoredDecl);
83
1.85M
  Mockup.Specifier = const_cast<NamespaceDecl *>(NS);
84
1.85M
  return FindOrInsert(Context, Mockup);
85
1.85M
}
86
87
NestedNameSpecifier *
88
NestedNameSpecifier::Create(const ASTContext &Context,
89
                            NestedNameSpecifier *Prefix,
90
247
                            NamespaceAliasDecl *Alias) {
91
247
  assert(Alias && "Namespace alias cannot be NULL");
92
0
  assert((!Prefix ||
93
247
          (Prefix->getAsType() == nullptr &&
94
247
           Prefix->getAsIdentifier() == nullptr)) &&
95
247
         "Broken nested name specifier");
96
0
  NestedNameSpecifier Mockup;
97
247
  Mockup.Prefix.setPointer(Prefix);
98
247
  Mockup.Prefix.setInt(StoredDecl);
99
247
  Mockup.Specifier = Alias;
100
247
  return FindOrInsert(Context, Mockup);
101
247
}
102
103
NestedNameSpecifier *
104
NestedNameSpecifier::Create(const ASTContext &Context,
105
                            NestedNameSpecifier *Prefix,
106
11.8M
                            bool Template, const Type *T) {
107
11.8M
  assert(T && "Type cannot be NULL");
108
0
  NestedNameSpecifier Mockup;
109
11.8M
  Mockup.Prefix.setPointer(Prefix);
110
11.8M
  Mockup.Prefix.setInt(Template? 
StoredTypeSpecWithTemplate9.34k
:
StoredTypeSpec11.8M
);
111
11.8M
  Mockup.Specifier = const_cast<Type*>(T);
112
11.8M
  return FindOrInsert(Context, Mockup);
113
11.8M
}
114
115
NestedNameSpecifier *
116
0
NestedNameSpecifier::Create(const ASTContext &Context, IdentifierInfo *II) {
117
0
  assert(II && "Identifier cannot be NULL");
118
0
  NestedNameSpecifier Mockup;
119
0
  Mockup.Prefix.setPointer(nullptr);
120
0
  Mockup.Prefix.setInt(StoredIdentifier);
121
0
  Mockup.Specifier = II;
122
0
  return FindOrInsert(Context, Mockup);
123
0
}
124
125
NestedNameSpecifier *
126
297k
NestedNameSpecifier::GlobalSpecifier(const ASTContext &Context) {
127
297k
  if (!Context.GlobalNestedNameSpecifier)
128
4.40k
    Context.GlobalNestedNameSpecifier =
129
4.40k
        new (Context, alignof(NestedNameSpecifier)) NestedNameSpecifier();
130
297k
  return Context.GlobalNestedNameSpecifier;
131
297k
}
132
133
NestedNameSpecifier *
134
NestedNameSpecifier::SuperSpecifier(const ASTContext &Context,
135
58
                                    CXXRecordDecl *RD) {
136
58
  NestedNameSpecifier Mockup;
137
58
  Mockup.Prefix.setPointer(nullptr);
138
58
  Mockup.Prefix.setInt(StoredDecl);
139
58
  Mockup.Specifier = RD;
140
58
  return FindOrInsert(Context, Mockup);
141
58
}
142
143
144M
NestedNameSpecifier::SpecifierKind NestedNameSpecifier::getKind() const {
144
144M
  if (!Specifier)
145
5.36M
    return Global;
146
147
139M
  switch (Prefix.getInt()) {
148
440k
  case StoredIdentifier:
149
440k
    return Identifier;
150
151
29.7M
  case StoredDecl: {
152
29.7M
    NamedDecl *ND = static_cast<NamedDecl *>(Specifier);
153
29.7M
    if (isa<CXXRecordDecl>(ND))
154
1.59k
      return Super;
155
29.7M
    return isa<NamespaceDecl>(ND) ? 
Namespace29.7M
:
NamespaceAlias6.81k
;
156
29.7M
  }
157
158
109M
  case StoredTypeSpec:
159
109M
    return TypeSpec;
160
161
36.7k
  case StoredTypeSpecWithTemplate:
162
36.7k
    return TypeSpecWithTemplate;
163
139M
  }
164
165
0
  llvm_unreachable("Invalid NNS Kind!");
166
0
}
167
168
/// Retrieve the namespace stored in this nested name specifier.
169
3.02M
NamespaceDecl *NestedNameSpecifier::getAsNamespace() const {
170
3.02M
  if (Prefix.getInt() == StoredDecl)
171
3.02M
    return dyn_cast<NamespaceDecl>(static_cast<NamedDecl *>(Specifier));
172
173
5.31k
  return nullptr;
174
3.02M
}
175
176
/// Retrieve the namespace alias stored in this nested name specifier.
177
6.10k
NamespaceAliasDecl *NestedNameSpecifier::getAsNamespaceAlias() const {
178
6.10k
  if (Prefix.getInt() == StoredDecl)
179
844
    return dyn_cast<NamespaceAliasDecl>(static_cast<NamedDecl *>(Specifier));
180
181
5.25k
  return nullptr;
182
6.10k
}
183
184
/// Retrieve the record declaration stored in this nested name specifier.
185
102k
CXXRecordDecl *NestedNameSpecifier::getAsRecordDecl() const {
186
102k
  switch (Prefix.getInt()) {
187
0
  case StoredIdentifier:
188
0
    return nullptr;
189
190
119
  case StoredDecl:
191
119
    return dyn_cast<CXXRecordDecl>(static_cast<NamedDecl *>(Specifier));
192
193
101k
  case StoredTypeSpec:
194
101k
  case StoredTypeSpecWithTemplate:
195
101k
    return getAsType()->getAsCXXRecordDecl();
196
102k
  }
197
198
0
  llvm_unreachable("Invalid NNS Kind!");
199
0
}
200
201
28.7M
NestedNameSpecifierDependence NestedNameSpecifier::getDependence() const {
202
28.7M
  switch (getKind()) {
203
156k
  case Identifier: {
204
    // Identifier specifiers always represent dependent types
205
156k
    auto F = NestedNameSpecifierDependence::Dependent |
206
156k
             NestedNameSpecifierDependence::Instantiation;
207
    // Prefix can contain unexpanded template parameters.
208
156k
    if (getPrefix())
209
156k
      return F | getPrefix()->getDependence();
210
316
    return F;
211
156k
  }
212
213
3.59M
  case Namespace:
214
3.59M
  case NamespaceAlias:
215
4.95M
  case Global:
216
4.95M
    return NestedNameSpecifierDependence::None;
217
218
337
  case Super: {
219
337
    CXXRecordDecl *RD = static_cast<CXXRecordDecl *>(Specifier);
220
337
    for (const auto &Base : RD->bases())
221
345
      if (Base.getType()->isDependentType())
222
        // FIXME: must also be instantiation-dependent.
223
192
        return NestedNameSpecifierDependence::Dependent;
224
145
    return NestedNameSpecifierDependence::None;
225
337
  }
226
227
23.6M
  case TypeSpec:
228
23.6M
  case TypeSpecWithTemplate:
229
23.6M
    return toNestedNameSpecifierDependendence(getAsType()->getDependence());
230
28.7M
  }
231
0
  llvm_unreachable("Invalid NNS Kind!");
232
0
}
233
234
20.7M
bool NestedNameSpecifier::isDependent() const {
235
20.7M
  return getDependence() & NestedNameSpecifierDependence::Dependent;
236
20.7M
}
237
238
11.5k
bool NestedNameSpecifier::isInstantiationDependent() const {
239
11.5k
  return getDependence() & NestedNameSpecifierDependence::Instantiation;
240
11.5k
}
241
242
409k
bool NestedNameSpecifier::containsUnexpandedParameterPack() const {
243
409k
  return getDependence() & NestedNameSpecifierDependence::UnexpandedPack;
244
409k
}
245
246
188
bool NestedNameSpecifier::containsErrors() const {
247
188
  return getDependence() & NestedNameSpecifierDependence::Error;
248
188
}
249
250
/// Print this nested name specifier to the given output
251
/// stream.
252
void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
253
118k
                                bool ResolveTemplateArguments) const {
254
118k
  if (getPrefix())
255
17.9k
    getPrefix()->print(OS, Policy);
256
257
118k
  switch (getKind()) {
258
17
  case Identifier:
259
17
    OS << getAsIdentifier()->getName();
260
17
    break;
261
262
19.7k
  case Namespace:
263
19.7k
    if (getAsNamespace()->isAnonymousNamespace())
264
0
      return;
265
266
19.7k
    OS << getAsNamespace()->getName();
267
19.7k
    break;
268
269
16
  case NamespaceAlias:
270
16
    OS << getAsNamespaceAlias()->getName();
271
16
    break;
272
273
450
  case Global:
274
450
    break;
275
276
16
  case Super:
277
16
    OS << "__super";
278
16
    break;
279
280
0
  case TypeSpecWithTemplate:
281
0
    OS << "template ";
282
    // Fall through to print the type.
283
0
    LLVM_FALLTHROUGH;
284
285
98.4k
  case TypeSpec: {
286
98.4k
    const auto *Record =
287
98.4k
            dyn_cast_or_null<ClassTemplateSpecializationDecl>(getAsRecordDecl());
288
98.4k
    if (ResolveTemplateArguments && 
Record92.9k
) {
289
        // Print the type trait with resolved template parameters.
290
88.5k
        Record->printName(OS);
291
88.5k
        printTemplateArgumentList(
292
88.5k
            OS, Record->getTemplateArgs().asArray(), Policy,
293
88.5k
            Record->getSpecializedTemplate()->getTemplateParameters());
294
88.5k
        break;
295
88.5k
    }
296
9.95k
    const Type *T = getAsType();
297
298
9.95k
    PrintingPolicy InnerPolicy(Policy);
299
9.95k
    InnerPolicy.SuppressScope = true;
300
301
    // Nested-name-specifiers are intended to contain minimally-qualified
302
    // types. An actual ElaboratedType will not occur, since we'll store
303
    // just the type that is referred to in the nested-name-specifier (e.g.,
304
    // a TypedefType, TagType, etc.). However, when we are dealing with
305
    // dependent template-id types (e.g., Outer<T>::template Inner<U>),
306
    // the type requires its own nested-name-specifier for uniqueness, so we
307
    // suppress that nested-name-specifier during printing.
308
9.95k
    assert(!isa<ElaboratedType>(T) &&
309
9.95k
           "Elaborated type in nested-name-specifier");
310
9.95k
    if (const TemplateSpecializationType *SpecType
311
9.95k
          = dyn_cast<TemplateSpecializationType>(T)) {
312
      // Print the template name without its corresponding
313
      // nested-name-specifier.
314
5.28k
      SpecType->getTemplateName().print(OS, InnerPolicy,
315
5.28k
                                        TemplateName::Qualified::None);
316
317
      // Print the template argument list.
318
5.28k
      printTemplateArgumentList(OS, SpecType->template_arguments(),
319
5.28k
                                InnerPolicy);
320
5.28k
    } else 
if (const auto *4.67k
DepSpecType4.67k
=
321
4.67k
                   dyn_cast<DependentTemplateSpecializationType>(T)) {
322
      // Print the template name without its corresponding
323
      // nested-name-specifier.
324
0
      OS << DepSpecType->getIdentifier()->getName();
325
      // Print the template argument list.
326
0
      printTemplateArgumentList(OS, DepSpecType->template_arguments(),
327
0
                                InnerPolicy);
328
4.67k
    } else {
329
      // Print the type normally
330
4.67k
      QualType(T, 0).print(OS, InnerPolicy);
331
4.67k
    }
332
9.95k
    break;
333
98.4k
  }
334
118k
  }
335
336
118k
  OS << "::";
337
118k
}
338
339
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(const LangOptions &LO) const {
340
0
  dump(llvm::errs(), LO);
341
0
}
342
343
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump() const { dump(llvm::errs()); }
344
345
0
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS) const {
346
0
  LangOptions LO;
347
0
  dump(OS, LO);
348
0
}
349
350
LLVM_DUMP_METHOD void NestedNameSpecifier::dump(llvm::raw_ostream &OS,
351
0
                                                const LangOptions &LO) const {
352
0
  print(OS, PrintingPolicy(LO));
353
0
}
354
355
unsigned
356
18.6M
NestedNameSpecifierLoc::getLocalDataLength(NestedNameSpecifier *Qualifier) {
357
18.6M
  assert(Qualifier && "Expected a non-NULL qualifier");
358
359
  // Location of the trailing '::'.
360
0
  unsigned Length = sizeof(SourceLocation::UIntTy);
361
362
18.6M
  switch (Qualifier->getKind()) {
363
153k
  case NestedNameSpecifier::Global:
364
    // Nothing more to add.
365
153k
    break;
366
367
40.8k
  case NestedNameSpecifier::Identifier:
368
8.88M
  case NestedNameSpecifier::Namespace:
369
8.88M
  case NestedNameSpecifier::NamespaceAlias:
370
8.88M
  case NestedNameSpecifier::Super:
371
    // The location of the identifier or namespace name.
372
8.88M
    Length += sizeof(SourceLocation::UIntTy);
373
8.88M
    break;
374
375
4.41k
  case NestedNameSpecifier::TypeSpecWithTemplate:
376
9.60M
  case NestedNameSpecifier::TypeSpec:
377
    // The "void*" that points at the TypeLoc data.
378
    // Note: the 'template' keyword is part of the TypeLoc.
379
9.60M
    Length += sizeof(void *);
380
9.60M
    break;
381
18.6M
  }
382
383
18.6M
  return Length;
384
18.6M
}
385
386
unsigned
387
87.2M
NestedNameSpecifierLoc::getDataLength(NestedNameSpecifier *Qualifier) {
388
87.2M
  unsigned Length = 0;
389
105M
  for (; Qualifier; 
Qualifier = Qualifier->getPrefix()18.6M
)
390
18.6M
    Length += getLocalDataLength(Qualifier);
391
87.2M
  return Length;
392
87.2M
}
393
394
/// Load a (possibly unaligned) source location from a given address
395
/// and offset.
396
86.8M
static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) {
397
86.8M
  SourceLocation::UIntTy Raw;
398
86.8M
  memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(Raw));
399
86.8M
  return SourceLocation::getFromRawEncoding(Raw);
400
86.8M
}
401
402
/// Load a (possibly unaligned) pointer from a given address and
403
/// offset.
404
59.7M
static void *LoadPointer(void *Data, unsigned Offset) {
405
59.7M
  void *Result;
406
59.7M
  memcpy(&Result, static_cast<char *>(Data) + Offset, sizeof(void*));
407
59.7M
  return Result;
408
59.7M
}
409
410
33.2M
SourceRange NestedNameSpecifierLoc::getSourceRange() const {
411
33.2M
  if (!Qualifier)
412
173k
    return SourceRange();
413
414
33.0M
  NestedNameSpecifierLoc First = *this;
415
38.9M
  while (NestedNameSpecifierLoc Prefix = First.getPrefix())
416
5.92M
    First = Prefix;
417
418
33.0M
  return SourceRange(First.getLocalSourceRange().getBegin(),
419
33.0M
                     getLocalSourceRange().getEnd());
420
33.2M
}
421
422
72.7M
SourceRange NestedNameSpecifierLoc::getLocalSourceRange() const {
423
72.7M
  if (!Qualifier)
424
0
    return SourceRange();
425
426
72.7M
  unsigned Offset = getDataLength(Qualifier->getPrefix());
427
72.7M
  switch (Qualifier->getKind()) {
428
2.84M
  case NestedNameSpecifier::Global:
429
2.84M
    return LoadSourceLocation(Data, Offset);
430
431
126k
  case NestedNameSpecifier::Identifier:
432
14.0M
  case NestedNameSpecifier::Namespace:
433
14.0M
  case NestedNameSpecifier::NamespaceAlias:
434
14.0M
  case NestedNameSpecifier::Super:
435
14.0M
    return SourceRange(
436
14.0M
        LoadSourceLocation(Data, Offset),
437
14.0M
        LoadSourceLocation(Data, Offset + sizeof(SourceLocation::UIntTy)));
438
439
7.91k
  case NestedNameSpecifier::TypeSpecWithTemplate:
440
55.8M
  case NestedNameSpecifier::TypeSpec: {
441
    // The "void*" that points at the TypeLoc data.
442
    // Note: the 'template' keyword is part of the TypeLoc.
443
55.8M
    void *TypeData = LoadPointer(Data, Offset);
444
55.8M
    TypeLoc TL(Qualifier->getAsType(), TypeData);
445
55.8M
    return SourceRange(TL.getBeginLoc(),
446
55.8M
                       LoadSourceLocation(Data, Offset + sizeof(void*)));
447
7.91k
  }
448
72.7M
  }
449
450
0
  llvm_unreachable("Invalid NNS Kind!");
451
0
}
452
453
3.84M
TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
454
3.84M
  if (Qualifier->getKind() != NestedNameSpecifier::TypeSpec &&
455
3.84M
      
Qualifier->getKind() != NestedNameSpecifier::TypeSpecWithTemplate3.15k
)
456
105
    return TypeLoc();
457
458
  // The "void*" that points at the TypeLoc data.
459
3.84M
  unsigned Offset = getDataLength(Qualifier->getPrefix());
460
3.84M
  void *TypeData = LoadPointer(Data, Offset);
461
3.84M
  return TypeLoc(Qualifier->getAsType(), TypeData);
462
3.84M
}
463
464
static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
465
16.3M
                   unsigned &BufferCapacity) {
466
16.3M
  if (Start == End)
467
14
    return;
468
469
16.3M
  if (BufferSize + (End - Start) > BufferCapacity) {
470
    // Reallocate the buffer.
471
7.81M
    unsigned NewCapacity = std::max(
472
7.81M
        (unsigned)(BufferCapacity ? 
BufferCapacity * 2230k
:
sizeof(void *) * 27.58M
),
473
7.81M
        (unsigned)(BufferSize + (End - Start)));
474
7.81M
    if (!BufferCapacity) {
475
7.58M
      char *NewBuffer = static_cast<char *>(llvm::safe_malloc(NewCapacity));
476
7.58M
      if (Buffer)
477
10
        memcpy(NewBuffer, Buffer, BufferSize);
478
7.58M
      Buffer = NewBuffer;
479
7.58M
    } else {
480
230k
      Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, NewCapacity));
481
230k
    }
482
7.81M
    BufferCapacity = NewCapacity;
483
7.81M
  }
484
16.3M
  assert(Buffer && Start && End && End > Start && "Illegal memory buffer copy");
485
0
  memcpy(Buffer + BufferSize, Start, End - Start);
486
16.3M
  BufferSize += End - Start;
487
16.3M
}
488
489
/// Save a source location to the given buffer.
490
static void SaveSourceLocation(SourceLocation Loc, char *&Buffer,
491
9.81M
                               unsigned &BufferSize, unsigned &BufferCapacity) {
492
9.81M
  SourceLocation::UIntTy Raw = Loc.getRawEncoding();
493
9.81M
  Append(reinterpret_cast<char *>(&Raw),
494
9.81M
         reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize,
495
9.81M
         BufferCapacity);
496
9.81M
}
497
498
/// Save a pointer to the given buffer.
499
static void SavePointer(void *Ptr, char *&Buffer, unsigned &BufferSize,
500
6.33M
                        unsigned &BufferCapacity) {
501
6.33M
  Append(reinterpret_cast<char *>(&Ptr),
502
6.33M
         reinterpret_cast<char *>(&Ptr) + sizeof(void *),
503
6.33M
         Buffer, BufferSize, BufferCapacity);
504
6.33M
}
505
506
NestedNameSpecifierLocBuilder::
507
NestedNameSpecifierLocBuilder(const NestedNameSpecifierLocBuilder &Other)
508
9.38M
    : Representation(Other.Representation) {
509
9.38M
  if (!Other.Buffer)
510
9.34M
    return;
511
512
36.4k
  if (Other.BufferCapacity == 0) {
513
    // Shallow copy is okay.
514
27.1k
    Buffer = Other.Buffer;
515
27.1k
    BufferSize = Other.BufferSize;
516
27.1k
    return;
517
27.1k
  }
518
519
  // Deep copy
520
9.29k
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
521
9.29k
         BufferCapacity);
522
9.29k
}
523
524
NestedNameSpecifierLocBuilder &
525
NestedNameSpecifierLocBuilder::
526
1.89M
operator=(const NestedNameSpecifierLocBuilder &Other) {
527
1.89M
  Representation = Other.Representation;
528
529
1.89M
  if (Buffer && 
Other.Buffer39
&&
BufferCapacity >= Other.BufferSize39
) {
530
    // Re-use our storage.
531
26
    BufferSize = Other.BufferSize;
532
26
    memcpy(Buffer, Other.Buffer, BufferSize);
533
26
    return *this;
534
26
  }
535
536
  // Free our storage, if we have any.
537
1.89M
  if (BufferCapacity) {
538
0
    free(Buffer);
539
0
    BufferCapacity = 0;
540
0
  }
541
542
1.89M
  if (!Other.Buffer) {
543
    // Empty.
544
1.55M
    Buffer = nullptr;
545
1.55M
    BufferSize = 0;
546
1.55M
    return *this;
547
1.55M
  }
548
549
336k
  if (Other.BufferCapacity == 0) {
550
    // Shallow copy is okay.
551
133k
    Buffer = Other.Buffer;
552
133k
    BufferSize = Other.BufferSize;
553
133k
    return *this;
554
133k
  }
555
556
  // Deep copy.
557
202k
  BufferSize = 0;
558
202k
  Append(Other.Buffer, Other.Buffer + Other.BufferSize, Buffer, BufferSize,
559
202k
         BufferCapacity);
560
202k
  return *this;
561
336k
}
562
563
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
564
                                           SourceLocation TemplateKWLoc,
565
                                           TypeLoc TL,
566
5.72M
                                           SourceLocation ColonColonLoc) {
567
5.72M
  Representation = NestedNameSpecifier::Create(Context, Representation,
568
5.72M
                                               TemplateKWLoc.isValid(),
569
5.72M
                                               TL.getTypePtr());
570
571
  // Push source-location info into the buffer.
572
5.72M
  SavePointer(TL.getOpaqueData(), Buffer, BufferSize, BufferCapacity);
573
5.72M
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
574
5.72M
}
575
576
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
577
                                           IdentifierInfo *Identifier,
578
                                           SourceLocation IdentifierLoc,
579
35.7k
                                           SourceLocation ColonColonLoc) {
580
35.7k
  Representation = NestedNameSpecifier::Create(Context, Representation,
581
35.7k
                                               Identifier);
582
583
  // Push source-location info into the buffer.
584
35.7k
  SaveSourceLocation(IdentifierLoc, Buffer, BufferSize, BufferCapacity);
585
35.7k
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
586
35.7k
}
587
588
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
589
                                           NamespaceDecl *Namespace,
590
                                           SourceLocation NamespaceLoc,
591
1.54M
                                           SourceLocation ColonColonLoc) {
592
1.54M
  Representation = NestedNameSpecifier::Create(Context, Representation,
593
1.54M
                                               Namespace);
594
595
  // Push source-location info into the buffer.
596
1.54M
  SaveSourceLocation(NamespaceLoc, Buffer, BufferSize, BufferCapacity);
597
1.54M
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
598
1.54M
}
599
600
void NestedNameSpecifierLocBuilder::Extend(ASTContext &Context,
601
                                           NamespaceAliasDecl *Alias,
602
                                           SourceLocation AliasLoc,
603
243
                                           SourceLocation ColonColonLoc) {
604
243
  Representation = NestedNameSpecifier::Create(Context, Representation, Alias);
605
606
  // Push source-location info into the buffer.
607
243
  SaveSourceLocation(AliasLoc, Buffer, BufferSize, BufferCapacity);
608
243
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
609
243
}
610
611
void NestedNameSpecifierLocBuilder::MakeGlobal(ASTContext &Context,
612
285k
                                               SourceLocation ColonColonLoc) {
613
285k
  assert(!Representation && "Already have a nested-name-specifier!?");
614
0
  Representation = NestedNameSpecifier::GlobalSpecifier(Context);
615
616
  // Push source-location info into the buffer.
617
285k
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
618
285k
}
619
620
void NestedNameSpecifierLocBuilder::MakeSuper(ASTContext &Context,
621
                                              CXXRecordDecl *RD,
622
                                              SourceLocation SuperLoc,
623
58
                                              SourceLocation ColonColonLoc) {
624
58
  Representation = NestedNameSpecifier::SuperSpecifier(Context, RD);
625
626
  // Push source-location info into the buffer.
627
58
  SaveSourceLocation(SuperLoc, Buffer, BufferSize, BufferCapacity);
628
58
  SaveSourceLocation(ColonColonLoc, Buffer, BufferSize, BufferCapacity);
629
58
}
630
631
void NestedNameSpecifierLocBuilder::MakeTrivial(ASTContext &Context,
632
                                                NestedNameSpecifier *Qualifier,
633
630k
                                                SourceRange R) {
634
630k
  Representation = Qualifier;
635
636
  // Construct bogus (but well-formed) source information for the
637
  // nested-name-specifier.
638
630k
  BufferSize = 0;
639
630k
  SmallVector<NestedNameSpecifier *, 4> Stack;
640
1.25M
  for (NestedNameSpecifier *NNS = Qualifier; NNS; 
NNS = NNS->getPrefix()627k
)
641
627k
    Stack.push_back(NNS);
642
1.25M
  while (!Stack.empty()) {
643
627k
    NestedNameSpecifier *NNS = Stack.pop_back_val();
644
627k
    switch (NNS->getKind()) {
645
108
      case NestedNameSpecifier::Identifier:
646
15.7k
      case NestedNameSpecifier::Namespace:
647
15.7k
      case NestedNameSpecifier::NamespaceAlias:
648
15.7k
        SaveSourceLocation(R.getBegin(), Buffer, BufferSize, BufferCapacity);
649
15.7k
        break;
650
651
610k
      case NestedNameSpecifier::TypeSpec:
652
611k
      case NestedNameSpecifier::TypeSpecWithTemplate: {
653
611k
        TypeSourceInfo *TSInfo
654
611k
        = Context.getTrivialTypeSourceInfo(QualType(NNS->getAsType(), 0),
655
611k
                                           R.getBegin());
656
611k
        SavePointer(TSInfo->getTypeLoc().getOpaqueData(), Buffer, BufferSize,
657
611k
                    BufferCapacity);
658
611k
        break;
659
610k
      }
660
661
40
      case NestedNameSpecifier::Global:
662
40
      case NestedNameSpecifier::Super:
663
40
        break;
664
627k
    }
665
666
    // Save the location of the '::'.
667
627k
    SaveSourceLocation(Stack.empty()? 
R.getEnd()622k
:
R.getBegin()5.10k
,
668
627k
                       Buffer, BufferSize, BufferCapacity);
669
627k
  }
670
630k
}
671
672
7.84M
void NestedNameSpecifierLocBuilder::Adopt(NestedNameSpecifierLoc Other) {
673
7.84M
  if (BufferCapacity)
674
147k
    free(Buffer);
675
676
7.84M
  if (!Other) {
677
0
    Representation = nullptr;
678
0
    BufferSize = 0;
679
0
    return;
680
0
  }
681
682
  // Rather than copying the data (which is wasteful), "adopt" the
683
  // pointer (which points into the ASTContext) but set the capacity to zero to
684
  // indicate that we don't own it.
685
7.84M
  Representation = Other.getNestedNameSpecifier();
686
7.84M
  Buffer = static_cast<char *>(Other.getOpaqueData());
687
7.84M
  BufferSize = Other.getDataLength();
688
7.84M
  BufferCapacity = 0;
689
7.84M
}
690
691
NestedNameSpecifierLoc
692
11.5M
NestedNameSpecifierLocBuilder::getWithLocInContext(ASTContext &Context) const {
693
11.5M
  if (!Representation)
694
2.89M
    return NestedNameSpecifierLoc();
695
696
  // If we adopted our data pointer from elsewhere in the AST context, there's
697
  // no need to copy the memory.
698
8.65M
  if (BufferCapacity == 0)
699
2.83M
    return NestedNameSpecifierLoc(Representation, Buffer);
700
701
  // FIXME: After copying the source-location information, should we free
702
  // our (temporary) buffer and adopt the ASTContext-allocated memory?
703
  // Doing so would optimize repeated calls to getWithLocInContext().
704
5.82M
  void *Mem = Context.Allocate(BufferSize, alignof(void *));
705
5.82M
  memcpy(Mem, Buffer, BufferSize);
706
5.82M
  return NestedNameSpecifierLoc(Representation, Mem);
707
8.65M
}