Coverage Report

Created: 2023-09-12 09:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Serialization/ASTCommon.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
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 common functions that both ASTReader and ASTWriter use.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "ASTCommon.h"
14
#include "clang/AST/DeclCXX.h"
15
#include "clang/AST/DeclObjC.h"
16
#include "clang/Basic/IdentifierTable.h"
17
#include "clang/Serialization/ASTDeserializationListener.h"
18
#include "llvm/Support/DJB.h"
19
20
using namespace clang;
21
22
// Give ASTDeserializationListener's VTable a home.
23
14.8k
ASTDeserializationListener::~ASTDeserializationListener() { }
24
25
serialization::TypeIdx
26
1.91M
serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
27
1.91M
  unsigned ID = 0;
28
1.91M
  switch (BT->getKind()) {
29
66.3k
  case BuiltinType::Void:
30
66.3k
    ID = PREDEF_TYPE_VOID_ID;
31
66.3k
    break;
32
107k
  case BuiltinType::Bool:
33
107k
    ID = PREDEF_TYPE_BOOL_ID;
34
107k
    break;
35
0
  case BuiltinType::Char_U:
36
0
    ID = PREDEF_TYPE_CHAR_U_ID;
37
0
    break;
38
15.1k
  case BuiltinType::UChar:
39
15.1k
    ID = PREDEF_TYPE_UCHAR_ID;
40
15.1k
    break;
41
10.9k
  case BuiltinType::UShort:
42
10.9k
    ID = PREDEF_TYPE_USHORT_ID;
43
10.9k
    break;
44
79.0k
  case BuiltinType::UInt:
45
79.0k
    ID = PREDEF_TYPE_UINT_ID;
46
79.0k
    break;
47
43.5k
  case BuiltinType::ULong:
48
43.5k
    ID = PREDEF_TYPE_ULONG_ID;
49
43.5k
    break;
50
26.9k
  case BuiltinType::ULongLong:
51
26.9k
    ID = PREDEF_TYPE_ULONGLONG_ID;
52
26.9k
    break;
53
108
  case BuiltinType::UInt128:
54
108
    ID = PREDEF_TYPE_UINT128_ID;
55
108
    break;
56
35.4k
  case BuiltinType::Char_S:
57
35.4k
    ID = PREDEF_TYPE_CHAR_S_ID;
58
35.4k
    break;
59
648
  case BuiltinType::SChar:
60
648
    ID = PREDEF_TYPE_SCHAR_ID;
61
648
    break;
62
1.53k
  case BuiltinType::WChar_S:
63
1.53k
  case BuiltinType::WChar_U:
64
1.53k
    ID = PREDEF_TYPE_WCHAR_ID;
65
1.53k
    break;
66
30.3k
  case BuiltinType::Short:
67
30.3k
    ID = PREDEF_TYPE_SHORT_ID;
68
30.3k
    break;
69
1.14M
  case BuiltinType::Int:
70
1.14M
    ID = PREDEF_TYPE_INT_ID;
71
1.14M
    break;
72
32.3k
  case BuiltinType::Long:
73
32.3k
    ID = PREDEF_TYPE_LONG_ID;
74
32.3k
    break;
75
21.3k
  case BuiltinType::LongLong:
76
21.3k
    ID = PREDEF_TYPE_LONGLONG_ID;
77
21.3k
    break;
78
439
  case BuiltinType::Int128:
79
439
    ID = PREDEF_TYPE_INT128_ID;
80
439
    break;
81
1.89k
  case BuiltinType::Half:
82
1.89k
    ID = PREDEF_TYPE_HALF_ID;
83
1.89k
    break;
84
37.3k
  case BuiltinType::Float:
85
37.3k
    ID = PREDEF_TYPE_FLOAT_ID;
86
37.3k
    break;
87
39.3k
  case BuiltinType::Double:
88
39.3k
    ID = PREDEF_TYPE_DOUBLE_ID;
89
39.3k
    break;
90
6.16k
  case BuiltinType::LongDouble:
91
6.16k
    ID = PREDEF_TYPE_LONGDOUBLE_ID;
92
6.16k
    break;
93
34
  case BuiltinType::ShortAccum:
94
34
    ID = PREDEF_TYPE_SHORT_ACCUM_ID;
95
34
    break;
96
75
  case BuiltinType::Accum:
97
75
    ID = PREDEF_TYPE_ACCUM_ID;
98
75
    break;
99
29
  case BuiltinType::LongAccum:
100
29
    ID = PREDEF_TYPE_LONG_ACCUM_ID;
101
29
    break;
102
10
  case BuiltinType::UShortAccum:
103
10
    ID = PREDEF_TYPE_USHORT_ACCUM_ID;
104
10
    break;
105
13
  case BuiltinType::UAccum:
106
13
    ID = PREDEF_TYPE_UACCUM_ID;
107
13
    break;
108
10
  case BuiltinType::ULongAccum:
109
10
    ID = PREDEF_TYPE_ULONG_ACCUM_ID;
110
10
    break;
111
14
  case BuiltinType::ShortFract:
112
14
    ID = PREDEF_TYPE_SHORT_FRACT_ID;
113
14
    break;
114
16
  case BuiltinType::Fract:
115
16
    ID = PREDEF_TYPE_FRACT_ID;
116
16
    break;
117
20
  case BuiltinType::LongFract:
118
20
    ID = PREDEF_TYPE_LONG_FRACT_ID;
119
20
    break;
120
13
  case BuiltinType::UShortFract:
121
13
    ID = PREDEF_TYPE_USHORT_FRACT_ID;
122
13
    break;
123
13
  case BuiltinType::UFract:
124
13
    ID = PREDEF_TYPE_UFRACT_ID;
125
13
    break;
126
13
  case BuiltinType::ULongFract:
127
13
    ID = PREDEF_TYPE_ULONG_FRACT_ID;
128
13
    break;
129
8
  case BuiltinType::SatShortAccum:
130
8
    ID = PREDEF_TYPE_SAT_SHORT_ACCUM_ID;
131
8
    break;
132
8
  case BuiltinType::SatAccum:
133
8
    ID = PREDEF_TYPE_SAT_ACCUM_ID;
134
8
    break;
135
8
  case BuiltinType::SatLongAccum:
136
8
    ID = PREDEF_TYPE_SAT_LONG_ACCUM_ID;
137
8
    break;
138
4
  case BuiltinType::SatUShortAccum:
139
4
    ID = PREDEF_TYPE_SAT_USHORT_ACCUM_ID;
140
4
    break;
141
4
  case BuiltinType::SatUAccum:
142
4
    ID = PREDEF_TYPE_SAT_UACCUM_ID;
143
4
    break;
144
4
  case BuiltinType::SatULongAccum:
145
4
    ID = PREDEF_TYPE_SAT_ULONG_ACCUM_ID;
146
4
    break;
147
8
  case BuiltinType::SatShortFract:
148
8
    ID = PREDEF_TYPE_SAT_SHORT_FRACT_ID;
149
8
    break;
150
8
  case BuiltinType::SatFract:
151
8
    ID = PREDEF_TYPE_SAT_FRACT_ID;
152
8
    break;
153
8
  case BuiltinType::SatLongFract:
154
8
    ID = PREDEF_TYPE_SAT_LONG_FRACT_ID;
155
8
    break;
156
4
  case BuiltinType::SatUShortFract:
157
4
    ID = PREDEF_TYPE_SAT_USHORT_FRACT_ID;
158
4
    break;
159
4
  case BuiltinType::SatUFract:
160
4
    ID = PREDEF_TYPE_SAT_UFRACT_ID;
161
4
    break;
162
4
  case BuiltinType::SatULongFract:
163
4
    ID = PREDEF_TYPE_SAT_ULONG_FRACT_ID;
164
4
    break;
165
3.19k
  case BuiltinType::Float16:
166
3.19k
    ID = PREDEF_TYPE_FLOAT16_ID;
167
3.19k
    break;
168
0
  case BuiltinType::Float128:
169
0
    ID = PREDEF_TYPE_FLOAT128_ID;
170
0
    break;
171
0
  case BuiltinType::Ibm128:
172
0
    ID = PREDEF_TYPE_IBM128_ID;
173
0
    break;
174
1.73k
  case BuiltinType::NullPtr:
175
1.73k
    ID = PREDEF_TYPE_NULLPTR_ID;
176
1.73k
    break;
177
0
  case BuiltinType::Char8:
178
0
    ID = PREDEF_TYPE_CHAR8_ID;
179
0
    break;
180
489
  case BuiltinType::Char16:
181
489
    ID = PREDEF_TYPE_CHAR16_ID;
182
489
    break;
183
495
  case BuiltinType::Char32:
184
495
    ID = PREDEF_TYPE_CHAR32_ID;
185
495
    break;
186
16.0k
  case BuiltinType::Overload:
187
16.0k
    ID = PREDEF_TYPE_OVERLOAD_ID;
188
16.0k
    break;
189
10.1k
  case BuiltinType::BoundMember:
190
10.1k
    ID = PREDEF_TYPE_BOUND_MEMBER;
191
10.1k
    break;
192
132
  case BuiltinType::PseudoObject:
193
132
    ID = PREDEF_TYPE_PSEUDO_OBJECT;
194
132
    break;
195
127k
  case BuiltinType::Dependent:
196
127k
    ID = PREDEF_TYPE_DEPENDENT_ID;
197
127k
    break;
198
0
  case BuiltinType::UnknownAny:
199
0
    ID = PREDEF_TYPE_UNKNOWN_ANY;
200
0
    break;
201
0
  case BuiltinType::ARCUnbridgedCast:
202
0
    ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
203
0
    break;
204
434
  case BuiltinType::ObjCId:
205
434
    ID = PREDEF_TYPE_OBJC_ID;
206
434
    break;
207
111
  case BuiltinType::ObjCClass:
208
111
    ID = PREDEF_TYPE_OBJC_CLASS;
209
111
    break;
210
221
  case BuiltinType::ObjCSel:
211
221
    ID = PREDEF_TYPE_OBJC_SEL;
212
221
    break;
213
0
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
214
1.78k
  case BuiltinType::Id: \
215
1.78k
    ID = PREDEF_TYPE_##Id##_ID; \
216
1.78k
    break;
217
221
#include "clang/Basic/OpenCLImageTypes.def"
218
0
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
219
192
  case BuiltinType::Id: \
220
192
    ID = PREDEF_TYPE_##Id##_ID; \
221
192
    break;
222
50
#include "clang/Basic/OpenCLExtensionTypes.def"
223
20
  case BuiltinType::OCLSampler:
224
20
    ID = PREDEF_TYPE_SAMPLER_ID;
225
20
    break;
226
20
  case BuiltinType::OCLEvent:
227
20
    ID = PREDEF_TYPE_EVENT_ID;
228
20
    break;
229
10
  case BuiltinType::OCLClkEvent:
230
10
    ID = PREDEF_TYPE_CLK_EVENT_ID;
231
10
    break;
232
10
  case BuiltinType::OCLQueue:
233
10
    ID = PREDEF_TYPE_QUEUE_ID;
234
10
    break;
235
10
  case BuiltinType::OCLReserveID:
236
10
    ID = PREDEF_TYPE_RESERVE_ID_ID;
237
10
    break;
238
0
#define SVE_TYPE(Name, Id, SingletonId) \
239
2.76k
  case BuiltinType::Id: \
240
2.76k
    ID = PREDEF_TYPE_##Id##_ID; \
241
2.76k
    break;
242
10
#include "clang/Basic/AArch64SVEACLETypes.def"
243
0
#define PPC_VECTOR_TYPE(Name, Id, Size) \
244
2.20k
  case BuiltinType::Id: \
245
2.20k
    ID = PREDEF_TYPE_##Id##_ID; \
246
2.20k
    break;
247
53
#include "clang/Basic/PPCTypes.def"
248
0
#define RVV_TYPE(Name, Id, SingletonId)                                        \
249
0
  case BuiltinType::Id:                                                        \
250
0
    ID = PREDEF_TYPE_##Id##_ID;                                                \
251
0
    break;
252
1.10k
#include "clang/Basic/RISCVVTypes.def"
253
0
#define WASM_TYPE(Name, Id, SingletonId)                                       \
254
0
  case BuiltinType::Id:                                                        \
255
0
    ID = PREDEF_TYPE_##Id##_ID;                                                \
256
0
    break;
257
0
#include "clang/Basic/WebAssemblyReferenceTypes.def"
258
34.7k
  case BuiltinType::BuiltinFn:
259
34.7k
    ID = PREDEF_TYPE_BUILTIN_FN;
260
34.7k
    break;
261
0
  case BuiltinType::IncompleteMatrixIdx:
262
0
    ID = PREDEF_TYPE_INCOMPLETE_MATRIX_IDX;
263
0
    break;
264
2.03k
  case BuiltinType::OMPArraySection:
265
2.03k
    ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
266
2.03k
    break;
267
44
  case BuiltinType::OMPArrayShaping:
268
44
    ID = PREDEF_TYPE_OMP_ARRAY_SHAPING;
269
44
    break;
270
26
  case BuiltinType::OMPIterator:
271
26
    ID = PREDEF_TYPE_OMP_ITERATOR;
272
26
    break;
273
91
  case BuiltinType::BFloat16:
274
91
    ID = PREDEF_TYPE_BFLOAT16_ID;
275
91
    break;
276
1.91M
  }
