1 //===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the ODRHash class, which calculates a hash based
11 /// on AST nodes, which is stable across different runs.
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/ODRHash.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/NestedNameSpecifier.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/AST/TypeVisitor.h"
22 using namespace clang
;
24 void ODRHash::AddStmt(const Stmt
*S
) {
25 assert(S
&& "Expecting non-null pointer.");
26 S
->ProcessODRHash(ID
, *this);
29 void ODRHash::AddIdentifierInfo(const IdentifierInfo
*II
) {
30 assert(II
&& "Expecting non-null pointer.");
31 ID
.AddString(II
->getName());
34 void ODRHash::AddDeclarationName(DeclarationName Name
, bool TreatAsDecl
) {
36 // Matches the NamedDecl check in AddDecl
39 AddDeclarationNameImpl(Name
);
42 // Matches the ClassTemplateSpecializationDecl check in AddDecl
46 void ODRHash::AddDeclarationNameImpl(DeclarationName Name
) {
47 // Index all DeclarationName and use index numbers to refer to them.
48 auto Result
= DeclNameMap
.insert(std::make_pair(Name
, DeclNameMap
.size()));
49 ID
.AddInteger(Result
.first
->second
);
51 // If found in map, the DeclarationName has previously been processed.
55 // First time processing each DeclarationName, also process its details.
56 AddBoolean(Name
.isEmpty());
60 auto Kind
= Name
.getNameKind();
63 case DeclarationName::Identifier
:
64 AddIdentifierInfo(Name
.getAsIdentifierInfo());
66 case DeclarationName::ObjCZeroArgSelector
:
67 case DeclarationName::ObjCOneArgSelector
:
68 case DeclarationName::ObjCMultiArgSelector
: {
69 Selector S
= Name
.getObjCSelector();
70 AddBoolean(S
.isNull());
71 AddBoolean(S
.isKeywordSelector());
72 AddBoolean(S
.isUnarySelector());
73 unsigned NumArgs
= S
.getNumArgs();
74 ID
.AddInteger(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(i
);
82 AddIdentifierInfo(II
);
87 case DeclarationName::CXXConstructorName
:
88 case DeclarationName::CXXDestructorName
:
89 AddQualType(Name
.getCXXNameType());
91 case DeclarationName::CXXOperatorName
:
92 ID
.AddInteger(Name
.getCXXOverloadedOperator());
94 case DeclarationName::CXXLiteralOperatorName
:
95 AddIdentifierInfo(Name
.getCXXLiteralIdentifier());
97 case DeclarationName::CXXConversionFunctionName
:
98 AddQualType(Name
.getCXXNameType());
100 case DeclarationName::CXXUsingDirective
:
102 case DeclarationName::CXXDeductionGuideName
: {
103 auto *Template
= Name
.getCXXDeductionGuideTemplate();
104 AddBoolean(Template
);
112 void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier
*NNS
) {
113 assert(NNS
&& "Expecting non-null pointer.");
114 const auto *Prefix
= NNS
->getPrefix();
117 AddNestedNameSpecifier(Prefix
);
119 auto Kind
= NNS
->getKind();
122 case NestedNameSpecifier::Identifier
:
123 AddIdentifierInfo(NNS
->getAsIdentifier());
125 case NestedNameSpecifier::Namespace
:
126 AddDecl(NNS
->getAsNamespace());
128 case NestedNameSpecifier::NamespaceAlias
:
129 AddDecl(NNS
->getAsNamespaceAlias());
131 case NestedNameSpecifier::TypeSpec
:
132 case NestedNameSpecifier::TypeSpecWithTemplate
:
133 AddType(NNS
->getAsType());
135 case NestedNameSpecifier::Global
:
136 case NestedNameSpecifier::Super
:
141 void ODRHash::AddTemplateName(TemplateName Name
) {
142 auto Kind
= Name
.getKind();
146 case TemplateName::Template
:
147 AddDecl(Name
.getAsTemplateDecl());
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
:
161 void ODRHash::AddTemplateArgument(TemplateArgument TA
) {
162 const auto Kind
= TA
.getKind();
166 case TemplateArgument::Null
:
167 llvm_unreachable("Expected valid TemplateArgument");
168 case TemplateArgument::Type
:
169 AddQualType(TA
.getAsType());
171 case TemplateArgument::Declaration
:
172 AddDecl(TA
.getAsDecl());
174 case TemplateArgument::NullPtr
:
175 ID
.AddPointer(nullptr);
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
);
183 case TemplateArgument::Template
:
184 case TemplateArgument::TemplateExpansion
:
185 AddTemplateName(TA
.getAsTemplateOrTemplatePattern());
187 case TemplateArgument::Expression
:
188 AddStmt(TA
.getAsExpr());
190 case TemplateArgument::Pack
:
191 ID
.AddInteger(TA
.pack_size());
192 for (auto SubTA
: TA
.pack_elements()) {
193 AddTemplateArgument(SubTA
);
199 void ODRHash::AddTemplateParameterList(const TemplateParameterList
*TPL
) {
200 assert(TPL
&& "Expecting non-null pointer.");
202 ID
.AddInteger(TPL
->size());
203 for (auto *ND
: TPL
->asArray()) {
208 void ODRHash::clear() {
214 unsigned ODRHash::CalculateHash() {
215 // Append the bools to the end of the data segment backwards. This allows
216 // for the bools data to be compressed 32 times smaller compared to using
218 const unsigned unsigned_bits
= sizeof(unsigned) * CHAR_BIT
;
219 const unsigned size
= Bools
.size();
220 const unsigned remainder
= size
% unsigned_bits
;
221 const unsigned loops
= size
/ unsigned_bits
;
222 auto I
= Bools
.rbegin();
224 for (unsigned i
= 0; i
< remainder
; ++i
) {
229 ID
.AddInteger(value
);
231 for (unsigned i
= 0; i
< loops
; ++i
) {
233 for (unsigned j
= 0; j
< unsigned_bits
; ++j
) {
238 ID
.AddInteger(value
);
241 assert(I
== Bools
.rend());
243 return ID
.ComputeHash();
247 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
248 // methods process the relevant parts of the Decl.
249 class ODRDeclVisitor
: public ConstDeclVisitor
<ODRDeclVisitor
> {
250 typedef ConstDeclVisitor
<ODRDeclVisitor
> Inherited
;
251 llvm::FoldingSetNodeID
&ID
;
255 ODRDeclVisitor(llvm::FoldingSetNodeID
&ID
, ODRHash
&Hash
)
256 : ID(ID
), Hash(Hash
) {}
258 void AddStmt(const Stmt
*S
) {
265 void AddIdentifierInfo(const IdentifierInfo
*II
) {
268 Hash
.AddIdentifierInfo(II
);
272 void AddQualType(QualType T
) {
276 void AddDecl(const Decl
*D
) {
283 void AddTemplateArgument(TemplateArgument TA
) {
284 Hash
.AddTemplateArgument(TA
);
287 void Visit(const Decl
*D
) {
288 ID
.AddInteger(D
->getKind());
292 void VisitNamedDecl(const NamedDecl
*D
) {
293 Hash
.AddDeclarationName(D
->getDeclName());
294 Inherited::VisitNamedDecl(D
);
297 void VisitValueDecl(const ValueDecl
*D
) {
298 if (auto *DD
= dyn_cast
<DeclaratorDecl
>(D
); DD
&& DD
->getTypeSourceInfo())
299 AddQualType(DD
->getTypeSourceInfo()->getType());
301 Inherited::VisitValueDecl(D
);
304 void VisitVarDecl(const VarDecl
*D
) {
305 Hash
.AddBoolean(D
->isStaticLocal());
306 Hash
.AddBoolean(D
->isConstexpr());
307 const bool HasInit
= D
->hasInit();
308 Hash
.AddBoolean(HasInit
);
310 AddStmt(D
->getInit());
312 Inherited::VisitVarDecl(D
);
315 void VisitParmVarDecl(const ParmVarDecl
*D
) {
316 // TODO: Handle default arguments.
317 Inherited::VisitParmVarDecl(D
);
320 void VisitAccessSpecDecl(const AccessSpecDecl
*D
) {
321 ID
.AddInteger(D
->getAccess());
322 Inherited::VisitAccessSpecDecl(D
);
325 void VisitStaticAssertDecl(const StaticAssertDecl
*D
) {
326 AddStmt(D
->getAssertExpr());
327 AddStmt(D
->getMessage());
329 Inherited::VisitStaticAssertDecl(D
);
332 void VisitFieldDecl(const FieldDecl
*D
) {
333 const bool IsBitfield
= D
->isBitField();
334 Hash
.AddBoolean(IsBitfield
);
337 AddStmt(D
->getBitWidth());
340 Hash
.AddBoolean(D
->isMutable());
341 AddStmt(D
->getInClassInitializer());
343 Inherited::VisitFieldDecl(D
);
346 void VisitObjCIvarDecl(const ObjCIvarDecl
*D
) {
347 ID
.AddInteger(D
->getCanonicalAccessControl());
348 Inherited::VisitObjCIvarDecl(D
);
351 void VisitObjCPropertyDecl(const ObjCPropertyDecl
*D
) {
352 ID
.AddInteger(D
->getPropertyAttributes());
353 ID
.AddInteger(D
->getPropertyImplementation());
354 AddQualType(D
->getTypeSourceInfo()->getType());
357 Inherited::VisitObjCPropertyDecl(D
);
360 void VisitFunctionDecl(const FunctionDecl
*D
) {
361 // Handled by the ODRHash for FunctionDecl
362 ID
.AddInteger(D
->getODRHash());
364 Inherited::VisitFunctionDecl(D
);
367 void VisitCXXMethodDecl(const CXXMethodDecl
*D
) {
368 // Handled by the ODRHash for FunctionDecl
370 Inherited::VisitCXXMethodDecl(D
);
373 void VisitObjCMethodDecl(const ObjCMethodDecl
*Method
) {
374 ID
.AddInteger(Method
->getDeclKind());
375 Hash
.AddBoolean(Method
->isInstanceMethod()); // false if class method
376 Hash
.AddBoolean(Method
->isVariadic());
377 Hash
.AddBoolean(Method
->isSynthesizedAccessorStub());
378 Hash
.AddBoolean(Method
->isDefined());
379 Hash
.AddBoolean(Method
->isDirectMethod());
380 Hash
.AddBoolean(Method
->isThisDeclarationADesignatedInitializer());
381 Hash
.AddBoolean(Method
->hasSkippedBody());
383 ID
.AddInteger(llvm::to_underlying(Method
->getImplementationControl()));
384 ID
.AddInteger(Method
->getMethodFamily());
385 ImplicitParamDecl
*Cmd
= Method
->getCmdDecl();
386 Hash
.AddBoolean(Cmd
);
388 ID
.AddInteger(Cmd
->getParameterKind());
390 ImplicitParamDecl
*Self
= Method
->getSelfDecl();
391 Hash
.AddBoolean(Self
);
393 ID
.AddInteger(Self
->getParameterKind());
397 if (Method
->getReturnTypeSourceInfo())
398 AddQualType(Method
->getReturnTypeSourceInfo()->getType());
400 ID
.AddInteger(Method
->param_size());
401 for (auto Param
: Method
->parameters())
402 Hash
.AddSubDecl(Param
);
404 if (Method
->hasBody()) {
405 const bool IsDefinition
= Method
->isThisDeclarationADefinition();
406 Hash
.AddBoolean(IsDefinition
);
408 Stmt
*Body
= Method
->getBody();
409 Hash
.AddBoolean(Body
);
413 // Filter out sub-Decls which will not be processed in order to get an
414 // accurate count of Decl's.
415 llvm::SmallVector
<const Decl
*, 16> Decls
;
416 for (Decl
*SubDecl
: Method
->decls())
417 if (ODRHash::isSubDeclToBeProcessed(SubDecl
, Method
))
418 Decls
.push_back(SubDecl
);
420 ID
.AddInteger(Decls
.size());
421 for (auto SubDecl
: Decls
)
422 Hash
.AddSubDecl(SubDecl
);
425 Hash
.AddBoolean(false);
428 Inherited::VisitObjCMethodDecl(Method
);
431 void VisitTypedefNameDecl(const TypedefNameDecl
*D
) {
432 AddQualType(D
->getUnderlyingType());
434 Inherited::VisitTypedefNameDecl(D
);
437 void VisitTypedefDecl(const TypedefDecl
*D
) {
438 Inherited::VisitTypedefDecl(D
);
441 void VisitTypeAliasDecl(const TypeAliasDecl
*D
) {
442 Inherited::VisitTypeAliasDecl(D
);
445 void VisitFriendDecl(const FriendDecl
*D
) {
446 TypeSourceInfo
*TSI
= D
->getFriendType();
447 Hash
.AddBoolean(TSI
);
449 AddQualType(TSI
->getType());
451 AddDecl(D
->getFriendDecl());
455 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl
*D
) {
456 // Only care about default arguments as part of the definition.
457 const bool hasDefaultArgument
=
458 D
->hasDefaultArgument() && !D
->defaultArgumentWasInherited();
459 Hash
.AddBoolean(hasDefaultArgument
);
460 if (hasDefaultArgument
) {
461 AddTemplateArgument(D
->getDefaultArgument());
463 Hash
.AddBoolean(D
->isParameterPack());
465 const TypeConstraint
*TC
= D
->getTypeConstraint();
466 Hash
.AddBoolean(TC
!= nullptr);
468 AddStmt(TC
->getImmediatelyDeclaredConstraint());
470 Inherited::VisitTemplateTypeParmDecl(D
);
473 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl
*D
) {
474 // Only care about default arguments as part of the definition.
475 const bool hasDefaultArgument
=
476 D
->hasDefaultArgument() && !D
->defaultArgumentWasInherited();
477 Hash
.AddBoolean(hasDefaultArgument
);
478 if (hasDefaultArgument
) {
479 AddStmt(D
->getDefaultArgument());
481 Hash
.AddBoolean(D
->isParameterPack());
483 Inherited::VisitNonTypeTemplateParmDecl(D
);
486 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl
*D
) {
487 // Only care about default arguments as part of the definition.
488 const bool hasDefaultArgument
=
489 D
->hasDefaultArgument() && !D
->defaultArgumentWasInherited();
490 Hash
.AddBoolean(hasDefaultArgument
);
491 if (hasDefaultArgument
) {
492 AddTemplateArgument(D
->getDefaultArgument().getArgument());
494 Hash
.AddBoolean(D
->isParameterPack());
496 Inherited::VisitTemplateTemplateParmDecl(D
);
499 void VisitTemplateDecl(const TemplateDecl
*D
) {
500 Hash
.AddTemplateParameterList(D
->getTemplateParameters());
502 Inherited::VisitTemplateDecl(D
);
505 void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl
*D
) {
506 Hash
.AddBoolean(D
->isMemberSpecialization());
507 Inherited::VisitRedeclarableTemplateDecl(D
);
510 void VisitFunctionTemplateDecl(const FunctionTemplateDecl
*D
) {
511 AddDecl(D
->getTemplatedDecl());
512 ID
.AddInteger(D
->getTemplatedDecl()->getODRHash());
513 Inherited::VisitFunctionTemplateDecl(D
);
516 void VisitEnumConstantDecl(const EnumConstantDecl
*D
) {
517 AddStmt(D
->getInitExpr());
518 Inherited::VisitEnumConstantDecl(D
);
523 // Only allow a small portion of Decl's to be processed. Remove this once
524 // all Decl's can be handled.
525 bool ODRHash::isSubDeclToBeProcessed(const Decl
*D
, const DeclContext
*Parent
) {
526 if (D
->isImplicit()) return false;
527 if (D
->getDeclContext() != Parent
) return false;
529 switch (D
->getKind()) {
532 case Decl::AccessSpec
:
533 case Decl::CXXConstructor
:
534 case Decl::CXXDestructor
:
535 case Decl::CXXMethod
:
536 case Decl::EnumConstant
: // Only found in EnumDecl's.
539 case Decl::FunctionTemplate
:
540 case Decl::StaticAssert
:
541 case Decl::TypeAlias
:
544 case Decl::ObjCMethod
:
546 case Decl::ObjCProperty
:
551 void ODRHash::AddSubDecl(const Decl
*D
) {
552 assert(D
&& "Expecting non-null pointer.");
554 ODRDeclVisitor(ID
, *this).Visit(D
);
557 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl
*Record
) {
558 assert(Record
&& Record
->hasDefinition() &&
559 "Expected non-null record to be a definition.");
561 const DeclContext
*DC
= Record
;
563 if (isa
<ClassTemplateSpecializationDecl
>(DC
)) {
566 DC
= DC
->getParent();
571 // Filter out sub-Decls which will not be processed in order to get an
572 // accurate count of Decl's.
573 llvm::SmallVector
<const Decl
*, 16> Decls
;
574 for (Decl
*SubDecl
: Record
->decls()) {
575 if (isSubDeclToBeProcessed(SubDecl
, Record
)) {
576 Decls
.push_back(SubDecl
);
577 if (auto *Function
= dyn_cast
<FunctionDecl
>(SubDecl
)) {
578 // Compute/Preload ODRHash into FunctionDecl.
579 Function
->getODRHash();
584 ID
.AddInteger(Decls
.size());
585 for (auto SubDecl
: Decls
) {
589 const ClassTemplateDecl
*TD
= Record
->getDescribedClassTemplate();
592 AddTemplateParameterList(TD
->getTemplateParameters());
595 ID
.AddInteger(Record
->getNumBases());
596 auto Bases
= Record
->bases();
597 for (const auto &Base
: Bases
) {
598 AddQualType(Base
.getTypeSourceInfo()->getType());
599 ID
.AddInteger(Base
.isVirtual());
600 ID
.AddInteger(Base
.getAccessSpecifierAsWritten());
604 void ODRHash::AddRecordDecl(const RecordDecl
*Record
) {
605 assert(!isa
<CXXRecordDecl
>(Record
) &&
606 "For CXXRecordDecl should call AddCXXRecordDecl.");
609 // Filter out sub-Decls which will not be processed in order to get an
610 // accurate count of Decl's.
611 llvm::SmallVector
<const Decl
*, 16> Decls
;
612 for (Decl
*SubDecl
: Record
->decls()) {
613 if (isSubDeclToBeProcessed(SubDecl
, Record
))
614 Decls
.push_back(SubDecl
);
617 ID
.AddInteger(Decls
.size());
618 for (const Decl
*SubDecl
: Decls
)
622 void ODRHash::AddObjCInterfaceDecl(const ObjCInterfaceDecl
*IF
) {
625 auto *SuperClass
= IF
->getSuperClass();
626 AddBoolean(SuperClass
);
628 ID
.AddInteger(SuperClass
->getODRHash());
630 // Hash referenced protocols.
631 ID
.AddInteger(IF
->getReferencedProtocols().size());
632 for (const ObjCProtocolDecl
*RefP
: IF
->protocols()) {
633 // Hash the name only as a referenced protocol can be a forward declaration.
634 AddDeclarationName(RefP
->getDeclName());
637 // Filter out sub-Decls which will not be processed in order to get an
638 // accurate count of Decl's.
639 llvm::SmallVector
<const Decl
*, 16> Decls
;
640 for (Decl
*SubDecl
: IF
->decls())
641 if (isSubDeclToBeProcessed(SubDecl
, IF
))
642 Decls
.push_back(SubDecl
);
644 ID
.AddInteger(Decls
.size());
645 for (auto *SubDecl
: Decls
)
649 void ODRHash::AddFunctionDecl(const FunctionDecl
*Function
,
651 assert(Function
&& "Expecting non-null pointer.");
653 // Skip functions that are specializations or in specialization context.
654 const DeclContext
*DC
= Function
;
656 if (isa
<ClassTemplateSpecializationDecl
>(DC
)) return;
657 if (auto *F
= dyn_cast
<FunctionDecl
>(DC
)) {
658 if (F
->isFunctionTemplateSpecialization()) {
659 if (!isa
<CXXMethodDecl
>(DC
)) return;
660 if (DC
->getLexicalParent()->isFileContext()) return;
661 // Skip class scope explicit function template specializations,
662 // as they have not yet been instantiated.
663 if (F
->getDependentSpecializationInfo())
665 // Inline method specializations are the only supported
666 // specialization for now.
669 DC
= DC
->getParent();
672 ID
.AddInteger(Function
->getDeclKind());
674 const auto *SpecializationArgs
= Function
->getTemplateSpecializationArgs();
675 AddBoolean(SpecializationArgs
);
676 if (SpecializationArgs
) {
677 ID
.AddInteger(SpecializationArgs
->size());
678 for (const TemplateArgument
&TA
: SpecializationArgs
->asArray()) {
679 AddTemplateArgument(TA
);
683 if (const auto *Method
= dyn_cast
<CXXMethodDecl
>(Function
)) {
684 AddBoolean(Method
->isConst());
685 AddBoolean(Method
->isVolatile());
688 ID
.AddInteger(Function
->getStorageClass());
689 AddBoolean(Function
->isInlineSpecified());
690 AddBoolean(Function
->isVirtualAsWritten());
691 AddBoolean(Function
->isPure());
692 AddBoolean(Function
->isDeletedAsWritten());
693 AddBoolean(Function
->isExplicitlyDefaulted());
697 AddQualType(Function
->getReturnType());
699 ID
.AddInteger(Function
->param_size());
700 for (auto *Param
: Function
->parameters())
708 const bool HasBody
= Function
->isThisDeclarationADefinition() &&
709 !Function
->isDefaulted() && !Function
->isDeleted() &&
710 !Function
->isLateTemplateParsed();
716 auto *Body
= Function
->getBody();
721 // Filter out sub-Decls which will not be processed in order to get an
722 // accurate count of Decl's.
723 llvm::SmallVector
<const Decl
*, 16> Decls
;
724 for (Decl
*SubDecl
: Function
->decls()) {
725 if (isSubDeclToBeProcessed(SubDecl
, Function
)) {
726 Decls
.push_back(SubDecl
);
730 ID
.AddInteger(Decls
.size());
731 for (auto SubDecl
: Decls
) {
736 void ODRHash::AddEnumDecl(const EnumDecl
*Enum
) {
738 AddDeclarationName(Enum
->getDeclName());
740 AddBoolean(Enum
->isScoped());
741 if (Enum
->isScoped())
742 AddBoolean(Enum
->isScopedUsingClassTag());
744 if (Enum
->getIntegerTypeSourceInfo())
745 AddQualType(Enum
->getIntegerType());
747 // Filter out sub-Decls which will not be processed in order to get an
748 // accurate count of Decl's.
749 llvm::SmallVector
<const Decl
*, 16> Decls
;
750 for (Decl
*SubDecl
: Enum
->decls()) {
751 if (isSubDeclToBeProcessed(SubDecl
, Enum
)) {
752 assert(isa
<EnumConstantDecl
>(SubDecl
) && "Unexpected Decl");
753 Decls
.push_back(SubDecl
);
757 ID
.AddInteger(Decls
.size());
758 for (auto SubDecl
: Decls
) {
764 void ODRHash::AddObjCProtocolDecl(const ObjCProtocolDecl
*P
) {
767 // Hash referenced protocols.
768 ID
.AddInteger(P
->getReferencedProtocols().size());
769 for (const ObjCProtocolDecl
*RefP
: P
->protocols()) {
770 // Hash the name only as a referenced protocol can be a forward declaration.
771 AddDeclarationName(RefP
->getDeclName());
774 // Filter out sub-Decls which will not be processed in order to get an
775 // accurate count of Decl's.
776 llvm::SmallVector
<const Decl
*, 16> Decls
;
777 for (Decl
*SubDecl
: P
->decls()) {
778 if (isSubDeclToBeProcessed(SubDecl
, P
)) {
779 Decls
.push_back(SubDecl
);
783 ID
.AddInteger(Decls
.size());
784 for (auto *SubDecl
: Decls
) {
789 void ODRHash::AddDecl(const Decl
*D
) {
790 assert(D
&& "Expecting non-null pointer.");
791 D
= D
->getCanonicalDecl();
793 const NamedDecl
*ND
= dyn_cast
<NamedDecl
>(D
);
796 ID
.AddInteger(D
->getKind());
800 AddDeclarationName(ND
->getDeclName());
802 const auto *Specialization
=
803 dyn_cast
<ClassTemplateSpecializationDecl
>(D
);
804 AddBoolean(Specialization
);
805 if (Specialization
) {
806 const TemplateArgumentList
&List
= Specialization
->getTemplateArgs();
807 ID
.AddInteger(List
.size());
808 for (const TemplateArgument
&TA
: List
.asArray())
809 AddTemplateArgument(TA
);
814 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
815 // methods process the relevant parts of the Type.
816 class ODRTypeVisitor
: public TypeVisitor
<ODRTypeVisitor
> {
817 typedef TypeVisitor
<ODRTypeVisitor
> Inherited
;
818 llvm::FoldingSetNodeID
&ID
;
822 ODRTypeVisitor(llvm::FoldingSetNodeID
&ID
, ODRHash
&Hash
)
823 : ID(ID
), Hash(Hash
) {}
825 void AddStmt(Stmt
*S
) {
832 void AddDecl(const Decl
*D
) {
839 void AddQualType(QualType T
) {
843 void AddType(const Type
*T
) {
850 void AddNestedNameSpecifier(const NestedNameSpecifier
*NNS
) {
851 Hash
.AddBoolean(NNS
);
853 Hash
.AddNestedNameSpecifier(NNS
);
857 void AddIdentifierInfo(const IdentifierInfo
*II
) {
860 Hash
.AddIdentifierInfo(II
);
864 void VisitQualifiers(Qualifiers Quals
) {
865 ID
.AddInteger(Quals
.getAsOpaqueValue());
868 // Return the RecordType if the typedef only strips away a keyword.
869 // Otherwise, return the original type.
870 static const Type
*RemoveTypedef(const Type
*T
) {
871 const auto *TypedefT
= dyn_cast
<TypedefType
>(T
);
876 const TypedefNameDecl
*D
= TypedefT
->getDecl();
877 QualType UnderlyingType
= D
->getUnderlyingType();
879 if (UnderlyingType
.hasLocalQualifiers()) {
883 const auto *ElaboratedT
= dyn_cast
<ElaboratedType
>(UnderlyingType
);
888 if (ElaboratedT
->getQualifier() != nullptr) {
892 QualType NamedType
= ElaboratedT
->getNamedType();
893 if (NamedType
.hasLocalQualifiers()) {
897 const auto *RecordT
= dyn_cast
<RecordType
>(NamedType
);
902 const IdentifierInfo
*TypedefII
= TypedefT
->getDecl()->getIdentifier();
903 const IdentifierInfo
*RecordII
= RecordT
->getDecl()->getIdentifier();
904 if (!TypedefII
|| !RecordII
||
905 TypedefII
->getName() != RecordII
->getName()) {
912 void Visit(const Type
*T
) {
913 T
= RemoveTypedef(T
);
914 ID
.AddInteger(T
->getTypeClass());
918 void VisitType(const Type
*T
) {}
920 void VisitAdjustedType(const AdjustedType
*T
) {
921 AddQualType(T
->getOriginalType());
926 void VisitDecayedType(const DecayedType
*T
) {
927 // getDecayedType and getPointeeType are derived from getAdjustedType
928 // and don't need to be separately processed.
929 VisitAdjustedType(T
);
932 void VisitArrayType(const ArrayType
*T
) {
933 AddQualType(T
->getElementType());
934 ID
.AddInteger(llvm::to_underlying(T
->getSizeModifier()));
935 VisitQualifiers(T
->getIndexTypeQualifiers());
938 void VisitConstantArrayType(const ConstantArrayType
*T
) {
939 T
->getSize().Profile(ID
);
943 void VisitDependentSizedArrayType(const DependentSizedArrayType
*T
) {
944 AddStmt(T
->getSizeExpr());
948 void VisitIncompleteArrayType(const IncompleteArrayType
*T
) {
952 void VisitVariableArrayType(const VariableArrayType
*T
) {
953 AddStmt(T
->getSizeExpr());
957 void VisitAttributedType(const AttributedType
*T
) {
958 ID
.AddInteger(T
->getAttrKind());
959 AddQualType(T
->getModifiedType());
964 void VisitBlockPointerType(const BlockPointerType
*T
) {
965 AddQualType(T
->getPointeeType());
969 void VisitBuiltinType(const BuiltinType
*T
) {
970 ID
.AddInteger(T
->getKind());
974 void VisitComplexType(const ComplexType
*T
) {
975 AddQualType(T
->getElementType());
979 void VisitDecltypeType(const DecltypeType
*T
) {
980 AddStmt(T
->getUnderlyingExpr());
984 void VisitDependentDecltypeType(const DependentDecltypeType
*T
) {
985 VisitDecltypeType(T
);
988 void VisitDeducedType(const DeducedType
*T
) {
989 AddQualType(T
->getDeducedType());
993 void VisitAutoType(const AutoType
*T
) {
994 ID
.AddInteger((unsigned)T
->getKeyword());
995 ID
.AddInteger(T
->isConstrained());
996 if (T
->isConstrained()) {
997 AddDecl(T
->getTypeConstraintConcept());
998 ID
.AddInteger(T
->getTypeConstraintArguments().size());
999 for (const auto &TA
: T
->getTypeConstraintArguments())
1000 Hash
.AddTemplateArgument(TA
);
1002 VisitDeducedType(T
);
1005 void VisitDeducedTemplateSpecializationType(
1006 const DeducedTemplateSpecializationType
*T
) {
1007 Hash
.AddTemplateName(T
->getTemplateName());
1008 VisitDeducedType(T
);
1011 void VisitDependentAddressSpaceType(const DependentAddressSpaceType
*T
) {
1012 AddQualType(T
->getPointeeType());
1013 AddStmt(T
->getAddrSpaceExpr());
1017 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType
*T
) {
1018 AddQualType(T
->getElementType());
1019 AddStmt(T
->getSizeExpr());
1023 void VisitFunctionType(const FunctionType
*T
) {
1024 AddQualType(T
->getReturnType());
1025 T
->getExtInfo().Profile(ID
);
1026 Hash
.AddBoolean(T
->isConst());
1027 Hash
.AddBoolean(T
->isVolatile());
1028 Hash
.AddBoolean(T
->isRestrict());
1032 void VisitFunctionNoProtoType(const FunctionNoProtoType
*T
) {
1033 VisitFunctionType(T
);
1036 void VisitFunctionProtoType(const FunctionProtoType
*T
) {
1037 ID
.AddInteger(T
->getNumParams());
1038 for (auto ParamType
: T
->getParamTypes())
1039 AddQualType(ParamType
);
1041 VisitFunctionType(T
);
1044 void VisitInjectedClassNameType(const InjectedClassNameType
*T
) {
1045 AddDecl(T
->getDecl());
1049 void VisitMemberPointerType(const MemberPointerType
*T
) {
1050 AddQualType(T
->getPointeeType());
1051 AddType(T
->getClass());
1055 void VisitObjCObjectPointerType(const ObjCObjectPointerType
*T
) {
1056 AddQualType(T
->getPointeeType());
1060 void VisitObjCObjectType(const ObjCObjectType
*T
) {
1061 AddDecl(T
->getInterface());
1063 auto TypeArgs
= T
->getTypeArgsAsWritten();
1064 ID
.AddInteger(TypeArgs
.size());
1065 for (auto Arg
: TypeArgs
) {
1069 auto Protocols
= T
->getProtocols();
1070 ID
.AddInteger(Protocols
.size());
1071 for (auto *Protocol
: Protocols
) {
1075 Hash
.AddBoolean(T
->isKindOfType());
1080 void VisitObjCInterfaceType(const ObjCInterfaceType
*T
) {
1081 // This type is handled by the parent type ObjCObjectType.
1082 VisitObjCObjectType(T
);
1085 void VisitObjCTypeParamType(const ObjCTypeParamType
*T
) {
1086 AddDecl(T
->getDecl());
1087 auto Protocols
= T
->getProtocols();
1088 ID
.AddInteger(Protocols
.size());
1089 for (auto *Protocol
: Protocols
) {
1096 void VisitPackExpansionType(const PackExpansionType
*T
) {
1097 AddQualType(T
->getPattern());
1101 void VisitParenType(const ParenType
*T
) {
1102 AddQualType(T
->getInnerType());
1106 void VisitPipeType(const PipeType
*T
) {
1107 AddQualType(T
->getElementType());
1108 Hash
.AddBoolean(T
->isReadOnly());
1112 void VisitPointerType(const PointerType
*T
) {
1113 AddQualType(T
->getPointeeType());
1117 void VisitReferenceType(const ReferenceType
*T
) {
1118 AddQualType(T
->getPointeeTypeAsWritten());
1122 void VisitLValueReferenceType(const LValueReferenceType
*T
) {
1123 VisitReferenceType(T
);
1126 void VisitRValueReferenceType(const RValueReferenceType
*T
) {
1127 VisitReferenceType(T
);
1131 VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType
*T
) {
1132 AddDecl(T
->getAssociatedDecl());
1133 Hash
.AddTemplateArgument(T
->getArgumentPack());
1137 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType
*T
) {
1138 AddDecl(T
->getAssociatedDecl());
1139 AddQualType(T
->getReplacementType());
1143 void VisitTagType(const TagType
*T
) {
1144 AddDecl(T
->getDecl());
1148 void VisitRecordType(const RecordType
*T
) { VisitTagType(T
); }
1149 void VisitEnumType(const EnumType
*T
) { VisitTagType(T
); }
1151 void VisitTemplateSpecializationType(const TemplateSpecializationType
*T
) {
1152 ID
.AddInteger(T
->template_arguments().size());
1153 for (const auto &TA
: T
->template_arguments()) {
1154 Hash
.AddTemplateArgument(TA
);
1156 Hash
.AddTemplateName(T
->getTemplateName());
1160 void VisitTemplateTypeParmType(const TemplateTypeParmType
*T
) {
1161 ID
.AddInteger(T
->getDepth());
1162 ID
.AddInteger(T
->getIndex());
1163 Hash
.AddBoolean(T
->isParameterPack());
1164 AddDecl(T
->getDecl());
1167 void VisitTypedefType(const TypedefType
*T
) {
1168 AddDecl(T
->getDecl());
1172 void VisitTypeOfExprType(const TypeOfExprType
*T
) {
1173 AddStmt(T
->getUnderlyingExpr());
1174 Hash
.AddBoolean(T
->isSugared());
1178 void VisitTypeOfType(const TypeOfType
*T
) {
1179 AddQualType(T
->getUnmodifiedType());
1183 void VisitTypeWithKeyword(const TypeWithKeyword
*T
) {
1184 ID
.AddInteger(llvm::to_underlying(T
->getKeyword()));
1188 void VisitDependentNameType(const DependentNameType
*T
) {
1189 AddNestedNameSpecifier(T
->getQualifier());
1190 AddIdentifierInfo(T
->getIdentifier());
1191 VisitTypeWithKeyword(T
);
1194 void VisitDependentTemplateSpecializationType(
1195 const DependentTemplateSpecializationType
*T
) {
1196 AddIdentifierInfo(T
->getIdentifier());
1197 AddNestedNameSpecifier(T
->getQualifier());
1198 ID
.AddInteger(T
->template_arguments().size());
1199 for (const auto &TA
: T
->template_arguments()) {
1200 Hash
.AddTemplateArgument(TA
);
1202 VisitTypeWithKeyword(T
);
1205 void VisitElaboratedType(const ElaboratedType
*T
) {
1206 AddNestedNameSpecifier(T
->getQualifier());
1207 AddQualType(T
->getNamedType());
1208 VisitTypeWithKeyword(T
);
1211 void VisitUnaryTransformType(const UnaryTransformType
*T
) {
1212 AddQualType(T
->getUnderlyingType());
1213 AddQualType(T
->getBaseType());
1217 void VisitUnresolvedUsingType(const UnresolvedUsingType
*T
) {
1218 AddDecl(T
->getDecl());
1222 void VisitVectorType(const VectorType
*T
) {
1223 AddQualType(T
->getElementType());
1224 ID
.AddInteger(T
->getNumElements());
1225 ID
.AddInteger(llvm::to_underlying(T
->getVectorKind()));
1229 void VisitExtVectorType(const ExtVectorType
* T
) {
1235 void ODRHash::AddType(const Type
*T
) {
1236 assert(T
&& "Expecting non-null pointer.");
1237 ODRTypeVisitor(ID
, *this).Visit(T
);
1240 void ODRHash::AddQualType(QualType T
) {
1241 AddBoolean(T
.isNull());
1244 SplitQualType split
= T
.split();
1245 ID
.AddInteger(split
.Quals
.getAsOpaqueValue());
1249 void ODRHash::AddBoolean(bool Value
) {
1250 Bools
.push_back(Value
);