blob: 24371db64d07f53ce71d0fd94dd7f8a4b83fdbb5 [file] [log] [blame]
Richard Trieue7f7ed22017-02-22 01:11:25 +00001//===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// This file implements the ODRHash class, which calculates a hash based
12/// on AST nodes, which is stable across different runs.
13///
14//===----------------------------------------------------------------------===//
15
16#include "clang/AST/ODRHash.h"
17
18#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/NestedNameSpecifier.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/TypeVisitor.h"
22
23using namespace clang;
24
Richard Trieu639d7b62017-02-22 22:22:42 +000025void ODRHash::AddStmt(const Stmt *S) {
26 assert(S && "Expecting non-null pointer.");
27 S->ProcessODRHash(ID, *this);
28}
Richard Trieud0786092017-02-23 00:23:01 +000029
30void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
31 assert(II && "Expecting non-null pointer.");
32 ID.AddString(II->getName());
33}
34
Richard Trieu8459ddf2017-02-24 02:59:12 +000035void ODRHash::AddDeclarationName(DeclarationName Name) {
36 AddBoolean(Name.isEmpty());
37 if (Name.isEmpty())
38 return;
39
40 auto Kind = Name.getNameKind();
41 ID.AddInteger(Kind);
42 switch (Kind) {
43 case DeclarationName::Identifier:
44 AddIdentifierInfo(Name.getAsIdentifierInfo());
45 break;
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));
56 }
57 break;
58 }
59 case DeclarationName::CXXConstructorName:
60 case DeclarationName::CXXDestructorName:
61 AddQualType(Name.getCXXNameType());
62 break;
63 case DeclarationName::CXXOperatorName:
64 ID.AddInteger(Name.getCXXOverloadedOperator());
65 break;
66 case DeclarationName::CXXLiteralOperatorName:
67 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
68 break;
69 case DeclarationName::CXXConversionFunctionName:
70 AddQualType(Name.getCXXNameType());
71 break;
72 case DeclarationName::CXXUsingDirective:
73 break;
74 case DeclarationName::CXXDeductionGuideName: {
75 auto *Template = Name.getCXXDeductionGuideTemplate();
76 AddBoolean(Template);
77 if (Template) {
78 AddDecl(Template);
79 }
80 }
81 }
82}
83
Richard Trieuce81b192017-05-17 03:23:35 +000084void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
85 assert(NNS && "Expecting non-null pointer.");
86 const auto *Prefix = NNS->getPrefix();
87 AddBoolean(Prefix);
88 if (Prefix) {
89 AddNestedNameSpecifier(Prefix);
90 }
91 auto Kind = NNS->getKind();
92 ID.AddInteger(Kind);
93 switch (Kind) {
94 case NestedNameSpecifier::Identifier:
95 AddIdentifierInfo(NNS->getAsIdentifier());
96 break;
97 case NestedNameSpecifier::Namespace:
98 AddDecl(NNS->getAsNamespace());
99 break;
100 case NestedNameSpecifier::NamespaceAlias:
101 AddDecl(NNS->getAsNamespaceAlias());
102 break;
103 case NestedNameSpecifier::TypeSpec:
104 case NestedNameSpecifier::TypeSpecWithTemplate:
105 AddType(NNS->getAsType());
106 break;
107 case NestedNameSpecifier::Global:
108 case NestedNameSpecifier::Super:
109 break;
110 }
111}
112
Richard Trieue7f7ed22017-02-22 01:11:25 +0000113void ODRHash::AddTemplateName(TemplateName Name) {}
Richard Trieue7f7ed22017-02-22 01:11:25 +0000114void ODRHash::AddTemplateArgument(TemplateArgument TA) {}
115void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
116
117void ODRHash::clear() {
118 DeclMap.clear();
119 TypeMap.clear();
120 Bools.clear();
121 ID.clear();
122}
123
124unsigned 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
127 // ID.AddBoolean
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();
133 unsigned value = 0;
134 for (unsigned i = 0; i < remainder; ++i) {
135 value <<= 1;
136 value |= *I;
137 ++I;
138 }
139 ID.AddInteger(value);
140
141 for (unsigned i = 0; i < loops; ++i) {
142 value = 0;
143 for (unsigned j = 0; j < unsigned_bits; ++j) {
144 value <<= 1;
145 value |= *I;
146 ++I;
147 }
148 ID.AddInteger(value);
149 }
150
151 assert(I == Bools.rend());
152 Bools.clear();
153 return ID.ComputeHash();
154}
155
156// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
157// methods process the relevant parts of the Decl.
158class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
159 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
160 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000161 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000162
163public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000164 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
165 : ID(ID), Hash(Hash) {}
166
167 void AddStmt(const Stmt *S) {
168 Hash.AddBoolean(S);
169 if (S) {
170 Hash.AddStmt(S);
171 }
172 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000173
Richard Trieud0786092017-02-23 00:23:01 +0000174 void AddIdentifierInfo(const IdentifierInfo *II) {
175 Hash.AddBoolean(II);
176 if (II) {
177 Hash.AddIdentifierInfo(II);
178 }
179 }
180
Richard Trieubcaaf962017-02-23 03:25:57 +0000181 void AddQualType(QualType T) {
182 Hash.AddQualType(T);
183 }
184
Richard Trieue7f7ed22017-02-22 01:11:25 +0000185 void Visit(const Decl *D) {
186 ID.AddInteger(D->getKind());
187 Inherited::Visit(D);
188 }
189
Richard Trieud0786092017-02-23 00:23:01 +0000190 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000191 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000192 Inherited::VisitNamedDecl(D);
193 }
194
Richard Trieubcaaf962017-02-23 03:25:57 +0000195 void VisitValueDecl(const ValueDecl *D) {
196 AddQualType(D->getType());
197 Inherited::VisitValueDecl(D);
198 }
199
Richard Trieu02552272017-05-02 23:58:52 +0000200 void VisitParmVarDecl(const ParmVarDecl *D) {
201 // TODO: Handle default arguments.
202 Inherited::VisitParmVarDecl(D);
203 }
204
Richard Trieue7f7ed22017-02-22 01:11:25 +0000205 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
206 ID.AddInteger(D->getAccess());
207 Inherited::VisitAccessSpecDecl(D);
208 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000209
210 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
211 AddStmt(D->getAssertExpr());
212 AddStmt(D->getMessage());
213
214 Inherited::VisitStaticAssertDecl(D);
215 }
Richard Trieud0786092017-02-23 00:23:01 +0000216
217 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000218 const bool IsBitfield = D->isBitField();
219 Hash.AddBoolean(IsBitfield);
220
221 if (IsBitfield) {
222 AddStmt(D->getBitWidth());
223 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000224
225 Hash.AddBoolean(D->isMutable());
226 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000227
228 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000229 }
Richard Trieu48143742017-02-28 21:24:38 +0000230
231 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000232 ID.AddInteger(D->getStorageClass());
233 Hash.AddBoolean(D->isInlineSpecified());
234 Hash.AddBoolean(D->isVirtualAsWritten());
235 Hash.AddBoolean(D->isPure());
236 Hash.AddBoolean(D->isDeletedAsWritten());
237
Richard Trieu02552272017-05-02 23:58:52 +0000238 ID.AddInteger(D->param_size());
239
240 for (auto *Param : D->parameters()) {
241 Hash.AddSubDecl(Param);
242 }
243
Richard Trieu48143742017-02-28 21:24:38 +0000244 Inherited::VisitFunctionDecl(D);
245 }
246
247 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000248 Hash.AddBoolean(D->isConst());
249 Hash.AddBoolean(D->isVolatile());
250
Richard Trieu48143742017-02-28 21:24:38 +0000251 Inherited::VisitCXXMethodDecl(D);
252 }
Richard Trieu33562c22017-03-08 00:13:19 +0000253
254 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
255 AddQualType(D->getUnderlyingType());
256
257 Inherited::VisitTypedefNameDecl(D);
258 }
259
260 void VisitTypedefDecl(const TypedefDecl *D) {
261 Inherited::VisitTypedefDecl(D);
262 }
263
264 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
265 Inherited::VisitTypeAliasDecl(D);
266 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000267};
268
269// Only allow a small portion of Decl's to be processed. Remove this once
270// all Decl's can be handled.
271bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
272 if (D->isImplicit()) return false;
273 if (D->getDeclContext() != Parent) return false;
274
275 switch (D->getKind()) {
276 default:
277 return false;
278 case Decl::AccessSpec:
Richard Trieu48143742017-02-28 21:24:38 +0000279 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000280 case Decl::Field:
Richard Trieu639d7b62017-02-22 22:22:42 +0000281 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000282 case Decl::TypeAlias:
283 case Decl::Typedef:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000284 return true;
285 }
286}
287
288void ODRHash::AddSubDecl(const Decl *D) {
289 assert(D && "Expecting non-null pointer.");
290 AddDecl(D);
291
Richard Trieu639d7b62017-02-22 22:22:42 +0000292 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000293}
294
295void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
296 assert(Record && Record->hasDefinition() &&
297 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000298
299 if (isa<ClassTemplateSpecializationDecl>(Record)) {
300 return;
301 }
302
Richard Trieue7f7ed22017-02-22 01:11:25 +0000303 AddDecl(Record);
304
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);
311 }
312 }
313
314 ID.AddInteger(Decls.size());
315 for (auto SubDecl : Decls) {
316 AddSubDecl(SubDecl);
317 }
318}
319
320void 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) {
327 return;
328 }
329
330 ID.AddInteger(D->getKind());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000331
332 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
333 AddDeclarationName(ND->getDeclName());
334 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000335}
336
Richard Trieubcaaf962017-02-23 03:25:57 +0000337// Process a Type pointer. Add* methods call back into ODRHash while Visit*
338// methods process the relevant parts of the Type.
339class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
340 typedef TypeVisitor<ODRTypeVisitor> Inherited;
341 llvm::FoldingSetNodeID &ID;
342 ODRHash &Hash;
343
344public:
345 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
346 : ID(ID), Hash(Hash) {}
347
348 void AddStmt(Stmt *S) {
349 Hash.AddBoolean(S);
350 if (S) {
351 Hash.AddStmt(S);
352 }
353 }
354
Richard Trieu8459ddf2017-02-24 02:59:12 +0000355 void AddDecl(Decl *D) {
356 Hash.AddBoolean(D);
357 if (D) {
358 Hash.AddDecl(D);
359 }
360 }
361
Richard Trieu02552272017-05-02 23:58:52 +0000362 void AddQualType(QualType T) {
363 Hash.AddQualType(T);
364 }
365
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000366 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
367 Hash.AddBoolean(NNS);
368 if (NNS) {
369 Hash.AddNestedNameSpecifier(NNS);
370 }
371 }
372
373 void AddIdentifierInfo(const IdentifierInfo *II) {
374 Hash.AddBoolean(II);
375 if (II) {
376 Hash.AddIdentifierInfo(II);
377 }
378 }
379
Richard Trieu02552272017-05-02 23:58:52 +0000380 void VisitQualifiers(Qualifiers Quals) {
381 ID.AddInteger(Quals.getAsOpaqueValue());
382 }
383
Richard Trieubcaaf962017-02-23 03:25:57 +0000384 void Visit(const Type *T) {
385 ID.AddInteger(T->getTypeClass());
386 Inherited::Visit(T);
387 }
388
389 void VisitType(const Type *T) {}
390
Richard Trieu02552272017-05-02 23:58:52 +0000391 void VisitAdjustedType(const AdjustedType *T) {
392 AddQualType(T->getOriginalType());
393 AddQualType(T->getAdjustedType());
394 VisitType(T);
395 }
396
397 void VisitDecayedType(const DecayedType *T) {
398 AddQualType(T->getDecayedType());
399 AddQualType(T->getPointeeType());
400 VisitAdjustedType(T);
401 }
402
403 void VisitArrayType(const ArrayType *T) {
404 AddQualType(T->getElementType());
405 ID.AddInteger(T->getSizeModifier());
406 VisitQualifiers(T->getIndexTypeQualifiers());
407 VisitType(T);
408 }
409 void VisitConstantArrayType(const ConstantArrayType *T) {
410 T->getSize().Profile(ID);
411 VisitArrayType(T);
412 }
413
414 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
415 AddStmt(T->getSizeExpr());
416 VisitArrayType(T);
417 }
418
419 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
420 VisitArrayType(T);
421 }
422
423 void VisitVariableArrayType(const VariableArrayType *T) {
424 AddStmt(T->getSizeExpr());
425 VisitArrayType(T);
426 }
427
Richard Trieubcaaf962017-02-23 03:25:57 +0000428 void VisitBuiltinType(const BuiltinType *T) {
429 ID.AddInteger(T->getKind());
430 VisitType(T);
431 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000432
Richard Trieu02552272017-05-02 23:58:52 +0000433 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());
439 VisitType(T);
440 }
441
442 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
443 VisitFunctionType(T);
444 }
445
446 void VisitFunctionProtoType(const FunctionProtoType *T) {
447 ID.AddInteger(T->getNumParams());
448 for (auto ParamType : T->getParamTypes())
449 AddQualType(ParamType);
450
451 VisitFunctionType(T);
452 }
453
Richard Trieu8459ddf2017-02-24 02:59:12 +0000454 void VisitTypedefType(const TypedefType *T) {
455 AddDecl(T->getDecl());
Richard Trieub35ef2a2017-05-09 03:24:34 +0000456 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000457 VisitType(T);
458 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000459
460 void VisitTagType(const TagType *T) {
461 AddDecl(T->getDecl());
462 VisitType(T);
463 }
464
465 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
466 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
467
468 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
469 ID.AddInteger(T->getKeyword());
470 VisitType(T);
471 };
472
473 void VisitDependentNameType(const DependentNameType *T) {
474 AddNestedNameSpecifier(T->getQualifier());
475 AddIdentifierInfo(T->getIdentifier());
476 VisitTypeWithKeyword(T);
477 }
478
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);
486 }
487 VisitTypeWithKeyword(T);
488 }
489
490 void VisitElaboratedType(const ElaboratedType *T) {
491 AddNestedNameSpecifier(T->getQualifier());
492 AddQualType(T->getNamedType());
493 VisitTypeWithKeyword(T);
494 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000495};
496
497void 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) {
504 return;
505 }
506
507 ODRTypeVisitor(ID, *this).Visit(T);
508}
509
510void ODRHash::AddQualType(QualType T) {
511 AddBoolean(T.isNull());
512 if (T.isNull())
513 return;
514 SplitQualType split = T.split();
515 ID.AddInteger(split.Quals.getAsOpaqueValue());
516 AddType(split.Ty);
517}
518
Richard Trieue7f7ed22017-02-22 01:11:25 +0000519void ODRHash::AddBoolean(bool Value) {
520 Bools.push_back(Value);
521}