Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/AST/ODRHash.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- 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
/// \file
10
/// This file implements the ODRHash class, which calculates a hash based
11
/// on AST nodes, which is stable across different runs.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/AST/ODRHash.h"
16
17
#include "clang/AST/DeclVisitor.h"
18
#include "clang/AST/NestedNameSpecifier.h"
19
#include "clang/AST/StmtVisitor.h"
20
#include "clang/AST/TypeVisitor.h"
21
22
using namespace clang;
23
24
53.2k
void ODRHash::AddStmt(const Stmt *S) {
25
53.2k
  assert(S && "Expecting non-null pointer.");
26
53.2k
  S->ProcessODRHash(ID, *this);
27
53.2k
}
28
29
477k
void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
30
477k
  assert(II && "Expecting non-null pointer.");
31
477k
  ID.AddString(II->getName());
32
477k
}
33
34
873k
void ODRHash::AddDeclarationName(DeclarationName Name, bool TreatAsDecl) {
35
873k
  if (TreatAsDecl)
36
59
    // Matches the NamedDecl check in AddDecl
37
59
    AddBoolean(true);
38
873k
39
873k
  AddDeclarationNameImpl(Name);
40
873k
41
873k
  if (TreatAsDecl)
42
59
    // Matches the ClassTemplateSpecializationDecl check in AddDecl
43
59
    AddBoolean(false);
44
873k
}
45
46
873k
void ODRHash::AddDeclarationNameImpl(DeclarationName Name) {
47
873k
  // Index all DeclarationName and use index numbers to refer to them.
48
873k
  auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
49
873k
  ID.AddInteger(Result.first->second);
50
873k
  if (!Result.second) {
51
320k
    // If found in map, the DeclarationName has previously been processed.
52
320k
    return;
53
320k
  }
54
553k
55
553k
  // First time processing each DeclarationName, also process its details.
56
553k
  AddBoolean(Name.isEmpty());
57
553k
  if (Name.isEmpty())
58
62.1k
    return;
59
490k
60
490k
  auto Kind = Name.getNameKind();
61
490k
  ID.AddInteger(Kind);
62
490k
  switch (Kind) {
63
490k
  case DeclarationName::Identifier:
64
477k
    AddIdentifierInfo(Name.getAsIdentifierInfo());
65
477k
    break;
66
490k
  case DeclarationName::ObjCZeroArgSelector:
67
32
  case DeclarationName::ObjCOneArgSelector:
68
32
  case DeclarationName::ObjCMultiArgSelector: {
69
32
    Selector S = Name.getObjCSelector();
70
32
    AddBoolean(S.isNull());
71
32
    AddBoolean(S.isKeywordSelector());
72
32
    AddBoolean(S.isUnarySelector());
73
32
    unsigned NumArgs = S.getNumArgs();
74
32
    ID.AddInteger(NumArgs);
75
55
    for (unsigned i = 0; i < NumArgs; 
++i23
) {
76
23
      const IdentifierInfo *II = S.getIdentifierInfoForSlot(i);
77
23
      AddBoolean(II);
78
23
      if (II) {
79
20
        AddIdentifierInfo(II);
80
20
      }
81
23
    }
82
32
    break;
83
32
  }
84
10.2k
  case DeclarationName::CXXConstructorName:
85
10.2k
  case DeclarationName::CXXDestructorName:
86
10.2k
    AddQualType(Name.getCXXNameType());
87
10.2k
    break;
88
10.2k
  case DeclarationName::CXXOperatorName:
89
2.76k
    ID.AddInteger(Name.getCXXOverloadedOperator());
90
2.76k
    break;
91
10.2k
  case DeclarationName::CXXLiteralOperatorName:
92
2
    AddIdentifierInfo(Name.getCXXLiteralIdentifier());
93
2
    break;
94
10.2k
  case DeclarationName::CXXConversionFunctionName:
95
475
    AddQualType(Name.getCXXNameType());
96
475
    break;
97
10.2k
  case DeclarationName::CXXUsingDirective:
98
0
    break;
99
10.2k
  case DeclarationName::CXXDeductionGuideName: {
100
16
    auto *Template = Name.getCXXDeductionGuideTemplate();
101
16
    AddBoolean(Template);
102
16
    if (Template) {
103
16
      AddDecl(Template);
104
16
    }
105
16
  }
106
490k
  }
107
490k
}
108
109
578
void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
110
578
  assert(NNS && "Expecting non-null pointer.");
111
578
  const auto *Prefix = NNS->getPrefix();
112
578
  AddBoolean(Prefix);
113
578
  if (Prefix) {
114
34
    AddNestedNameSpecifier(Prefix);
115
34
  }
116
578
  auto Kind = NNS->getKind();
117
578
  ID.AddInteger(Kind);
118
578
  switch (Kind) {
119
578
  case NestedNameSpecifier::Identifier:
120
14
    AddIdentifierInfo(NNS->getAsIdentifier());
121
14
    break;
122
578
  case NestedNameSpecifier::Namespace:
123
129
    AddDecl(NNS->getAsNamespace());
124
129
    break;
125
578
  case NestedNameSpecifier::NamespaceAlias:
126
12
    AddDecl(NNS->getAsNamespaceAlias());
127
12
    break;
128
578
  case NestedNameSpecifier::TypeSpec:
129
417
  case NestedNameSpecifier::TypeSpecWithTemplate:
130
417
    AddType(NNS->getAsType());
131
417
    break;
132
417
  case NestedNameSpecifier::Global:
133
6
  case NestedNameSpecifier::Super:
134
6
    break;
135
578
  }
