blob: 7a3a6964e5b5dc07b99763b02b47196ba3eeeba2 [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) {
Richard Trieuf65efae2018-02-22 05:32:25 +000036 // Index all DeclarationName and use index numbers to refer to them.
37 auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
38 ID.AddInteger(Result.first->second);
39 if (!Result.second) {
40 // If found in map, the the DeclarationName has previously been processed.
41 return;
42 }
43
44 // First time processing each DeclarationName, also process its details.
Richard Trieu8459ddf2017-02-24 02:59:12 +000045 AddBoolean(Name.isEmpty());
46 if (Name.isEmpty())
47 return;
48
49 auto Kind = Name.getNameKind();
50 ID.AddInteger(Kind);
51 switch (Kind) {
52 case DeclarationName::Identifier:
53 AddIdentifierInfo(Name.getAsIdentifierInfo());
54 break;
55 case DeclarationName::ObjCZeroArgSelector:
56 case DeclarationName::ObjCOneArgSelector:
57 case DeclarationName::ObjCMultiArgSelector: {
58 Selector S = Name.getObjCSelector();
59 AddBoolean(S.isNull());
60 AddBoolean(S.isKeywordSelector());
61 AddBoolean(S.isUnarySelector());
62 unsigned NumArgs = S.getNumArgs();
63 for (unsigned i = 0; i < NumArgs; ++i) {
64 AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
65 }
66 break;
67 }
68 case DeclarationName::CXXConstructorName:
69 case DeclarationName::CXXDestructorName:
70 AddQualType(Name.getCXXNameType());
71 break;
72 case DeclarationName::CXXOperatorName:
73 ID.AddInteger(Name.getCXXOverloadedOperator());
74 break;
75 case DeclarationName::CXXLiteralOperatorName:
76 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
77 break;
78 case DeclarationName::CXXConversionFunctionName:
79 AddQualType(Name.getCXXNameType());
80 break;
81 case DeclarationName::CXXUsingDirective:
82 break;
83 case DeclarationName::CXXDeductionGuideName: {
84 auto *Template = Name.getCXXDeductionGuideTemplate();
85 AddBoolean(Template);
86 if (Template) {
87 AddDecl(Template);
88 }
89 }
90 }
91}
92
Richard Trieuce81b192017-05-17 03:23:35 +000093void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieu96b41642017-07-01 02:00:05 +000094 assert(NNS && "Expecting non-null pointer.");
95 const auto *Prefix = NNS->getPrefix();
96 AddBoolean(Prefix);
97 if (Prefix) {
98 AddNestedNameSpecifier(Prefix);
Richard Trieuce81b192017-05-17 03:23:35 +000099 }
100 auto Kind = NNS->getKind();
101 ID.AddInteger(Kind);
102 switch (Kind) {
103 case NestedNameSpecifier::Identifier:
104 AddIdentifierInfo(NNS->getAsIdentifier());
105 break;
106 case NestedNameSpecifier::Namespace:
107 AddDecl(NNS->getAsNamespace());
108 break;
109 case NestedNameSpecifier::NamespaceAlias:
110 AddDecl(NNS->getAsNamespaceAlias());
111 break;
112 case NestedNameSpecifier::TypeSpec:
113 case NestedNameSpecifier::TypeSpecWithTemplate:
114 AddType(NNS->getAsType());
115 break;
116 case NestedNameSpecifier::Global:
117 case NestedNameSpecifier::Super:
118 break;
119 }
120}
121
Richard Trieu96b49622017-05-31 00:31:58 +0000122void ODRHash::AddTemplateName(TemplateName Name) {
123 auto Kind = Name.getKind();
124 ID.AddInteger(Kind);
125
126 switch (Kind) {
127 case TemplateName::Template:
128 AddDecl(Name.getAsTemplateDecl());
129 break;
130 // TODO: Support these cases.
131 case TemplateName::OverloadedTemplate:
132 case TemplateName::QualifiedTemplate:
133 case TemplateName::DependentTemplate:
134 case TemplateName::SubstTemplateTemplateParm:
135 case TemplateName::SubstTemplateTemplateParmPack:
136 break;
137 }
138}
139
Richard Trieu3b261bb72017-06-13 22:21:18 +0000140void ODRHash::AddTemplateArgument(TemplateArgument TA) {
141 const auto Kind = TA.getKind();
142 ID.AddInteger(Kind);
Richard Trieu1dcb4052017-06-14 01:28:00 +0000143
144 switch (Kind) {
145 case TemplateArgument::Null:
Richard Trieu8844c522017-06-30 22:40:33 +0000146 llvm_unreachable("Expected valid TemplateArgument");
Richard Trieu1dcb4052017-06-14 01:28:00 +0000147 case TemplateArgument::Type:
Richard Trieu8844c522017-06-30 22:40:33 +0000148 AddQualType(TA.getAsType());
149 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000150 case TemplateArgument::Declaration:
Richard Trieu7282d322018-04-25 00:31:15 +0000151 AddDecl(TA.getAsDecl());
152 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000153 case TemplateArgument::NullPtr:
154 case TemplateArgument::Integral:
Richard Trieuee132d62017-06-14 03:17:26 +0000155 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000156 case TemplateArgument::Template:
157 case TemplateArgument::TemplateExpansion:
Richard Trieuee132d62017-06-14 03:17:26 +0000158 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
Richard Trieu1dcb4052017-06-14 01:28:00 +0000159 break;
160 case TemplateArgument::Expression:
161 AddStmt(TA.getAsExpr());
162 break;
163 case TemplateArgument::Pack:
Richard Trieud9201d02017-06-15 01:35:06 +0000164 ID.AddInteger(TA.pack_size());
165 for (auto SubTA : TA.pack_elements()) {
166 AddTemplateArgument(SubTA);
167 }
Richard Trieu1dcb4052017-06-14 01:28:00 +0000168 break;
169 }
Richard Trieu3b261bb72017-06-13 22:21:18 +0000170}
171
Richard Trieu498117b2017-08-23 02:43:59 +0000172void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
173 assert(TPL && "Expecting non-null pointer.");
174
175 ID.AddInteger(TPL->size());
176 for (auto *ND : TPL->asArray()) {
177 AddSubDecl(ND);
178 }
179}
Richard Trieue7f7ed22017-02-22 01:11:25 +0000180
181void ODRHash::clear() {
Richard Trieuf65efae2018-02-22 05:32:25 +0000182 DeclNameMap.clear();
Richard Trieue7f7ed22017-02-22 01:11:25 +0000183 TypeMap.clear();
184 Bools.clear();
185 ID.clear();
186}
187
188unsigned ODRHash::CalculateHash() {
189 // Append the bools to the end of the data segment backwards. This allows
190 // for the bools data to be compressed 32 times smaller compared to using
191 // ID.AddBoolean
192 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
193 const unsigned size = Bools.size();
194 const unsigned remainder = size % unsigned_bits;
195 const unsigned loops = size / unsigned_bits;
196 auto I = Bools.rbegin();
197 unsigned value = 0;
198 for (unsigned i = 0; i < remainder; ++i) {
199 value <<= 1;
200 value |= *I;
201 ++I;
202 }
203 ID.AddInteger(value);
204
205 for (unsigned i = 0; i < loops; ++i) {
206 value = 0;
207 for (unsigned j = 0; j < unsigned_bits; ++j) {
208 value <<= 1;
209 value |= *I;
210 ++I;
211 }
212 ID.AddInteger(value);
213 }
214
215 assert(I == Bools.rend());
216 Bools.clear();
217 return ID.ComputeHash();
218}
219
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000220namespace {
Richard Trieue7f7ed22017-02-22 01:11:25 +0000221// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
222// methods process the relevant parts of the Decl.
223class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
224 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
225 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000226 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000227
228public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000229 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
230 : ID(ID), Hash(Hash) {}
231
232 void AddStmt(const Stmt *S) {
233 Hash.AddBoolean(S);
234 if (S) {
235 Hash.AddStmt(S);
236 }
237 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000238
Richard Trieud0786092017-02-23 00:23:01 +0000239 void AddIdentifierInfo(const IdentifierInfo *II) {
240 Hash.AddBoolean(II);
241 if (II) {
242 Hash.AddIdentifierInfo(II);
243 }
244 }
245
Richard Trieubcaaf962017-02-23 03:25:57 +0000246 void AddQualType(QualType T) {
247 Hash.AddQualType(T);
248 }
249
Richard Trieuac6a1b62017-07-08 02:04:42 +0000250 void AddDecl(const Decl *D) {
251 Hash.AddBoolean(D);
252 if (D) {
253 Hash.AddDecl(D);
254 }
255 }
256
Richard Trieu498117b2017-08-23 02:43:59 +0000257 void AddTemplateArgument(TemplateArgument TA) {
258 Hash.AddTemplateArgument(TA);
259 }
260
Richard Trieue7f7ed22017-02-22 01:11:25 +0000261 void Visit(const Decl *D) {
262 ID.AddInteger(D->getKind());
263 Inherited::Visit(D);
264 }
265
Richard Trieud0786092017-02-23 00:23:01 +0000266 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000267 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000268 Inherited::VisitNamedDecl(D);
269 }
270
Richard Trieubcaaf962017-02-23 03:25:57 +0000271 void VisitValueDecl(const ValueDecl *D) {
Richard Trieu9747a7c2017-07-14 01:36:41 +0000272 if (!isa<FunctionDecl>(D)) {
273 AddQualType(D->getType());
274 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000275 Inherited::VisitValueDecl(D);
276 }
277
Richard Trieu6e13ff32017-06-16 02:44:29 +0000278 void VisitVarDecl(const VarDecl *D) {
279 Hash.AddBoolean(D->isStaticLocal());
280 Hash.AddBoolean(D->isConstexpr());
281 const bool HasInit = D->hasInit();
282 Hash.AddBoolean(HasInit);
283 if (HasInit) {
284 AddStmt(D->getInit());
285 }
286 Inherited::VisitVarDecl(D);
287 }
288
Richard Trieu02552272017-05-02 23:58:52 +0000289 void VisitParmVarDecl(const ParmVarDecl *D) {
290 // TODO: Handle default arguments.
291 Inherited::VisitParmVarDecl(D);
292 }
293
Richard Trieue7f7ed22017-02-22 01:11:25 +0000294 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
295 ID.AddInteger(D->getAccess());
296 Inherited::VisitAccessSpecDecl(D);
297 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000298
299 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
300 AddStmt(D->getAssertExpr());
301 AddStmt(D->getMessage());
302
303 Inherited::VisitStaticAssertDecl(D);
304 }
Richard Trieud0786092017-02-23 00:23:01 +0000305
306 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000307 const bool IsBitfield = D->isBitField();
308 Hash.AddBoolean(IsBitfield);
309
310 if (IsBitfield) {
311 AddStmt(D->getBitWidth());
312 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000313
314 Hash.AddBoolean(D->isMutable());
315 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000316
317 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000318 }
Richard Trieu48143742017-02-28 21:24:38 +0000319
320 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000321 ID.AddInteger(D->getStorageClass());
322 Hash.AddBoolean(D->isInlineSpecified());
323 Hash.AddBoolean(D->isVirtualAsWritten());
324 Hash.AddBoolean(D->isPure());
325 Hash.AddBoolean(D->isDeletedAsWritten());
326
Richard Trieu02552272017-05-02 23:58:52 +0000327 ID.AddInteger(D->param_size());
328
329 for (auto *Param : D->parameters()) {
330 Hash.AddSubDecl(Param);
331 }
332
Richard Trieu9747a7c2017-07-14 01:36:41 +0000333 AddQualType(D->getReturnType());
334
Richard Trieu7282d322018-04-25 00:31:15 +0000335 const auto* SpecializationArgs = D->getTemplateSpecializationArgs();
336 Hash.AddBoolean(SpecializationArgs);
337 if (SpecializationArgs) {
338 ID.AddInteger(SpecializationArgs->size());
339 for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
340 Hash.AddTemplateArgument(TA);
341 }
342 }
343
Richard Trieu48143742017-02-28 21:24:38 +0000344 Inherited::VisitFunctionDecl(D);
345 }
346
347 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000348 Hash.AddBoolean(D->isConst());
349 Hash.AddBoolean(D->isVolatile());
350
Richard Trieu48143742017-02-28 21:24:38 +0000351 Inherited::VisitCXXMethodDecl(D);
352 }
Richard Trieu33562c22017-03-08 00:13:19 +0000353
354 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
355 AddQualType(D->getUnderlyingType());
356
357 Inherited::VisitTypedefNameDecl(D);
358 }
359
360 void VisitTypedefDecl(const TypedefDecl *D) {
361 Inherited::VisitTypedefDecl(D);
362 }
363
364 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
365 Inherited::VisitTypeAliasDecl(D);
366 }
Richard Trieuac6a1b62017-07-08 02:04:42 +0000367
368 void VisitFriendDecl(const FriendDecl *D) {
369 TypeSourceInfo *TSI = D->getFriendType();
370 Hash.AddBoolean(TSI);
371 if (TSI) {
372 AddQualType(TSI->getType());
373 } else {
374 AddDecl(D->getFriendDecl());
375 }
376 }
Richard Trieu498117b2017-08-23 02:43:59 +0000377
378 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
379 // Only care about default arguments as part of the definition.
380 const bool hasDefaultArgument =
381 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
382 Hash.AddBoolean(hasDefaultArgument);
383 if (hasDefaultArgument) {
384 AddTemplateArgument(D->getDefaultArgument());
385 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000386 Hash.AddBoolean(D->isParameterPack());
Richard Trieu498117b2017-08-23 02:43:59 +0000387
388 Inherited::VisitTemplateTypeParmDecl(D);
389 }
390
391 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
392 // Only care about default arguments as part of the definition.
393 const bool hasDefaultArgument =
394 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
395 Hash.AddBoolean(hasDefaultArgument);
396 if (hasDefaultArgument) {
397 AddStmt(D->getDefaultArgument());
398 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000399 Hash.AddBoolean(D->isParameterPack());
Richard Trieu498117b2017-08-23 02:43:59 +0000400
401 Inherited::VisitNonTypeTemplateParmDecl(D);
402 }
403
404 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
405 // Only care about default arguments as part of the definition.
406 const bool hasDefaultArgument =
407 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
408 Hash.AddBoolean(hasDefaultArgument);
409 if (hasDefaultArgument) {
410 AddTemplateArgument(D->getDefaultArgument().getArgument());
411 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000412 Hash.AddBoolean(D->isParameterPack());
Richard Trieu498117b2017-08-23 02:43:59 +0000413
414 Inherited::VisitTemplateTemplateParmDecl(D);
415 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000416
417 void VisitTemplateDecl(const TemplateDecl *D) {
418 Hash.AddTemplateParameterList(D->getTemplateParameters());
419
420 Inherited::VisitTemplateDecl(D);
421 }
422
423 void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
424 Hash.AddBoolean(D->isMemberSpecialization());
425 Inherited::VisitRedeclarableTemplateDecl(D);
426 }
427
428 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
429 Visit(D->getTemplatedDecl());
430 ID.AddInteger(D->getTemplatedDecl()->getODRHash());
431 Inherited::VisitFunctionTemplateDecl(D);
432 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000433};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000434} // namespace
Richard Trieue7f7ed22017-02-22 01:11:25 +0000435
436// Only allow a small portion of Decl's to be processed. Remove this once
437// all Decl's can be handled.
438bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
439 if (D->isImplicit()) return false;
440 if (D->getDeclContext() != Parent) return false;
441
442 switch (D->getKind()) {
443 default:
444 return false;
445 case Decl::AccessSpec:
Richard Trieu1c71d512017-07-15 02:55:13 +0000446 case Decl::CXXConstructor:
447 case Decl::CXXDestructor:
Richard Trieu48143742017-02-28 21:24:38 +0000448 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000449 case Decl::Field:
Richard Trieuac6a1b62017-07-08 02:04:42 +0000450 case Decl::Friend:
Richard Trieu9359e8f2018-05-30 01:12:26 +0000451 case Decl::FunctionTemplate:
Richard Trieu639d7b62017-02-22 22:22:42 +0000452 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000453 case Decl::TypeAlias:
454 case Decl::Typedef:
Richard Trieu6e13ff32017-06-16 02:44:29 +0000455 case Decl::Var:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000456 return true;
457 }
458}
459
460void ODRHash::AddSubDecl(const Decl *D) {
461 assert(D && "Expecting non-null pointer.");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000462
Richard Trieu639d7b62017-02-22 22:22:42 +0000463 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000464}
465
466void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
467 assert(Record && Record->hasDefinition() &&
468 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000469
Richard Trieu5fb82ef2017-08-05 00:54:19 +0000470 const DeclContext *DC = Record;
471 while (DC) {
472 if (isa<ClassTemplateSpecializationDecl>(DC)) {
473 return;
474 }
475 DC = DC->getParent();
Richard Trieu02552272017-05-02 23:58:52 +0000476 }
477
Richard Trieue7f7ed22017-02-22 01:11:25 +0000478 AddDecl(Record);
479
480 // Filter out sub-Decls which will not be processed in order to get an
481 // accurate count of Decl's.
482 llvm::SmallVector<const Decl *, 16> Decls;
483 for (const Decl *SubDecl : Record->decls()) {
484 if (isWhitelistedDecl(SubDecl, Record)) {
485 Decls.push_back(SubDecl);
486 }
487 }
488
489 ID.AddInteger(Decls.size());
490 for (auto SubDecl : Decls) {
491 AddSubDecl(SubDecl);
492 }
Richard Trieu498117b2017-08-23 02:43:59 +0000493
494 const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
495 AddBoolean(TD);
496 if (TD) {
497 AddTemplateParameterList(TD->getTemplateParameters());
498 }
Richard Trieue13eabe2017-09-30 02:19:17 +0000499
500 ID.AddInteger(Record->getNumBases());
501 auto Bases = Record->bases();
502 for (auto Base : Bases) {
503 AddQualType(Base.getType());
504 ID.AddInteger(Base.isVirtual());
505 ID.AddInteger(Base.getAccessSpecifierAsWritten());
506 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000507}
508
Richard Trieue6caa262017-12-23 00:41:01 +0000509void ODRHash::AddFunctionDecl(const FunctionDecl *Function) {
510 assert(Function && "Expecting non-null pointer.");
511
512 // Skip hashing these kinds of function.
513 if (Function->isImplicit()) return;
514 if (Function->isDefaulted()) return;
515 if (Function->isDeleted()) return;
516 if (!Function->hasBody()) return;
517 if (!Function->getBody()) return;
518
519 // Skip functions that are specializations or in specialization context.
520 const DeclContext *DC = Function;
521 while (DC) {
522 if (isa<ClassTemplateSpecializationDecl>(DC)) return;
523 if (auto *F = dyn_cast<FunctionDecl>(DC))
524 if (F->isFunctionTemplateSpecialization()) return;
525 DC = DC->getParent();
526 }
527
528 AddDecl(Function);
529
530 AddQualType(Function->getReturnType());
531
532 ID.AddInteger(Function->param_size());
533 for (auto Param : Function->parameters())
534 AddSubDecl(Param);
535
536 AddStmt(Function->getBody());
537}
538
Richard Trieue7f7ed22017-02-22 01:11:25 +0000539void ODRHash::AddDecl(const Decl *D) {
540 assert(D && "Expecting non-null pointer.");
Richard Trieuf72fb7e2017-12-21 22:38:29 +0000541 D = D->getCanonicalDecl();
Richard Trieuf65efae2018-02-22 05:32:25 +0000542
543 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
544 AddDeclarationName(ND->getDeclName());
Richard Trieue7f7ed22017-02-22 01:11:25 +0000545 return;
546 }
547
548 ID.AddInteger(D->getKind());
Richard Trieuf65efae2018-02-22 05:32:25 +0000549 // TODO: Handle non-NamedDecl here.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000550}
551
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000552namespace {
Richard Trieubcaaf962017-02-23 03:25:57 +0000553// Process a Type pointer. Add* methods call back into ODRHash while Visit*
554// methods process the relevant parts of the Type.
555class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
556 typedef TypeVisitor<ODRTypeVisitor> Inherited;
557 llvm::FoldingSetNodeID &ID;
558 ODRHash &Hash;
559
560public:
561 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
562 : ID(ID), Hash(Hash) {}
563
564 void AddStmt(Stmt *S) {
565 Hash.AddBoolean(S);
566 if (S) {
567 Hash.AddStmt(S);
568 }
569 }
570
Richard Trieu8459ddf2017-02-24 02:59:12 +0000571 void AddDecl(Decl *D) {
572 Hash.AddBoolean(D);
573 if (D) {
574 Hash.AddDecl(D);
575 }
576 }
577
Richard Trieu02552272017-05-02 23:58:52 +0000578 void AddQualType(QualType T) {
579 Hash.AddQualType(T);
580 }
581
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000582 void AddType(const Type *T) {
583 Hash.AddBoolean(T);
584 if (T) {
585 Hash.AddType(T);
586 }
587 }
588
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000589 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieu96b41642017-07-01 02:00:05 +0000590 Hash.AddBoolean(NNS);
591 if (NNS) {
592 Hash.AddNestedNameSpecifier(NNS);
593 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000594 }
595
596 void AddIdentifierInfo(const IdentifierInfo *II) {
597 Hash.AddBoolean(II);
598 if (II) {
599 Hash.AddIdentifierInfo(II);
600 }
601 }
602
Richard Trieu02552272017-05-02 23:58:52 +0000603 void VisitQualifiers(Qualifiers Quals) {
604 ID.AddInteger(Quals.getAsOpaqueValue());
605 }
606
Richard Trieubcaaf962017-02-23 03:25:57 +0000607 void Visit(const Type *T) {
608 ID.AddInteger(T->getTypeClass());
609 Inherited::Visit(T);
610 }
611
612 void VisitType(const Type *T) {}
613
Richard Trieu02552272017-05-02 23:58:52 +0000614 void VisitAdjustedType(const AdjustedType *T) {
615 AddQualType(T->getOriginalType());
616 AddQualType(T->getAdjustedType());
617 VisitType(T);
618 }
619
620 void VisitDecayedType(const DecayedType *T) {
621 AddQualType(T->getDecayedType());
622 AddQualType(T->getPointeeType());
623 VisitAdjustedType(T);
624 }
625
626 void VisitArrayType(const ArrayType *T) {
627 AddQualType(T->getElementType());
628 ID.AddInteger(T->getSizeModifier());
629 VisitQualifiers(T->getIndexTypeQualifiers());
630 VisitType(T);
631 }
632 void VisitConstantArrayType(const ConstantArrayType *T) {
633 T->getSize().Profile(ID);
634 VisitArrayType(T);
635 }
636
637 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
638 AddStmt(T->getSizeExpr());
639 VisitArrayType(T);
640 }
641
642 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
643 VisitArrayType(T);
644 }
645
646 void VisitVariableArrayType(const VariableArrayType *T) {
647 AddStmt(T->getSizeExpr());
648 VisitArrayType(T);
649 }
650
Richard Trieubcaaf962017-02-23 03:25:57 +0000651 void VisitBuiltinType(const BuiltinType *T) {
652 ID.AddInteger(T->getKind());
653 VisitType(T);
654 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000655
Richard Trieu02552272017-05-02 23:58:52 +0000656 void VisitFunctionType(const FunctionType *T) {
657 AddQualType(T->getReturnType());
658 T->getExtInfo().Profile(ID);
659 Hash.AddBoolean(T->isConst());
660 Hash.AddBoolean(T->isVolatile());
661 Hash.AddBoolean(T->isRestrict());
662 VisitType(T);
663 }
664
665 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
666 VisitFunctionType(T);
667 }
668
669 void VisitFunctionProtoType(const FunctionProtoType *T) {
670 ID.AddInteger(T->getNumParams());
671 for (auto ParamType : T->getParamTypes())
672 AddQualType(ParamType);
673
674 VisitFunctionType(T);
675 }
676
Richard Trieu15970af2018-04-13 22:34:43 +0000677 void VisitPointerType(const PointerType *T) {
678 AddQualType(T->getPointeeType());
679 VisitType(T);
680 }
681
682 void VisitReferenceType(const ReferenceType *T) {
683 AddQualType(T->getPointeeTypeAsWritten());
684 VisitType(T);
685 }
686
687 void VisitLValueReferenceType(const LValueReferenceType *T) {
688 VisitReferenceType(T);
689 }
690
691 void VisitRValueReferenceType(const RValueReferenceType *T) {
692 VisitReferenceType(T);
693 }
694
Richard Trieu8459ddf2017-02-24 02:59:12 +0000695 void VisitTypedefType(const TypedefType *T) {
696 AddDecl(T->getDecl());
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000697 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
698 VisitQualifiers(UnderlyingType.getQualifiers());
Richard Trieucaaccee2018-04-12 02:26:49 +0000699 while (true) {
700 if (const TypedefType *Underlying =
701 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
702 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
703 continue;
704 }
705 if (const ElaboratedType *Underlying =
706 dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
707 UnderlyingType = Underlying->getNamedType();
708 continue;
709 }
710
711 break;
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000712 }
713 AddType(UnderlyingType.getTypePtr());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000714 VisitType(T);
715 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000716
717 void VisitTagType(const TagType *T) {
718 AddDecl(T->getDecl());
719 VisitType(T);
720 }
721
722 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
723 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
724
725 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
726 ID.AddInteger(T->getKeyword());
727 VisitType(T);
728 };
729
730 void VisitDependentNameType(const DependentNameType *T) {
731 AddNestedNameSpecifier(T->getQualifier());
732 AddIdentifierInfo(T->getIdentifier());
733 VisitTypeWithKeyword(T);
734 }
735
736 void VisitDependentTemplateSpecializationType(
737 const DependentTemplateSpecializationType *T) {
738 AddIdentifierInfo(T->getIdentifier());
739 AddNestedNameSpecifier(T->getQualifier());
740 ID.AddInteger(T->getNumArgs());
741 for (const auto &TA : T->template_arguments()) {
742 Hash.AddTemplateArgument(TA);
743 }
744 VisitTypeWithKeyword(T);
745 }
746
747 void VisitElaboratedType(const ElaboratedType *T) {
748 AddNestedNameSpecifier(T->getQualifier());
749 AddQualType(T->getNamedType());
750 VisitTypeWithKeyword(T);
751 }
Richard Trieu96b49622017-05-31 00:31:58 +0000752
753 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
754 ID.AddInteger(T->getNumArgs());
755 for (const auto &TA : T->template_arguments()) {
756 Hash.AddTemplateArgument(TA);
757 }
758 Hash.AddTemplateName(T->getTemplateName());
759 VisitType(T);
760 }
Richard Trieud9201d02017-06-15 01:35:06 +0000761
762 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
763 ID.AddInteger(T->getDepth());
764 ID.AddInteger(T->getIndex());
765 Hash.AddBoolean(T->isParameterPack());
766 AddDecl(T->getDecl());
767 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000768};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000769} // namespace
Richard Trieubcaaf962017-02-23 03:25:57 +0000770
771void ODRHash::AddType(const Type *T) {
772 assert(T && "Expecting non-null pointer.");
773 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
774 ID.AddInteger(Result.first->second);
775 // On first encounter of a Type pointer, process it. Every time afterwards,
776 // only the index value is needed.
777 if (!Result.second) {
778 return;
779 }
780
781 ODRTypeVisitor(ID, *this).Visit(T);
782}
783
784void ODRHash::AddQualType(QualType T) {
785 AddBoolean(T.isNull());
786 if (T.isNull())
787 return;
788 SplitQualType split = T.split();
789 ID.AddInteger(split.Quals.getAsOpaqueValue());
790 AddType(split.Ty);
791}
792
Richard Trieue7f7ed22017-02-22 01:11:25 +0000793void ODRHash::AddBoolean(bool Value) {
794 Bools.push_back(Value);
795}