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 void ODRHash::AddTemplateArgument(TemplateArgument TA) {}
115 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
117 void ODRHash::clear() {
124 unsigned ODRHash::CalculateHash() {
125 // Append the bools to the end of the data segment backwards. This allows
126 // for the bools data to be compressed 32 times smaller compared to using
128 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
129 const unsigned size = Bools.size();
130 const unsigned remainder = size % unsigned_bits;
131 const unsigned loops = size / unsigned_bits;
132 auto I = Bools.rbegin();
134 for (unsigned i = 0; i < remainder; ++i) {
139 ID.AddInteger(value);
141 for (unsigned i = 0; i < loops; ++i) {
143 for (unsigned j = 0; j < unsigned_bits; ++j) {
148 ID.AddInteger(value);
151 assert(I == Bools.rend());
153 return ID.ComputeHash();
156 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
157 // methods process the relevant parts of the Decl.
158 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
159 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
160 llvm::FoldingSetNodeID &ID;
164 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
165 : ID(ID), Hash(Hash) {}
167 void AddStmt(const Stmt *S) {
174 void AddIdentifierInfo(const IdentifierInfo *II) {
177 Hash.AddIdentifierInfo(II);
181 void AddQualType(QualType T) {
185 void Visit(const Decl *D) {
186 ID.AddInteger(D->getKind());
190 void VisitNamedDecl(const NamedDecl *D) {
191 Hash.AddDeclarationName(D->getDeclName());
192 Inherited::VisitNamedDecl(D);
195 void VisitValueDecl(const ValueDecl *D) {
196 AddQualType(D->getType());
197 Inherited::VisitValueDecl(D);
200 void VisitParmVarDecl(const ParmVarDecl *D) {
201 // TODO: Handle default arguments.
202 Inherited::VisitParmVarDecl(D);
205 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
206 ID.AddInteger(D->getAccess());
207 Inherited::VisitAccessSpecDecl(D);
210 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
211 AddStmt(D->getAssertExpr());
212 AddStmt(D->getMessage());
214 Inherited::VisitStaticAssertDecl(D);
217 void VisitFieldDecl(const FieldDecl *D) {
218 const bool IsBitfield = D->isBitField();
219 Hash.AddBoolean(IsBitfield);
222 AddStmt(D->getBitWidth());
225 Hash.AddBoolean(D->isMutable());
226 AddStmt(D->getInClassInitializer());
228 Inherited::VisitFieldDecl(D);
231 void VisitFunctionDecl(const FunctionDecl *D) {
232 ID.AddInteger(D->getStorageClass());
233 Hash.AddBoolean(D->isInlineSpecified());
234 Hash.AddBoolean(D->isVirtualAsWritten());
235 Hash.AddBoolean(D->isPure());
236 Hash.AddBoolean(D->isDeletedAsWritten());
238 ID.AddInteger(D->param_size());
240 for (auto *Param : D->parameters()) {
241 Hash.AddSubDecl(Param);
244 Inherited::VisitFunctionDecl(D);
247 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
248 Hash.AddBoolean(D->isConst());
249 Hash.AddBoolean(D->isVolatile());
251 Inherited::VisitCXXMethodDecl(D);
254 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
255 AddQualType(D->getUnderlyingType());
257 Inherited::VisitTypedefNameDecl(D);
260 void VisitTypedefDecl(const TypedefDecl *D) {
261 Inherited::VisitTypedefDecl(D);
264 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
265 Inherited::VisitTypeAliasDecl(D);
269 // Only allow a small portion of Decl's to be processed. Remove this once
270 // all Decl's can be handled.
271 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
272 if (D->isImplicit()) return false;
273 if (D->getDeclContext() != Parent) return false;
275 switch (D->getKind()) {
278 case Decl::AccessSpec:
279 case Decl::CXXMethod:
281 case Decl::StaticAssert:
282 case Decl::TypeAlias:
288 void ODRHash::AddSubDecl(const Decl *D) {
289 assert(D && "Expecting non-null pointer.");
292 ODRDeclVisitor(ID, *this).Visit(D);
295 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
296 assert(Record && Record->hasDefinition() &&
297 "Expected non-null record to be a definition.");
299 if (isa<ClassTemplateSpecializationDecl>(Record)) {
305 // Filter out sub-Decls which will not be processed in order to get an
306 // accurate count of Decl's.
307 llvm::SmallVector<const Decl *, 16> Decls;
308 for (const Decl *SubDecl : Record->decls()) {
309 if (isWhitelistedDecl(SubDecl, Record)) {
310 Decls.push_back(SubDecl);
314 ID.AddInteger(Decls.size());
315 for (auto SubDecl : Decls) {
320 void ODRHash::AddDecl(const Decl *D) {
321 assert(D && "Expecting non-null pointer.");
322 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
323 ID.AddInteger(Result.first->second);
324 // On first encounter of a Decl pointer, process it. Every time afterwards,
325 // only the index value is needed.
326 if (!Result.second) {
330 ID.AddInteger(D->getKind());
332 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
333 AddDeclarationName(ND->getDeclName());
337 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
338 // methods process the relevant parts of the Type.
339 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
340 typedef TypeVisitor<ODRTypeVisitor> Inherited;
341 llvm::FoldingSetNodeID &ID;
345 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
346 : ID(ID), Hash(Hash) {}
348 void AddStmt(Stmt *S) {
355 void AddDecl(Decl *D) {
362 void AddQualType(QualType T) {
366 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
367 Hash.AddBoolean(NNS);
369 Hash.AddNestedNameSpecifier(NNS);
373 void AddIdentifierInfo(const IdentifierInfo *II) {
376 Hash.AddIdentifierInfo(II);
380 void VisitQualifiers(Qualifiers Quals) {
381 ID.AddInteger(Quals.getAsOpaqueValue());
384 void Visit(const Type *T) {
385 ID.AddInteger(T->getTypeClass());
389 void VisitType(const Type *T) {}
391 void VisitAdjustedType(const AdjustedType *T) {
392 AddQualType(T->getOriginalType());
393 AddQualType(T->getAdjustedType());
397 void VisitDecayedType(const DecayedType *T) {
398 AddQualType(T->getDecayedType());
399 AddQualType(T->getPointeeType());
400 VisitAdjustedType(T);
403 void VisitArrayType(const ArrayType *T) {
404 AddQualType(T->getElementType());
405 ID.AddInteger(T->getSizeModifier());
406 VisitQualifiers(T->getIndexTypeQualifiers());
409 void VisitConstantArrayType(const ConstantArrayType *T) {
410 T->getSize().Profile(ID);
414 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
415 AddStmt(T->getSizeExpr());
419 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
423 void VisitVariableArrayType(const VariableArrayType *T) {
424 AddStmt(T->getSizeExpr());
428 void VisitBuiltinType(const BuiltinType *T) {
429 ID.AddInteger(T->getKind());
433 void VisitFunctionType(const FunctionType *T) {
434 AddQualType(T->getReturnType());
435 T->getExtInfo().Profile(ID);
436 Hash.AddBoolean(T->isConst());
437 Hash.AddBoolean(T->isVolatile());
438 Hash.AddBoolean(T->isRestrict());
442 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
443 VisitFunctionType(T);
446 void VisitFunctionProtoType(const FunctionProtoType *T) {
447 ID.AddInteger(T->getNumParams());
448 for (auto ParamType : T->getParamTypes())
449 AddQualType(ParamType);
451 VisitFunctionType(T);
454 void VisitTypedefType(const TypedefType *T) {
455 AddDecl(T->getDecl());
456 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
460 void VisitTagType(const TagType *T) {
461 AddDecl(T->getDecl());
465 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
466 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
468 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
469 ID.AddInteger(T->getKeyword());
473 void VisitDependentNameType(const DependentNameType *T) {
474 AddNestedNameSpecifier(T->getQualifier());
475 AddIdentifierInfo(T->getIdentifier());
476 VisitTypeWithKeyword(T);
479 void VisitDependentTemplateSpecializationType(
480 const DependentTemplateSpecializationType *T) {
481 AddIdentifierInfo(T->getIdentifier());
482 AddNestedNameSpecifier(T->getQualifier());
483 ID.AddInteger(T->getNumArgs());
484 for (const auto &TA : T->template_arguments()) {
485 Hash.AddTemplateArgument(TA);
487 VisitTypeWithKeyword(T);
490 void VisitElaboratedType(const ElaboratedType *T) {
491 AddNestedNameSpecifier(T->getQualifier());
492 AddQualType(T->getNamedType());
493 VisitTypeWithKeyword(T);
497 void ODRHash::AddType(const Type *T) {
498 assert(T && "Expecting non-null pointer.");
499 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
500 ID.AddInteger(Result.first->second);
501 // On first encounter of a Type pointer, process it. Every time afterwards,
502 // only the index value is needed.
503 if (!Result.second) {
507 ODRTypeVisitor(ID, *this).Visit(T);
510 void ODRHash::AddQualType(QualType T) {
511 AddBoolean(T.isNull());
514 SplitQualType split = T.split();
515 ID.AddInteger(split.Quals.getAsOpaqueValue());
519 void ODRHash::AddBoolean(bool Value) {
520 Bools.push_back(Value);