277
278
1.91M
  return TypeIdx(ID);
279
1.91M
}
280
281
328k
unsigned serialization::ComputeHash(Selector Sel) {
282
328k
  unsigned N = Sel.getNumArgs();
283
328k
  if (N == 0)
284
155k
    ++N;
285
328k
  unsigned R = 5381;
286
722k
  for (unsigned I = 0; I != N; 
++I393k
)
287
393k
    if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
288
393k
      R = llvm::djbHash(II->getName(), R);
289
328k
  return R;
290
328k
}
291
292
const DeclContext *
293
41.1k
serialization::getDefinitiveDeclContext(const DeclContext *DC) {
294
41.1k
  switch (DC->getDeclKind()) {
295
  // These entities may have multiple definitions.
296
0
  case Decl::TranslationUnit:
297
0
  case Decl::ExternCContext:
298
40.0k
  case Decl::Namespace:
299
40.0k
  case Decl::LinkageSpec:
300
40.0k
  case Decl::Export:
301
40.0k
    return nullptr;
302
303
  // C/C++ tag types can only be defined in one place.
304
0
  case Decl::Enum:
305
0
  case Decl::Record:
306
0
    if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
307
0
      return Def;
308
0
    return nullptr;
309
310
  // FIXME: These can be defined in one place... except special member
311
  // functions and out-of-line definitions.
312
785
  case Decl::CXXRecord:
313
1.07k
  case Decl::ClassTemplateSpecialization:
314
1.09k
  case Decl::ClassTemplatePartialSpecialization:
315
1.09k
    return nullptr;
316
317
  // Each function, method, and block declaration is its own DeclContext.
318
0
  case Decl::Function:
319
0
  case Decl::CXXMethod:
320
0
  case Decl::CXXConstructor:
321
0
  case Decl::CXXDestructor:
322
0
  case Decl::CXXConversion:
323
0
  case Decl::ObjCMethod:
324
0
  case Decl::Block:
325
0
  case Decl::Captured:
326
    // Objective C categories, category implementations, and class
327
    // implementations can only be defined in one place.
328
0
  case Decl::ObjCCategory:
329
0
  case Decl::ObjCCategoryImpl:
330
0
  case Decl::ObjCImplementation:
331
0
    return DC;
332
333
0
  case Decl::ObjCProtocol:
334
0
    if (const ObjCProtocolDecl *Def
335
0
          = cast<ObjCProtocolDecl>(DC)->getDefinition())
336
0
      return Def;
337
0
    return nullptr;
338
339
  // FIXME: These are defined in one place, but properties in class extensions
340
  // end up being back-patched into the main interface. See
341
  // Sema::HandlePropertyInClassExtension for the offending code.
342
1
  case Decl::ObjCInterface:
343
1
    return nullptr;
344
345
0
  default:
346
0
    llvm_unreachable("Unhandled DeclContext in AST reader");
347
41.1k
  }
348
349
0
  llvm_unreachable("Unhandled decl kind");
350
0
}
351
352
35.1k
bool serialization::isRedeclarableDeclKind(unsigned Kind) {
353
35.1k
  switch (static_cast<Decl::Kind>(Kind)) {
354
0
  case Decl::TranslationUnit:
355
0
  case Decl::ExternCContext:
356
    // Special case of a "merged" declaration.
357
0
    return true;
358
359
3.66k
  case Decl::Namespace:
360
3.66k
  case Decl::NamespaceAlias:
361
8.17k
  case Decl::Typedef:
362
8.18k
  case Decl::TypeAlias:
363
10.1k
  case Decl::Enum:
364
11.2k
  case Decl::Record:
365
14.2k
  case Decl::CXXRecord:
366
14.3k
  case Decl::ClassTemplateSpecialization:
367
14.3k
  case Decl::ClassTemplatePartialSpecialization:
368
14.4k
  case Decl::VarTemplateSpecialization:
369
14.4k
  case Decl::VarTemplatePartialSpecialization:
370
20.2k
  case Decl::Function:
371
20.2k
  case Decl::CXXDeductionGuide:
372
25.5k
  case Decl::CXXMethod:
373
26.8k
  case Decl::CXXConstructor:
374
27.0k
  case Decl::CXXDestructor:
375
27.0k
  case Decl::CXXConversion:
376
27.1k
  case Decl::UsingShadow:
377
27.1k
  case Decl::ConstructorUsingShadow:
378
28.9k
  case Decl::Var:
379
31.1k
  case Decl::FunctionTemplate:
380
32.0k
  case Decl::ClassTemplate:
381
32.1k
  case Decl::VarTemplate:
382
32.1k
  case Decl::TypeAliasTemplate:
383
32.5k
  case Decl::ObjCProtocol:
384
35.1k
  case Decl::ObjCInterface:
385
35.1k
  case Decl::Empty:
386
35.1k
    return true;
387
388
  // Never redeclarable.
389
0
  case Decl::UsingDirective:
390
0
  case Decl::Label:
391
0
  case Decl::UnresolvedUsingTypename:
392
0
  case Decl::TemplateTypeParm:
393
0
  case Decl::EnumConstant:
394
0
  case Decl::UnresolvedUsingValue:
395
0
  case Decl::IndirectField:
396
0
  case Decl::Field:
397
0
  case Decl::MSProperty:
398
0
  case Decl::MSGuid:
399
0
  case Decl::UnnamedGlobalConstant:
400
0
  case Decl::TemplateParamObject:
401
0
  case Decl::ObjCIvar:
402
0
  case Decl::ObjCAtDefsField:
403
0
  case Decl::NonTypeTemplateParm:
404
0
  case Decl::TemplateTemplateParm:
405
0
  case Decl::Using:
406
0
  case Decl::UsingEnum:
407
0
  case Decl::UsingPack:
408
0
  case Decl::ObjCMethod:
409
0
  case Decl::ObjCCategory:
410
0
  case Decl::ObjCCategoryImpl:
411
0
  case Decl::ObjCImplementation:
412
0
  case Decl::ObjCProperty:
413
0
  case Decl::ObjCCompatibleAlias:
414
0
  case Decl::LinkageSpec:
415
0
  case Decl::Export:
416
0
  case Decl::ObjCPropertyImpl:
417
0
  case Decl::PragmaComment:
418
0
  case Decl::PragmaDetectMismatch:
419
0
  case Decl::FileScopeAsm:
420
0
  case Decl::TopLevelStmt:
421
0
  case Decl::AccessSpec:
422
0
  case Decl::Friend:
423
0
  case Decl::FriendTemplate:
424
0
  case Decl::StaticAssert:
425
0
  case Decl::Block:
426
0
  case Decl::Captured:
427
0
  case Decl::ClassScopeFunctionSpecialization:
428
0
  case Decl::Import:
429
0
  case Decl::OMPThreadPrivate:
430
0
  case Decl::OMPAllocate:
431
0
  case Decl::OMPRequires:
432
0
  case Decl::OMPCapturedExpr:
433
0
  case Decl::OMPDeclareReduction:
434
0
  case Decl::OMPDeclareMapper:
435
0
  case Decl::BuiltinTemplate:
436
0
  case Decl::Decomposition:
437
0
  case Decl::Binding:
438
0
  case Decl::Concept:
439
0
  case Decl::ImplicitConceptSpecialization:
440
0
  case Decl::LifetimeExtendedTemporary:
441
0
  case Decl::RequiresExprBody:
442
0
  case Decl::UnresolvedUsingIfExists:
443
0
  case Decl::HLSLBuffer:
444
0
    return false;
445
446
  // These indirectly derive from Redeclarable<T> but are not actually
447
  // redeclarable.
448
0
  case Decl::ImplicitParam:
449
0
  case Decl::ParmVar:
450
0
  case Decl::ObjCTypeParam:
451
0
    return false;
452
35.1k
  }
453
454
0
  llvm_unreachable("Unhandled declaration kind");
455
0
}
456
457
3.11M
bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
458
  // Friend declarations in dependent contexts aren't anonymous in the usual
