Coverage Report

Created: 2019-07-24 05:18

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