136
578
}
137
138
4.77k
void ODRHash::AddTemplateName(TemplateName Name) {
139
4.77k
  auto Kind = Name.getKind();
140
4.77k
  ID.AddInteger(Kind);
141
4.77k
142
4.77k
  switch (Kind) {
143
4.77k
  case TemplateName::Template:
144
4.77k
    AddDecl(Name.getAsTemplateDecl());
145
4.77k
    break;
146
4.77k
  // TODO: Support these cases.
147
4.77k
  case TemplateName::OverloadedTemplate:
148
2
  case TemplateName::AssumedTemplate:
149
2
  case TemplateName::QualifiedTemplate:
150
2
  case TemplateName::DependentTemplate:
151
2
  case TemplateName::SubstTemplateTemplateParm:
152
2
  case TemplateName::SubstTemplateTemplateParmPack:
153
2
    break;
154
4.77k
  }
155
4.77k
}
156
157
6.35k
void ODRHash::AddTemplateArgument(TemplateArgument TA) {
158
6.35k
  const auto Kind = TA.getKind();
159
6.35k
  ID.AddInteger(Kind);
160
6.35k
161
6.35k
  switch (Kind) {
162
6.35k
    case TemplateArgument::Null:
163
0
      llvm_unreachable("Expected valid TemplateArgument");
164
6.35k
    case TemplateArgument::Type:
165
3.61k
      AddQualType(TA.getAsType());
166
3.61k
      break;
167
6.35k
    case TemplateArgument::Declaration:
168
76
      AddDecl(TA.getAsDecl());
169
76
      break;
170
6.35k
    case TemplateArgument::NullPtr:
171
242
    case TemplateArgument::Integral:
172
242
      break;
173
242
    case TemplateArgument::Template:
174
66
    case TemplateArgument::TemplateExpansion:
175
66
      AddTemplateName(TA.getAsTemplateOrTemplatePattern());
176
66
      break;
177
2.34k
    case TemplateArgument::Expression:
178
2.34k
      AddStmt(TA.getAsExpr());
179
2.34k
      break;
180
66
    case TemplateArgument::Pack:
181
9
      ID.AddInteger(TA.pack_size());
182
13
      for (auto SubTA : TA.pack_elements()) {
183
13
        AddTemplateArgument(SubTA);
184
13
      }
185
9
      break;
186
6.35k
  }
187
6.35k
}
188
189
2.09k
void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
190
2.09k
  assert(TPL && "Expecting non-null pointer.");
191
2.09k
192
2.09k
  ID.AddInteger(TPL->size());
193
2.68k
  for (auto *ND : TPL->asArray()) {
194
2.68k
    AddSubDecl(ND);
195
2.68k
  }
