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
22using namespace clang;
23
24void ODRHash::AddStmt(const Stmt *S) {
25 assert(S && "Expecting non-null pointer.");
26 S->ProcessODRHash(ID, Hash&: *this);
27}
28
29void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
30 assert(II && "Expecting non-null pointer.");
31 ID.AddString(String: II->getName());
32}
33
34void ODRHash::AddDeclarationName(DeclarationName Name, bool TreatAsDecl) {
35 if (TreatAsDecl)
36 // Matches the NamedDecl check in AddDecl
37 AddBoolean(value: true);
38
39 AddDeclarationNameImpl(Name);
40
41 if (TreatAsDecl)
42 // Matches the ClassTemplateSpecializationDecl check in AddDecl
43 AddBoolean(value: false);
44}
45
46void ODRHash::AddDeclarationNameImpl(DeclarationName Name) {
47 // Index all DeclarationName and use index numbers to refer to them.
48 auto Result = DeclNameMap.insert(KV: std::make_pair(x&: Name, y: DeclNameMap.size()));
49 ID.AddInteger(I: Result.first->second);
50 if (!Result.second) {
51 // If found in map, the DeclarationName has previously been processed.
52 return;
53 }
54
55 // First time processing each DeclarationName, also process its details.
56 AddBoolean(value: Name.isEmpty());
57 if (Name.isEmpty())
58 return;
59
60 auto Kind = Name.getNameKind();
61 ID.AddInteger(I: Kind);
62 switch (Kind) {
63 case DeclarationName::Identifier:
64 AddIdentifierInfo(II: Name.getAsIdentifierInfo());
65 break;
66 case DeclarationName::ObjCZeroArgSelector:
67 case DeclarationName::ObjCOneArgSelector:
68 case DeclarationName::ObjCMultiArgSelector: {
69 Selector S = Name.getObjCSelector();
70 AddBoolean(value: S.isNull());
71 AddBoolean(value: S.isKeywordSelector());
72 AddBoolean(value: S.isUnarySelector());
73 unsigned NumArgs = S.getNumArgs();
74 ID.AddInteger(I: NumArgs);
75 // Compare all selector slots. For selectors with arguments it means all arg
76 // slots. And if there are no arguments, compare the first-and-only slot.
77 unsigned SlotsToCheck = NumArgs > 0 ? NumArgs : 1;
78 for (unsigned i = 0; i < SlotsToCheck; ++i) {
79 const IdentifierInfo *II = S.getIdentifierInfoForSlot(argIndex: i);
80 AddBoolean(value: II);
81 if (II) {
82 AddIdentifierInfo(II);
83 }
84 }
85 break;
86 }
87 case DeclarationName::CXXConstructorName:
88 case DeclarationName::CXXDestructorName:
89 AddQualType(T: Name.getCXXNameType());
90 break;
91 case DeclarationName::CXXOperatorName:
92 ID.AddInteger(I: Name.getCXXOverloadedOperator());
93 break;
94 case DeclarationName::CXXLiteralOperatorName:
95 AddIdentifierInfo(II: Name.getCXXLiteralIdentifier());
96 break;
97 case DeclarationName::CXXConversionFunctionName:
98 AddQualType(T: Name.getCXXNameType());
99 break;
100 case DeclarationName::CXXUsingDirective:
101 break;
102 case DeclarationName::CXXDeductionGuideName: {
103 auto *Template = Name.getCXXDeductionGuideTemplate();
104 AddBoolean(value: Template);
105 if (Template) {
106 AddDecl(Template);
107 }
108 }
109 }
110}
111
112void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
113 assert(NNS && "Expecting non-null pointer.");
114 const auto *Prefix = NNS->getPrefix();
115 AddBoolean(value: Prefix);
116 if (Prefix) {
117 AddNestedNameSpecifier(NNS: Prefix);
118 }
119 auto Kind = NNS->getKind();
120 ID.AddInteger(I: Kind);
121 switch (Kind) {
122 case NestedNameSpecifier::Identifier:
123 AddIdentifierInfo(II: NNS->getAsIdentifier());
124 break;
125 case NestedNameSpecifier::Namespace:
126 AddDecl(NNS->getAsNamespace());
127 break;
128 case NestedNameSpecifier::NamespaceAlias:
129 AddDecl(NNS->getAsNamespaceAlias());
130 break;
131 case NestedNameSpecifier::TypeSpec:
132 case NestedNameSpecifier::TypeSpecWithTemplate:
133 AddType(T: NNS->getAsType());
134 break;
135 case NestedNameSpecifier::Global:
136 case NestedNameSpecifier::Super:
137 break;
138 }
139}
140
141void ODRHash::AddTemplateName(TemplateName Name) {
142 auto Kind = Name.getKind();
143 ID.AddInteger(I: Kind);
144
145 switch (Kind) {
146 case TemplateName::Template:
147 AddDecl(Name.getAsTemplateDecl());
148 break;
149 // TODO: Support these cases.
150 case TemplateName::OverloadedTemplate:
151 case TemplateName::AssumedTemplate:
152 case TemplateName::QualifiedTemplate:
153 case TemplateName::DependentTemplate:
154 case TemplateName::SubstTemplateTemplateParm:
155 case TemplateName::SubstTemplateTemplateParmPack:
156 case TemplateName::UsingTemplate:
157 break;
158 }
159}
160
161void ODRHash::AddTemplateArgument(TemplateArgument TA) {
162 const auto Kind = TA.getKind();
163 ID.AddInteger(I: Kind);
164
165 switch (Kind) {
166 case TemplateArgument::Null:
167 llvm_unreachable("Expected valid TemplateArgument");
168 case TemplateArgument::Type:
169 AddQualType(T: TA.getAsType());
170 break;
171 case TemplateArgument::Declaration:
172 AddDecl(TA.getAsDecl());
173 break;
174 case TemplateArgument::NullPtr:
175 ID.AddPointer(Ptr: nullptr);
176 break;
177 case TemplateArgument::Integral: {
178 // There are integrals (e.g.: _BitInt(128)) that cannot be represented as
179 // any builtin integral type, so we use the hash of APSInt instead.
180 TA.getAsIntegral().Profile(ID);
181 break;
182 }
183 case TemplateArgument::StructuralValue:
184 AddQualType(T: TA.getStructuralValueType());
185 AddStructuralValue(TA.getAsStructuralValue());
186 break;
187 case TemplateArgument::Template:
188 case TemplateArgument::TemplateExpansion:
189 AddTemplateName(Name: TA.getAsTemplateOrTemplatePattern());
190 break;
191 case TemplateArgument::Expression:
192 AddStmt(TA.getAsExpr());
193 break;
194 case TemplateArgument::Pack:
195 ID.AddInteger(I: TA.pack_size());
196 for (auto SubTA : TA.pack_elements()) {
197 AddTemplateArgument(TA: SubTA);
198 }
199 break;
200 }
201}
202
203void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
204 assert(TPL && "Expecting non-null pointer.");
205
206 ID.AddInteger(I: TPL->size());
207 for (auto *ND : TPL->asArray()) {
208 AddSubDecl(ND);
209 }
210}
211
212void ODRHash::clear() {
213 DeclNameMap.clear();
214 Bools.clear();
215 ID.clear();
216}
217
218unsigned ODRHash::CalculateHash() {
219 // Append the bools to the end of the data segment backwards. This allows
220 // for the bools data to be compressed 32 times smaller compared to using
221 // ID.AddBoolean
222 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
223 const unsigned size = Bools.size();
224 const unsigned remainder = size % unsigned_bits;
225 const unsigned loops = size / unsigned_bits;
226 auto I = Bools.rbegin();
227 unsigned value = 0;
228 for (unsigned i = 0; i < remainder; ++i) {
229 value <<= 1;
230 value |= *I;
231 ++I;
232 }
233 ID.AddInteger(I: value);
234
235 for (unsigned i = 0; i < loops; ++i) {
236 value = 0;
237 for (unsigned j = 0; j < unsigned_bits; ++j) {
238 value <<= 1;
239 value |= *I;
240 ++I;
241 }
242 ID.AddInteger(I: value);
243 }
244
245 assert(I == Bools.rend());
246 Bools.clear();
247 return ID.ComputeHash();
248}
249
250namespace {
251// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
252// methods process the relevant parts of the Decl.
253class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
254 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
255 llvm::FoldingSetNodeID &ID;
256 ODRHash &Hash;
257
258public:
259 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
260 : ID(ID), Hash(Hash) {}
261
262 void AddStmt(const Stmt *S) {
263 Hash.AddBoolean(value: S);
264 if (S) {
265 Hash.AddStmt(S);
266 }
267 }
268
269 void AddIdentifierInfo(const IdentifierInfo *II) {
270 Hash.AddBoolean(value: II);
271 if (II) {
272 Hash.AddIdentifierInfo(II);
273 }
274 }
275
276 void AddQualType(QualType T) {
277 Hash.AddQualType(T);
278 }
279
280 void AddDecl(const Decl *D) {
281 Hash.AddBoolean(value: D);
282 if (D) {
283 Hash.AddDecl(D);
284 }
285 }
286
287 void AddTemplateArgument(TemplateArgument TA) {
288 Hash.AddTemplateArgument(TA);
289 }
290
291 void Visit(const Decl *D) {
292 ID.AddInteger(I: D->getKind());
293 Inherited::Visit(D);
294 }
295
296 void VisitNamedDecl(const NamedDecl *D) {
297 Hash.AddDeclarationName(Name: D->getDeclName());
298 Inherited::VisitNamedDecl(D);
299 }
300
301 void VisitValueDecl(const ValueDecl *D) {
302 if (auto *DD = dyn_cast<DeclaratorDecl>(Val: D); DD && DD->getTypeSourceInfo())
303 AddQualType(T: DD->getTypeSourceInfo()->getType());
304
305 Inherited::VisitValueDecl(D);
306 }
307
308 void VisitVarDecl(const VarDecl *D) {
309 Hash.AddBoolean(value: D->isStaticLocal());
310 Hash.AddBoolean(value: D->isConstexpr());
311 const bool HasInit = D->hasInit();
312 Hash.AddBoolean(value: HasInit);
313 if (HasInit) {
314 AddStmt(D->getInit());
315 }
316 Inherited::VisitVarDecl(D);
317 }
318
319 void VisitParmVarDecl(const ParmVarDecl *D) {
320 // TODO: Handle default arguments.
321 Inherited::VisitParmVarDecl(D);
322 }
323
324 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
325 ID.AddInteger(D->getAccess());
326 Inherited::VisitAccessSpecDecl(D);
327 }
328
329 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
330 AddStmt(D->getAssertExpr());
331 AddStmt(D->getMessage());
332
333 Inherited::VisitStaticAssertDecl(D);
334 }
335
336 void VisitFieldDecl(const FieldDecl *D) {
337 const bool IsBitfield = D->isBitField();
338 Hash.AddBoolean(value: IsBitfield);
339
340 if (IsBitfield) {
341 AddStmt(D->getBitWidth());
342 }
343
344 Hash.AddBoolean(value: D->isMutable());
345 AddStmt(D->getInClassInitializer());
346
347 Inherited::VisitFieldDecl(D);
348 }
349
350 void VisitObjCIvarDecl(const ObjCIvarDecl *D) {
351 ID.AddInteger(I: D->getCanonicalAccessControl());
352 Inherited::VisitObjCIvarDecl(D);
353 }
354
355 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
356 ID.AddInteger(I: D->getPropertyAttributes());
357 ID.AddInteger(I: D->getPropertyImplementation());
358 AddQualType(T: D->getTypeSourceInfo()->getType());
359 AddDecl(D);
360
361 Inherited::VisitObjCPropertyDecl(D);
362 }
363
364 void VisitFunctionDecl(const FunctionDecl *D) {
365 // Handled by the ODRHash for FunctionDecl
366 ID.AddInteger(I: D->getODRHash());
367
368 Inherited::VisitFunctionDecl(D);
369 }
370
371 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
372 // Handled by the ODRHash for FunctionDecl
373
374 Inherited::VisitCXXMethodDecl(D);
375 }
376
377 void VisitObjCMethodDecl(const ObjCMethodDecl *Method) {
378 ID.AddInteger(Method->getDeclKind());
379 Hash.AddBoolean(value: Method->isInstanceMethod()); // false if class method
380 Hash.AddBoolean(value: Method->isVariadic());
381 Hash.AddBoolean(value: Method->isSynthesizedAccessorStub());
382 Hash.AddBoolean(value: Method->isDefined());
383 Hash.AddBoolean(value: Method->isDirectMethod());
384 Hash.AddBoolean(value: Method->isThisDeclarationADesignatedInitializer());
385 Hash.AddBoolean(value: Method->hasSkippedBody());
386
387 ID.AddInteger(I: llvm::to_underlying(E: Method->getImplementationControl()));
388 ID.AddInteger(I: Method->getMethodFamily());
389 ImplicitParamDecl *Cmd = Method->getCmdDecl();
390 Hash.AddBoolean(value: Cmd);
391 if (Cmd)
392 ID.AddInteger(I: llvm::to_underlying(E: Cmd->getParameterKind()));
393
394 ImplicitParamDecl *Self = Method->getSelfDecl();
395 Hash.AddBoolean(value: Self);
396 if (Self)
397 ID.AddInteger(I: llvm::to_underlying(E: Self->getParameterKind()));
398
399 AddDecl(Method);
400
401 if (Method->getReturnTypeSourceInfo())
402 AddQualType(T: Method->getReturnTypeSourceInfo()->getType());
403
404 ID.AddInteger(I: Method->param_size());
405 for (auto Param : Method->parameters())
406 Hash.AddSubDecl(Param);
407
408 if (Method->hasBody()) {
409 const bool IsDefinition = Method->isThisDeclarationADefinition();
410 Hash.AddBoolean(value: IsDefinition);
411 if (IsDefinition) {
412 Stmt *Body = Method->getBody();
413 Hash.AddBoolean(value: Body);
414 if (Body)
415 AddStmt(S: Body);
416
417 // Filter out sub-Decls which will not be processed in order to get an
418 // accurate count of Decl's.
419 llvm::SmallVector<const Decl *, 16> Decls;
420 for (Decl *SubDecl : Method->decls())
421 if (ODRHash::isSubDeclToBeProcessed(SubDecl, Method))
422 Decls.push_back(SubDecl);
423
424 ID.AddInteger(I: Decls.size());
425 for (auto SubDecl : Decls)
426 Hash.AddSubDecl(D: SubDecl);
427 }
428 } else {
429 Hash.AddBoolean(value: false);
430 }
431
432 Inherited::VisitObjCMethodDecl(Method);
433 }
434
435 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
436 AddQualType(T: D->getUnderlyingType());
437
438 Inherited::VisitTypedefNameDecl(D);
439 }
440
441 void VisitTypedefDecl(const TypedefDecl *D) {
442 Inherited::VisitTypedefDecl(D);
443 }
444
445 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
446 Inherited::VisitTypeAliasDecl(D);
447 }
448
449 void VisitFriendDecl(const FriendDecl *D) {
450 TypeSourceInfo *TSI = D->getFriendType();
451 Hash.AddBoolean(value: TSI);
452 if (TSI) {
453 AddQualType(T: TSI->getType());
454 } else {
455 AddDecl(D->getFriendDecl());
456 }
457 }
458
459 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
460 // Only care about default arguments as part of the definition.
461 const bool hasDefaultArgument =
462 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
463 Hash.AddBoolean(value: hasDefaultArgument);
464 if (hasDefaultArgument) {
465 AddTemplateArgument(TA: D->getDefaultArgument());
466 }
467 Hash.AddBoolean(value: D->isParameterPack());
468
469 const TypeConstraint *TC = D->getTypeConstraint();
470 Hash.AddBoolean(value: TC != nullptr);
471 if (TC)
472 AddStmt(TC->getImmediatelyDeclaredConstraint());
473
474 Inherited::VisitTemplateTypeParmDecl(D);
475 }
476
477 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
478 // Only care about default arguments as part of the definition.
479 const bool hasDefaultArgument =
480 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
481 Hash.AddBoolean(value: hasDefaultArgument);
482 if (hasDefaultArgument) {
483 AddStmt(D->getDefaultArgument());
484 }
485 Hash.AddBoolean(value: D->isParameterPack());
486
487 Inherited::VisitNonTypeTemplateParmDecl(D);
488 }
489
490 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
491 // Only care about default arguments as part of the definition.
492 const bool hasDefaultArgument =
493 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
494 Hash.AddBoolean(value: hasDefaultArgument);
495 if (hasDefaultArgument) {
496 AddTemplateArgument(TA: D->getDefaultArgument().getArgument());
497 }
498 Hash.AddBoolean(value: D->isParameterPack());
499
500 Inherited::VisitTemplateTemplateParmDecl(D);
501 }
502
503 void VisitTemplateDecl(const TemplateDecl *D) {
504 Hash.AddTemplateParameterList(TPL: D->getTemplateParameters());
505
506 Inherited::VisitTemplateDecl(D);
507 }
508
509 void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
510 Hash.AddBoolean(value: D->isMemberSpecialization());
511 Inherited::VisitRedeclarableTemplateDecl(D);
512 }
513
514 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
515 AddDecl(D->getTemplatedDecl());
516 ID.AddInteger(I: D->getTemplatedDecl()->getODRHash());
517 Inherited::VisitFunctionTemplateDecl(D);
518 }
519
520 void VisitEnumConstantDecl(const EnumConstantDecl *D) {
521 AddStmt(D->getInitExpr());
522 Inherited::VisitEnumConstantDecl(D);
523 }
524};
525} // namespace
526
527// Only allow a small portion of Decl's to be processed. Remove this once
528// all Decl's can be handled.
529bool ODRHash::isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent) {
530 if (D->isImplicit()) return false;
531 if (D->getDeclContext() != Parent) return false;
532
533 switch (D->getKind()) {
534 default:
535 return false;
536 case Decl::AccessSpec:
537 case Decl::CXXConstructor:
538 case Decl::CXXDestructor:
539 case Decl::CXXMethod:
540 case Decl::EnumConstant: // Only found in EnumDecl's.
541 case Decl::Field:
542 case Decl::Friend:
543 case Decl::FunctionTemplate:
544 case Decl::StaticAssert:
545 case Decl::TypeAlias:
546 case Decl::Typedef:
547 case Decl::Var:
548 case Decl::ObjCMethod:
549 case Decl::ObjCIvar:
550 case Decl::ObjCProperty:
551 return true;
552 }
553}
554
555void ODRHash::AddSubDecl(const Decl *D) {
556 assert(D && "Expecting non-null pointer.");
557
558 ODRDeclVisitor(ID, *this).Visit(D);
559}
560
561void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
562 assert(Record && Record->hasDefinition() &&
563 "Expected non-null record to be a definition.");
564
565 const DeclContext *DC = Record;
566 while (DC) {
567 if (isa<ClassTemplateSpecializationDecl>(Val: DC)) {
568 return;
569 }
570 DC = DC->getParent();
571 }
572
573 AddDecl(Record);
574
575 // Filter out sub-Decls which will not be processed in order to get an
576 // accurate count of Decl's.
577 llvm::SmallVector<const Decl *, 16> Decls;
578 for (Decl *SubDecl : Record->decls()) {
579 if (isSubDeclToBeProcessed(SubDecl, Record)) {
580 Decls.push_back(SubDecl);
581 if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
582 // Compute/Preload ODRHash into FunctionDecl.
583 Function->getODRHash();
584 }
585 }
586 }
587
588 ID.AddInteger(I: Decls.size());
589 for (auto SubDecl : Decls) {
590 AddSubDecl(D: SubDecl);
591 }
592
593 const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
594 AddBoolean(value: TD);
595 if (TD) {
596 AddTemplateParameterList(TPL: TD->getTemplateParameters());
597 }
598
599 ID.AddInteger(I: Record->getNumBases());
600 auto Bases = Record->bases();
601 for (const auto &Base : Bases) {
602 AddQualType(T: Base.getTypeSourceInfo()->getType());
603 ID.AddInteger(I: Base.isVirtual());
604 ID.AddInteger(I: Base.getAccessSpecifierAsWritten());
605 }
606}
607
608void ODRHash::AddRecordDecl(const RecordDecl *Record) {
609 assert(!isa<CXXRecordDecl>(Record) &&
610 "For CXXRecordDecl should call AddCXXRecordDecl.");
611 AddDecl(Record);
612
613 // Filter out sub-Decls which will not be processed in order to get an
614 // accurate count of Decl's.
615 llvm::SmallVector<const Decl *, 16> Decls;
616 for (Decl *SubDecl : Record->decls()) {
617 if (isSubDeclToBeProcessed(SubDecl, Record))
618 Decls.push_back(SubDecl);
619 }
620
621 ID.AddInteger(I: Decls.size());
622 for (const Decl *SubDecl : Decls)
623 AddSubDecl(D: SubDecl);
624}
625
626void ODRHash::AddObjCInterfaceDecl(const ObjCInterfaceDecl *IF) {
627 AddDecl(IF);
628
629 auto *SuperClass = IF->getSuperClass();
630 AddBoolean(value: SuperClass);
631 if (SuperClass)
632 ID.AddInteger(I: SuperClass->getODRHash());
633
634 // Hash referenced protocols.
635 ID.AddInteger(I: IF->getReferencedProtocols().size());
636 for (const ObjCProtocolDecl *RefP : IF->protocols()) {
637 // Hash the name only as a referenced protocol can be a forward declaration.
638 AddDeclarationName(Name: RefP->getDeclName());
639 }
640
641 // Filter out sub-Decls which will not be processed in order to get an
642 // accurate count of Decl's.
643 llvm::SmallVector<const Decl *, 16> Decls;
644 for (Decl *SubDecl : IF->decls())
645 if (isSubDeclToBeProcessed(SubDecl, IF))
646 Decls.push_back(SubDecl);
647
648 ID.AddInteger(I: Decls.size());
649 for (auto *SubDecl : Decls)
650 AddSubDecl(D: SubDecl);
651}
652
653void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
654 bool SkipBody) {
655 assert(Function && "Expecting non-null pointer.");
656
657 // Skip functions that are specializations or in specialization context.
658 const DeclContext *DC = Function;
659 while (DC) {
660 if (isa<ClassTemplateSpecializationDecl>(Val: DC)) return;
661 if (auto *F = dyn_cast<FunctionDecl>(DC)) {
662 if (F->isFunctionTemplateSpecialization()) {
663 if (!isa<CXXMethodDecl>(Val: DC)) return;
664 if (DC->getLexicalParent()->isFileContext()) return;
665 // Skip class scope explicit function template specializations,
666 // as they have not yet been instantiated.
667 if (F->getDependentSpecializationInfo())
668 return;
669 // Inline method specializations are the only supported
670 // specialization for now.
671 }
672 }
673 DC = DC->getParent();
674 }
675
676 ID.AddInteger(Function->getDeclKind());
677
678 const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
679 AddBoolean(value: SpecializationArgs);
680 if (SpecializationArgs) {
681 ID.AddInteger(I: SpecializationArgs->size());
682 for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
683 AddTemplateArgument(TA);
684 }
685 }
686
687 if (const auto *Method = dyn_cast<CXXMethodDecl>(Val: Function)) {
688 AddBoolean(value: Method->isConst());
689 AddBoolean(value: Method->isVolatile());
690 }
691
692 ID.AddInteger(I: Function->getStorageClass());
693 AddBoolean(value: Function->isInlineSpecified());
694 AddBoolean(value: Function->isVirtualAsWritten());
695 AddBoolean(value: Function->isPureVirtual());
696 AddBoolean(value: Function->isDeletedAsWritten());
697 AddBoolean(value: Function->isExplicitlyDefaulted());
698
699 StringLiteral *DeletedMessage = Function->getDeletedMessage();
700 AddBoolean(value: DeletedMessage);
701
702 if (DeletedMessage)
703 ID.AddString(String: DeletedMessage->getBytes());
704
705 AddDecl(Function);
706
707 AddQualType(T: Function->getReturnType());
708
709 ID.AddInteger(I: Function->param_size());
710 for (auto *Param : Function->parameters())
711 AddSubDecl(Param);
712
713 if (SkipBody) {
714 AddBoolean(value: false);
715 return;
716 }
717
718 const bool HasBody = Function->isThisDeclarationADefinition() &&
719 !Function->isDefaulted() && !Function->isDeleted() &&
720 !Function->isLateTemplateParsed();
721 AddBoolean(value: HasBody);
722 if (!HasBody) {
723 return;
724 }
725
726 auto *Body = Function->getBody();
727 AddBoolean(value: Body);
728 if (Body)
729 AddStmt(S: Body);
730
731 // Filter out sub-Decls which will not be processed in order to get an
732 // accurate count of Decl's.
733 llvm::SmallVector<const Decl *, 16> Decls;
734 for (Decl *SubDecl : Function->decls()) {
735 if (isSubDeclToBeProcessed(SubDecl, Function)) {
736 Decls.push_back(SubDecl);
737 }
738 }
739
740 ID.AddInteger(I: Decls.size());
741 for (auto SubDecl : Decls) {
742 AddSubDecl(D: SubDecl);
743 }
744}
745
746void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
747 assert(Enum);
748 AddDeclarationName(Name: Enum->getDeclName());
749
750 AddBoolean(value: Enum->isScoped());
751 if (Enum->isScoped())
752 AddBoolean(value: Enum->isScopedUsingClassTag());
753
754 if (Enum->getIntegerTypeSourceInfo())
755 AddQualType(T: Enum->getIntegerType().getCanonicalType());
756
757 // Filter out sub-Decls which will not be processed in order to get an
758 // accurate count of Decl's.
759 llvm::SmallVector<const Decl *, 16> Decls;
760 for (Decl *SubDecl : Enum->decls()) {
761 if (isSubDeclToBeProcessed(SubDecl, Enum)) {
762 assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
763 Decls.push_back(SubDecl);
764 }
765 }
766
767 ID.AddInteger(I: Decls.size());
768 for (auto SubDecl : Decls) {
769 AddSubDecl(D: SubDecl);
770 }
771
772}
773
774void ODRHash::AddObjCProtocolDecl(const ObjCProtocolDecl *P) {
775 AddDecl(P);
776
777 // Hash referenced protocols.
778 ID.AddInteger(I: P->getReferencedProtocols().size());
779 for (const ObjCProtocolDecl *RefP : P->protocols()) {
780 // Hash the name only as a referenced protocol can be a forward declaration.
781 AddDeclarationName(Name: RefP->getDeclName());
782 }
783
784 // Filter out sub-Decls which will not be processed in order to get an
785 // accurate count of Decl's.
786 llvm::SmallVector<const Decl *, 16> Decls;
787 for (Decl *SubDecl : P->decls()) {
788 if (isSubDeclToBeProcessed(SubDecl, P)) {
789 Decls.push_back(SubDecl);
790 }
791 }
792
793 ID.AddInteger(I: Decls.size());
794 for (auto *SubDecl : Decls) {
795 AddSubDecl(D: SubDecl);
796 }
797}
798
799void ODRHash::AddDecl(const Decl *D) {
800 assert(D && "Expecting non-null pointer.");
801 D = D->getCanonicalDecl();
802
803 const NamedDecl *ND = dyn_cast<NamedDecl>(Val: D);
804 AddBoolean(value: ND);
805 if (!ND) {
806 ID.AddInteger(I: D->getKind());
807 return;
808 }
809
810 AddDeclarationName(Name: ND->getDeclName());
811
812 const auto *Specialization =
813 dyn_cast<ClassTemplateSpecializationDecl>(Val: D);
814 AddBoolean(value: Specialization);
815 if (Specialization) {
816 const TemplateArgumentList &List = Specialization->getTemplateArgs();
817 ID.AddInteger(I: List.size());
818 for (const TemplateArgument &TA : List.asArray())
819 AddTemplateArgument(TA);
820 }
821}
822
823namespace {
824// Process a Type pointer. Add* methods call back into ODRHash while Visit*
825// methods process the relevant parts of the Type.
826class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
827 typedef TypeVisitor<ODRTypeVisitor> Inherited;
828 llvm::FoldingSetNodeID &ID;
829 ODRHash &Hash;
830
831public:
832 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
833 : ID(ID), Hash(Hash) {}
834
835 void AddStmt(Stmt *S) {
836 Hash.AddBoolean(value: S);
837 if (S) {
838 Hash.AddStmt(S);
839 }
840 }
841
842 void AddDecl(const Decl *D) {
843 Hash.AddBoolean(value: D);
844 if (D) {
845 Hash.AddDecl(D);
846 }
847 }
848
849 void AddQualType(QualType T) {
850 Hash.AddQualType(T);
851 }
852
853 void AddType(const Type *T) {
854 Hash.AddBoolean(value: T);
855 if (T) {
856 Hash.AddType(T);
857 }
858 }
859
860 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
861 Hash.AddBoolean(value: NNS);
862 if (NNS) {
863 Hash.AddNestedNameSpecifier(NNS);
864 }
865 }
866
867 void AddIdentifierInfo(const IdentifierInfo *II) {
868 Hash.AddBoolean(value: II);
869 if (II) {
870 Hash.AddIdentifierInfo(II);
871 }
872 }
873
874 void VisitQualifiers(Qualifiers Quals) {
875 ID.AddInteger(I: Quals.getAsOpaqueValue());
876 }
877
878 // Return the RecordType if the typedef only strips away a keyword.
879 // Otherwise, return the original type.
880 static const Type *RemoveTypedef(const Type *T) {
881 const auto *TypedefT = dyn_cast<TypedefType>(Val: T);
882 if (!TypedefT) {
883 return T;
884 }
885
886 const TypedefNameDecl *D = TypedefT->getDecl();
887 QualType UnderlyingType = D->getUnderlyingType();
888
889 if (UnderlyingType.hasLocalQualifiers()) {
890 return T;
891 }
892
893 const auto *ElaboratedT = dyn_cast<ElaboratedType>(Val&: UnderlyingType);
894 if (!ElaboratedT) {
895 return T;
896 }
897
898 if (ElaboratedT->getQualifier() != nullptr) {
899 return T;
900 }
901
902 QualType NamedType = ElaboratedT->getNamedType();
903 if (NamedType.hasLocalQualifiers()) {
904 return T;
905 }
906
907 const auto *RecordT = dyn_cast<RecordType>(Val&: NamedType);
908 if (!RecordT) {
909 return T;
910 }
911
912 const IdentifierInfo *TypedefII = TypedefT->getDecl()->getIdentifier();
913 const IdentifierInfo *RecordII = RecordT->getDecl()->getIdentifier();
914 if (!TypedefII || !RecordII ||
915 TypedefII->getName() != RecordII->getName()) {
916 return T;
917 }
918
919 return RecordT;
920 }
921
922 void Visit(const Type *T) {
923 T = RemoveTypedef(T);
924 ID.AddInteger(I: T->getTypeClass());
925 Inherited::Visit(T);
926 }
927
928 void VisitType(const Type *T) {}
929
930 void VisitAdjustedType(const AdjustedType *T) {
931 AddQualType(T: T->getOriginalType());
932
933 VisitType(T);
934 }
935
936 void VisitDecayedType(const DecayedType *T) {
937 // getDecayedType and getPointeeType are derived from getAdjustedType
938 // and don't need to be separately processed.
939 VisitAdjustedType(T);
940 }
941
942 void VisitArrayType(const ArrayType *T) {
943 AddQualType(T: T->getElementType());
944 ID.AddInteger(I: llvm::to_underlying(E: T->getSizeModifier()));
945 VisitQualifiers(Quals: T->getIndexTypeQualifiers());
946 VisitType(T);
947 }
948 void VisitConstantArrayType(const ConstantArrayType *T) {
949 T->getSize().Profile(id&: ID);
950 VisitArrayType(T);
951 }
952
953 void VisitArrayParameterType(const ArrayParameterType *T) {
954 VisitConstantArrayType(T);
955 }
956
957 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
958 AddStmt(T->getSizeExpr());
959 VisitArrayType(T);
960 }
961
962 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
963 VisitArrayType(T);
964 }
965
966 void VisitVariableArrayType(const VariableArrayType *T) {
967 AddStmt(T->getSizeExpr());
968 VisitArrayType(T);
969 }
970
971 void VisitAttributedType(const AttributedType *T) {
972 ID.AddInteger(I: T->getAttrKind());
973 AddQualType(T: T->getModifiedType());
974
975 VisitType(T);
976 }
977
978 void VisitBlockPointerType(const BlockPointerType *T) {
979 AddQualType(T: T->getPointeeType());
980 VisitType(T);
981 }
982
983 void VisitBuiltinType(const BuiltinType *T) {
984 ID.AddInteger(I: T->getKind());
985 VisitType(T);
986 }
987
988 void VisitComplexType(const ComplexType *T) {
989 AddQualType(T: T->getElementType());
990 VisitType(T);
991 }
992
993 void VisitDecltypeType(const DecltypeType *T) {
994 AddStmt(T->getUnderlyingExpr());
995 VisitType(T);
996 }
997
998 void VisitDependentDecltypeType(const DependentDecltypeType *T) {
999 VisitDecltypeType(T);
1000 }
1001
1002 void VisitDeducedType(const DeducedType *T) {
1003 AddQualType(T: T->getDeducedType());
1004 VisitType(T);
1005 }
1006
1007 void VisitAutoType(const AutoType *T) {
1008 ID.AddInteger(I: (unsigned)T->getKeyword());
1009 ID.AddInteger(I: T->isConstrained());
1010 if (T->isConstrained()) {
1011 AddDecl(T->getTypeConstraintConcept());
1012 ID.AddInteger(I: T->getTypeConstraintArguments().size());
1013 for (const auto &TA : T->getTypeConstraintArguments())
1014 Hash.AddTemplateArgument(TA);
1015 }
1016 VisitDeducedType(T);
1017 }
1018
1019 void VisitDeducedTemplateSpecializationType(
1020 const DeducedTemplateSpecializationType *T) {
1021 Hash.AddTemplateName(Name: T->getTemplateName());
1022 VisitDeducedType(T);
1023 }
1024
1025 void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) {
1026 AddQualType(T: T->getPointeeType());
1027 AddStmt(T->getAddrSpaceExpr());
1028 VisitType(T);
1029 }
1030
1031 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
1032 AddQualType(T: T->getElementType());
1033 AddStmt(T->getSizeExpr());
1034 VisitType(T);
1035 }
1036
1037 void VisitFunctionType(const FunctionType *T) {
1038 AddQualType(T: T->getReturnType());
1039 T->getExtInfo().Profile(ID);
1040 Hash.AddBoolean(value: T->isConst());
1041 Hash.AddBoolean(value: T->isVolatile());
1042 Hash.AddBoolean(value: T->isRestrict());
1043 VisitType(T);
1044 }
1045
1046 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
1047 VisitFunctionType(T);
1048 }
1049
1050 void VisitFunctionProtoType(const FunctionProtoType *T) {
1051 ID.AddInteger(I: T->getNumParams());
1052 for (auto ParamType : T->getParamTypes())
1053 AddQualType(T: ParamType);
1054
1055 VisitFunctionType(T);
1056 }
1057
1058 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
1059 AddDecl(T->getDecl());
1060 VisitType(T);
1061 }
1062
1063 void VisitMemberPointerType(const MemberPointerType *T) {
1064 AddQualType(T: T->getPointeeType());
1065 AddType(T: T->getClass());
1066 VisitType(T);
1067 }
1068
1069 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
1070 AddQualType(T: T->getPointeeType());
1071 VisitType(T);
1072 }
1073
1074 void VisitObjCObjectType(const ObjCObjectType *T) {
1075 AddDecl(T->getInterface());
1076
1077 auto TypeArgs = T->getTypeArgsAsWritten();
1078 ID.AddInteger(I: TypeArgs.size());
1079 for (auto Arg : TypeArgs) {
1080 AddQualType(T: Arg);
1081 }
1082
1083 auto Protocols = T->getProtocols();
1084 ID.AddInteger(Protocols.size());
1085 for (auto *Protocol : Protocols) {
1086 AddDecl(Protocol);
1087 }
1088
1089 Hash.AddBoolean(value: T->isKindOfType());
1090
1091 VisitType(T);
1092 }
1093
1094 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1095 // This type is handled by the parent type ObjCObjectType.
1096 VisitObjCObjectType(T);
1097 }
1098
1099 void VisitObjCTypeParamType(const ObjCTypeParamType *T) {
1100 AddDecl(T->getDecl());
1101 auto Protocols = T->getProtocols();
1102 ID.AddInteger(Protocols.size());
1103 for (auto *Protocol : Protocols) {
1104 AddDecl(Protocol);
1105 }
1106
1107 VisitType(T);
1108 }
1109
1110 void VisitPackExpansionType(const PackExpansionType *T) {
1111 AddQualType(T: T->getPattern());
1112 VisitType(T);
1113 }
1114
1115 void VisitParenType(const ParenType *T) {
1116 AddQualType(T: T->getInnerType());
1117 VisitType(T);
1118 }
1119
1120 void VisitPipeType(const PipeType *T) {
1121 AddQualType(T: T->getElementType());
1122 Hash.AddBoolean(value: T->isReadOnly());
1123 VisitType(T);
1124 }
1125
1126 void VisitPointerType(const PointerType *T) {
1127 AddQualType(T: T->getPointeeType());
1128 VisitType(T);
1129 }
1130
1131 void VisitReferenceType(const ReferenceType *T) {
1132 AddQualType(T: T->getPointeeTypeAsWritten());
1133 VisitType(T);
1134 }
1135
1136 void VisitLValueReferenceType(const LValueReferenceType *T) {
1137 VisitReferenceType(T);
1138 }
1139
1140 void VisitRValueReferenceType(const RValueReferenceType *T) {
1141 VisitReferenceType(T);
1142 }
1143
1144 void
1145 VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
1146 AddDecl(D: T->getAssociatedDecl());
1147 Hash.AddTemplateArgument(TA: T->getArgumentPack());
1148 VisitType(T);
1149 }
1150
1151 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
1152 AddDecl(D: T->getAssociatedDecl());
1153 AddQualType(T: T->getReplacementType());
1154 VisitType(T);
1155 }
1156
1157 void VisitTagType(const TagType *T) {
1158 AddDecl(T->getDecl());
1159 VisitType(T);
1160 }
1161
1162 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
1163 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
1164
1165 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
1166 ID.AddInteger(I: T->template_arguments().size());
1167 for (const auto &TA : T->template_arguments()) {
1168 Hash.AddTemplateArgument(TA);
1169 }
1170 Hash.AddTemplateName(Name: T->getTemplateName());
1171 VisitType(T);
1172 }
1173
1174 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1175 ID.AddInteger(I: T->getDepth());
1176 ID.AddInteger(I: T->getIndex());
1177 Hash.AddBoolean(value: T->isParameterPack());
1178 AddDecl(T->getDecl());
1179 }
1180
1181 void VisitTypedefType(const TypedefType *T) {
1182 AddDecl(T->getDecl());
1183 VisitType(T);
1184 }
1185
1186 void VisitTypeOfExprType(const TypeOfExprType *T) {
1187 AddStmt(T->getUnderlyingExpr());
1188 Hash.AddBoolean(value: T->isSugared());
1189
1190 VisitType(T);
1191 }
1192 void VisitTypeOfType(const TypeOfType *T) {
1193 AddQualType(T: T->getUnmodifiedType());
1194 VisitType(T);
1195 }
1196
1197 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
1198 ID.AddInteger(I: llvm::to_underlying(E: T->getKeyword()));
1199 VisitType(T);
1200 };
1201
1202 void VisitDependentNameType(const DependentNameType *T) {
1203 AddNestedNameSpecifier(NNS: T->getQualifier());
1204 AddIdentifierInfo(II: T->getIdentifier());
1205 VisitTypeWithKeyword(T);
1206 }
1207
1208 void VisitDependentTemplateSpecializationType(
1209 const DependentTemplateSpecializationType *T) {
1210 AddIdentifierInfo(II: T->getIdentifier());
1211 AddNestedNameSpecifier(NNS: T->getQualifier());
1212 ID.AddInteger(I: T->template_arguments().size());
1213 for (const auto &TA : T->template_arguments()) {
1214 Hash.AddTemplateArgument(TA);
1215 }
1216 VisitTypeWithKeyword(T);
1217 }
1218
1219 void VisitElaboratedType(const ElaboratedType *T) {
1220 AddNestedNameSpecifier(NNS: T->getQualifier());
1221 AddQualType(T: T->getNamedType());
1222 VisitTypeWithKeyword(T);
1223 }
1224
1225 void VisitUnaryTransformType(const UnaryTransformType *T) {
1226 AddQualType(T: T->getUnderlyingType());
1227 AddQualType(T: T->getBaseType());
1228 VisitType(T);
1229 }
1230
1231 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
1232 AddDecl(T->getDecl());
1233 VisitType(T);
1234 }
1235
1236 void VisitVectorType(const VectorType *T) {
1237 AddQualType(T: T->getElementType());
1238 ID.AddInteger(I: T->getNumElements());
1239 ID.AddInteger(I: llvm::to_underlying(E: T->getVectorKind()));
1240 VisitType(T);
1241 }
1242
1243 void VisitExtVectorType(const ExtVectorType * T) {
1244 VisitVectorType(T);
1245 }
1246};
1247} // namespace
1248
1249void ODRHash::AddType(const Type *T) {
1250 assert(T && "Expecting non-null pointer.");
1251 ODRTypeVisitor(ID, *this).Visit(T);
1252}
1253
1254void ODRHash::AddQualType(QualType T) {
1255 AddBoolean(value: T.isNull());
1256 if (T.isNull())
1257 return;
1258 SplitQualType split = T.split();
1259 ID.AddInteger(I: split.Quals.getAsOpaqueValue());
1260 AddType(T: split.Ty);
1261}
1262
1263void ODRHash::AddBoolean(bool Value) {
1264 Bools.push_back(Elt: Value);
1265}
1266
1267void ODRHash::AddStructuralValue(const APValue &Value) {
1268 ID.AddInteger(I: Value.getKind());
1269
1270 // 'APValue::Profile' uses pointer values to make hash for LValue and
1271 // MemberPointer, but they differ from one compiler invocation to another.
1272 // So, handle them explicitly here.
1273
1274 switch (Value.getKind()) {
1275 case APValue::LValue: {
1276 const APValue::LValueBase &Base = Value.getLValueBase();
1277 if (!Base) {
1278 ID.AddInteger(I: Value.getLValueOffset().getQuantity());
1279 break;
1280 }
1281
1282 assert(Base.is<const ValueDecl *>());
1283 AddDecl(Base.get<const ValueDecl *>());
1284 ID.AddInteger(I: Value.getLValueOffset().getQuantity());
1285
1286 bool OnePastTheEnd = Value.isLValueOnePastTheEnd();
1287 if (Value.hasLValuePath()) {
1288 QualType TypeSoFar = Base.getType();
1289 for (APValue::LValuePathEntry E : Value.getLValuePath()) {
1290 if (const auto *AT = TypeSoFar->getAsArrayTypeUnsafe()) {
1291 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
1292 OnePastTheEnd |= CAT->getSize() == E.getAsArrayIndex();
1293 TypeSoFar = AT->getElementType();
1294 } else {
1295 const Decl *D = E.getAsBaseOrMember().getPointer();
1296 if (const auto *FD = dyn_cast<FieldDecl>(Val: D)) {
1297 if (FD->getParent()->isUnion())
1298 ID.AddInteger(I: FD->getFieldIndex());
1299 TypeSoFar = FD->getType();
1300 } else {
1301 TypeSoFar =
1302 D->getASTContext().getRecordType(cast<CXXRecordDecl>(Val: D));
1303 }
1304 }
1305 }
1306 }
1307 unsigned Val = 0;
1308 if (Value.isNullPointer())
1309 Val |= 1 << 0;
1310 if (OnePastTheEnd)
1311 Val |= 1 << 1;
1312 if (Value.hasLValuePath())
1313 Val |= 1 << 2;
1314 ID.AddInteger(I: Val);
1315 break;
1316 }
1317 case APValue::MemberPointer: {
1318 const ValueDecl *D = Value.getMemberPointerDecl();
1319 assert(D);
1320 AddDecl(D);
1321 ID.AddInteger(
1322 D->getASTContext().getMemberPointerPathAdjustment(Value).getQuantity());
1323 break;
1324 }
1325 default:
1326 Value.Profile(ID);
1327 }
1328}
1329

source code of clang/lib/AST/ODRHash.cpp