1 //===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements the ODRHash class, which calculates a hash based
12 /// on AST nodes, which is stable across different runs.
14 //===----------------------------------------------------------------------===//
16 #include "clang/AST/ODRHash.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/AST/NestedNameSpecifier.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "clang/AST/TypeVisitor.h"
23 using namespace clang;
25 void ODRHash::AddStmt(const Stmt *S) {
26 assert(S && "Expecting non-null pointer.");
27 S->ProcessODRHash(ID, *this);
30 void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
31 assert(II && "Expecting non-null pointer.");
32 ID.AddString(II->getName());
35 void ODRHash::AddDeclarationName(DeclarationName Name) {
36 // Index all DeclarationName and use index numbers to refer to them.
37 auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
38 ID.AddInteger(Result.first->second);
40 // If found in map, the the DeclarationName has previously been processed.
44 // First time processing each DeclarationName, also process its details.
45 AddBoolean(Name.isEmpty());
49 auto Kind = Name.getNameKind();
52 case DeclarationName::Identifier:
53 AddIdentifierInfo(Name.getAsIdentifierInfo());
55 case DeclarationName::ObjCZeroArgSelector:
56 case DeclarationName::ObjCOneArgSelector:
57 case DeclarationName::ObjCMultiArgSelector: {
58 Selector S = Name.getObjCSelector();
59 AddBoolean(S.isNull());
60 AddBoolean(S.isKeywordSelector());
61 AddBoolean(S.isUnarySelector());
62 unsigned NumArgs = S.getNumArgs();
63 for (unsigned i = 0; i < NumArgs; ++i) {
64 AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
68 case DeclarationName::CXXConstructorName:
69 case DeclarationName::CXXDestructorName:
70 AddQualType(Name.getCXXNameType());
72 case DeclarationName::CXXOperatorName:
73 ID.AddInteger(Name.getCXXOverloadedOperator());
75 case DeclarationName::CXXLiteralOperatorName:
76 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
78 case DeclarationName::CXXConversionFunctionName:
79 AddQualType(Name.getCXXNameType());
81 case DeclarationName::CXXUsingDirective:
83 case DeclarationName::CXXDeductionGuideName: {
84 auto *Template = Name.getCXXDeductionGuideTemplate();
93 void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
94 assert(NNS && "Expecting non-null pointer.");
95 const auto *Prefix = NNS->getPrefix();
98 AddNestedNameSpecifier(Prefix);
100 auto Kind = NNS->getKind();
103 case NestedNameSpecifier::Identifier:
104 AddIdentifierInfo(NNS->getAsIdentifier());
106 case NestedNameSpecifier::Namespace:
107 AddDecl(NNS->getAsNamespace());
109 case NestedNameSpecifier::NamespaceAlias:
110 AddDecl(NNS->getAsNamespaceAlias());
112 case NestedNameSpecifier::TypeSpec:
113 case NestedNameSpecifier::TypeSpecWithTemplate:
114 AddType(NNS->getAsType());
116 case NestedNameSpecifier::Global:
117 case NestedNameSpecifier::Super:
122 void ODRHash::AddTemplateName(TemplateName Name) {
123 auto Kind = Name.getKind();
127 case TemplateName::Template:
128 AddDecl(Name.getAsTemplateDecl());
130 // TODO: Support these cases.
131 case TemplateName::OverloadedTemplate:
132 case TemplateName::QualifiedTemplate:
133 case TemplateName::DependentTemplate:
134 case TemplateName::SubstTemplateTemplateParm:
135 case TemplateName::SubstTemplateTemplateParmPack:
140 void ODRHash::AddTemplateArgument(TemplateArgument TA) {
141 const auto Kind = TA.getKind();
145 case TemplateArgument::Null:
146 llvm_unreachable("Expected valid TemplateArgument");
147 case TemplateArgument::Type:
148 AddQualType(TA.getAsType());
150 case TemplateArgument::Declaration:
151 AddDecl(TA.getAsDecl());
153 case TemplateArgument::NullPtr:
154 case TemplateArgument::Integral:
156 case TemplateArgument::Template:
157 case TemplateArgument::TemplateExpansion:
158 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
160 case TemplateArgument::Expression:
161 AddStmt(TA.getAsExpr());
163 case TemplateArgument::Pack:
164 ID.AddInteger(TA.pack_size());
165 for (auto SubTA : TA.pack_elements()) {
166 AddTemplateArgument(SubTA);
172 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
173 assert(TPL && "Expecting non-null pointer.");
175 ID.AddInteger(TPL->size());
176 for (auto *ND : TPL->asArray()) {
181 void ODRHash::clear() {
187 unsigned ODRHash::CalculateHash() {
188 // Append the bools to the end of the data segment backwards. This allows
189 // for the bools data to be compressed 32 times smaller compared to using
191 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
192 const unsigned size = Bools.size();
193 const unsigned remainder = size % unsigned_bits;
194 const unsigned loops = size / unsigned_bits;
195 auto I = Bools.rbegin();
197 for (unsigned i = 0; i < remainder; ++i) {
202 ID.AddInteger(value);
204 for (unsigned i = 0; i < loops; ++i) {
206 for (unsigned j = 0; j < unsigned_bits; ++j) {
211 ID.AddInteger(value);
214 assert(I == Bools.rend());
216 return ID.ComputeHash();
220 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
221 // methods process the relevant parts of the Decl.
222 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
223 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
224 llvm::FoldingSetNodeID &ID;
228 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
229 : ID(ID), Hash(Hash) {}
231 void AddStmt(const Stmt *S) {
238 void AddIdentifierInfo(const IdentifierInfo *II) {
241 Hash.AddIdentifierInfo(II);
245 void AddQualType(QualType T) {
249 void AddDecl(const Decl *D) {
256 void AddTemplateArgument(TemplateArgument TA) {
257 Hash.AddTemplateArgument(TA);
260 void Visit(const Decl *D) {
261 ID.AddInteger(D->getKind());
265 void VisitNamedDecl(const NamedDecl *D) {
266 Hash.AddDeclarationName(D->getDeclName());
267 Inherited::VisitNamedDecl(D);
270 void VisitValueDecl(const ValueDecl *D) {
271 if (!isa<FunctionDecl>(D)) {
272 AddQualType(D->getType());
274 Inherited::VisitValueDecl(D);
277 void VisitVarDecl(const VarDecl *D) {
278 Hash.AddBoolean(D->isStaticLocal());
279 Hash.AddBoolean(D->isConstexpr());
280 const bool HasInit = D->hasInit();
281 Hash.AddBoolean(HasInit);
283 AddStmt(D->getInit());
285 Inherited::VisitVarDecl(D);
288 void VisitParmVarDecl(const ParmVarDecl *D) {
289 // TODO: Handle default arguments.
290 Inherited::VisitParmVarDecl(D);
293 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
294 ID.AddInteger(D->getAccess());
295 Inherited::VisitAccessSpecDecl(D);
298 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
299 AddStmt(D->getAssertExpr());
300 AddStmt(D->getMessage());
302 Inherited::VisitStaticAssertDecl(D);
305 void VisitFieldDecl(const FieldDecl *D) {
306 const bool IsBitfield = D->isBitField();
307 Hash.AddBoolean(IsBitfield);
310 AddStmt(D->getBitWidth());
313 Hash.AddBoolean(D->isMutable());
314 AddStmt(D->getInClassInitializer());
316 Inherited::VisitFieldDecl(D);
319 void VisitFunctionDecl(const FunctionDecl *D) {
320 // Handled by the ODRHash for FunctionDecl
321 ID.AddInteger(D->getODRHash());
323 Inherited::VisitFunctionDecl(D);
326 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
327 // Handled by the ODRHash for FunctionDecl
329 Inherited::VisitCXXMethodDecl(D);
332 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
333 AddQualType(D->getUnderlyingType());
335 Inherited::VisitTypedefNameDecl(D);
338 void VisitTypedefDecl(const TypedefDecl *D) {
339 Inherited::VisitTypedefDecl(D);
342 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
343 Inherited::VisitTypeAliasDecl(D);
346 void VisitFriendDecl(const FriendDecl *D) {
347 TypeSourceInfo *TSI = D->getFriendType();
348 Hash.AddBoolean(TSI);
350 AddQualType(TSI->getType());
352 AddDecl(D->getFriendDecl());
356 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
357 // Only care about default arguments as part of the definition.
358 const bool hasDefaultArgument =
359 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
360 Hash.AddBoolean(hasDefaultArgument);
361 if (hasDefaultArgument) {
362 AddTemplateArgument(D->getDefaultArgument());
364 Hash.AddBoolean(D->isParameterPack());
366 Inherited::VisitTemplateTypeParmDecl(D);
369 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
370 // Only care about default arguments as part of the definition.
371 const bool hasDefaultArgument =
372 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
373 Hash.AddBoolean(hasDefaultArgument);
374 if (hasDefaultArgument) {
375 AddStmt(D->getDefaultArgument());
377 Hash.AddBoolean(D->isParameterPack());
379 Inherited::VisitNonTypeTemplateParmDecl(D);
382 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
383 // Only care about default arguments as part of the definition.
384 const bool hasDefaultArgument =
385 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
386 Hash.AddBoolean(hasDefaultArgument);
387 if (hasDefaultArgument) {
388 AddTemplateArgument(D->getDefaultArgument().getArgument());
390 Hash.AddBoolean(D->isParameterPack());
392 Inherited::VisitTemplateTemplateParmDecl(D);
395 void VisitTemplateDecl(const TemplateDecl *D) {
396 Hash.AddTemplateParameterList(D->getTemplateParameters());
398 Inherited::VisitTemplateDecl(D);
401 void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
402 Hash.AddBoolean(D->isMemberSpecialization());
403 Inherited::VisitRedeclarableTemplateDecl(D);
406 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
407 AddDecl(D->getTemplatedDecl());
408 Inherited::VisitFunctionTemplateDecl(D);
411 void VisitEnumConstantDecl(const EnumConstantDecl *D) {
412 AddStmt(D->getInitExpr());
413 Inherited::VisitEnumConstantDecl(D);
418 // Only allow a small portion of Decl's to be processed. Remove this once
419 // all Decl's can be handled.
420 bool ODRHash::isWhitelistedDecl(const Decl *D, const DeclContext *Parent) {
421 if (D->isImplicit()) return false;
422 if (D->getDeclContext() != Parent) return false;
424 switch (D->getKind()) {
427 case Decl::AccessSpec:
428 case Decl::CXXConstructor:
429 case Decl::CXXDestructor:
430 case Decl::CXXMethod:
431 case Decl::EnumConstant: // Only found in EnumDecl's.
434 case Decl::FunctionTemplate:
435 case Decl::StaticAssert:
436 case Decl::TypeAlias:
443 void ODRHash::AddSubDecl(const Decl *D) {
444 assert(D && "Expecting non-null pointer.");
446 ODRDeclVisitor(ID, *this).Visit(D);
449 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
450 assert(Record && Record->hasDefinition() &&
451 "Expected non-null record to be a definition.");
453 const DeclContext *DC = Record;
455 if (isa<ClassTemplateSpecializationDecl>(DC)) {
458 DC = DC->getParent();
463 // Filter out sub-Decls which will not be processed in order to get an
464 // accurate count of Decl's.
465 llvm::SmallVector<const Decl *, 16> Decls;
466 for (Decl *SubDecl : Record->decls()) {
467 if (isWhitelistedDecl(SubDecl, Record)) {
468 Decls.push_back(SubDecl);
469 if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
470 // Compute/Preload ODRHash into FunctionDecl.
471 Function->getODRHash();
476 ID.AddInteger(Decls.size());
477 for (auto SubDecl : Decls) {
481 const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
484 AddTemplateParameterList(TD->getTemplateParameters());
487 ID.AddInteger(Record->getNumBases());
488 auto Bases = Record->bases();
489 for (auto Base : Bases) {
490 AddQualType(Base.getType());
491 ID.AddInteger(Base.isVirtual());
492 ID.AddInteger(Base.getAccessSpecifierAsWritten());
496 void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
498 assert(Function && "Expecting non-null pointer.");
500 // Skip functions that are specializations or in specialization context.
501 const DeclContext *DC = Function;
503 if (isa<ClassTemplateSpecializationDecl>(DC)) return;
504 if (auto *F = dyn_cast<FunctionDecl>(DC)) {
505 if (F->isFunctionTemplateSpecialization()) {
506 if (!isa<CXXMethodDecl>(DC)) return;
507 if (DC->getLexicalParent()->isFileContext()) return;
508 // Inline method specializations are the only supported
509 // specialization for now.
512 DC = DC->getParent();
515 ID.AddInteger(Function->getDeclKind());
517 const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
518 AddBoolean(SpecializationArgs);
519 if (SpecializationArgs) {
520 ID.AddInteger(SpecializationArgs->size());
521 for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
522 AddTemplateArgument(TA);
526 if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
527 AddBoolean(Method->isConst());
528 AddBoolean(Method->isVolatile());
531 ID.AddInteger(Function->getStorageClass());
532 AddBoolean(Function->isInlineSpecified());
533 AddBoolean(Function->isVirtualAsWritten());
534 AddBoolean(Function->isPure());
535 AddBoolean(Function->isDeletedAsWritten());
536 AddBoolean(Function->isExplicitlyDefaulted());
540 AddQualType(Function->getReturnType());
542 ID.AddInteger(Function->param_size());
543 for (auto Param : Function->parameters())
551 const bool HasBody = Function->isThisDeclarationADefinition() &&
552 !Function->isDefaulted() && !Function->isDeleted() &&
553 !Function->isLateTemplateParsed();
556 auto *Body = Function->getBody();
563 void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
565 AddDeclarationName(Enum->getDeclName());
567 AddBoolean(Enum->isScoped());
568 if (Enum->isScoped())
569 AddBoolean(Enum->isScopedUsingClassTag());
571 if (Enum->getIntegerTypeSourceInfo())
572 AddQualType(Enum->getIntegerType());
574 // Filter out sub-Decls which will not be processed in order to get an
575 // accurate count of Decl's.
576 llvm::SmallVector<const Decl *, 16> Decls;
577 for (Decl *SubDecl : Enum->decls()) {
578 if (isWhitelistedDecl(SubDecl, Enum)) {
579 assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
580 Decls.push_back(SubDecl);
584 ID.AddInteger(Decls.size());
585 for (auto SubDecl : Decls) {
591 void ODRHash::AddDecl(const Decl *D) {
592 assert(D && "Expecting non-null pointer.");
593 D = D->getCanonicalDecl();
595 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
596 AddDeclarationName(ND->getDeclName());
600 ID.AddInteger(D->getKind());
601 // TODO: Handle non-NamedDecl here.
605 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
606 // methods process the relevant parts of the Type.
607 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
608 typedef TypeVisitor<ODRTypeVisitor> Inherited;
609 llvm::FoldingSetNodeID &ID;
613 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
614 : ID(ID), Hash(Hash) {}
616 void AddStmt(Stmt *S) {
623 void AddDecl(Decl *D) {
630 void AddQualType(QualType T) {
634 void AddType(const Type *T) {
641 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
642 Hash.AddBoolean(NNS);
644 Hash.AddNestedNameSpecifier(NNS);
648 void AddIdentifierInfo(const IdentifierInfo *II) {
651 Hash.AddIdentifierInfo(II);
655 void VisitQualifiers(Qualifiers Quals) {
656 ID.AddInteger(Quals.getAsOpaqueValue());
659 void Visit(const Type *T) {
660 ID.AddInteger(T->getTypeClass());
664 void VisitType(const Type *T) {}
666 void VisitAdjustedType(const AdjustedType *T) {
667 AddQualType(T->getOriginalType());
668 AddQualType(T->getAdjustedType());
672 void VisitDecayedType(const DecayedType *T) {
673 AddQualType(T->getDecayedType());
674 AddQualType(T->getPointeeType());
675 VisitAdjustedType(T);
678 void VisitArrayType(const ArrayType *T) {
679 AddQualType(T->getElementType());
680 ID.AddInteger(T->getSizeModifier());
681 VisitQualifiers(T->getIndexTypeQualifiers());
684 void VisitConstantArrayType(const ConstantArrayType *T) {
685 T->getSize().Profile(ID);
689 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
690 AddStmt(T->getSizeExpr());
694 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
698 void VisitVariableArrayType(const VariableArrayType *T) {
699 AddStmt(T->getSizeExpr());
703 void VisitBuiltinType(const BuiltinType *T) {
704 ID.AddInteger(T->getKind());
708 void VisitFunctionType(const FunctionType *T) {
709 AddQualType(T->getReturnType());
710 T->getExtInfo().Profile(ID);
711 Hash.AddBoolean(T->isConst());
712 Hash.AddBoolean(T->isVolatile());
713 Hash.AddBoolean(T->isRestrict());
717 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
718 VisitFunctionType(T);
721 void VisitFunctionProtoType(const FunctionProtoType *T) {
722 ID.AddInteger(T->getNumParams());
723 for (auto ParamType : T->getParamTypes())
724 AddQualType(ParamType);
726 VisitFunctionType(T);
729 void VisitPointerType(const PointerType *T) {
730 AddQualType(T->getPointeeType());
734 void VisitReferenceType(const ReferenceType *T) {
735 AddQualType(T->getPointeeTypeAsWritten());
739 void VisitLValueReferenceType(const LValueReferenceType *T) {
740 VisitReferenceType(T);
743 void VisitRValueReferenceType(const RValueReferenceType *T) {
744 VisitReferenceType(T);
747 void VisitTypedefType(const TypedefType *T) {
748 AddDecl(T->getDecl());
749 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
750 VisitQualifiers(UnderlyingType.getQualifiers());
752 if (const TypedefType *Underlying =
753 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
754 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
757 if (const ElaboratedType *Underlying =
758 dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
759 UnderlyingType = Underlying->getNamedType();
765 AddType(UnderlyingType.getTypePtr());
769 void VisitTagType(const TagType *T) {
770 AddDecl(T->getDecl());
774 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
775 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
777 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
778 ID.AddInteger(T->getKeyword());
782 void VisitDependentNameType(const DependentNameType *T) {
783 AddNestedNameSpecifier(T->getQualifier());
784 AddIdentifierInfo(T->getIdentifier());
785 VisitTypeWithKeyword(T);
788 void VisitDependentTemplateSpecializationType(
789 const DependentTemplateSpecializationType *T) {
790 AddIdentifierInfo(T->getIdentifier());
791 AddNestedNameSpecifier(T->getQualifier());
792 ID.AddInteger(T->getNumArgs());
793 for (const auto &TA : T->template_arguments()) {
794 Hash.AddTemplateArgument(TA);
796 VisitTypeWithKeyword(T);
799 void VisitElaboratedType(const ElaboratedType *T) {
800 AddNestedNameSpecifier(T->getQualifier());
801 AddQualType(T->getNamedType());
802 VisitTypeWithKeyword(T);
805 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
806 ID.AddInteger(T->getNumArgs());
807 for (const auto &TA : T->template_arguments()) {
808 Hash.AddTemplateArgument(TA);
810 Hash.AddTemplateName(T->getTemplateName());
814 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
815 ID.AddInteger(T->getDepth());
816 ID.AddInteger(T->getIndex());
817 Hash.AddBoolean(T->isParameterPack());
818 AddDecl(T->getDecl());
823 void ODRHash::AddType(const Type *T) {
824 assert(T && "Expecting non-null pointer.");
825 ODRTypeVisitor(ID, *this).Visit(T);
828 void ODRHash::AddQualType(QualType T) {
829 AddBoolean(T.isNull());
832 SplitQualType split = T.split();
833 ID.AddInteger(split.Quals.getAsOpaqueValue());
837 void ODRHash::AddBoolean(bool Value) {
838 Bools.push_back(Value);