196
2.09k
}
197
198
935
void ODRHash::clear() {
199
935
  DeclNameMap.clear();
200
935
  Bools.clear();
201
935
  ID.clear();
202
935
}
203
204
131k
unsigned ODRHash::CalculateHash() {
205
131k
  // Append the bools to the end of the data segment backwards.  This allows
206
131k
  // for the bools data to be compressed 32 times smaller compared to using
207
131k
  // ID.AddBoolean
208
131k
  const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
209
131k
  const unsigned size = Bools.size();
210
131k
  const unsigned remainder = size % unsigned_bits;
211
131k
  const unsigned loops = size / unsigned_bits;
212
131k
  auto I = Bools.rbegin();
213
131k
  unsigned value = 0;
214
1.83M
  for (unsigned i = 0; i < remainder; 
++i1.70M
) {
215
1.70M
    value <<= 1;
216
1.70M
    value |= *I;
217
1.70M
    ++I;
218
1.70M
  }
219
131k
  ID.AddInteger(value);
220
131k
221
223k
  for (unsigned i = 0; i < loops; 
++i92.1k
) {
222
92.1k
    value = 0;
223
3.04M
    for (unsigned j = 0; j < unsigned_bits; 
++j2.94M
) {
224
2.94M
      value <<= 1;
225
2.94M
      value |= *I;
226
2.94M
      ++I;
227
2.94M
    }
228
92.1k
    ID.AddInteger(value);
229
92.1k
  }
230
131k
231
131k
  assert(I == Bools.rend());
232
131k
  Bools.clear();
233
131k
  return ID.ComputeHash();
234
131k
}
235
236
namespace {
237
// Process a Decl pointer.  Add* methods call back into ODRHash while Visit*
238
// methods process the relevant parts of the Decl.
239
class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
240
  typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
241
  llvm::FoldingSetNodeID &ID;
242
  ODRHash &Hash;
243
244
public:
245
  ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
246
203k
      : ID(ID), Hash(Hash) {}
247
248
16.3k
  void AddStmt(const Stmt *S) {
249
16.3k
    Hash.AddBoolean(S);
250
16.3k
    if (S) {
251
12.2k
      Hash.AddStmt(S);
252
12.2k
    }
253
16.3k
  }
254
255
0
  void AddIdentifierInfo(const IdentifierInfo *II) {
256
0
    Hash.AddBoolean(II);
257
0
    if (II) {
258
0
      Hash.AddIdentifierInfo(II);
259
0
    }
260
0
  }
261
262
194k
  void AddQualType(QualType T) {
263
194k
    Hash.AddQualType(T);
264
194k
  }
265
266
533
  void AddDecl(const Decl *D) {
267
533
    Hash.AddBoolean(D);
268
533
    if (D) {
269
533
      Hash.AddDecl(D);
270
533
    }
271
533
  }
272
273
114
  void AddTemplateArgument(TemplateArgument TA) {
274
114
    Hash.AddTemplateArgument(TA);
275
114
  }
276
277
203k
  void Visit(const Decl *D) {
278
203k
    ID.AddInteger(D->getKind());
279
203k
    Inherited::Visit(D);
280
203k
  }
281
282
203k
  void VisitNamedDecl(const NamedDecl *D) {
283
203k
    Hash.AddDeclarationName(D->getDeclName());
284
203k
    Inherited::VisitNamedDecl(D);
285
203k
  }
286
287
200k
  void VisitValueDecl(const ValueDecl *D) {
288
200k
    if (!isa<FunctionDecl>(D)) {
289
194k
      AddQualType(D->getType());
290
194k
    }
291
200k
    Inherited::VisitValueDecl(D);
292
200k
  }
293
294
187k
  void VisitVarDecl(const VarDecl *D) {
295
187k
    Hash.AddBoolean(D->isStaticLocal());
296
187k
    Hash.AddBoolean(D->isConstexpr());
297
187k
    const bool HasInit = D->hasInit();
298
187k
    Hash.AddBoolean(HasInit);
299
187k
    if (HasInit) {
300
10.0k
      AddStmt(D->getInit());
301
10.0k
    }
302
187k
    Inherited::VisitVarDecl(D);
303
187k
  }
304
305
169k
  void VisitParmVarDecl(const ParmVarDecl *D) {
306
169k
    // TODO: Handle default arguments.
307
169k
    Inherited::VisitParmVarDecl(D);
308
169k
  }
309
310
607
  void VisitAccessSpecDecl(const AccessSpecDecl *D) {
311
607
    ID.AddInteger(D->getAccess());
312
607
    Inherited::VisitAccessSpecDecl(D);
313
607
  }
314
315
29
  void VisitStaticAssertDecl(const StaticAssertDecl *D) {
316
29
    AddStmt(D->getAssertExpr());
317
29
    AddStmt(D->getMessage());
318
29
319
29
    Inherited::VisitStaticAssertDecl(D);
320
29
  }
321
322
3.91k
  void VisitFieldDecl(const FieldDecl *D) {
323
3.91k
    const bool IsBitfield = D->isBitField();
324
3.91k
    Hash.AddBoolean(IsBitfield);
325
3.91k
326
3.91k
    if (IsBitfield) {
327
90
      AddStmt(D->getBitWidth());
328
90
    }
329
3.91k
330
3.91k
    Hash.AddBoolean(D->isMutable());
331
3.91k
    AddStmt(D->getInClassInitializer());
332
3.91k
333
3.91k
    Inherited::VisitFieldDecl(D);
334
3.91k
  }
335
336
5.93k
  void VisitFunctionDecl(const FunctionDecl *D) {
337
5.93k
    // Handled by the ODRHash for FunctionDecl
338
5.93k
    ID.AddInteger(D->getODRHash());
339
5.93k
340
5.93k
    Inherited::VisitFunctionDecl(D);
341
5.93k
  }
342
343
5.93k
  void VisitCXXMethodDecl(const CXXMethodDecl *D) {
344
5.93k
    // Handled by the ODRHash for FunctionDecl
345
5.93k
346
5.93k
    Inherited::VisitCXXMethodDecl(D);
347
5.93k
  }
348
349
399
  void VisitTypedefNameDecl(const TypedefNameDecl *D) {
350
399
    AddQualType(D->getUnderlyingType());
351
399
352
399
    Inherited::VisitTypedefNameDecl(D);
353
399
  }
354
355
277
  void VisitTypedefDecl(const TypedefDecl *D) {
356
277
    Inherited::VisitTypedefDecl(D);
357
277
  }
358
359
122
  void VisitTypeAliasDecl(const TypeAliasDecl *D) {
360
122
    Inherited::VisitTypeAliasDecl(D);
361
122
  }
362
363
233
  void VisitFriendDecl(const FriendDecl *D) {
364
233
    TypeSourceInfo *TSI = D->getFriendType();
365
233
    Hash.AddBoolean(TSI);
366
233
    if (TSI) {
367
52
      AddQualType(TSI->getType());
368
181
    } else {
369
181
      AddDecl(D->getFriendDecl());
370
181
    }
371
233
  }
372
373
1.94k
  void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
374
1.94k
    // Only care about default arguments as part of the definition.
375
1.94k
    const bool hasDefaultArgument =
376
1.94k
        D->hasDefaultArgument() && 
!D->defaultArgumentWasInherited()80
;
377
1.94k
    Hash.AddBoolean(hasDefaultArgument);
378
1.94k
    if (hasDefaultArgument) {
379
74
      AddTemplateArgument(D->getDefaultArgument());
380
74
    }
381
1.94k
    Hash.AddBoolean(D->isParameterPack());
382
1.94k
383
1.94k
    Inherited::VisitTemplateTypeParmDecl(D);
384
1.94k
  }
385
386
663
  void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
387
663
    // Only care about default arguments as part of the definition.
388
663
    const bool hasDefaultArgument =
389
663
        D->hasDefaultArgument() && 
!D->defaultArgumentWasInherited()47
;
390
663
    Hash.AddBoolean(hasDefaultArgument);
391
663
    if (hasDefaultArgument) {
392
44
      AddStmt(D->getDefaultArgument());
393
44
    }
394
663
    Hash.AddBoolean(D->isParameterPack());
395
663
396
663
    Inherited::VisitNonTypeTemplateParmDecl(D);
397
663
  }
398
399
135
  void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
400
135
    // Only care about default arguments as part of the definition.
401
135
    const bool hasDefaultArgument =
402
135
        D->hasDefaultArgument() && 
!D->defaultArgumentWasInherited()40
;
403
135
    Hash.AddBoolean(hasDefaultArgument);
404
135
    if (hasDefaultArgument) {
405
40
      AddTemplateArgument(D->getDefaultArgument().getArgument());
406
40
    }
407
135
    Hash.AddBoolean(D->isParameterPack());
408
135
409
135
    Inherited::VisitTemplateTemplateParmDecl(D);
410
135
  }
411
412
487
  void VisitTemplateDecl(const TemplateDecl *D) {
413
487
    Hash.AddTemplateParameterList(D->getTemplateParameters());
414
487
415
487
    Inherited::VisitTemplateDecl(D);
416
487
  }