459
  // sense, but they cannot be found by name lookup in their semantic context
460
  // (or indeed in any context), so we treat them as anonymous.
461
  //
462
  // This doesn't apply to friend tag decls; Sema makes those available to name
463
  // lookup in the surrounding context.
464
3.11M
  if (D->getFriendObjectKind() &&
465
3.11M
      
D->getLexicalDeclContext()->isDependentContext()25.0k
&&
!isa<TagDecl>(D)10.9k
) {
466
    // For function templates and class templates, the template is numbered and
467
    // not its pattern.
468
10.9k
    if (auto *FD = dyn_cast<FunctionDecl>(D))
469
8.49k
      return !FD->getDescribedFunctionTemplate();
470
2.47k
    if (auto *RD = dyn_cast<CXXRecordDecl>(D))
471
0
      return !RD->getDescribedClassTemplate();
472
2.47k
    return true;
473
2.47k
  }
474
475
  // At block scope, we number everything that we need to deduplicate, since we
476
  // can't just use name matching to keep things lined up.
477
  // FIXME: This is only necessary for an inline function or a template or
478
  // similar.
479
3.10M
  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
480
1.23M
    if (auto *VD = dyn_cast<VarDecl>(D))
481
1.08M
      return VD->isStaticLocal();
482
    // FIXME: What about CapturedDecls (and declarations nested within them)?
483
144k
    return isa<TagDecl, BlockDecl>(D);
484
1.23M
  }
485
486
  // Otherwise, we only care about anonymous class members / block-scope decls.
487
  // FIXME: We need to handle blocks within inline / templated variables too.
488
1.87M
  if (D->getDeclName())
489
1.56M
    return false;
490
309k
  if (!isa<RecordDecl, ObjCInterfaceDecl>(D->getLexicalDeclContext()))
491
163k
    return false;
492
146k
  return isa<TagDecl, FieldDecl>(D);
493
309k
}