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 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
134 void ODRHash::clear() {
141 unsigned ODRHash::CalculateHash() {
142 // Append the bools to the end of the data segment backwards. This allows
143 // for the bools data to be compressed 32 times smaller compared to using
145 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
146 const unsigned size = Bools.size();
147 const unsigned remainder = size % unsigned_bits;
148 const unsigned loops = size / unsigned_bits;
149 auto I = Bools.rbegin();
151 for (unsigned i = 0; i < remainder; ++i) {
156 ID.AddInteger(value);
158 for (unsigned i = 0; i < loops; ++i) {
160 for (unsigned j = 0; j < unsigned_bits; ++j) {
165 ID.AddInteger(value);
168 assert(I == Bools.rend());
170 return ID.ComputeHash();
173 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
174 // methods process the relevant parts of the Decl.
175 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
176 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
177 llvm::FoldingSetNodeID &ID;
181 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
182 : ID(ID), Hash(Hash) {}
184 void AddStmt(const Stmt *S) {
191 void AddIdentifierInfo(const IdentifierInfo *II) {
194 Hash.AddIdentifierInfo(II);
198 void AddQualType(QualType T) {
202 void Visit(const Decl *D) {
203 ID.AddInteger(D->getKind());
207 void VisitNamedDecl(const NamedDecl *D) {
208 Hash.AddDeclarationName(D->getDeclName());
209 Inherited::VisitNamedDecl(D);
212 void VisitValueDecl(const ValueDecl *D) {
213 AddQualType(D->getType());
214 Inherited::VisitValueDecl(D);
217 void VisitParmVarDecl(const ParmVarDecl *D) {
218 // TODO: Handle default arguments.
219 Inherited::VisitParmVarDecl(D);
222 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
223 ID.AddInteger(D->getAccess());
224 Inherited::VisitAccessSpecDecl(D);
227 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
228 AddStmt(D->getAssertExpr());
229 AddStmt(D->getMessage());
231 Inherited::VisitStaticAssertDecl(D);
234 void VisitFieldDecl(const FieldDecl *D) {
235 const bool IsBitfield = D->isBitField();
236 Hash.AddBoolean(IsBitfield);
239 AddStmt(D->getBitWidth());
242 Hash.AddBoolean(D->isMutable());
243 AddStmt(D->getInClassInitializer());
245 Inherited::VisitFieldDecl(D);
248 void VisitFunctionDecl(const FunctionDecl *D) {
249 ID.AddInteger(D->getStorageClass());
250 Hash.AddBoolean(D->isInlineSpecified());
251 Hash.AddBoolean(D->isVirtualAsWritten());
252 Hash.AddBoolean(D->isPure());
253 Hash.AddBoolean(D->isDeletedAsWritten());
255 ID.AddInteger(D->param_size());
257 for (auto *Param : D->parameters()) {
258 Hash.AddSubDecl(Param);
261 Inherited::VisitFunctionDecl(D);
264 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
265 Hash.AddBoolean(D->isConst());
266 Hash.AddBoolean(D->isVolatile());
268 Inherited::VisitCXXMethodDecl(D);
271 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
272 AddQualType(D->getUnderlyingType());
274 Inherited::VisitTypedefNameDecl(D);
277 void VisitTypedefDecl(const TypedefDecl *D) {
278 Inherited::VisitTypedefDecl(D);
281 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
282 Inherited::VisitTypeAliasDecl(D);
286 // Only allow a small portion of Decl's to be processed. Remove this once
287 // all Decl's can be handled.
288 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
289 if (D->isImplicit()) return false;
290 if (D->getDeclContext() != Parent) return false;
292 switch (D->getKind()) {
295 case Decl::AccessSpec:
296 case Decl::CXXMethod:
298 case Decl::StaticAssert:
299 case Decl::TypeAlias:
305 void ODRHash::AddSubDecl(const Decl *D) {
306 assert(D && "Expecting non-null pointer.");
309 ODRDeclVisitor(ID, *this).Visit(D);
312 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
313 assert(Record && Record->hasDefinition() &&
314 "Expected non-null record to be a definition.");
316 if (isa<ClassTemplateSpecializationDecl>(Record)) {
322 // Filter out sub-Decls which will not be processed in order to get an
323 // accurate count of Decl's.
324 llvm::SmallVector<const Decl *, 16> Decls;
325 for (const Decl *SubDecl : Record->decls()) {
326 if (isWhitelistedDecl(SubDecl, Record)) {
327 Decls.push_back(SubDecl);
331 ID.AddInteger(Decls.size());
332 for (auto SubDecl : Decls) {
337 void ODRHash::AddDecl(const Decl *D) {
338 assert(D && "Expecting non-null pointer.");
339 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
340 ID.AddInteger(Result.first->second);
341 // On first encounter of a Decl pointer, process it. Every time afterwards,
342 // only the index value is needed.
343 if (!Result.second) {
347 ID.AddInteger(D->getKind());
349 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
350 AddDeclarationName(ND->getDeclName());
354 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
355 // methods process the relevant parts of the Type.
356 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
357 typedef TypeVisitor<ODRTypeVisitor> Inherited;
358 llvm::FoldingSetNodeID &ID;
362 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
363 : ID(ID), Hash(Hash) {}
365 void AddStmt(Stmt *S) {
372 void AddDecl(Decl *D) {
379 void AddQualType(QualType T) {
383 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
384 Hash.AddBoolean(NNS);
386 Hash.AddNestedNameSpecifier(NNS);
390 void AddIdentifierInfo(const IdentifierInfo *II) {
393 Hash.AddIdentifierInfo(II);
397 void VisitQualifiers(Qualifiers Quals) {
398 ID.AddInteger(Quals.getAsOpaqueValue());
401 void Visit(const Type *T) {
402 ID.AddInteger(T->getTypeClass());
406 void VisitType(const Type *T) {}
408 void VisitAdjustedType(const AdjustedType *T) {
409 AddQualType(T->getOriginalType());
410 AddQualType(T->getAdjustedType());
414 void VisitDecayedType(const DecayedType *T) {
415 AddQualType(T->getDecayedType());
416 AddQualType(T->getPointeeType());
417 VisitAdjustedType(T);
420 void VisitArrayType(const ArrayType *T) {
421 AddQualType(T->getElementType());
422 ID.AddInteger(T->getSizeModifier());
423 VisitQualifiers(T->getIndexTypeQualifiers());
426 void VisitConstantArrayType(const ConstantArrayType *T) {
427 T->getSize().Profile(ID);
431 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
432 AddStmt(T->getSizeExpr());
436 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
440 void VisitVariableArrayType(const VariableArrayType *T) {
441 AddStmt(T->getSizeExpr());
445 void VisitBuiltinType(const BuiltinType *T) {
446 ID.AddInteger(T->getKind());
450 void VisitFunctionType(const FunctionType *T) {
451 AddQualType(T->getReturnType());
452 T->getExtInfo().Profile(ID);
453 Hash.AddBoolean(T->isConst());
454 Hash.AddBoolean(T->isVolatile());
455 Hash.AddBoolean(T->isRestrict());
459 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
460 VisitFunctionType(T);
463 void VisitFunctionProtoType(const FunctionProtoType *T) {
464 ID.AddInteger(T->getNumParams());
465 for (auto ParamType : T->getParamTypes())
466 AddQualType(ParamType);
468 VisitFunctionType(T);
471 void VisitTypedefType(const TypedefType *T) {
472 AddDecl(T->getDecl());
473 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
477 void VisitTagType(const TagType *T) {
478 AddDecl(T->getDecl());
482 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
483 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
485 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
486 ID.AddInteger(T->getKeyword());
490 void VisitDependentNameType(const DependentNameType *T) {
491 AddNestedNameSpecifier(T->getQualifier());
492 AddIdentifierInfo(T->getIdentifier());
493 VisitTypeWithKeyword(T);
496 void VisitDependentTemplateSpecializationType(
497 const DependentTemplateSpecializationType *T) {
498 AddIdentifierInfo(T->getIdentifier());
499 AddNestedNameSpecifier(T->getQualifier());
500 ID.AddInteger(T->getNumArgs());
501 for (const auto &TA : T->template_arguments()) {
502 Hash.AddTemplateArgument(TA);
504 VisitTypeWithKeyword(T);
507 void VisitElaboratedType(const ElaboratedType *T) {
508 AddNestedNameSpecifier(T->getQualifier());
509 AddQualType(T->getNamedType());
510 VisitTypeWithKeyword(T);
513 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
514 ID.AddInteger(T->getNumArgs());
515 for (const auto &TA : T->template_arguments()) {
516 Hash.AddTemplateArgument(TA);
518 Hash.AddTemplateName(T->getTemplateName());
523 void ODRHash::AddType(const Type *T) {
524 assert(T && "Expecting non-null pointer.");
525 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
526 ID.AddInteger(Result.first->second);
527 // On first encounter of a Type pointer, process it. Every time afterwards,
528 // only the index value is needed.
529 if (!Result.second) {
533 ODRTypeVisitor(ID, *this).Visit(T);
536 void ODRHash::AddQualType(QualType T) {
537 AddBoolean(T.isNull());
540 SplitQualType split = T.split();
541 ID.AddInteger(split.Quals.getAsOpaqueValue());
545 void ODRHash::AddBoolean(bool Value) {
546 Bools.push_back(Value);