417
418
352
  void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
419
352
    Hash.AddBoolean(D->isMemberSpecialization());
420
352
    Inherited::VisitRedeclarableTemplateDecl(D);
421
352
  }
422
423
352
  void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
424
352
    AddDecl(D->getTemplatedDecl());
425
352
    ID.AddInteger(D->getTemplatedDecl()->getODRHash());
426
352
    Inherited::VisitFunctionTemplateDecl(D);
427
352
  }
428
429
2.19k
  void VisitEnumConstantDecl(const EnumConstantDecl *D) {
430
2.19k
    AddStmt(D->getInitExpr());
431
2.19k
    Inherited::VisitEnumConstantDecl(D);
432
2.19k
  }
433
};
434
} // namespace
435
436
// Only allow a small portion of Decl's to be processed.  Remove this once
437
// all Decl's can be handled.
438
160k
bool ODRHash::isWhitelistedDecl(const Decl *D, const DeclContext *Parent) {
439
160k
  if (D->isImplicit()) 
return false48.2k
;
440
112k
  if (D->getDeclContext() != Parent) 
return false14
;
441
112k
442
112k
  switch (D->getKind()) {
443
112k
    default:
444
80.6k
      return false;
445
112k
    case Decl::AccessSpec:
446
31.6k
    case Decl::CXXConstructor:
447
31.6k
    case Decl::CXXDestructor:
448
31.6k
    case Decl::CXXMethod:
449
31.6k
    case Decl::EnumConstant: // Only found in EnumDecl's.
450
31.6k
    case Decl::Field:
451
31.6k
    case Decl::Friend:
452
31.6k
    case Decl::FunctionTemplate:
453
31.6k
    case Decl::StaticAssert:
454
31.6k
    case Decl::TypeAlias:
455
31.6k
    case Decl::Typedef:
456
31.6k
    case Decl::Var:
457
31.6k
      return true;
458
112k
  }
459
112k
}
460
461
203k
void ODRHash::AddSubDecl(const Decl *D) {
462
203k
  assert(D && "Expecting non-null pointer.");
463
203k
464
203k
  ODRDeclVisitor(ID, *this).Visit(D);
465
203k
}
466
467
25.8k
void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
468
25.8k
  assert(Record && Record->hasDefinition() &&
469
25.8k
         "Expected non-null record to be a definition.");
470
25.8k
471
25.8k
  const DeclContext *DC = Record;
472
124k
  while (DC) {
473
100k
    if (isa<ClassTemplateSpecializationDecl>(DC)) {
474
1.58k
      return;
475
1.58k
    }
476
98.9k
    DC = DC->getParent();
477
98.9k
  }
478
25.8k
479
25.8k
  AddDecl(Record);
480
24.2k
481
24.2k
  // Filter out sub-Decls which will not be processed in order to get an
482
24.2k
  // accurate count of Decl's.
483
24.2k
  llvm::SmallVector<const Decl *, 16> Decls;
484
46.1k
  for (Decl *SubDecl : Record->decls()) {
485
46.1k
    if (isWhitelistedDecl(SubDecl, Record)) {
486
11.0k
      Decls.push_back(SubDecl);
487
11.0k
      if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
488
5.77k
        // Compute/Preload ODRHash into FunctionDecl.
489
5.77k
        Function->getODRHash();
490
5.77k
      }
491
11.0k
    }
492
46.1k
  }
493
24.2k
494
24.2k
  ID.AddInteger(Decls.size());
495
24.2k
  for (auto SubDecl : Decls) {
496
11.0k
    AddSubDecl(SubDecl);
497
11.0k
  }
498
24.2k
499
24.2k
  const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
500
24.2k
  AddBoolean(TD);
501
24.2k
  if (TD) {
502
1.60k
    AddTemplateParameterList(TD->getTemplateParameters());
503
1.60k
  }
504
24.2k
505
24.2k
  ID.AddInteger(Record->getNumBases());
506
24.2k
  auto Bases = Record->bases();
507
24.2k
  for (auto Base : Bases) {
508
405
    AddQualType(Base.getType());
509
405
    ID.AddInteger(Base.isVirtual());
510
405
    ID.AddInteger(Base.getAccessSpecifierAsWritten());
511
405
  }
512
24.2k
}
513
514
void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
515
104k
                              bool SkipBody) {
516
104k
  assert(Function && "Expecting non-null pointer.");
517
104k
518
104k
  // Skip functions that are specializations or in specialization context.
519
104k
  const DeclContext *DC = Function;
520
326k
  while (DC) {
521
225k
    if (isa<ClassTemplateSpecializationDecl>(DC)) 
return2.44k
;
522
223k
    if (auto *F = dyn_cast<FunctionDecl>(DC)) {
523
106k
      if (F->isFunctionTemplateSpecialization()) {
524
1.06k
        if (!isa<CXXMethodDecl>(DC)) 
return949
;
525
117
        if (DC->getLexicalParent()->isFileContext()) 
return28
;
526
222k
        // Inline method specializations are the only supported
527
222k
        // specialization for now.
528
222k
      }
529
106k
    }
530
222k
    DC = DC->getParent();
531
222k
  }
532
104k
533
104k
  ID.AddInteger(Function->getDeclKind());
534
101k
535
101k
  const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
536
101k
  AddBoolean(SpecializationArgs);
537
101k
  if (SpecializationArgs) {
538
60
    ID.AddInteger(SpecializationArgs->size());
539
86
    for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
540
86
      AddTemplateArgument(TA);
541
86
    }
542
60
  }
543
101k
544
101k
  if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
545
9.75k
    AddBoolean(Method->isConst());
546
9.75k
    AddBoolean(Method->isVolatile());
547
9.75k
  }
