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 // Unlike the other pointer handling functions, allow null pointers here.
91 // Skip inlined namespaces.
92 auto Kind = NNS->getKind();
93 if (Kind == NestedNameSpecifier::Namespace) {
94 if (NNS->getAsNamespace()->isInline()) {
95 return AddNestedNameSpecifier(NNS->getPrefix());
102 AddNestedNameSpecifier(NNS->getPrefix());
106 case NestedNameSpecifier::Identifier:
107 AddIdentifierInfo(NNS->getAsIdentifier());
109 case NestedNameSpecifier::Namespace:
110 AddDecl(NNS->getAsNamespace());
112 case NestedNameSpecifier::NamespaceAlias:
113 AddDecl(NNS->getAsNamespaceAlias());
115 case NestedNameSpecifier::TypeSpec:
116 case NestedNameSpecifier::TypeSpecWithTemplate:
117 AddType(NNS->getAsType());
119 case NestedNameSpecifier::Global:
120 case NestedNameSpecifier::Super:
125 void ODRHash::AddTemplateName(TemplateName Name) {
126 auto Kind = Name.getKind();
130 case TemplateName::Template:
131 AddDecl(Name.getAsTemplateDecl());
133 // TODO: Support these cases.
134 case TemplateName::OverloadedTemplate:
135 case TemplateName::QualifiedTemplate:
136 case TemplateName::DependentTemplate:
137 case TemplateName::SubstTemplateTemplateParm:
138 case TemplateName::SubstTemplateTemplateParmPack:
143 void ODRHash::AddTemplateArgument(TemplateArgument TA) {
144 const auto Kind = TA.getKind();
148 case TemplateArgument::Null:
149 case TemplateArgument::Type:
150 case TemplateArgument::Declaration:
151 case TemplateArgument::NullPtr:
152 case TemplateArgument::Integral:
154 case TemplateArgument::Template:
155 case TemplateArgument::TemplateExpansion:
156 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
158 case TemplateArgument::Expression:
159 AddStmt(TA.getAsExpr());
161 case TemplateArgument::Pack:
162 ID.AddInteger(TA.pack_size());
163 for (auto SubTA : TA.pack_elements()) {
164 AddTemplateArgument(SubTA);
170 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
172 void ODRHash::clear() {
179 unsigned ODRHash::CalculateHash() {
180 // Append the bools to the end of the data segment backwards. This allows
181 // for the bools data to be compressed 32 times smaller compared to using
183 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
184 const unsigned size = Bools.size();
185 const unsigned remainder = size % unsigned_bits;
186 const unsigned loops = size / unsigned_bits;
187 auto I = Bools.rbegin();
189 for (unsigned i = 0; i < remainder; ++i) {
194 ID.AddInteger(value);
196 for (unsigned i = 0; i < loops; ++i) {
198 for (unsigned j = 0; j < unsigned_bits; ++j) {
203 ID.AddInteger(value);
206 assert(I == Bools.rend());
208 return ID.ComputeHash();
211 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
212 // methods process the relevant parts of the Decl.
213 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
214 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
215 llvm::FoldingSetNodeID &ID;
219 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
220 : ID(ID), Hash(Hash) {}
222 void AddStmt(const Stmt *S) {
229 void AddIdentifierInfo(const IdentifierInfo *II) {
232 Hash.AddIdentifierInfo(II);
236 void AddQualType(QualType T) {
240 void Visit(const Decl *D) {
241 ID.AddInteger(D->getKind());
245 void VisitNamedDecl(const NamedDecl *D) {
246 Hash.AddDeclarationName(D->getDeclName());
247 Inherited::VisitNamedDecl(D);
250 void VisitValueDecl(const ValueDecl *D) {
251 AddQualType(D->getType());
252 Inherited::VisitValueDecl(D);
255 void VisitVarDecl(const VarDecl *D) {
256 Hash.AddBoolean(D->isStaticLocal());
257 Hash.AddBoolean(D->isConstexpr());
258 const bool HasInit = D->hasInit();
259 Hash.AddBoolean(HasInit);
261 AddStmt(D->getInit());
263 Inherited::VisitVarDecl(D);
266 void VisitParmVarDecl(const ParmVarDecl *D) {
267 // TODO: Handle default arguments.
268 Inherited::VisitParmVarDecl(D);
271 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
272 ID.AddInteger(D->getAccess());
273 Inherited::VisitAccessSpecDecl(D);
276 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
277 AddStmt(D->getAssertExpr());
278 AddStmt(D->getMessage());
280 Inherited::VisitStaticAssertDecl(D);
283 void VisitFieldDecl(const FieldDecl *D) {
284 const bool IsBitfield = D->isBitField();
285 Hash.AddBoolean(IsBitfield);
288 AddStmt(D->getBitWidth());
291 Hash.AddBoolean(D->isMutable());
292 AddStmt(D->getInClassInitializer());
294 Inherited::VisitFieldDecl(D);
297 void VisitFunctionDecl(const FunctionDecl *D) {
298 ID.AddInteger(D->getStorageClass());
299 Hash.AddBoolean(D->isInlineSpecified());
300 Hash.AddBoolean(D->isVirtualAsWritten());
301 Hash.AddBoolean(D->isPure());
302 Hash.AddBoolean(D->isDeletedAsWritten());
304 ID.AddInteger(D->param_size());
306 for (auto *Param : D->parameters()) {
307 Hash.AddSubDecl(Param);
310 Inherited::VisitFunctionDecl(D);
313 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
314 Hash.AddBoolean(D->isConst());
315 Hash.AddBoolean(D->isVolatile());
317 Inherited::VisitCXXMethodDecl(D);
320 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
321 AddQualType(D->getUnderlyingType());
323 Inherited::VisitTypedefNameDecl(D);
326 void VisitTypedefDecl(const TypedefDecl *D) {
327 Inherited::VisitTypedefDecl(D);
330 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
331 Inherited::VisitTypeAliasDecl(D);
335 // Only allow a small portion of Decl's to be processed. Remove this once
336 // all Decl's can be handled.
337 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
338 if (D->isImplicit()) return false;
339 if (D->getDeclContext() != Parent) return false;
341 switch (D->getKind()) {
344 case Decl::AccessSpec:
345 case Decl::CXXMethod:
347 case Decl::StaticAssert:
348 case Decl::TypeAlias:
355 void ODRHash::AddSubDecl(const Decl *D) {
356 assert(D && "Expecting non-null pointer.");
359 ODRDeclVisitor(ID, *this).Visit(D);
362 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
363 assert(Record && Record->hasDefinition() &&
364 "Expected non-null record to be a definition.");
366 if (isa<ClassTemplateSpecializationDecl>(Record)) {
372 // Filter out sub-Decls which will not be processed in order to get an
373 // accurate count of Decl's.
374 llvm::SmallVector<const Decl *, 16> Decls;
375 for (const Decl *SubDecl : Record->decls()) {
376 if (isWhitelistedDecl(SubDecl, Record)) {
377 Decls.push_back(SubDecl);
381 ID.AddInteger(Decls.size());
382 for (auto SubDecl : Decls) {
387 void ODRHash::AddDecl(const Decl *D) {
388 assert(D && "Expecting non-null pointer.");
389 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
390 ID.AddInteger(Result.first->second);
391 // On first encounter of a Decl pointer, process it. Every time afterwards,
392 // only the index value is needed.
393 if (!Result.second) {
397 ID.AddInteger(D->getKind());
399 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
400 AddDeclarationName(ND->getDeclName());
404 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
405 // methods process the relevant parts of the Type.
406 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
407 typedef TypeVisitor<ODRTypeVisitor> Inherited;
408 llvm::FoldingSetNodeID &ID;
412 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
413 : ID(ID), Hash(Hash) {}
415 void AddStmt(Stmt *S) {
422 void AddDecl(Decl *D) {
429 void AddQualType(QualType T) {
433 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
434 Hash.AddNestedNameSpecifier(NNS);
437 void AddIdentifierInfo(const IdentifierInfo *II) {
440 Hash.AddIdentifierInfo(II);
444 void VisitQualifiers(Qualifiers Quals) {
445 ID.AddInteger(Quals.getAsOpaqueValue());
448 void Visit(const Type *T) {
449 ID.AddInteger(T->getTypeClass());
453 void VisitType(const Type *T) {}
455 void VisitAdjustedType(const AdjustedType *T) {
456 AddQualType(T->getOriginalType());
457 AddQualType(T->getAdjustedType());
461 void VisitDecayedType(const DecayedType *T) {
462 AddQualType(T->getDecayedType());
463 AddQualType(T->getPointeeType());
464 VisitAdjustedType(T);
467 void VisitArrayType(const ArrayType *T) {
468 AddQualType(T->getElementType());
469 ID.AddInteger(T->getSizeModifier());
470 VisitQualifiers(T->getIndexTypeQualifiers());
473 void VisitConstantArrayType(const ConstantArrayType *T) {
474 T->getSize().Profile(ID);
478 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
479 AddStmt(T->getSizeExpr());
483 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
487 void VisitVariableArrayType(const VariableArrayType *T) {
488 AddStmt(T->getSizeExpr());
492 void VisitBuiltinType(const BuiltinType *T) {
493 ID.AddInteger(T->getKind());
497 void VisitFunctionType(const FunctionType *T) {
498 AddQualType(T->getReturnType());
499 T->getExtInfo().Profile(ID);
500 Hash.AddBoolean(T->isConst());
501 Hash.AddBoolean(T->isVolatile());
502 Hash.AddBoolean(T->isRestrict());
506 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
507 VisitFunctionType(T);
510 void VisitFunctionProtoType(const FunctionProtoType *T) {
511 ID.AddInteger(T->getNumParams());
512 for (auto ParamType : T->getParamTypes())
513 AddQualType(ParamType);
515 VisitFunctionType(T);
518 void VisitTypedefType(const TypedefType *T) {
519 AddDecl(T->getDecl());
520 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
524 void VisitTagType(const TagType *T) {
525 AddDecl(T->getDecl());
529 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
530 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
532 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
533 ID.AddInteger(T->getKeyword());
537 void VisitDependentNameType(const DependentNameType *T) {
538 AddNestedNameSpecifier(T->getQualifier());
539 AddIdentifierInfo(T->getIdentifier());
540 VisitTypeWithKeyword(T);
543 void VisitDependentTemplateSpecializationType(
544 const DependentTemplateSpecializationType *T) {
545 AddIdentifierInfo(T->getIdentifier());
546 AddNestedNameSpecifier(T->getQualifier());
547 ID.AddInteger(T->getNumArgs());
548 for (const auto &TA : T->template_arguments()) {
549 Hash.AddTemplateArgument(TA);
551 VisitTypeWithKeyword(T);
554 void VisitElaboratedType(const ElaboratedType *T) {
555 AddNestedNameSpecifier(T->getQualifier());
556 AddQualType(T->getNamedType());
557 VisitTypeWithKeyword(T);
560 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
561 ID.AddInteger(T->getNumArgs());
562 for (const auto &TA : T->template_arguments()) {
563 Hash.AddTemplateArgument(TA);
565 Hash.AddTemplateName(T->getTemplateName());
569 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
570 ID.AddInteger(T->getDepth());
571 ID.AddInteger(T->getIndex());
572 Hash.AddBoolean(T->isParameterPack());
573 AddDecl(T->getDecl());
577 void ODRHash::AddType(const Type *T) {
578 assert(T && "Expecting non-null pointer.");
579 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
580 ID.AddInteger(Result.first->second);
581 // On first encounter of a Type pointer, process it. Every time afterwards,
582 // only the index value is needed.
583 if (!Result.second) {
587 ODRTypeVisitor(ID, *this).Visit(T);
590 void ODRHash::AddQualType(QualType T) {
591 AddBoolean(T.isNull());
594 SplitQualType split = T.split();
595 ID.AddInteger(split.Quals.getAsOpaqueValue());
599 void ODRHash::AddBoolean(bool Value) {
600 Bools.push_back(Value);