blob: 0a4898c94df5b20b51039f4806d6deec26032056 [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) {
Richard Trieuf21b8032017-06-09 20:11:51 +000085 // Unlike the other pointer handling functions, allow null pointers here.
86 if (!NNS) {
87 AddBoolean(false);
88 return;
Richard Trieuce81b192017-05-17 03:23:35 +000089 }
Richard Trieuf21b8032017-06-09 20:11:51 +000090
91 // Skip inlined namespaces.
Richard Trieuce81b192017-05-17 03:23:35 +000092 auto Kind = NNS->getKind();
Richard Trieuf21b8032017-06-09 20:11:51 +000093 if (Kind == NestedNameSpecifier::Namespace) {
94 if (NNS->getAsNamespace()->isInline()) {
95 return AddNestedNameSpecifier(NNS->getPrefix());
96 }
97 }
98
99 AddBoolean(true);
100
101 // Process prefix
102 AddNestedNameSpecifier(NNS->getPrefix());
103
Richard Trieuce81b192017-05-17 03:23:35 +0000104 ID.AddInteger(Kind);
105 switch (Kind) {
106 case NestedNameSpecifier::Identifier:
107 AddIdentifierInfo(NNS->getAsIdentifier());
108 break;
109 case NestedNameSpecifier::Namespace:
110 AddDecl(NNS->getAsNamespace());
111 break;
112 case NestedNameSpecifier::NamespaceAlias:
113 AddDecl(NNS->getAsNamespaceAlias());
114 break;
115 case NestedNameSpecifier::TypeSpec:
116 case NestedNameSpecifier::TypeSpecWithTemplate:
117 AddType(NNS->getAsType());
118 break;
119 case NestedNameSpecifier::Global:
120 case NestedNameSpecifier::Super:
121 break;
122 }
123}
124
Richard Trieu96b49622017-05-31 00:31:58 +0000125void ODRHash::AddTemplateName(TemplateName Name) {
126 auto Kind = Name.getKind();
127 ID.AddInteger(Kind);
128
129 switch (Kind) {
130 case TemplateName::Template:
131 AddDecl(Name.getAsTemplateDecl());
132 break;
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:
139 break;
140 }
141}
142
Richard Trieu3b261bb72017-06-13 22:21:18 +0000143void ODRHash::AddTemplateArgument(TemplateArgument TA) {
144 const auto Kind = TA.getKind();
145 ID.AddInteger(Kind);
146}
147
Richard Trieue7f7ed22017-02-22 01:11:25 +0000148void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
149
150void ODRHash::clear() {
151 DeclMap.clear();
152 TypeMap.clear();
153 Bools.clear();
154 ID.clear();
155}
156
157unsigned ODRHash::CalculateHash() {
158 // Append the bools to the end of the data segment backwards. This allows
159 // for the bools data to be compressed 32 times smaller compared to using
160 // ID.AddBoolean
161 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
162 const unsigned size = Bools.size();
163 const unsigned remainder = size % unsigned_bits;
164 const unsigned loops = size / unsigned_bits;
165 auto I = Bools.rbegin();
166 unsigned value = 0;
167 for (unsigned i = 0; i < remainder; ++i) {
168 value <<= 1;
169 value |= *I;
170 ++I;
171 }
172 ID.AddInteger(value);
173
174 for (unsigned i = 0; i < loops; ++i) {
175 value = 0;
176 for (unsigned j = 0; j < unsigned_bits; ++j) {
177 value <<= 1;
178 value |= *I;
179 ++I;
180 }
181 ID.AddInteger(value);
182 }
183
184 assert(I == Bools.rend());
185 Bools.clear();
186 return ID.ComputeHash();
187}
188
189// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
190// methods process the relevant parts of the Decl.
191class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
192 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
193 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000194 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000195
196public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000197 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
198 : ID(ID), Hash(Hash) {}
199
200 void AddStmt(const Stmt *S) {
201 Hash.AddBoolean(S);
202 if (S) {
203 Hash.AddStmt(S);
204 }
205 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000206
Richard Trieud0786092017-02-23 00:23:01 +0000207 void AddIdentifierInfo(const IdentifierInfo *II) {
208 Hash.AddBoolean(II);
209 if (II) {
210 Hash.AddIdentifierInfo(II);
211 }
212 }
213
Richard Trieubcaaf962017-02-23 03:25:57 +0000214 void AddQualType(QualType T) {
215 Hash.AddQualType(T);
216 }
217
Richard Trieue7f7ed22017-02-22 01:11:25 +0000218 void Visit(const Decl *D) {
219 ID.AddInteger(D->getKind());
220 Inherited::Visit(D);
221 }
222
Richard Trieud0786092017-02-23 00:23:01 +0000223 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000224 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000225 Inherited::VisitNamedDecl(D);
226 }
227
Richard Trieubcaaf962017-02-23 03:25:57 +0000228 void VisitValueDecl(const ValueDecl *D) {
229 AddQualType(D->getType());
230 Inherited::VisitValueDecl(D);
231 }
232
Richard Trieu02552272017-05-02 23:58:52 +0000233 void VisitParmVarDecl(const ParmVarDecl *D) {
234 // TODO: Handle default arguments.
235 Inherited::VisitParmVarDecl(D);
236 }
237
Richard Trieue7f7ed22017-02-22 01:11:25 +0000238 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
239 ID.AddInteger(D->getAccess());
240 Inherited::VisitAccessSpecDecl(D);
241 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000242
243 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
244 AddStmt(D->getAssertExpr());
245 AddStmt(D->getMessage());
246
247 Inherited::VisitStaticAssertDecl(D);
248 }
Richard Trieud0786092017-02-23 00:23:01 +0000249
250 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000251 const bool IsBitfield = D->isBitField();
252 Hash.AddBoolean(IsBitfield);
253
254 if (IsBitfield) {
255 AddStmt(D->getBitWidth());
256 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000257
258 Hash.AddBoolean(D->isMutable());
259 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000260
261 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000262 }
Richard Trieu48143742017-02-28 21:24:38 +0000263
264 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000265 ID.AddInteger(D->getStorageClass());
266 Hash.AddBoolean(D->isInlineSpecified());
267 Hash.AddBoolean(D->isVirtualAsWritten());
268 Hash.AddBoolean(D->isPure());
269 Hash.AddBoolean(D->isDeletedAsWritten());
270
Richard Trieu02552272017-05-02 23:58:52 +0000271 ID.AddInteger(D->param_size());
272
273 for (auto *Param : D->parameters()) {
274 Hash.AddSubDecl(Param);
275 }
276
Richard Trieu48143742017-02-28 21:24:38 +0000277 Inherited::VisitFunctionDecl(D);
278 }
279
280 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000281 Hash.AddBoolean(D->isConst());
282 Hash.AddBoolean(D->isVolatile());
283
Richard Trieu48143742017-02-28 21:24:38 +0000284 Inherited::VisitCXXMethodDecl(D);
285 }
Richard Trieu33562c22017-03-08 00:13:19 +0000286
287 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
288 AddQualType(D->getUnderlyingType());
289
290 Inherited::VisitTypedefNameDecl(D);
291 }
292
293 void VisitTypedefDecl(const TypedefDecl *D) {
294 Inherited::VisitTypedefDecl(D);
295 }
296
297 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
298 Inherited::VisitTypeAliasDecl(D);
299 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000300};
301
302// Only allow a small portion of Decl's to be processed. Remove this once
303// all Decl's can be handled.
304bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
305 if (D->isImplicit()) return false;
306 if (D->getDeclContext() != Parent) return false;
307
308 switch (D->getKind()) {
309 default:
310 return false;
311 case Decl::AccessSpec:
Richard Trieu48143742017-02-28 21:24:38 +0000312 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000313 case Decl::Field:
Richard Trieu639d7b62017-02-22 22:22:42 +0000314 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000315 case Decl::TypeAlias:
316 case Decl::Typedef:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000317 return true;
318 }
319}
320
321void ODRHash::AddSubDecl(const Decl *D) {
322 assert(D && "Expecting non-null pointer.");
323 AddDecl(D);
324
Richard Trieu639d7b62017-02-22 22:22:42 +0000325 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000326}
327
328void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
329 assert(Record && Record->hasDefinition() &&
330 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000331
332 if (isa<ClassTemplateSpecializationDecl>(Record)) {
333 return;
334 }
335
Richard Trieue7f7ed22017-02-22 01:11:25 +0000336 AddDecl(Record);
337
338 // Filter out sub-Decls which will not be processed in order to get an
339 // accurate count of Decl's.
340 llvm::SmallVector<const Decl *, 16> Decls;
341 for (const Decl *SubDecl : Record->decls()) {
342 if (isWhitelistedDecl(SubDecl, Record)) {
343 Decls.push_back(SubDecl);
344 }
345 }
346
347 ID.AddInteger(Decls.size());
348 for (auto SubDecl : Decls) {
349 AddSubDecl(SubDecl);
350 }
351}
352
353void ODRHash::AddDecl(const Decl *D) {
354 assert(D && "Expecting non-null pointer.");
355 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
356 ID.AddInteger(Result.first->second);
357 // On first encounter of a Decl pointer, process it. Every time afterwards,
358 // only the index value is needed.
359 if (!Result.second) {
360 return;
361 }
362
363 ID.AddInteger(D->getKind());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000364
365 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
366 AddDeclarationName(ND->getDeclName());
367 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000368}
369
Richard Trieubcaaf962017-02-23 03:25:57 +0000370// Process a Type pointer. Add* methods call back into ODRHash while Visit*
371// methods process the relevant parts of the Type.
372class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
373 typedef TypeVisitor<ODRTypeVisitor> Inherited;
374 llvm::FoldingSetNodeID &ID;
375 ODRHash &Hash;
376
377public:
378 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
379 : ID(ID), Hash(Hash) {}
380
381 void AddStmt(Stmt *S) {
382 Hash.AddBoolean(S);
383 if (S) {
384 Hash.AddStmt(S);
385 }
386 }
387
Richard Trieu8459ddf2017-02-24 02:59:12 +0000388 void AddDecl(Decl *D) {
389 Hash.AddBoolean(D);
390 if (D) {
391 Hash.AddDecl(D);
392 }
393 }
394
Richard Trieu02552272017-05-02 23:58:52 +0000395 void AddQualType(QualType T) {
396 Hash.AddQualType(T);
397 }
398
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000399 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieuf21b8032017-06-09 20:11:51 +0000400 Hash.AddNestedNameSpecifier(NNS);
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000401 }
402
403 void AddIdentifierInfo(const IdentifierInfo *II) {
404 Hash.AddBoolean(II);
405 if (II) {
406 Hash.AddIdentifierInfo(II);
407 }
408 }
409
Richard Trieu02552272017-05-02 23:58:52 +0000410 void VisitQualifiers(Qualifiers Quals) {
411 ID.AddInteger(Quals.getAsOpaqueValue());
412 }
413
Richard Trieubcaaf962017-02-23 03:25:57 +0000414 void Visit(const Type *T) {
415 ID.AddInteger(T->getTypeClass());
416 Inherited::Visit(T);
417 }
418
419 void VisitType(const Type *T) {}
420
Richard Trieu02552272017-05-02 23:58:52 +0000421 void VisitAdjustedType(const AdjustedType *T) {
422 AddQualType(T->getOriginalType());
423 AddQualType(T->getAdjustedType());
424 VisitType(T);
425 }
426
427 void VisitDecayedType(const DecayedType *T) {
428 AddQualType(T->getDecayedType());
429 AddQualType(T->getPointeeType());
430 VisitAdjustedType(T);
431 }
432
433 void VisitArrayType(const ArrayType *T) {
434 AddQualType(T->getElementType());
435 ID.AddInteger(T->getSizeModifier());
436 VisitQualifiers(T->getIndexTypeQualifiers());
437 VisitType(T);
438 }
439 void VisitConstantArrayType(const ConstantArrayType *T) {
440 T->getSize().Profile(ID);
441 VisitArrayType(T);
442 }
443
444 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
445 AddStmt(T->getSizeExpr());
446 VisitArrayType(T);
447 }
448
449 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
450 VisitArrayType(T);
451 }
452
453 void VisitVariableArrayType(const VariableArrayType *T) {
454 AddStmt(T->getSizeExpr());
455 VisitArrayType(T);
456 }
457
Richard Trieubcaaf962017-02-23 03:25:57 +0000458 void VisitBuiltinType(const BuiltinType *T) {
459 ID.AddInteger(T->getKind());
460 VisitType(T);
461 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000462
Richard Trieu02552272017-05-02 23:58:52 +0000463 void VisitFunctionType(const FunctionType *T) {
464 AddQualType(T->getReturnType());
465 T->getExtInfo().Profile(ID);
466 Hash.AddBoolean(T->isConst());
467 Hash.AddBoolean(T->isVolatile());
468 Hash.AddBoolean(T->isRestrict());
469 VisitType(T);
470 }
471
472 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
473 VisitFunctionType(T);
474 }
475
476 void VisitFunctionProtoType(const FunctionProtoType *T) {
477 ID.AddInteger(T->getNumParams());
478 for (auto ParamType : T->getParamTypes())
479 AddQualType(ParamType);
480
481 VisitFunctionType(T);
482 }
483
Richard Trieu8459ddf2017-02-24 02:59:12 +0000484 void VisitTypedefType(const TypedefType *T) {
485 AddDecl(T->getDecl());
Richard Trieub35ef2a2017-05-09 03:24:34 +0000486 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000487 VisitType(T);
488 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000489
490 void VisitTagType(const TagType *T) {
491 AddDecl(T->getDecl());
492 VisitType(T);
493 }
494
495 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
496 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
497
498 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
499 ID.AddInteger(T->getKeyword());
500 VisitType(T);
501 };
502
503 void VisitDependentNameType(const DependentNameType *T) {
504 AddNestedNameSpecifier(T->getQualifier());
505 AddIdentifierInfo(T->getIdentifier());
506 VisitTypeWithKeyword(T);
507 }
508
509 void VisitDependentTemplateSpecializationType(
510 const DependentTemplateSpecializationType *T) {
511 AddIdentifierInfo(T->getIdentifier());
512 AddNestedNameSpecifier(T->getQualifier());
513 ID.AddInteger(T->getNumArgs());
514 for (const auto &TA : T->template_arguments()) {
515 Hash.AddTemplateArgument(TA);
516 }
517 VisitTypeWithKeyword(T);
518 }
519
520 void VisitElaboratedType(const ElaboratedType *T) {
521 AddNestedNameSpecifier(T->getQualifier());
522 AddQualType(T->getNamedType());
523 VisitTypeWithKeyword(T);
524 }
Richard Trieu96b49622017-05-31 00:31:58 +0000525
526 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
527 ID.AddInteger(T->getNumArgs());
528 for (const auto &TA : T->template_arguments()) {
529 Hash.AddTemplateArgument(TA);
530 }
531 Hash.AddTemplateName(T->getTemplateName());
532 VisitType(T);
533 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000534};
535
536void ODRHash::AddType(const Type *T) {
537 assert(T && "Expecting non-null pointer.");
538 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
539 ID.AddInteger(Result.first->second);
540 // On first encounter of a Type pointer, process it. Every time afterwards,
541 // only the index value is needed.
542 if (!Result.second) {
543 return;
544 }
545
546 ODRTypeVisitor(ID, *this).Visit(T);
547}
548
549void ODRHash::AddQualType(QualType T) {
550 AddBoolean(T.isNull());
551 if (T.isNull())
552 return;
553 SplitQualType split = T.split();
554 ID.AddInteger(split.Quals.getAsOpaqueValue());
555 AddType(split.Ty);
556}
557
Richard Trieue7f7ed22017-02-22 01:11:25 +0000558void ODRHash::AddBoolean(bool Value) {
559 Bools.push_back(Value);
560}