548
101k
549
101k
  ID.AddInteger(Function->getStorageClass());
550
101k
  AddBoolean(Function->isInlineSpecified());
551
101k
  AddBoolean(Function->isVirtualAsWritten());
552
101k
  AddBoolean(Function->isPure());
553
101k
  AddBoolean(Function->isDeletedAsWritten());
554
101k
  AddBoolean(Function->isExplicitlyDefaulted());
555
101k
556
101k
  AddDecl(Function);
557
101k
558
101k
  AddQualType(Function->getReturnType());
559
101k
560
101k
  ID.AddInteger(Function->param_size());
561
101k
  for (auto Param : Function->parameters())
562
169k
    AddSubDecl(Param);
563
101k
564
101k
  if (SkipBody) {
565
14
    AddBoolean(false);
566
14
    return;
567
14
  }
568
101k
569
101k
  const bool HasBody = Function->isThisDeclarationADefinition() &&
570
101k
                       
!Function->isDefaulted()40.4k
&&
!Function->isDeleted()37.2k
&&
571
101k
                       
!Function->isLateTemplateParsed()37.2k
;
572
101k
  AddBoolean(HasBody);
573
101k
  if (!HasBody) {
574
63.8k
    return;
575
63.8k
  }
576
37.2k
577
37.2k
  auto *Body = Function->getBody();
578
37.2k
  AddBoolean(Body);
579
37.2k
  if (Body)
580
37.2k
    AddStmt(Body);
581
37.2k
582
37.2k
  // Filter out sub-Decls which will not be processed in order to get an
583
37.2k
  // accurate count of Decl's.
584
37.2k
  llvm::SmallVector<const Decl *, 16> Decls;
585
111k
  for (Decl *SubDecl : Function->decls()) {
586
111k
    if (isWhitelistedDecl(SubDecl, Function)) {
587
17.8k
      Decls.push_back(SubDecl);
588
17.8k
    }
589
111k
  }
590
37.2k
591
37.2k
  ID.AddInteger(Decls.size());
592
37.2k
  for (auto SubDecl : Decls) {
593
17.8k
    AddSubDecl(SubDecl);
594
17.8k
  }
595
37.2k
}
596
597
342
void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
598
342
  assert(Enum);
599
342
  AddDeclarationName(Enum->getDeclName());
600
342
601
342
  AddBoolean(Enum->isScoped());
602
342
  if (Enum->isScoped())
603
37
    AddBoolean(Enum->isScopedUsingClassTag());
604
342
605
342
  if (Enum->getIntegerTypeSourceInfo())
606
59
    AddQualType(Enum->getIntegerType());
607
342
608
342
  // Filter out sub-Decls which will not be processed in order to get an
609
342
  // accurate count of Decl's.
610
342
  llvm::SmallVector<const Decl *, 16> Decls;
611
2.17k
  for (Decl *SubDecl : Enum->decls()) {
612
2.17k
    if (isWhitelistedDecl(SubDecl, Enum)) {
613
2.17k
      assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
614
2.17k
      Decls.push_back(SubDecl);
615
2.17k
    }
616
2.17k
  }
617
342
618
342
  ID.AddInteger(Decls.size());
619
2.17k
  for (auto SubDecl : Decls) {
620
2.17k
    AddSubDecl(SubDecl);
621
2.17k
  }
622
342
623
342
}
624
625
669k
void ODRHash::AddDecl(const Decl *D) {
626
669k
  assert(D && "Expecting non-null pointer.");
627
669k
  D = D->getCanonicalDecl();
628
669k
629
669k
  const NamedDecl *ND = dyn_cast<NamedDecl>(D);
630
669k
  AddBoolean(ND);
631
669k
  if (!ND) {
632
119
    ID.AddInteger(D->getKind());
633
119
    return;
634
119
  }
635
669k
636
669k
  AddDeclarationName(ND->getDeclName());
637
669k
638
669k
  const auto *Specialization =
639
669k
            dyn_cast<ClassTemplateSpecializationDecl>(D);
640
669k
  AddBoolean(Specialization);
641
669k
  if (Specialization) {
642
561
    const TemplateArgumentList &List = Specialization->getTemplateArgs();
643
561
    ID.AddInteger(List.size());
644
561
    for (const TemplateArgument &TA : List.asArray())
645
942
      AddTemplateArgument(TA);
646
561
  }
647
669k
}
648
649
namespace {
650
// Process a Type pointer.  Add* methods call back into ODRHash while Visit*
651
// methods process the relevant parts of the Type.
652
class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
653
  typedef TypeVisitor<ODRTypeVisitor> Inherited;
654
  llvm::FoldingSetNodeID &ID;
655
  ODRHash &Hash;
656
657
public:
658
  ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
659
1.01M
      : ID(ID), Hash(Hash) {}
660
661
1.37k
  void AddStmt(Stmt *S) {
662
1.37k
    Hash.AddBoolean(S);
663
1.37k
    if (S) {
664
1.37k
      Hash.AddStmt(S);
665
1.37k
    }
666
1.37k
  }
667
668
350k
  void AddDecl(Decl *D) {
669
350k
    Hash.AddBoolean(D);
670
350k
    if (D) {
671
349k
      Hash.AddDecl(D);
672
349k
    }
673
350k
  }
674
675
282k
  void AddQualType(QualType T) {
676
282k
    Hash.AddQualType(T);
677
282k
  }
678
679
323k
  void AddType(const Type *T) {
680
323k
    Hash.AddBoolean(T);
681
323k
    if (T) {
682
323k
      Hash.AddType(T);
683
323k
    }
684
323k
  }
685
686
978
  void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
687
978
    Hash.AddBoolean(NNS);
688
978
    if (NNS) {
689
289
      Hash.AddNestedNameSpecifier(NNS);
690
289
    }
691
978
  }
