Coverage Report

Created: 2022-01-18 06:27

/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
10.7k
ASTDeserializationListener::~ASTDeserializationListener() { }
24
25
serialization::TypeIdx
26
3.25M
serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
27
3.25M
  unsigned ID = 0;
28
3.25M
  switch (BT->getKind()) {
29
151k
  case BuiltinType::Void:
30
151k
    ID = PREDEF_TYPE_VOID_ID;
31
151k
    break;
32
318k
  case BuiltinType::Bool:
33
318k
    ID = PREDEF_TYPE_BOOL_ID;
34
318k
    break;
35
120
  case BuiltinType::Char_U:
36
120
    ID = PREDEF_TYPE_CHAR_U_ID;
37
120
    break;
38
16.8k
  case BuiltinType::UChar:
39
16.8k
    ID = PREDEF_TYPE_UCHAR_ID;
40
16.8k
    break;
41
12.1k
  case BuiltinType::UShort:
42
12.1k
    ID = PREDEF_TYPE_USHORT_ID;
43
12.1k
    break;
44
166k
  case BuiltinType::UInt:
45
166k
    ID = PREDEF_TYPE_UINT_ID;
46
166k
    break;
47
98.4k
  case BuiltinType::ULong:
48
98.4k
    ID = PREDEF_TYPE_ULONG_ID;
49
98.4k
    break;
50
41.5k
  case BuiltinType::ULongLong:
51
41.5k
    ID = PREDEF_TYPE_ULONGLONG_ID;
52
41.5k
    break;
53
290
  case BuiltinType::UInt128:
54
290
    ID = PREDEF_TYPE_UINT128_ID;
55
290
    break;
56
52.0k
  case BuiltinType::Char_S:
57
52.0k
    ID = PREDEF_TYPE_CHAR_S_ID;
58
52.0k
    break;
59
1.55k
  case BuiltinType::SChar:
60
1.55k
    ID = PREDEF_TYPE_SCHAR_ID;
61
1.55k
    break;
62
6.91k
  case BuiltinType::WChar_S:
63
6.91k
  case BuiltinType::WChar_U:
64
6.91k
    ID = PREDEF_TYPE_WCHAR_ID;
65
6.91k
    break;
66
32.8k
  case BuiltinType::Short:
67
32.8k
    ID = PREDEF_TYPE_SHORT_ID;
68
32.8k
    break;
69
1.41M
  case BuiltinType::Int:
70
1.41M
    ID = PREDEF_TYPE_INT_ID;
71
1.41M
    break;
72
52.6k
  case BuiltinType::Long:
73
52.6k
    ID = PREDEF_TYPE_LONG_ID;
74
52.6k
    break;
75
22.6k
  case BuiltinType::LongLong:
76
22.6k
    ID = PREDEF_TYPE_LONGLONG_ID;
77
22.6k
    break;
78
1.48k
  case BuiltinType::Int128:
79
1.48k
    ID = PREDEF_TYPE_INT128_ID;
80
1.48k
    break;
81
1.94k
  case BuiltinType::Half:
82
1.94k
    ID = PREDEF_TYPE_HALF_ID;
83
1.94k
    break;
84
48.1k
  case BuiltinType::Float:
85
48.1k
    ID = PREDEF_TYPE_FLOAT_ID;
86
48.1k
    break;
87
56.1k
  case BuiltinType::Double:
88
56.1k
    ID = PREDEF_TYPE_DOUBLE_ID;
89
56.1k
    break;
90
19.1k
  case BuiltinType::LongDouble:
91
19.1k
    ID = PREDEF_TYPE_LONGDOUBLE_ID;
92
19.1k
    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
298
  case BuiltinType::Float16:
166
298
    ID = PREDEF_TYPE_FLOAT16_ID;
167
298
    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
9.72k
  case BuiltinType::NullPtr:
175
9.72k
    ID = PREDEF_TYPE_NULLPTR_ID;
176
9.72k
    break;
177
0
  case BuiltinType::Char8:
178
0
    ID = PREDEF_TYPE_CHAR8_ID;
179
0
    break;
180
2.38k
  case BuiltinType::Char16:
181
2.38k
    ID = PREDEF_TYPE_CHAR16_ID;
182
2.38k
    break;
183
2.41k
  case BuiltinType::Char32:
184
2.41k
    ID = PREDEF_TYPE_CHAR32_ID;
185
2.41k
    break;
186
88.1k
  case BuiltinType::Overload:
187
88.1k
    ID = PREDEF_TYPE_OVERLOAD_ID;
188
88.1k
    break;
189
39.1k
  case BuiltinType::BoundMember:
190
39.1k
    ID = PREDEF_TYPE_BOUND_MEMBER;
191
39.1k
    break;
192
79
  case BuiltinType::PseudoObject:
193
79
    ID = PREDEF_TYPE_PSEUDO_OBJECT;
194
79
    break;
195
543k
  case BuiltinType::Dependent:
196
543k
    ID = PREDEF_TYPE_DEPENDENT_ID;
197
543k
    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
619
  case BuiltinType::ObjCId:
205
619
    ID = PREDEF_TYPE_OBJC_ID;
206
619
    break;
207
114
  case BuiltinType::ObjCClass:
208
114
    ID = PREDEF_TYPE_OBJC_CLASS;
209
114
    break;
210
204
  case BuiltinType::ObjCSel:
211
204
    ID = PREDEF_TYPE_OBJC_SEL;
212
204
    break;
213
0
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
214
1.96k
  case BuiltinType::Id: \
215
1.96k
    ID = PREDEF_TYPE_##Id##_ID; \
216
1.96k
    break;
217
204
#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.12k
  case BuiltinType::Id: \
240
2.12k
    ID = PREDEF_TYPE_##Id##_ID; \
241
2.12k
    break;
242
10
#include "clang/Basic/AArch64SVEACLETypes.def"
243
0
#define PPC_VECTOR_TYPE(Name, Id, Size) \
244
2.04k
  case BuiltinType::Id: \
245
2.04k
    ID = PREDEF_TYPE_##Id##_ID; \
246
2.04k
    break;
247
44
#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.02k
#include "clang/Basic/RISCVVTypes.def"
253
43.6k
  case BuiltinType::BuiltinFn:
254
43.6k
    ID = PREDEF_TYPE_BUILTIN_FN;
255
43.6k
    break;
256
0
  case BuiltinType::IncompleteMatrixIdx:
257
0
    ID = PREDEF_TYPE_INCOMPLETE_MATRIX_IDX;
258
0
    break;
259
1.86k
  case BuiltinType::OMPArraySection:
260
1.86k
    ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
261
1.86k
    break;
262
44
  case BuiltinType::OMPArrayShaping:
263
44
    ID = PREDEF_TYPE_OMP_ARRAY_SHAPING;
264
44
    break;
265
16
  case BuiltinType::OMPIterator:
266
16
    ID = PREDEF_TYPE_OMP_ITERATOR;
267
16
    break;
268
0
  case BuiltinType::BFloat16:
269
0
    ID = PREDEF_TYPE_BFLOAT16_ID;
270
0
    break;
271
3.25M
  }
