blob: f037827da83472fbe484b0184590d5afca610140 [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);
Richard Trieu1dcb4052017-06-14 01:28:00 +0000146
147 switch (Kind) {
148 case TemplateArgument::Null:
149 case TemplateArgument::Type:
150 case TemplateArgument::Declaration:
151 case TemplateArgument::NullPtr:
152 case TemplateArgument::Integral:
Richard Trieuee132d62017-06-14 03:17:26 +0000153 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000154 case TemplateArgument::Template:
155 case TemplateArgument::TemplateExpansion:
Richard Trieuee132d62017-06-14 03:17:26 +0000156 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
Richard Trieu1dcb4052017-06-14 01:28:00 +0000157 break;
158 case TemplateArgument::Expression:
159 AddStmt(TA.getAsExpr());
160 break;
161 case TemplateArgument::Pack:
162 break;
163 }
Richard Trieu3b261bb72017-06-13 22:21:18 +0000164}
165
Richard Trieue7f7ed22017-02-22 01:11:25 +0000166void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
167
168void ODRHash::clear() {
169 DeclMap.clear();
170 TypeMap.clear();
171 Bools.clear();
172 ID.clear();
173}
174
175unsigned ODRHash::CalculateHash() {
176 // Append the bools to the end of the data segment backwards. This allows
177 // for the bools data to be compressed 32 times smaller compared to using
178 // ID.AddBoolean
179 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
180 const unsigned size = Bools.size();
181 const unsigned remainder = size % unsigned_bits;
182 const unsigned loops = size / unsigned_bits;
183 auto I = Bools.rbegin();
184 unsigned value = 0;
185 for (unsigned i = 0; i < remainder; ++i) {
186 value <<= 1;
187 value |= *I;
188 ++I;
189 }
190 ID.AddInteger(value);
191
192 for (unsigned i = 0; i < loops; ++i) {
193 value = 0;
194 for (unsigned j = 0; j < unsigned_bits; ++j) {
195 value <<= 1;
196 value |= *I;
197 ++I;
198 }
199 ID.AddInteger(value);
200 }
201
202 assert(I == Bools.rend());
203 Bools.clear();
204 return ID.ComputeHash();
205}
206
207// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
208// methods process the relevant parts of the Decl.
209class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
210 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
211 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000212 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000213
214public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000215 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
216 : ID(ID), Hash(Hash) {}
217
218 void AddStmt(const Stmt *S) {
219 Hash.AddBoolean(S);
220 if (S) {
221 Hash.AddStmt(S);
222 }
223 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000224
Richard Trieud0786092017-02-23 00:23:01 +0000225 void AddIdentifierInfo(const IdentifierInfo *II) {
226 Hash.AddBoolean(II);
227 if (II) {
228 Hash.AddIdentifierInfo(II);
229 }
230 }
231
Richard Trieubcaaf962017-02-23 03:25:57 +0000232 void AddQualType(QualType T) {
233 Hash.AddQualType(T);
234 }
235
Richard Trieue7f7ed22017-02-22 01:11:25 +0000236 void Visit(const Decl *D) {
237 ID.AddInteger(D->getKind());
238 Inherited::Visit(D);
239 }
240
Richard Trieud0786092017-02-23 00:23:01 +0000241 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000242 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000243 Inherited::VisitNamedDecl(D);
244 }
245
Richard Trieubcaaf962017-02-23 03:25:57 +0000246 void VisitValueDecl(const ValueDecl *D) {
247 AddQualType(D->getType());
248 Inherited::VisitValueDecl(D);
249 }
250
Richard Trieu02552272017-05-02 23:58:52 +0000251 void VisitParmVarDecl(const ParmVarDecl *D) {
252 // TODO: Handle default arguments.
253 Inherited::VisitParmVarDecl(D);
254 }
255
Richard Trieue7f7ed22017-02-22 01:11:25 +0000256 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
257 ID.AddInteger(D->getAccess());
258 Inherited::VisitAccessSpecDecl(D);
259 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000260
261 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
262 AddStmt(D->getAssertExpr());
263 AddStmt(D->getMessage());
264
265 Inherited::VisitStaticAssertDecl(D);
266 }
Richard Trieud0786092017-02-23 00:23:01 +0000267
268 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000269 const bool IsBitfield = D->isBitField();
270 Hash.AddBoolean(IsBitfield);
271
272 if (IsBitfield) {
273 AddStmt(D->getBitWidth());
274 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000275
276 Hash.AddBoolean(D->isMutable());
277 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000278
279 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000280 }
Richard Trieu48143742017-02-28 21:24:38 +0000281
282 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000283 ID.AddInteger(D->getStorageClass());
284 Hash.AddBoolean(D->isInlineSpecified());
285 Hash.AddBoolean(D->isVirtualAsWritten());
286 Hash.AddBoolean(D->isPure());
287 Hash.AddBoolean(D->isDeletedAsWritten());
288
Richard Trieu02552272017-05-02 23:58:52 +0000289 ID.AddInteger(D->param_size());
290
291 for (auto *Param : D->parameters()) {
292 Hash.AddSubDecl(Param);
293 }
294
Richard Trieu48143742017-02-28 21:24:38 +0000295 Inherited::VisitFunctionDecl(D);
296 }
297
298 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000299 Hash.AddBoolean(D->isConst());
300 Hash.AddBoolean(D->isVolatile());
301
Richard Trieu48143742017-02-28 21:24:38 +0000302 Inherited::VisitCXXMethodDecl(D);
303 }
Richard Trieu33562c22017-03-08 00:13:19 +0000304
305 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
306 AddQualType(D->getUnderlyingType());
307
308 Inherited::VisitTypedefNameDecl(D);
309 }
310
311 void VisitTypedefDecl(const TypedefDecl *D) {
312 Inherited::VisitTypedefDecl(D);
313 }
314
315 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
316 Inherited::VisitTypeAliasDecl(D);
317 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000318};
319
320// Only allow a small portion of Decl's to be processed. Remove this once
321// all Decl's can be handled.
322bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
323 if (D->isImplicit()) return false;
324 if (D->getDeclContext() != Parent) return false;
325
326 switch (D->getKind()) {
327 default:
328 return false;
329 case Decl::AccessSpec:
Richard Trieu48143742017-02-28 21:24:38 +0000330 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000331 case Decl::Field:
Richard Trieu639d7b62017-02-22 22:22:42 +0000332 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000333 case Decl::TypeAlias:
334 case Decl::Typedef:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000335 return true;
336 }
337}
338
339void ODRHash::AddSubDecl(const Decl *D) {
340 assert(D && "Expecting non-null pointer.");
341 AddDecl(D);
342
Richard Trieu639d7b62017-02-22 22:22:42 +0000343 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000344}
345
346void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
347 assert(Record && Record->hasDefinition() &&
348 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000349
350 if (isa<ClassTemplateSpecializationDecl>(Record)) {
351 return;
352 }
353
Richard Trieue7f7ed22017-02-22 01:11:25 +0000354 AddDecl(Record);
355
356 // Filter out sub-Decls which will not be processed in order to get an
357 // accurate count of Decl's.
358 llvm::SmallVector<const Decl *, 16> Decls;
359 for (const Decl *SubDecl : Record->decls()) {
360 if (isWhitelistedDecl(SubDecl, Record)) {
361 Decls.push_back(SubDecl);
362 }
363 }
364
365 ID.AddInteger(Decls.size());
366 for (auto SubDecl : Decls) {
367 AddSubDecl(SubDecl);
368 }
369}
370
371void ODRHash::AddDecl(const Decl *D) {
372 assert(D && "Expecting non-null pointer.");
373 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
374 ID.AddInteger(Result.first->second);
375 // On first encounter of a Decl pointer, process it. Every time afterwards,
376 // only the index value is needed.
377 if (!Result.second) {
378 return;
379 }
380
381 ID.AddInteger(D->getKind());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000382
383 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
384 AddDeclarationName(ND->getDeclName());
385 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000386}
387
Richard Trieubcaaf962017-02-23 03:25:57 +0000388// Process a Type pointer. Add* methods call back into ODRHash while Visit*
389// methods process the relevant parts of the Type.
390class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
391 typedef TypeVisitor<ODRTypeVisitor> Inherited;
392 llvm::FoldingSetNodeID &ID;
393 ODRHash &Hash;
394
395public:
396 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
397 : ID(ID), Hash(Hash) {}
398
399 void AddStmt(Stmt *S) {
400 Hash.AddBoolean(S);
401 if (S) {
402 Hash.AddStmt(S);
403 }
404 }
405
Richard Trieu8459ddf2017-02-24 02:59:12 +0000406 void AddDecl(Decl *D) {
407 Hash.AddBoolean(D);
408 if (D) {
409 Hash.AddDecl(D);
410 }
411 }
412
Richard Trieu02552272017-05-02 23:58:52 +0000413 void AddQualType(QualType T) {
414 Hash.AddQualType(T);
415 }
416
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000417 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieuf21b8032017-06-09 20:11:51 +0000418 Hash.AddNestedNameSpecifier(NNS);
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000419 }
420
421 void AddIdentifierInfo(const IdentifierInfo *II) {
422 Hash.AddBoolean(II);
423 if (II) {
424 Hash.AddIdentifierInfo(II);
425 }
426 }
427
Richard Trieu02552272017-05-02 23:58:52 +0000428 void VisitQualifiers(Qualifiers Quals) {
429 ID.AddInteger(Quals.getAsOpaqueValue());
430 }
431
Richard Trieubcaaf962017-02-23 03:25:57 +0000432 void Visit(const Type *T) {
433 ID.AddInteger(T->getTypeClass());
434 Inherited::Visit(T);
435 }
436
437 void VisitType(const Type *T) {}
438
Richard Trieu02552272017-05-02 23:58:52 +0000439 void VisitAdjustedType(const AdjustedType *T) {
440 AddQualType(T->getOriginalType());
441 AddQualType(T->getAdjustedType());
442 VisitType(T);
443 }
444
445 void VisitDecayedType(const DecayedType *T) {
446 AddQualType(T->getDecayedType());
447 AddQualType(T->getPointeeType());
448 VisitAdjustedType(T);
449 }
450
451 void VisitArrayType(const ArrayType *T) {
452 AddQualType(T->getElementType());
453 ID.AddInteger(T->getSizeModifier());
454 VisitQualifiers(T->getIndexTypeQualifiers());
455 VisitType(T);
456 }
457 void VisitConstantArrayType(const ConstantArrayType *T) {
458 T->getSize().Profile(ID);
459 VisitArrayType(T);
460 }
461
462 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
463 AddStmt(T->getSizeExpr());
464 VisitArrayType(T);
465 }
466
467 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
468 VisitArrayType(T);
469 }
470
471 void VisitVariableArrayType(const VariableArrayType *T) {
472 AddStmt(T->getSizeExpr());
473 VisitArrayType(T);
474 }
475
Richard Trieubcaaf962017-02-23 03:25:57 +0000476 void VisitBuiltinType(const BuiltinType *T) {
477 ID.AddInteger(T->getKind());
478 VisitType(T);
479 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000480
Richard Trieu02552272017-05-02 23:58:52 +0000481 void VisitFunctionType(const FunctionType *T) {
482 AddQualType(T->getReturnType());
483 T->getExtInfo().Profile(ID);
484 Hash.AddBoolean(T->isConst());
485 Hash.AddBoolean(T->isVolatile());
486 Hash.AddBoolean(T->isRestrict());
487 VisitType(T);
488 }
489
490 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
491 VisitFunctionType(T);
492 }
493
494 void VisitFunctionProtoType(const FunctionProtoType *T) {
495 ID.AddInteger(T->getNumParams());
496 for (auto ParamType : T->getParamTypes())
497 AddQualType(ParamType);
498
499 VisitFunctionType(T);
500 }
501
Richard Trieu8459ddf2017-02-24 02:59:12 +0000502 void VisitTypedefType(const TypedefType *T) {
503 AddDecl(T->getDecl());
Richard Trieub35ef2a2017-05-09 03:24:34 +0000504 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000505 VisitType(T);
506 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000507
508 void VisitTagType(const TagType *T) {
509 AddDecl(T->getDecl());
510 VisitType(T);
511 }
512
513 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
514 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
515
516 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
517 ID.AddInteger(T->getKeyword());
518 VisitType(T);
519 };
520
521 void VisitDependentNameType(const DependentNameType *T) {
522 AddNestedNameSpecifier(T->getQualifier());
523 AddIdentifierInfo(T->getIdentifier());
524 VisitTypeWithKeyword(T);
525 }
526
527 void VisitDependentTemplateSpecializationType(
528 const DependentTemplateSpecializationType *T) {
529 AddIdentifierInfo(T->getIdentifier());
530 AddNestedNameSpecifier(T->getQualifier());
531 ID.AddInteger(T->getNumArgs());
532 for (const auto &TA : T->template_arguments()) {
533 Hash.AddTemplateArgument(TA);
534 }
535 VisitTypeWithKeyword(T);
536 }
537
538 void VisitElaboratedType(const ElaboratedType *T) {
539 AddNestedNameSpecifier(T->getQualifier());
540 AddQualType(T->getNamedType());
541 VisitTypeWithKeyword(T);
542 }
Richard Trieu96b49622017-05-31 00:31:58 +0000543
544 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
545 ID.AddInteger(T->getNumArgs());
546 for (const auto &TA : T->template_arguments()) {
547 Hash.AddTemplateArgument(TA);
548 }
549 Hash.AddTemplateName(T->getTemplateName());
550 VisitType(T);
551 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000552};
553
554void ODRHash::AddType(const Type *T) {
555 assert(T && "Expecting non-null pointer.");
556 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
557 ID.AddInteger(Result.first->second);
558 // On first encounter of a Type pointer, process it. Every time afterwards,
559 // only the index value is needed.
560 if (!Result.second) {
561 return;
562 }
563
564 ODRTypeVisitor(ID, *this).Visit(T);
565}
566
567void ODRHash::AddQualType(QualType T) {
568 AddBoolean(T.isNull());
569 if (T.isNull())
570 return;
571 SplitQualType split = T.split();
572 ID.AddInteger(split.Quals.getAsOpaqueValue());
573 AddType(split.Ty);
574}
575
Richard Trieue7f7ed22017-02-22 01:11:25 +0000576void ODRHash::AddBoolean(bool Value) {
577 Bools.push_back(Value);
578}