692
693
117
  void AddIdentifierInfo(const IdentifierInfo *II) {
694
117
    Hash.AddBoolean(II);
695
117
    if (II) {
696
117
      Hash.AddIdentifierInfo(II);
697
117
    }
698
117
  }
699
700
327k
  void VisitQualifiers(Qualifiers Quals) {
701
327k
    ID.AddInteger(Quals.getAsOpaqueValue());
702
327k
  }
703
704
  // Return the RecordType if the typedef only strips away a keyword.
705
  // Otherwise, return the original type.
706
1.01M
  static const Type *RemoveTypedef(const Type *T) {
707
1.01M
    const auto *TypedefT = dyn_cast<TypedefType>(T);
708
1.01M
    if (!TypedefT) {
709
689k
      return T;
710
689k
    }
711
324k
712
324k
    const TypedefNameDecl *D = TypedefT->getDecl();
713
324k
    QualType UnderlyingType = D->getUnderlyingType();
714
324k
715
324k
    if (UnderlyingType.hasLocalQualifiers()) {
716
1
      return T;
717
1
    }
718
324k
719
324k
    const auto *ElaboratedT = dyn_cast<ElaboratedType>(UnderlyingType);
720
324k
    if (!ElaboratedT) {
721
323k
      return T;
722
323k
    }
723
964
724
964
    if (ElaboratedT->getQualifier() != nullptr) {
725
3
      return T;
726
3
    }
727
961
728
961
    QualType NamedType = ElaboratedT->getNamedType();
729
961
    if (NamedType.hasLocalQualifiers()) {
730
0
      return T;
731
0
    }
732
961
733
961
    const auto *RecordT = dyn_cast<RecordType>(NamedType);
734
961
    if (!RecordT) {
735
516
      return T;
736
516
    }
737
445
738
445
    const IdentifierInfo *TypedefII = TypedefT->getDecl()->getIdentifier();
739
445
    const IdentifierInfo *RecordII = RecordT->getDecl()->getIdentifier();
740
445
    if (!TypedefII || !RecordII ||
741
445
        
TypedefII->getName() != RecordII->getName()312
) {
742
137
      return T;
743
137
    }
744
308
745
308
    return RecordT;
746
308
  }
747
748
1.01M
  void Visit(const Type *T) {
749
1.01M
    T = RemoveTypedef(T);
750
1.01M
    ID.AddInteger(T->getTypeClass());
751
1.01M
    Inherited::Visit(T);
752
1.01M
  }
753
754
1.00M
  void VisitType(const Type *T) {}
755
756
147
  void VisitAdjustedType(const AdjustedType *T) {
757
147
    QualType Original = T->getOriginalType();
758
147
    QualType Adjusted = T->getAdjustedType();
759
147
760
147
    // The original type and pointee type can be the same, as in the case of
761
147
    // function pointers decaying to themselves.  Set a bool and only process
762
147
    // the type once, to prevent doubling the work.
763
147
    SplitQualType split = Adjusted.split();
764
147
    if (auto Pointer = dyn_cast<PointerType>(split.Ty)) {
765
147
      if (Pointer->getPointeeType() == Original) {
766
66
        Hash.AddBoolean(true);
767
66
        ID.AddInteger(split.Quals.getAsOpaqueValue());
768
66
        AddQualType(Original);
769
66
        VisitType(T);
770
66
        return;
771
66
      }
772
81
    }
773
81
774
81
    // The original type and pointee type are different, such as in the case
775
81
    // of a array decaying to an element pointer.  Set a bool to false and
776
81
    // process both types.
777
81
    Hash.AddBoolean(false);
778
81
    AddQualType(Original);
779
81
    AddQualType(Adjusted);
780
81
781
81
    VisitType(T);
782
81
  }
783
784
147
  void VisitDecayedType(const DecayedType *T) {
785
147
    // getDecayedType and getPointeeType are derived from getAdjustedType
786
147
    // and don't need to be separately processed.
787
147
    VisitAdjustedType(T);
788
147
  }
789
790
3.95k
  void VisitArrayType(const ArrayType *T) {
791
3.95k
    AddQualType(T->getElementType());
792
3.95k
    ID.AddInteger(T->getSizeModifier());
793
3.95k
    VisitQualifiers(T->getIndexTypeQualifiers());
794
3.95k
    VisitType(T);
795
3.95k
  }
796
1.90k
  void VisitConstantArrayType(const ConstantArrayType *T) {
797
1.90k
    T->getSize().Profile(ID);
798
1.90k
    VisitArrayType(T);
799
1.90k
  }
800
801
386
  void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
802
386
    AddStmt(T->getSizeExpr());
803
386
    VisitArrayType(T);
804
386
  }
805
806
763
  void VisitIncompleteArrayType(const IncompleteArrayType *T) {
807
763
    VisitArrayType(T);
808
763
  }
809
810
903
  void VisitVariableArrayType(const VariableArrayType *T) {
811
903
    AddStmt(T->getSizeExpr());
812
903
    VisitArrayType(T);
813
903
  }
814
815
22
  void VisitAttributedType(const AttributedType *T) {
816
22
    ID.AddInteger(T->getAttrKind());
817
22
    AddQualType(T->getModifiedType());
818
22
    AddQualType(T->getEquivalentType());
819
22
820
22
    VisitType(T);
821
22
  }
822
823
58
  void VisitBlockPointerType(const BlockPointerType *T) {
824
58
    AddQualType(T->getPointeeType());
825
58
    VisitType(T);
826
58
  }
827
828
374k
  void VisitBuiltinType(const BuiltinType *T) {
829
374k
    ID.AddInteger(T->getKind());
830
374k
    VisitType(T);
831
374k
  }
