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 Visit(const Decl *D) {
232 ID.AddInteger(D->getKind());
236 void VisitNamedDecl(const NamedDecl *D) {
237 Hash.AddDeclarationName(D->getDeclName());
238 Inherited::VisitNamedDecl(D);
241 void VisitValueDecl(const ValueDecl *D) {
242 AddQualType(D->getType());
243 Inherited::VisitValueDecl(D);
246 void VisitVarDecl(const VarDecl *D) {
247 Hash.AddBoolean(D->isStaticLocal());
248 Hash.AddBoolean(D->isConstexpr());
249 const bool HasInit = D->hasInit();
250 Hash.AddBoolean(HasInit);
252 AddStmt(D->getInit());
254 Inherited::VisitVarDecl(D);
257 void VisitParmVarDecl(const ParmVarDecl *D) {
258 // TODO: Handle default arguments.
259 Inherited::VisitParmVarDecl(D);
262 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
263 ID.AddInteger(D->getAccess());
264 Inherited::VisitAccessSpecDecl(D);
267 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
268 AddStmt(D->getAssertExpr());
269 AddStmt(D->getMessage());
271 Inherited::VisitStaticAssertDecl(D);
274 void VisitFieldDecl(const FieldDecl *D) {
275 const bool IsBitfield = D->isBitField();
276 Hash.AddBoolean(IsBitfield);
279 AddStmt(D->getBitWidth());
282 Hash.AddBoolean(D->isMutable());
283 AddStmt(D->getInClassInitializer());
285 Inherited::VisitFieldDecl(D);
288 void VisitFunctionDecl(const FunctionDecl *D) {
289 ID.AddInteger(D->getStorageClass());
290 Hash.AddBoolean(D->isInlineSpecified());
291 Hash.AddBoolean(D->isVirtualAsWritten());
292 Hash.AddBoolean(D->isPure());
293 Hash.AddBoolean(D->isDeletedAsWritten());
295 ID.AddInteger(D->param_size());
297 for (auto *Param : D->parameters()) {
298 Hash.AddSubDecl(Param);
301 Inherited::VisitFunctionDecl(D);
304 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
305 Hash.AddBoolean(D->isConst());
306 Hash.AddBoolean(D->isVolatile());
308 Inherited::VisitCXXMethodDecl(D);
311 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
312 AddQualType(D->getUnderlyingType());
314 Inherited::VisitTypedefNameDecl(D);
317 void VisitTypedefDecl(const TypedefDecl *D) {
318 Inherited::VisitTypedefDecl(D);
321 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
322 Inherited::VisitTypeAliasDecl(D);
326 // Only allow a small portion of Decl's to be processed. Remove this once
327 // all Decl's can be handled.
328 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
329 if (D->isImplicit()) return false;
330 if (D->getDeclContext() != Parent) return false;
332 switch (D->getKind()) {
335 case Decl::AccessSpec:
336 case Decl::CXXMethod:
338 case Decl::StaticAssert:
339 case Decl::TypeAlias:
346 void ODRHash::AddSubDecl(const Decl *D) {
347 assert(D && "Expecting non-null pointer.");
350 ODRDeclVisitor(ID, *this).Visit(D);
353 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
354 assert(Record && Record->hasDefinition() &&
355 "Expected non-null record to be a definition.");
357 if (isa<ClassTemplateSpecializationDecl>(Record)) {
363 // Filter out sub-Decls which will not be processed in order to get an
364 // accurate count of Decl's.
365 llvm::SmallVector<const Decl *, 16> Decls;
366 for (const Decl *SubDecl : Record->decls()) {
367 if (isWhitelistedDecl(SubDecl, Record)) {
368 Decls.push_back(SubDecl);
372 ID.AddInteger(Decls.size());
373 for (auto SubDecl : Decls) {
378 void ODRHash::AddDecl(const Decl *D) {
379 assert(D && "Expecting non-null pointer.");
380 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
381 ID.AddInteger(Result.first->second);
382 // On first encounter of a Decl pointer, process it. Every time afterwards,
383 // only the index value is needed.
384 if (!Result.second) {
388 ID.AddInteger(D->getKind());
390 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
391 AddDeclarationName(ND->getDeclName());
395 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
396 // methods process the relevant parts of the Type.
397 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
398 typedef TypeVisitor<ODRTypeVisitor> Inherited;
399 llvm::FoldingSetNodeID &ID;
403 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
404 : ID(ID), Hash(Hash) {}
406 void AddStmt(Stmt *S) {
413 void AddDecl(Decl *D) {
420 void AddQualType(QualType T) {
424 void AddType(const Type *T) {
431 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
432 Hash.AddBoolean(NNS);
434 Hash.AddNestedNameSpecifier(NNS);
438 void AddIdentifierInfo(const IdentifierInfo *II) {
441 Hash.AddIdentifierInfo(II);
445 void VisitQualifiers(Qualifiers Quals) {
446 ID.AddInteger(Quals.getAsOpaqueValue());
449 void Visit(const Type *T) {
450 ID.AddInteger(T->getTypeClass());
454 void VisitType(const Type *T) {}
456 void VisitAdjustedType(const AdjustedType *T) {
457 AddQualType(T->getOriginalType());
458 AddQualType(T->getAdjustedType());
462 void VisitDecayedType(const DecayedType *T) {
463 AddQualType(T->getDecayedType());
464 AddQualType(T->getPointeeType());
465 VisitAdjustedType(T);
468 void VisitArrayType(const ArrayType *T) {
469 AddQualType(T->getElementType());
470 ID.AddInteger(T->getSizeModifier());
471 VisitQualifiers(T->getIndexTypeQualifiers());
474 void VisitConstantArrayType(const ConstantArrayType *T) {
475 T->getSize().Profile(ID);
479 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
480 AddStmt(T->getSizeExpr());
484 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
488 void VisitVariableArrayType(const VariableArrayType *T) {
489 AddStmt(T->getSizeExpr());
493 void VisitBuiltinType(const BuiltinType *T) {
494 ID.AddInteger(T->getKind());
498 void VisitFunctionType(const FunctionType *T) {
499 AddQualType(T->getReturnType());
500 T->getExtInfo().Profile(ID);
501 Hash.AddBoolean(T->isConst());
502 Hash.AddBoolean(T->isVolatile());
503 Hash.AddBoolean(T->isRestrict());
507 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
508 VisitFunctionType(T);
511 void VisitFunctionProtoType(const FunctionProtoType *T) {
512 ID.AddInteger(T->getNumParams());
513 for (auto ParamType : T->getParamTypes())
514 AddQualType(ParamType);
516 VisitFunctionType(T);
519 void VisitTypedefType(const TypedefType *T) {
520 AddDecl(T->getDecl());
521 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
522 VisitQualifiers(UnderlyingType.getQualifiers());
523 while (const TypedefType *Underlying =
524 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
525 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
527 AddType(UnderlyingType.getTypePtr());
531 void VisitTagType(const TagType *T) {
532 AddDecl(T->getDecl());
536 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
537 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
539 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
540 ID.AddInteger(T->getKeyword());
544 void VisitDependentNameType(const DependentNameType *T) {
545 AddNestedNameSpecifier(T->getQualifier());
546 AddIdentifierInfo(T->getIdentifier());
547 VisitTypeWithKeyword(T);
550 void VisitDependentTemplateSpecializationType(
551 const DependentTemplateSpecializationType *T) {
552 AddIdentifierInfo(T->getIdentifier());
553 AddNestedNameSpecifier(T->getQualifier());
554 ID.AddInteger(T->getNumArgs());
555 for (const auto &TA : T->template_arguments()) {
556 Hash.AddTemplateArgument(TA);
558 VisitTypeWithKeyword(T);
561 void VisitElaboratedType(const ElaboratedType *T) {
562 AddNestedNameSpecifier(T->getQualifier());
563 AddQualType(T->getNamedType());
564 VisitTypeWithKeyword(T);
567 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
568 ID.AddInteger(T->getNumArgs());
569 for (const auto &TA : T->template_arguments()) {
570 Hash.AddTemplateArgument(TA);
572 Hash.AddTemplateName(T->getTemplateName());
576 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
577 ID.AddInteger(T->getDepth());
578 ID.AddInteger(T->getIndex());
579 Hash.AddBoolean(T->isParameterPack());
580 AddDecl(T->getDecl());
584 void ODRHash::AddType(const Type *T) {
585 assert(T && "Expecting non-null pointer.");
586 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
587 ID.AddInteger(Result.first->second);
588 // On first encounter of a Type pointer, process it. Every time afterwards,
589 // only the index value is needed.
590 if (!Result.second) {
594 ODRTypeVisitor(ID, *this).Visit(T);
597 void ODRHash::AddQualType(QualType T) {
598 AddBoolean(T.isNull());
601 SplitQualType split = T.split();
602 ID.AddInteger(split.Quals.getAsOpaqueValue());
606 void ODRHash::AddBoolean(bool Value) {
607 Bools.push_back(Value);