272
273
3.25M
  return TypeIdx(ID);
274
3.25M
}
275
276
547k
unsigned serialization::ComputeHash(Selector Sel) {
277
547k
  unsigned N = Sel.getNumArgs();
278
547k
  if (N == 0)
279
272k
    ++N;
280
547k
  unsigned R = 5381;
281
1.19M
  for (unsigned I = 0; I != N; 
++I643k
)
282
643k
    if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
283
643k
      R = llvm::djbHash(II->getName(), R);
284
547k
  return R;
285
547k
}
286
287
const DeclContext *
288
30.4k
serialization::getDefinitiveDeclContext(const DeclContext *DC) {
289
30.4k
  switch (DC->getDeclKind()) {
290
  // These entities may have multiple definitions.
291
0
  case Decl::TranslationUnit:
292
0
  case Decl::ExternCContext:
293
29.9k
  case Decl::Namespace:
294
29.9k
  case Decl::LinkageSpec:
295
29.9k
  case Decl::Export:
296
29.9k
    return nullptr;
297
298
  // C/C++ tag types can only be defined in one place.
299
0
  case Decl::Enum:
300
0
  case Decl::Record:
301
0
    if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
302
0
      return Def;
303
0
    return nullptr;
304
305
  // FIXME: These can be defined in one place... except special member
306
  // functions and out-of-line definitions.
307
413
  case Decl::CXXRecord:
308
470
  case Decl::ClassTemplateSpecialization:
309
491
  case Decl::ClassTemplatePartialSpecialization:
310
491
    return nullptr;
311
312
  // Each function, method, and block declaration is its own DeclContext.
313
0
  case Decl::Function:
314
0
  case Decl::CXXMethod:
315
0
  case Decl::CXXConstructor:
316
0
  case Decl::CXXDestructor:
317
0
  case Decl::CXXConversion:
318
0
  case Decl::ObjCMethod:
319
0
  case Decl::Block:
320
0
  case Decl::Captured:
321
    // Objective C categories, category implementations, and class
322
    // implementations can only be defined in one place.
323
0
  case Decl::ObjCCategory:
324
0
  case Decl::ObjCCategoryImpl:
325
0
  case Decl::ObjCImplementation:
326
0
    return DC;
327
328
0
  case Decl::ObjCProtocol:
329
0
    if (const ObjCProtocolDecl *Def
330
0
          = cast<ObjCProtocolDecl>(DC)->getDefinition())
331
0
      return Def;
332
0
    return nullptr;
333
334
  // FIXME: These are defined in one place, but properties in class extensions
335
  // end up being back-patched into the main interface. See
336
  // Sema::HandlePropertyInClassExtension for the offending code.
337
0
  case Decl::ObjCInterface:
338
0
    return nullptr;
339
340
0
  default:
341
0
    llvm_unreachable("Unhandled DeclContext in AST reader");
342
30.4k
  }
343
344
0
  llvm_unreachable("Unhandled decl kind");
345
0
}
346
347
96.6k
bool serialization::isRedeclarableDeclKind(unsigned Kind) {
348
96.6k
  switch (static_cast<Decl::Kind>(Kind)) {
349
0
  case Decl::TranslationUnit:
350
0
  case Decl::ExternCContext:
351
    // Special case of a "merged" declaration.
352
0
    return true;
353
354
10.0k
  case Decl::Namespace:
355
10.0k
  case Decl::NamespaceAlias:
356
14.9k
  case Decl::Typedef:
357
14.9k
  case Decl::TypeAlias:
358
18.8k
  case Decl::Enum:
359
21.9k
  case Decl::Record:
360
30.3k
  case Decl::CXXRecord:
361
30.5k
  case Decl::ClassTemplateSpecialization:
362
30.6k
  case Decl::ClassTemplatePartialSpecialization:
363
30.6k
  case Decl::VarTemplateSpecialization:
364
30.6k
  case Decl::VarTemplatePartialSpecialization:
365
42.4k
  case Decl::Function:
366
42.4k
  case Decl::CXXDeductionGuide:
367
66.3k
  case Decl::CXXMethod:
368
72.4k
  case Decl::CXXConstructor:
369
73.0k
  case Decl::CXXDestructor:
370
73.1k
  case Decl::CXXConversion:
371
73.4k
  case Decl::UsingShadow:
372
73.4k
  case Decl::ConstructorUsingShadow:
373
77.6k
  case Decl::Var:
374
87.4k
  case Decl::FunctionTemplate:
375
90.8k
  case Decl::ClassTemplate:
376
90.8k
  case Decl::VarTemplate:
377
90.9k
  case Decl::TypeAliasTemplate:
378
91.6k
  case Decl::ObjCProtocol:
379
96.6k
  case Decl::ObjCInterface:
380
96.6k
  case Decl::Empty:
381
96.6k
    return true;
382
383
  // Never redeclarable.
384
0
  case Decl::UsingDirective:
385
0
  case Decl::Label:
386
0
  case Decl::UnresolvedUsingTypename:
387
0
  case Decl::TemplateTypeParm:
388
0
  case Decl::EnumConstant:
389
0
  case Decl::UnresolvedUsingValue:
390
0
  case Decl::IndirectField:
391
0
  case Decl::Field:
392
0
  case Decl::MSProperty:
393
0
  case Decl::MSGuid:
394
0
  case Decl::TemplateParamObject:
395
0
  case Decl::ObjCIvar:
396
0
  case Decl::ObjCAtDefsField:
397
0
  case Decl::NonTypeTemplateParm:
398
0
  case Decl::TemplateTemplateParm:
399
0
  case Decl::Using:
400
0
  case Decl::UsingEnum:
401
0
  case Decl::UsingPack:
402
0
  case Decl::ObjCMethod:
403
0
  case Decl::ObjCCategory:
404
0
  case Decl::ObjCCategoryImpl:
405
0
  case Decl::ObjCImplementation:
406
0
  case Decl::ObjCProperty:
407
0
  case Decl::ObjCCompatibleAlias:
408
0
  case Decl::LinkageSpec:
409
0
  case Decl::Export:
410
0
  case Decl::ObjCPropertyImpl:
411
0
  case Decl::PragmaComment:
412
0
  case Decl::PragmaDetectMismatch:
413
0
  case Decl::FileScopeAsm:
414
0
  case Decl::AccessSpec:
415
0
  case Decl::Friend:
416
0
  case Decl::FriendTemplate:
417
0
  case Decl::StaticAssert:
418
0
  case Decl::Block:
419
0
  case Decl::Captured:
420
0
  case Decl::ClassScopeFunctionSpecialization:
421
0
  case Decl::Import:
422
0
  case Decl::OMPThreadPrivate:
423
0
  case Decl::OMPAllocate:
424
0
  case Decl::OMPRequires:
425
0
  case Decl::OMPCapturedExpr:
426
0
  case Decl::OMPDeclareReduction:
427
0
  case Decl::OMPDeclareMapper:
428
0
  case Decl::BuiltinTemplate:
429
0
  case Decl::Decomposition:
430
0
  case Decl::Binding:
431
0
  case Decl::Concept:
432
0
  case Decl::LifetimeExtendedTemporary:
433
0
  case Decl::RequiresExprBody:
434
0
  case Decl::UnresolvedUsingIfExists:
435
0
    return false;
436
437
  // These indirectly derive from Redeclarable<T> but are not actually
438
  // redeclarable.
439
0
  case Decl::ImplicitParam:
440
0
  case Decl::ParmVar:
441
0
  case Decl::ObjCTypeParam:
442
0
    return false;
443
96.6k
  }
444
445
0
  llvm_unreachable("Unhandled declaration kind");
446
0
}
447
448
6.52M
bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
449
  // Friend declarations in dependent contexts aren't anonymous in the usual