832
833
16
  void VisitComplexType(const ComplexType *T) {
834
16
    AddQualType(T->getElementType());
835
16
    VisitType(T);
836
16
  }
837
838
50
  void VisitDecltypeType(const DecltypeType *T) {
839
50
    AddStmt(T->getUnderlyingExpr());
840
50
    AddQualType(T->getUnderlyingType());
841
50
    VisitType(T);
842
50
  }
843
844
0
  void VisitDependentDecltypeType(const DependentDecltypeType *T) {
845
0
    VisitDecltypeType(T);
846
0
  }
847
848
275
  void VisitDeducedType(const DeducedType *T) {
849
275
    AddQualType(T->getDeducedType());
850
275
    VisitType(T);
851
275
  }
852
853
269
  void VisitAutoType(const AutoType *T) {
854
269
    ID.AddInteger((unsigned)T->getKeyword());
855
269
    VisitDeducedType(T);
856
269
  }
857
858
  void VisitDeducedTemplateSpecializationType(
859
6
      const DeducedTemplateSpecializationType *T) {
860
6
    Hash.AddTemplateName(T->getTemplateName());
861
6
    VisitDeducedType(T);
862
6
  }
863
864
10
  void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) {
865
10
    AddQualType(T->getPointeeType());
866
10
    AddStmt(T->getAddrSpaceExpr());
867
10
    VisitType(T);
868
10
  }
869
870
6
  void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
871
6
    AddQualType(T->getElementType());
872
6
    AddStmt(T->getSizeExpr());
873
6
    VisitType(T);
874
6
  }
875
876
302
  void VisitFunctionType(const FunctionType *T) {
877
302
    AddQualType(T->getReturnType());
878
302
    T->getExtInfo().Profile(ID);
879
302
    Hash.AddBoolean(T->isConst());
880
302
    Hash.AddBoolean(T->isVolatile());
881
302
    Hash.AddBoolean(T->isRestrict());
882
302
    VisitType(T);
883
302
  }
884
885
0
  void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
886
0
    VisitFunctionType(T);
887
0
  }
888
889
302
  void VisitFunctionProtoType(const FunctionProtoType *T) {
890
302
    ID.AddInteger(T->getNumParams());
891
302
    for (auto ParamType : T->getParamTypes())
892
156
      AddQualType(ParamType);
893
302
894
302
    VisitFunctionType(T);
895
302
  }
896
897
3.02k
  void VisitInjectedClassNameType(const InjectedClassNameType *T) {
898
3.02k
    AddDecl(T->getDecl());
899
3.02k
    VisitType(T);
900
3.02k
  }
901
902
143
  void VisitMemberPointerType(const MemberPointerType *T) {
903
143
    AddQualType(T->getPointeeType());
904
143
    AddType(T->getClass());
905
143
    VisitType(T);
906
143
  }
907
908
115
  void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
909
115
    AddQualType(T->getPointeeType());
910
115
    VisitType(T);
911
115
  }
912
913
139
  void VisitObjCObjectType(const ObjCObjectType *T) {
914
139
    AddDecl(T->getInterface());
915
139
916
139
    auto TypeArgs = T->getTypeArgsAsWritten();
917
139
    ID.AddInteger(TypeArgs.size());
918
139
    for (auto Arg : TypeArgs) {
919
8
      AddQualType(Arg);
920
8
    }
921
139
922
139
    auto Protocols = T->getProtocols();
923
139
    ID.AddInteger(Protocols.size());
924
139
    for (auto Protocol : Protocols) {
925
21
      AddDecl(Protocol);
926
21
    }
927
139
928
139
    Hash.AddBoolean(T->isKindOfType());
929
139
930
139
    VisitType(T);
931
139
  }
932
933
61
  void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
934
61
    // This type is handled by the parent type ObjCObjectType.
935
61
    VisitObjCObjectType(T);
936
61
  }
937
938
8
  void VisitObjCTypeParamType(const ObjCTypeParamType *T) {
939
8
    AddDecl(T->getDecl());
940
8
    auto Protocols = T->getProtocols();
941
8
    ID.AddInteger(Protocols.size());
942
8
    for (auto Protocol : Protocols) {
943
7
      AddDecl(Protocol);
944
7
    }
945
8
946
8
    VisitType(T);
947
8
  }
948
949
36
  void VisitPackExpansionType(const PackExpansionType *T) {
950
36
    AddQualType(T->getPattern());
951
36
    VisitType(T);
952
36
  }
953
954
209
  void VisitParenType(const ParenType *T) {
955
209
    AddQualType(T->getInnerType());
956
209
    VisitType(T);
957
209
  }
958
959
12
  void VisitPipeType(const PipeType *T) {
960
12
    AddQualType(T->getElementType());
961
12
    Hash.AddBoolean(T->isReadOnly());
962
12
    VisitType(T);
963
12
  }
964
965
15.8k
  void VisitPointerType(const PointerType *T) {
966
15.8k
    AddQualType(T->getPointeeType());
967
15.8k
    VisitType(T);
968
15.8k
  }
969
970
4.49k
  void VisitReferenceType(const ReferenceType *T) {
971
4.49k
    AddQualType(T->getPointeeTypeAsWritten());
972
4.49k
    VisitType(T);
973
4.49k
  }
974
975
3.71k
  void VisitLValueReferenceType(const LValueReferenceType *T) {
976
3.71k
    VisitReferenceType(T);
977
3.71k
  }
978
979
785
  void VisitRValueReferenceType(const RValueReferenceType *T) {
980
785
    VisitReferenceType(T);
981
785
  }
982
983
  void
984
0
  VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
985
0
    AddType(T->getReplacedParameter());
986
0
    Hash.AddTemplateArgument(T->getArgumentPack());
987
0
    VisitType(T);
988
0
  }
989
990
12
  void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
991
12
    AddType(T->getReplacedParameter());
