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 AddBoolean(Name.isEmpty());
40 auto Kind = Name.getNameKind();
43 case DeclarationName::Identifier:
44 AddIdentifierInfo(Name.getAsIdentifierInfo());
46 case DeclarationName::ObjCZeroArgSelector:
47 case DeclarationName::ObjCOneArgSelector:
48 case DeclarationName::ObjCMultiArgSelector: {
49 Selector S = Name.getObjCSelector();
50 AddBoolean(S.isNull());
51 AddBoolean(S.isKeywordSelector());
52 AddBoolean(S.isUnarySelector());
53 unsigned NumArgs = S.getNumArgs();
54 for (unsigned i = 0; i < NumArgs; ++i) {
55 AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
59 case DeclarationName::CXXConstructorName:
60 case DeclarationName::CXXDestructorName:
61 AddQualType(Name.getCXXNameType());
63 case DeclarationName::CXXOperatorName:
64 ID.AddInteger(Name.getCXXOverloadedOperator());
66 case DeclarationName::CXXLiteralOperatorName:
67 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
69 case DeclarationName::CXXConversionFunctionName:
70 AddQualType(Name.getCXXNameType());
72 case DeclarationName::CXXUsingDirective:
74 case DeclarationName::CXXDeductionGuideName: {
75 auto *Template = Name.getCXXDeductionGuideTemplate();
84 void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
85 assert(NNS && "Expecting non-null pointer.");
86 const auto *Prefix = NNS->getPrefix();
89 AddNestedNameSpecifier(Prefix);
91 auto Kind = NNS->getKind();
94 case NestedNameSpecifier::Identifier:
95 AddIdentifierInfo(NNS->getAsIdentifier());
97 case NestedNameSpecifier::Namespace:
98 AddDecl(NNS->getAsNamespace());
100 case NestedNameSpecifier::NamespaceAlias:
101 AddDecl(NNS->getAsNamespaceAlias());
103 case NestedNameSpecifier::TypeSpec:
104 case NestedNameSpecifier::TypeSpecWithTemplate:
105 AddType(NNS->getAsType());
107 case NestedNameSpecifier::Global:
108 case NestedNameSpecifier::Super:
113 void ODRHash::AddTemplateName(TemplateName Name) {
114 auto Kind = Name.getKind();
118 case TemplateName::Template:
119 AddDecl(Name.getAsTemplateDecl());
121 // TODO: Support these cases.
122 case TemplateName::OverloadedTemplate:
123 case TemplateName::QualifiedTemplate:
124 case TemplateName::DependentTemplate:
125 case TemplateName::SubstTemplateTemplateParm:
126 case TemplateName::SubstTemplateTemplateParmPack:
131 void ODRHash::AddTemplateArgument(TemplateArgument TA) {
132 const auto Kind = TA.getKind();
136 case TemplateArgument::Null:
137 llvm_unreachable("Expected valid TemplateArgument");
138 case TemplateArgument::Type:
139 AddQualType(TA.getAsType());
141 case TemplateArgument::Declaration:
142 case TemplateArgument::NullPtr:
143 case TemplateArgument::Integral:
145 case TemplateArgument::Template:
146 case TemplateArgument::TemplateExpansion:
147 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
149 case TemplateArgument::Expression:
150 AddStmt(TA.getAsExpr());
152 case TemplateArgument::Pack:
153 ID.AddInteger(TA.pack_size());
154 for (auto SubTA : TA.pack_elements()) {
155 AddTemplateArgument(SubTA);
161 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
163 void ODRHash::clear() {
170 unsigned ODRHash::CalculateHash() {
171 // Append the bools to the end of the data segment backwards. This allows
172 // for the bools data to be compressed 32 times smaller compared to using
174 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
175 const unsigned size = Bools.size();
176 const unsigned remainder = size % unsigned_bits;
177 const unsigned loops = size / unsigned_bits;
178 auto I = Bools.rbegin();
180 for (unsigned i = 0; i < remainder; ++i) {
185 ID.AddInteger(value);
187 for (unsigned i = 0; i < loops; ++i) {
189 for (unsigned j = 0; j < unsigned_bits; ++j) {
194 ID.AddInteger(value);
197 assert(I == Bools.rend());
199 return ID.ComputeHash();
202 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
203 // methods process the relevant parts of the Decl.
204 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
205 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
206 llvm::FoldingSetNodeID &ID;
210 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
211 : ID(ID), Hash(Hash) {}
213 void AddStmt(const Stmt *S) {
220 void AddIdentifierInfo(const IdentifierInfo *II) {
223 Hash.AddIdentifierInfo(II);
227 void AddQualType(QualType T) {
231 void AddDecl(const Decl *D) {
238 void Visit(const Decl *D) {
239 ID.AddInteger(D->getKind());
243 void VisitNamedDecl(const NamedDecl *D) {
244 Hash.AddDeclarationName(D->getDeclName());
245 Inherited::VisitNamedDecl(D);
248 void VisitValueDecl(const ValueDecl *D) {
249 AddQualType(D->getType());
250 Inherited::VisitValueDecl(D);
253 void VisitVarDecl(const VarDecl *D) {
254 Hash.AddBoolean(D->isStaticLocal());
255 Hash.AddBoolean(D->isConstexpr());
256 const bool HasInit = D->hasInit();
257 Hash.AddBoolean(HasInit);
259 AddStmt(D->getInit());
261 Inherited::VisitVarDecl(D);
264 void VisitParmVarDecl(const ParmVarDecl *D) {
265 // TODO: Handle default arguments.
266 Inherited::VisitParmVarDecl(D);
269 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
270 ID.AddInteger(D->getAccess());
271 Inherited::VisitAccessSpecDecl(D);
274 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
275 AddStmt(D->getAssertExpr());
276 AddStmt(D->getMessage());
278 Inherited::VisitStaticAssertDecl(D);
281 void VisitFieldDecl(const FieldDecl *D) {
282 const bool IsBitfield = D->isBitField();
283 Hash.AddBoolean(IsBitfield);
286 AddStmt(D->getBitWidth());
289 Hash.AddBoolean(D->isMutable());
290 AddStmt(D->getInClassInitializer());
292 Inherited::VisitFieldDecl(D);
295 void VisitFunctionDecl(const FunctionDecl *D) {
296 ID.AddInteger(D->getStorageClass());
297 Hash.AddBoolean(D->isInlineSpecified());
298 Hash.AddBoolean(D->isVirtualAsWritten());
299 Hash.AddBoolean(D->isPure());
300 Hash.AddBoolean(D->isDeletedAsWritten());
302 ID.AddInteger(D->param_size());
304 for (auto *Param : D->parameters()) {
305 Hash.AddSubDecl(Param);
308 Inherited::VisitFunctionDecl(D);
311 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
312 Hash.AddBoolean(D->isConst());
313 Hash.AddBoolean(D->isVolatile());
315 Inherited::VisitCXXMethodDecl(D);
318 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
319 AddQualType(D->getUnderlyingType());
321 Inherited::VisitTypedefNameDecl(D);
324 void VisitTypedefDecl(const TypedefDecl *D) {
325 Inherited::VisitTypedefDecl(D);
328 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
329 Inherited::VisitTypeAliasDecl(D);
332 void VisitFriendDecl(const FriendDecl *D) {
333 TypeSourceInfo *TSI = D->getFriendType();
334 Hash.AddBoolean(TSI);
336 AddQualType(TSI->getType());
338 AddDecl(D->getFriendDecl());
343 // Only allow a small portion of Decl's to be processed. Remove this once
344 // all Decl's can be handled.
345 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
346 if (D->isImplicit()) return false;
347 if (D->getDeclContext() != Parent) return false;
349 switch (D->getKind()) {
352 case Decl::AccessSpec:
353 case Decl::CXXMethod:
356 case Decl::StaticAssert:
357 case Decl::TypeAlias:
364 void ODRHash::AddSubDecl(const Decl *D) {
365 assert(D && "Expecting non-null pointer.");
368 ODRDeclVisitor(ID, *this).Visit(D);
371 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
372 assert(Record && Record->hasDefinition() &&
373 "Expected non-null record to be a definition.");
375 if (isa<ClassTemplateSpecializationDecl>(Record)) {
381 // Filter out sub-Decls which will not be processed in order to get an
382 // accurate count of Decl's.
383 llvm::SmallVector<const Decl *, 16> Decls;
384 for (const Decl *SubDecl : Record->decls()) {
385 if (isWhitelistedDecl(SubDecl, Record)) {
386 Decls.push_back(SubDecl);
390 ID.AddInteger(Decls.size());
391 for (auto SubDecl : Decls) {
396 void ODRHash::AddDecl(const Decl *D) {
397 assert(D && "Expecting non-null pointer.");
398 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
399 ID.AddInteger(Result.first->second);
400 // On first encounter of a Decl pointer, process it. Every time afterwards,
401 // only the index value is needed.
402 if (!Result.second) {
406 ID.AddInteger(D->getKind());
408 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
409 AddDeclarationName(ND->getDeclName());
413 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
414 // methods process the relevant parts of the Type.
415 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
416 typedef TypeVisitor<ODRTypeVisitor> Inherited;
417 llvm::FoldingSetNodeID &ID;
421 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
422 : ID(ID), Hash(Hash) {}
424 void AddStmt(Stmt *S) {
431 void AddDecl(Decl *D) {
438 void AddQualType(QualType T) {
442 void AddType(const Type *T) {
449 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
450 Hash.AddBoolean(NNS);
452 Hash.AddNestedNameSpecifier(NNS);
456 void AddIdentifierInfo(const IdentifierInfo *II) {
459 Hash.AddIdentifierInfo(II);
463 void VisitQualifiers(Qualifiers Quals) {
464 ID.AddInteger(Quals.getAsOpaqueValue());
467 void Visit(const Type *T) {
468 ID.AddInteger(T->getTypeClass());
472 void VisitType(const Type *T) {}
474 void VisitAdjustedType(const AdjustedType *T) {
475 AddQualType(T->getOriginalType());
476 AddQualType(T->getAdjustedType());
480 void VisitDecayedType(const DecayedType *T) {
481 AddQualType(T->getDecayedType());
482 AddQualType(T->getPointeeType());
483 VisitAdjustedType(T);
486 void VisitArrayType(const ArrayType *T) {
487 AddQualType(T->getElementType());
488 ID.AddInteger(T->getSizeModifier());
489 VisitQualifiers(T->getIndexTypeQualifiers());
492 void VisitConstantArrayType(const ConstantArrayType *T) {
493 T->getSize().Profile(ID);
497 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
498 AddStmt(T->getSizeExpr());
502 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
506 void VisitVariableArrayType(const VariableArrayType *T) {
507 AddStmt(T->getSizeExpr());
511 void VisitBuiltinType(const BuiltinType *T) {
512 ID.AddInteger(T->getKind());
516 void VisitFunctionType(const FunctionType *T) {
517 AddQualType(T->getReturnType());
518 T->getExtInfo().Profile(ID);
519 Hash.AddBoolean(T->isConst());
520 Hash.AddBoolean(T->isVolatile());
521 Hash.AddBoolean(T->isRestrict());
525 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
526 VisitFunctionType(T);
529 void VisitFunctionProtoType(const FunctionProtoType *T) {
530 ID.AddInteger(T->getNumParams());
531 for (auto ParamType : T->getParamTypes())
532 AddQualType(ParamType);
534 VisitFunctionType(T);
537 void VisitTypedefType(const TypedefType *T) {
538 AddDecl(T->getDecl());
539 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
540 VisitQualifiers(UnderlyingType.getQualifiers());
541 while (const TypedefType *Underlying =
542 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
543 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
545 AddType(UnderlyingType.getTypePtr());
549 void VisitTagType(const TagType *T) {
550 AddDecl(T->getDecl());
554 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
555 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
557 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
558 ID.AddInteger(T->getKeyword());
562 void VisitDependentNameType(const DependentNameType *T) {
563 AddNestedNameSpecifier(T->getQualifier());
564 AddIdentifierInfo(T->getIdentifier());
565 VisitTypeWithKeyword(T);
568 void VisitDependentTemplateSpecializationType(
569 const DependentTemplateSpecializationType *T) {
570 AddIdentifierInfo(T->getIdentifier());
571 AddNestedNameSpecifier(T->getQualifier());
572 ID.AddInteger(T->getNumArgs());
573 for (const auto &TA : T->template_arguments()) {
574 Hash.AddTemplateArgument(TA);
576 VisitTypeWithKeyword(T);
579 void VisitElaboratedType(const ElaboratedType *T) {
580 AddNestedNameSpecifier(T->getQualifier());
581 AddQualType(T->getNamedType());
582 VisitTypeWithKeyword(T);
585 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
586 ID.AddInteger(T->getNumArgs());
587 for (const auto &TA : T->template_arguments()) {
588 Hash.AddTemplateArgument(TA);
590 Hash.AddTemplateName(T->getTemplateName());
594 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
595 ID.AddInteger(T->getDepth());
596 ID.AddInteger(T->getIndex());
597 Hash.AddBoolean(T->isParameterPack());
598 AddDecl(T->getDecl());
602 void ODRHash::AddType(const Type *T) {
603 assert(T && "Expecting non-null pointer.");
604 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
605 ID.AddInteger(Result.first->second);
606 // On first encounter of a Type pointer, process it. Every time afterwards,
607 // only the index value is needed.
608 if (!Result.second) {
612 ODRTypeVisitor(ID, *this).Visit(T);
615 void ODRHash::AddQualType(QualType T) {
616 AddBoolean(T.isNull());
619 SplitQualType split = T.split();
620 ID.AddInteger(split.Quals.getAsOpaqueValue());
624 void ODRHash::AddBoolean(bool Value) {
625 Bools.push_back(Value);