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 void ODRHash::AddTemplateName(TemplateName Name) {}
86 void ODRHash::AddTemplateArgument(TemplateArgument TA) {}
87 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
89 void ODRHash::clear() {
96 unsigned ODRHash::CalculateHash() {
97 // Append the bools to the end of the data segment backwards. This allows
98 // for the bools data to be compressed 32 times smaller compared to using
100 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
101 const unsigned size = Bools.size();
102 const unsigned remainder = size % unsigned_bits;
103 const unsigned loops = size / unsigned_bits;
104 auto I = Bools.rbegin();
106 for (unsigned i = 0; i < remainder; ++i) {
111 ID.AddInteger(value);
113 for (unsigned i = 0; i < loops; ++i) {
115 for (unsigned j = 0; j < unsigned_bits; ++j) {
120 ID.AddInteger(value);
123 assert(I == Bools.rend());
125 return ID.ComputeHash();
128 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
129 // methods process the relevant parts of the Decl.
130 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
131 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
132 llvm::FoldingSetNodeID &ID;
136 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
137 : ID(ID), Hash(Hash) {}
139 void AddStmt(const Stmt *S) {
146 void AddIdentifierInfo(const IdentifierInfo *II) {
149 Hash.AddIdentifierInfo(II);
153 void AddQualType(QualType T) {
157 void Visit(const Decl *D) {
158 ID.AddInteger(D->getKind());
162 void VisitNamedDecl(const NamedDecl *D) {
163 Hash.AddDeclarationName(D->getDeclName());
164 Inherited::VisitNamedDecl(D);
167 void VisitValueDecl(const ValueDecl *D) {
168 AddQualType(D->getType());
169 Inherited::VisitValueDecl(D);
172 void VisitParmVarDecl(const ParmVarDecl *D) {
173 AddStmt(D->getDefaultArg());
174 Inherited::VisitParmVarDecl(D);
177 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
178 ID.AddInteger(D->getAccess());
179 Inherited::VisitAccessSpecDecl(D);
182 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
183 AddStmt(D->getAssertExpr());
184 AddStmt(D->getMessage());
186 Inherited::VisitStaticAssertDecl(D);
189 void VisitFieldDecl(const FieldDecl *D) {
190 const bool IsBitfield = D->isBitField();
191 Hash.AddBoolean(IsBitfield);
194 AddStmt(D->getBitWidth());
197 Hash.AddBoolean(D->isMutable());
198 AddStmt(D->getInClassInitializer());
200 Inherited::VisitFieldDecl(D);
203 void VisitFunctionDecl(const FunctionDecl *D) {
204 ID.AddInteger(D->getStorageClass());
205 Hash.AddBoolean(D->isInlineSpecified());
206 Hash.AddBoolean(D->isVirtualAsWritten());
207 Hash.AddBoolean(D->isPure());
208 Hash.AddBoolean(D->isDeletedAsWritten());
210 ID.AddInteger(D->param_size());
212 for (auto *Param : D->parameters()) {
213 Hash.AddSubDecl(Param);
216 Inherited::VisitFunctionDecl(D);
219 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
220 Hash.AddBoolean(D->isConst());
221 Hash.AddBoolean(D->isVolatile());
223 Inherited::VisitCXXMethodDecl(D);
226 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
227 AddQualType(D->getUnderlyingType());
229 Inherited::VisitTypedefNameDecl(D);
232 void VisitTypedefDecl(const TypedefDecl *D) {
233 Inherited::VisitTypedefDecl(D);
236 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
237 Inherited::VisitTypeAliasDecl(D);
241 // Only allow a small portion of Decl's to be processed. Remove this once
242 // all Decl's can be handled.
243 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
244 if (D->isImplicit()) return false;
245 if (D->getDeclContext() != Parent) return false;
247 switch (D->getKind()) {
250 case Decl::AccessSpec:
251 case Decl::CXXMethod:
253 case Decl::StaticAssert:
254 case Decl::TypeAlias:
260 void ODRHash::AddSubDecl(const Decl *D) {
261 assert(D && "Expecting non-null pointer.");
264 ODRDeclVisitor(ID, *this).Visit(D);
267 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
268 assert(Record && Record->hasDefinition() &&
269 "Expected non-null record to be a definition.");
272 // Filter out sub-Decls which will not be processed in order to get an
273 // accurate count of Decl's.
274 llvm::SmallVector<const Decl *, 16> Decls;
275 for (const Decl *SubDecl : Record->decls()) {
276 if (isWhitelistedDecl(SubDecl, Record)) {
277 Decls.push_back(SubDecl);
281 ID.AddInteger(Decls.size());
282 for (auto SubDecl : Decls) {
287 void ODRHash::AddDecl(const Decl *D) {
288 assert(D && "Expecting non-null pointer.");
289 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
290 ID.AddInteger(Result.first->second);
291 // On first encounter of a Decl pointer, process it. Every time afterwards,
292 // only the index value is needed.
293 if (!Result.second) {
297 ID.AddInteger(D->getKind());
299 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
300 AddDeclarationName(ND->getDeclName());
304 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
305 // methods process the relevant parts of the Type.
306 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
307 typedef TypeVisitor<ODRTypeVisitor> Inherited;
308 llvm::FoldingSetNodeID &ID;
312 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
313 : ID(ID), Hash(Hash) {}
315 void AddStmt(Stmt *S) {
322 void AddDecl(Decl *D) {
329 void AddQualType(QualType T) {
333 void VisitQualifiers(Qualifiers Quals) {
334 ID.AddInteger(Quals.getAsOpaqueValue());
337 void Visit(const Type *T) {
338 ID.AddInteger(T->getTypeClass());
342 void VisitType(const Type *T) {}
344 void VisitAdjustedType(const AdjustedType *T) {
345 AddQualType(T->getOriginalType());
346 AddQualType(T->getAdjustedType());
350 void VisitDecayedType(const DecayedType *T) {
351 AddQualType(T->getDecayedType());
352 AddQualType(T->getPointeeType());
353 VisitAdjustedType(T);
356 void VisitArrayType(const ArrayType *T) {
357 AddQualType(T->getElementType());
358 ID.AddInteger(T->getSizeModifier());
359 VisitQualifiers(T->getIndexTypeQualifiers());
362 void VisitConstantArrayType(const ConstantArrayType *T) {
363 T->getSize().Profile(ID);
367 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
368 AddStmt(T->getSizeExpr());
372 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
376 void VisitVariableArrayType(const VariableArrayType *T) {
377 AddStmt(T->getSizeExpr());
381 void VisitBuiltinType(const BuiltinType *T) {
382 ID.AddInteger(T->getKind());
386 void VisitFunctionType(const FunctionType *T) {
387 AddQualType(T->getReturnType());
388 T->getExtInfo().Profile(ID);
389 Hash.AddBoolean(T->isConst());
390 Hash.AddBoolean(T->isVolatile());
391 Hash.AddBoolean(T->isRestrict());
395 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
396 VisitFunctionType(T);
399 void VisitFunctionProtoType(const FunctionProtoType *T) {
400 ID.AddInteger(T->getNumParams());
401 for (auto ParamType : T->getParamTypes())
402 AddQualType(ParamType);
404 const auto &epi = T->getExtProtoInfo();
405 ID.AddInteger(epi.Variadic);
406 ID.AddInteger(epi.TypeQuals);
407 ID.AddInteger(epi.RefQualifier);
408 ID.AddInteger(epi.ExceptionSpec.Type);
410 if (epi.ExceptionSpec.Type == EST_Dynamic) {
411 for (QualType Ex : epi.ExceptionSpec.Exceptions)
413 } else if (epi.ExceptionSpec.Type == EST_ComputedNoexcept &&
414 epi.ExceptionSpec.NoexceptExpr) {
415 AddStmt(epi.ExceptionSpec.NoexceptExpr);
416 } else if (epi.ExceptionSpec.Type == EST_Uninstantiated ||
417 epi.ExceptionSpec.Type == EST_Unevaluated) {
418 AddDecl(epi.ExceptionSpec.SourceDecl->getCanonicalDecl());
420 if (epi.ExtParameterInfos) {
421 for (unsigned i = 0; i != T->getNumParams(); ++i)
422 ID.AddInteger(epi.ExtParameterInfos[i].getOpaqueValue());
424 epi.ExtInfo.Profile(ID);
425 Hash.AddBoolean(epi.HasTrailingReturn);
427 VisitFunctionType(T);
430 void VisitTypedefType(const TypedefType *T) {
431 AddDecl(T->getDecl());
432 Hash.AddQualType(T->getDecl()->getUnderlyingType());
437 void ODRHash::AddType(const Type *T) {
438 assert(T && "Expecting non-null pointer.");
439 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
440 ID.AddInteger(Result.first->second);
441 // On first encounter of a Type pointer, process it. Every time afterwards,
442 // only the index value is needed.
443 if (!Result.second) {
447 ODRTypeVisitor(ID, *this).Visit(T);
450 void ODRHash::AddQualType(QualType T) {
451 AddBoolean(T.isNull());
454 SplitQualType split = T.split();
455 ID.AddInteger(split.Quals.getAsOpaqueValue());
459 void ODRHash::AddBoolean(bool Value) {
460 Bools.push_back(Value);