450
  // sense, but they cannot be found by name lookup in their semantic context
451
  // (or indeed in any context), so we treat them as anonymous.
452
  //
453
  // This doesn't apply to friend tag decls; Sema makes those available to name
454
  // lookup in the surrounding context.
455
6.52M
  if (D->getFriendObjectKind() &&
456
6.52M
      
D->getLexicalDeclContext()->isDependentContext()88.3k
&&
!isa<TagDecl>(D)46.5k
) {
457
    // For function templates and class templates, the template is numbered and
458
    // not its pattern.
459
46.5k
    if (auto *FD = dyn_cast<FunctionDecl>(D))
460
27.8k
      return !FD->getDescribedFunctionTemplate();
461
18.7k
    if (auto *RD = dyn_cast<CXXRecordDecl>(D))
462
0
      return !RD->getDescribedClassTemplate();
463
18.7k
    return true;
464
18.7k
  }
465
466
  // At block scope, we number everything that we need to deduplicate, since we
467
  // can't just use name matching to keep things lined up.
468
  // FIXME: This is only necessary for an inline function or a template or
469
  // similar.
470
6.48M
  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
471
2.11M
    if (auto *VD = dyn_cast<VarDecl>(D))
472
1.88M
      return VD->isStaticLocal();
473
    // FIXME: What about CapturedDecls (and declarations nested within them)?
474
230k
    return isa<TagDecl>(D) || 
isa<BlockDecl>(D)126k
;
475
2.11M
  }
476
477
  // Otherwise, we only care about anonymous class members / block-scope decls.
478
  // FIXME: We need to handle lambdas and blocks within inline / templated
479
  // variables too.
480
4.36M
  if (D->getDeclName() || 
!isa<RecordDecl>(D->getLexicalDeclContext())334k
)
481
4.22M
    return false;
482
142k
  return isa<TagDecl>(D) || 
isa<FieldDecl>(D)129k
;
483
4.36M
}