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 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
146 void ODRHash::clear() {
153 unsigned ODRHash::CalculateHash() {
154 // Append the bools to the end of the data segment backwards. This allows
155 // for the bools data to be compressed 32 times smaller compared to using
157 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
158 const unsigned size = Bools.size();
159 const unsigned remainder = size % unsigned_bits;
160 const unsigned loops = size / unsigned_bits;
161 auto I = Bools.rbegin();
163 for (unsigned i = 0; i < remainder; ++i) {
168 ID.AddInteger(value);
170 for (unsigned i = 0; i < loops; ++i) {
172 for (unsigned j = 0; j < unsigned_bits; ++j) {
177 ID.AddInteger(value);
180 assert(I == Bools.rend());
182 return ID.ComputeHash();
185 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
186 // methods process the relevant parts of the Decl.
187 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
188 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
189 llvm::FoldingSetNodeID &ID;
193 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
194 : ID(ID), Hash(Hash) {}
196 void AddStmt(const Stmt *S) {
203 void AddIdentifierInfo(const IdentifierInfo *II) {
206 Hash.AddIdentifierInfo(II);
210 void AddQualType(QualType T) {
214 void Visit(const Decl *D) {
215 ID.AddInteger(D->getKind());
219 void VisitNamedDecl(const NamedDecl *D) {
220 Hash.AddDeclarationName(D->getDeclName());
221 Inherited::VisitNamedDecl(D);
224 void VisitValueDecl(const ValueDecl *D) {
225 AddQualType(D->getType());
226 Inherited::VisitValueDecl(D);
229 void VisitParmVarDecl(const ParmVarDecl *D) {
230 // TODO: Handle default arguments.
231 Inherited::VisitParmVarDecl(D);
234 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
235 ID.AddInteger(D->getAccess());
236 Inherited::VisitAccessSpecDecl(D);
239 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
240 AddStmt(D->getAssertExpr());
241 AddStmt(D->getMessage());
243 Inherited::VisitStaticAssertDecl(D);
246 void VisitFieldDecl(const FieldDecl *D) {
247 const bool IsBitfield = D->isBitField();
248 Hash.AddBoolean(IsBitfield);
251 AddStmt(D->getBitWidth());
254 Hash.AddBoolean(D->isMutable());
255 AddStmt(D->getInClassInitializer());
257 Inherited::VisitFieldDecl(D);
260 void VisitFunctionDecl(const FunctionDecl *D) {
261 ID.AddInteger(D->getStorageClass());
262 Hash.AddBoolean(D->isInlineSpecified());
263 Hash.AddBoolean(D->isVirtualAsWritten());
264 Hash.AddBoolean(D->isPure());
265 Hash.AddBoolean(D->isDeletedAsWritten());
267 ID.AddInteger(D->param_size());
269 for (auto *Param : D->parameters()) {
270 Hash.AddSubDecl(Param);
273 Inherited::VisitFunctionDecl(D);
276 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
277 Hash.AddBoolean(D->isConst());
278 Hash.AddBoolean(D->isVolatile());
280 Inherited::VisitCXXMethodDecl(D);
283 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
284 AddQualType(D->getUnderlyingType());
286 Inherited::VisitTypedefNameDecl(D);
289 void VisitTypedefDecl(const TypedefDecl *D) {
290 Inherited::VisitTypedefDecl(D);
293 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
294 Inherited::VisitTypeAliasDecl(D);
298 // Only allow a small portion of Decl's to be processed. Remove this once
299 // all Decl's can be handled.
300 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
301 if (D->isImplicit()) return false;
302 if (D->getDeclContext() != Parent) return false;
304 switch (D->getKind()) {
307 case Decl::AccessSpec:
308 case Decl::CXXMethod:
310 case Decl::StaticAssert:
311 case Decl::TypeAlias:
317 void ODRHash::AddSubDecl(const Decl *D) {
318 assert(D && "Expecting non-null pointer.");
321 ODRDeclVisitor(ID, *this).Visit(D);
324 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
325 assert(Record && Record->hasDefinition() &&
326 "Expected non-null record to be a definition.");
328 if (isa<ClassTemplateSpecializationDecl>(Record)) {
334 // Filter out sub-Decls which will not be processed in order to get an
335 // accurate count of Decl's.
336 llvm::SmallVector<const Decl *, 16> Decls;
337 for (const Decl *SubDecl : Record->decls()) {
338 if (isWhitelistedDecl(SubDecl, Record)) {
339 Decls.push_back(SubDecl);
343 ID.AddInteger(Decls.size());
344 for (auto SubDecl : Decls) {
349 void ODRHash::AddDecl(const Decl *D) {
350 assert(D && "Expecting non-null pointer.");
351 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
352 ID.AddInteger(Result.first->second);
353 // On first encounter of a Decl pointer, process it. Every time afterwards,
354 // only the index value is needed.
355 if (!Result.second) {
359 ID.AddInteger(D->getKind());
361 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
362 AddDeclarationName(ND->getDeclName());
366 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
367 // methods process the relevant parts of the Type.
368 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
369 typedef TypeVisitor<ODRTypeVisitor> Inherited;
370 llvm::FoldingSetNodeID &ID;
374 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
375 : ID(ID), Hash(Hash) {}
377 void AddStmt(Stmt *S) {
384 void AddDecl(Decl *D) {
391 void AddQualType(QualType T) {
395 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
396 Hash.AddNestedNameSpecifier(NNS);
399 void AddIdentifierInfo(const IdentifierInfo *II) {
402 Hash.AddIdentifierInfo(II);
406 void VisitQualifiers(Qualifiers Quals) {
407 ID.AddInteger(Quals.getAsOpaqueValue());
410 void Visit(const Type *T) {
411 ID.AddInteger(T->getTypeClass());
415 void VisitType(const Type *T) {}
417 void VisitAdjustedType(const AdjustedType *T) {
418 AddQualType(T->getOriginalType());
419 AddQualType(T->getAdjustedType());
423 void VisitDecayedType(const DecayedType *T) {
424 AddQualType(T->getDecayedType());
425 AddQualType(T->getPointeeType());
426 VisitAdjustedType(T);
429 void VisitArrayType(const ArrayType *T) {
430 AddQualType(T->getElementType());
431 ID.AddInteger(T->getSizeModifier());
432 VisitQualifiers(T->getIndexTypeQualifiers());
435 void VisitConstantArrayType(const ConstantArrayType *T) {
436 T->getSize().Profile(ID);
440 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
441 AddStmt(T->getSizeExpr());
445 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
449 void VisitVariableArrayType(const VariableArrayType *T) {
450 AddStmt(T->getSizeExpr());
454 void VisitBuiltinType(const BuiltinType *T) {
455 ID.AddInteger(T->getKind());
459 void VisitFunctionType(const FunctionType *T) {
460 AddQualType(T->getReturnType());
461 T->getExtInfo().Profile(ID);
462 Hash.AddBoolean(T->isConst());
463 Hash.AddBoolean(T->isVolatile());
464 Hash.AddBoolean(T->isRestrict());
468 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
469 VisitFunctionType(T);
472 void VisitFunctionProtoType(const FunctionProtoType *T) {
473 ID.AddInteger(T->getNumParams());
474 for (auto ParamType : T->getParamTypes())
475 AddQualType(ParamType);
477 VisitFunctionType(T);
480 void VisitTypedefType(const TypedefType *T) {
481 AddDecl(T->getDecl());
482 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
486 void VisitTagType(const TagType *T) {
487 AddDecl(T->getDecl());
491 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
492 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
494 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
495 ID.AddInteger(T->getKeyword());
499 void VisitDependentNameType(const DependentNameType *T) {
500 AddNestedNameSpecifier(T->getQualifier());
501 AddIdentifierInfo(T->getIdentifier());
502 VisitTypeWithKeyword(T);
505 void VisitDependentTemplateSpecializationType(
506 const DependentTemplateSpecializationType *T) {
507 AddIdentifierInfo(T->getIdentifier());
508 AddNestedNameSpecifier(T->getQualifier());
509 ID.AddInteger(T->getNumArgs());
510 for (const auto &TA : T->template_arguments()) {
511 Hash.AddTemplateArgument(TA);
513 VisitTypeWithKeyword(T);
516 void VisitElaboratedType(const ElaboratedType *T) {
517 AddNestedNameSpecifier(T->getQualifier());
518 AddQualType(T->getNamedType());
519 VisitTypeWithKeyword(T);
522 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
523 ID.AddInteger(T->getNumArgs());
524 for (const auto &TA : T->template_arguments()) {
525 Hash.AddTemplateArgument(TA);
527 Hash.AddTemplateName(T->getTemplateName());
532 void ODRHash::AddType(const Type *T) {
533 assert(T && "Expecting non-null pointer.");
534 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
535 ID.AddInteger(Result.first->second);
536 // On first encounter of a Type pointer, process it. Every time afterwards,
537 // only the index value is needed.
538 if (!Result.second) {
542 ODRTypeVisitor(ID, *this).Visit(T);
545 void ODRHash::AddQualType(QualType T) {
546 AddBoolean(T.isNull());
549 SplitQualType split = T.split();
550 ID.AddInteger(split.Quals.getAsOpaqueValue());
554 void ODRHash::AddBoolean(bool Value) {
555 Bools.push_back(Value);