992
12
    AddQualType(T->getReplacementType());
993
12
    VisitType(T);
994
12
  }
995
996
14.4k
  void VisitTagType(const TagType *T) {
997
14.4k
    AddDecl(T->getDecl());
998
14.4k
    VisitType(T);
999
14.4k
  }
1000
1001
13.4k
  void VisitRecordType(const RecordType *T) { VisitTagType(T); }
1002
1.00k
  void VisitEnumType(const EnumType *T) { VisitTagType(T); }
1003
1004
4.70k
  void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
1005
4.70k
    ID.AddInteger(T->getNumArgs());
1006
5.18k
    for (const auto &TA : T->template_arguments()) {
1007
5.18k
      Hash.AddTemplateArgument(TA);
1008
5.18k
    }
1009
4.70k
    Hash.AddTemplateName(T->getTemplateName());
1010
4.70k
    VisitType(T);
1011
4.70k
  }
1012
1013
8.98k
  void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1014
8.98k
    ID.AddInteger(T->getDepth());
1015
8.98k
    ID.AddInteger(T->getIndex());
1016
8.98k
    Hash.AddBoolean(T->isParameterPack());
1017
8.98k
    AddDecl(T->getDecl());
1018
8.98k
  }
1019
1020
323k
  void VisitTypedefType(const TypedefType *T) {
1021
323k
    AddDecl(T->getDecl());
1022
323k
    QualType UnderlyingType = T->getDecl()->getUnderlyingType();
1023
323k
    VisitQualifiers(UnderlyingType.getQualifiers());
1024
325k
    while (true) {
1025
325k
      if (const TypedefType *Underlying =
1026
1.48k
              dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
1027
1.48k
        UnderlyingType = Underlying->getDecl()->getUnderlyingType();
1028
1.48k
        continue;
1029
1.48k
      }
1030
324k
      if (const ElaboratedType *Underlying =
1031
656
              dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
1032
656
        UnderlyingType = Underlying->getNamedType();
1033
656
        continue;
1034
656
      }
1035
323k
1036
323k
      break;
1037
323k
    }
1038
323k
    AddType(UnderlyingType.getTypePtr());
1039
323k
    VisitType(T);
1040
323k
  }
1041
1042
16
  void VisitTypeOfExprType(const TypeOfExprType *T) {
1043
16
    AddStmt(T->getUnderlyingExpr());
1044
16
    Hash.AddBoolean(T->isSugared());
1045
16
    if (T->isSugared())
1046
15
      AddQualType(T->desugar());
1047
16
1048
16
    VisitType(T);
1049
16
  }
1050
20
  void VisitTypeOfType(const TypeOfType *T) {
1051
20
    AddQualType(T->getUnderlyingType());
1052
20
    VisitType(T);
1053
20
  }
1054
1055
978
  void VisitTypeWithKeyword(const TypeWithKeyword *T) {
1056
978
    ID.AddInteger(T->getKeyword());
1057
978
    VisitType(T);
1058
978
  };
1059
1060
105
  void VisitDependentNameType(const DependentNameType *T) {
1061
105
    AddNestedNameSpecifier(T->getQualifier());
1062
105
    AddIdentifierInfo(T->getIdentifier());
1063
105
    VisitTypeWithKeyword(T);
1064
105
  }
1065
1066
  void VisitDependentTemplateSpecializationType(
1067
12
      const DependentTemplateSpecializationType *T) {
1068
12
    AddIdentifierInfo(T->getIdentifier());
1069
12
    AddNestedNameSpecifier(T->getQualifier());
1070
12
    ID.AddInteger(T->getNumArgs());
1071
12
    for (const auto &TA : T->template_arguments()) {
1072
12
      Hash.AddTemplateArgument(TA);
1073
12
    }
1074
12
    VisitTypeWithKeyword(T);
1075
12
  }
1076
1077
861
  void VisitElaboratedType(const ElaboratedType *T) {
1078
861
    AddNestedNameSpecifier(T->getQualifier());
1079
861
    AddQualType(T->getNamedType());
1080
861
    VisitTypeWithKeyword(T);
1081
861
  }
1082
1083
21
  void VisitUnaryTransformType(const UnaryTransformType *T) {
1084
21
    AddQualType(T->getUnderlyingType());
1085
21
    AddQualType(T->getBaseType());
1086
21
    VisitType(T);
1087
21
  }
1088
1089
7
  void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
1090
7
    AddDecl(T->getDecl());
1091
7
    VisitType(T);
1092
7
  }
1093
1094
255k
  void VisitVectorType(const VectorType *T) {
1095
255k
    AddQualType(T->getElementType());
1096
255k
    ID.AddInteger(T->getNumElements());
1097
255k
    ID.AddInteger(T->getVectorKind());
1098
255k
    VisitType(T);
1099
255k
  }
1100
1101
83.9k
  void VisitExtVectorType(const ExtVectorType * T) {
1102
83.9k
    VisitVectorType(T);
1103
83.9k
  }
1104
};
1105
} // namespace
1106
1107
1.01M
void ODRHash::AddType(const Type *T) {
1108
1.01M
  assert(T && "Expecting non-null pointer.");
1109
1.01M
  ODRTypeVisitor(ID, *this).Visit(T);
1110
1.01M
}
1111
1112
688k
void ODRHash::AddQualType(QualType T) {
1113
688k
  AddBoolean(T.isNull());
1114
688k
  if (T.isNull())
1115
67
    return;
1116
688k
  SplitQualType split = T.split();
1117
688k
  ID.AddInteger(split.Quals.getAsOpaqueValue());
1118
688k
  AddType(split.Ty);
1119
688k
}
1120
1121
4.65M
void ODRHash::AddBoolean(bool Value) {
1122
4.65M
  Bools.push_back(Value);
1123
4.65M
}