blob: 4104d3454e52367f58897eda5581bb6f2d800a3d [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:
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:
Richard Trieud9201d02017-06-15 01:35:06 +0000162 ID.AddInteger(TA.pack_size());
163 for (auto SubTA : TA.pack_elements()) {
164 AddTemplateArgument(SubTA);
165 }
Richard Trieu1dcb4052017-06-14 01:28:00 +0000166 break;
167 }
Richard Trieu3b261bb72017-06-13 22:21:18 +0000168}
169
Richard Trieu498117b2017-08-23 02:43:59 +0000170void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
171 assert(TPL && "Expecting non-null pointer.");
172
173 ID.AddInteger(TPL->size());
174 for (auto *ND : TPL->asArray()) {
175 AddSubDecl(ND);
176 }
177}
Richard Trieue7f7ed22017-02-22 01:11:25 +0000178
179void ODRHash::clear() {
Richard Trieuf65efae2018-02-22 05:32:25 +0000180 DeclNameMap.clear();
Richard Trieue7f7ed22017-02-22 01:11:25 +0000181 TypeMap.clear();
182 Bools.clear();
183 ID.clear();
184}
185
186unsigned ODRHash::CalculateHash() {
187 // Append the bools to the end of the data segment backwards. This allows
188 // for the bools data to be compressed 32 times smaller compared to using
189 // ID.AddBoolean
190 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
191 const unsigned size = Bools.size();
192 const unsigned remainder = size % unsigned_bits;
193 const unsigned loops = size / unsigned_bits;
194 auto I = Bools.rbegin();
195 unsigned value = 0;
196 for (unsigned i = 0; i < remainder; ++i) {
197 value <<= 1;
198 value |= *I;
199 ++I;
200 }
201 ID.AddInteger(value);
202
203 for (unsigned i = 0; i < loops; ++i) {
204 value = 0;
205 for (unsigned j = 0; j < unsigned_bits; ++j) {
206 value <<= 1;
207 value |= *I;
208 ++I;
209 }
210 ID.AddInteger(value);
211 }
212
213 assert(I == Bools.rend());
214 Bools.clear();
215 return ID.ComputeHash();
216}
217
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000218namespace {
Richard Trieue7f7ed22017-02-22 01:11:25 +0000219// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
220// methods process the relevant parts of the Decl.
221class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
222 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
223 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000224 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000225
226public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000227 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
228 : ID(ID), Hash(Hash) {}
229
230 void AddStmt(const Stmt *S) {
231 Hash.AddBoolean(S);
232 if (S) {
233 Hash.AddStmt(S);
234 }
235 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000236
Richard Trieud0786092017-02-23 00:23:01 +0000237 void AddIdentifierInfo(const IdentifierInfo *II) {
238 Hash.AddBoolean(II);
239 if (II) {
240 Hash.AddIdentifierInfo(II);
241 }
242 }
243
Richard Trieubcaaf962017-02-23 03:25:57 +0000244 void AddQualType(QualType T) {
245 Hash.AddQualType(T);
246 }
247
Richard Trieuac6a1b62017-07-08 02:04:42 +0000248 void AddDecl(const Decl *D) {
249 Hash.AddBoolean(D);
250 if (D) {
251 Hash.AddDecl(D);
252 }
253 }
254
Richard Trieu498117b2017-08-23 02:43:59 +0000255 void AddTemplateArgument(TemplateArgument TA) {
256 Hash.AddTemplateArgument(TA);
257 }
258
Richard Trieue7f7ed22017-02-22 01:11:25 +0000259 void Visit(const Decl *D) {
260 ID.AddInteger(D->getKind());
261 Inherited::Visit(D);
262 }
263
Richard Trieud0786092017-02-23 00:23:01 +0000264 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000265 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000266 Inherited::VisitNamedDecl(D);
267 }
268
Richard Trieubcaaf962017-02-23 03:25:57 +0000269 void VisitValueDecl(const ValueDecl *D) {
Richard Trieu9747a7c2017-07-14 01:36:41 +0000270 if (!isa<FunctionDecl>(D)) {
271 AddQualType(D->getType());
272 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000273 Inherited::VisitValueDecl(D);
274 }
275
Richard Trieu6e13ff32017-06-16 02:44:29 +0000276 void VisitVarDecl(const VarDecl *D) {
277 Hash.AddBoolean(D->isStaticLocal());
278 Hash.AddBoolean(D->isConstexpr());
279 const bool HasInit = D->hasInit();
280 Hash.AddBoolean(HasInit);
281 if (HasInit) {
282 AddStmt(D->getInit());
283 }
284 Inherited::VisitVarDecl(D);
285 }
286
Richard Trieu02552272017-05-02 23:58:52 +0000287 void VisitParmVarDecl(const ParmVarDecl *D) {
288 // TODO: Handle default arguments.
289 Inherited::VisitParmVarDecl(D);
290 }
291
Richard Trieue7f7ed22017-02-22 01:11:25 +0000292 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
293 ID.AddInteger(D->getAccess());
294 Inherited::VisitAccessSpecDecl(D);
295 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000296
297 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
298 AddStmt(D->getAssertExpr());
299 AddStmt(D->getMessage());
300
301 Inherited::VisitStaticAssertDecl(D);
302 }
Richard Trieud0786092017-02-23 00:23:01 +0000303
304 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000305 const bool IsBitfield = D->isBitField();
306 Hash.AddBoolean(IsBitfield);
307
308 if (IsBitfield) {
309 AddStmt(D->getBitWidth());
310 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000311
312 Hash.AddBoolean(D->isMutable());
313 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000314
315 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000316 }
Richard Trieu48143742017-02-28 21:24:38 +0000317
318 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000319 ID.AddInteger(D->getStorageClass());
320 Hash.AddBoolean(D->isInlineSpecified());
321 Hash.AddBoolean(D->isVirtualAsWritten());
322 Hash.AddBoolean(D->isPure());
323 Hash.AddBoolean(D->isDeletedAsWritten());
324
Richard Trieu02552272017-05-02 23:58:52 +0000325 ID.AddInteger(D->param_size());
326
327 for (auto *Param : D->parameters()) {
328 Hash.AddSubDecl(Param);
329 }
330
Richard Trieu9747a7c2017-07-14 01:36:41 +0000331 AddQualType(D->getReturnType());
332
Richard Trieu48143742017-02-28 21:24:38 +0000333 Inherited::VisitFunctionDecl(D);
334 }
335
336 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000337 Hash.AddBoolean(D->isConst());
338 Hash.AddBoolean(D->isVolatile());
339
Richard Trieu48143742017-02-28 21:24:38 +0000340 Inherited::VisitCXXMethodDecl(D);
341 }
Richard Trieu33562c22017-03-08 00:13:19 +0000342
343 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
344 AddQualType(D->getUnderlyingType());
345
346 Inherited::VisitTypedefNameDecl(D);
347 }
348
349 void VisitTypedefDecl(const TypedefDecl *D) {
350 Inherited::VisitTypedefDecl(D);
351 }
352
353 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
354 Inherited::VisitTypeAliasDecl(D);
355 }
Richard Trieuac6a1b62017-07-08 02:04:42 +0000356
357 void VisitFriendDecl(const FriendDecl *D) {
358 TypeSourceInfo *TSI = D->getFriendType();
359 Hash.AddBoolean(TSI);
360 if (TSI) {
361 AddQualType(TSI->getType());
362 } else {
363 AddDecl(D->getFriendDecl());
364 }
365 }
Richard Trieu498117b2017-08-23 02:43:59 +0000366
367 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
368 // Only care about default arguments as part of the definition.
369 const bool hasDefaultArgument =
370 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
371 Hash.AddBoolean(hasDefaultArgument);
372 if (hasDefaultArgument) {
373 AddTemplateArgument(D->getDefaultArgument());
374 }
375
376 Inherited::VisitTemplateTypeParmDecl(D);
377 }
378
379 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
380 // Only care about default arguments as part of the definition.
381 const bool hasDefaultArgument =
382 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
383 Hash.AddBoolean(hasDefaultArgument);
384 if (hasDefaultArgument) {
385 AddStmt(D->getDefaultArgument());
386 }
387
388 Inherited::VisitNonTypeTemplateParmDecl(D);
389 }
390
391 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *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 AddTemplateArgument(D->getDefaultArgument().getArgument());
398 }
399
400 Inherited::VisitTemplateTemplateParmDecl(D);
401 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000402};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000403} // namespace
Richard Trieue7f7ed22017-02-22 01:11:25 +0000404
405// Only allow a small portion of Decl's to be processed. Remove this once
406// all Decl's can be handled.
407bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
408 if (D->isImplicit()) return false;
409 if (D->getDeclContext() != Parent) return false;
410
411 switch (D->getKind()) {
412 default:
413 return false;
414 case Decl::AccessSpec:
Richard Trieu1c71d512017-07-15 02:55:13 +0000415 case Decl::CXXConstructor:
416 case Decl::CXXDestructor:
Richard Trieu48143742017-02-28 21:24:38 +0000417 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000418 case Decl::Field:
Richard Trieuac6a1b62017-07-08 02:04:42 +0000419 case Decl::Friend:
Richard Trieu639d7b62017-02-22 22:22:42 +0000420 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000421 case Decl::TypeAlias:
422 case Decl::Typedef:
Richard Trieu6e13ff32017-06-16 02:44:29 +0000423 case Decl::Var:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000424 return true;
425 }
426}
427
428void ODRHash::AddSubDecl(const Decl *D) {
429 assert(D && "Expecting non-null pointer.");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000430
Richard Trieu639d7b62017-02-22 22:22:42 +0000431 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000432}
433
434void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
435 assert(Record && Record->hasDefinition() &&
436 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000437
Richard Trieu5fb82ef2017-08-05 00:54:19 +0000438 const DeclContext *DC = Record;
439 while (DC) {
440 if (isa<ClassTemplateSpecializationDecl>(DC)) {
441 return;
442 }
443 DC = DC->getParent();
Richard Trieu02552272017-05-02 23:58:52 +0000444 }
445
Richard Trieue7f7ed22017-02-22 01:11:25 +0000446 AddDecl(Record);
447
448 // Filter out sub-Decls which will not be processed in order to get an
449 // accurate count of Decl's.
450 llvm::SmallVector<const Decl *, 16> Decls;
451 for (const Decl *SubDecl : Record->decls()) {
452 if (isWhitelistedDecl(SubDecl, Record)) {
453 Decls.push_back(SubDecl);
454 }
455 }
456
457 ID.AddInteger(Decls.size());
458 for (auto SubDecl : Decls) {
459 AddSubDecl(SubDecl);
460 }
Richard Trieu498117b2017-08-23 02:43:59 +0000461
462 const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
463 AddBoolean(TD);
464 if (TD) {
465 AddTemplateParameterList(TD->getTemplateParameters());
466 }
Richard Trieue13eabe2017-09-30 02:19:17 +0000467
468 ID.AddInteger(Record->getNumBases());
469 auto Bases = Record->bases();
470 for (auto Base : Bases) {
471 AddQualType(Base.getType());
472 ID.AddInteger(Base.isVirtual());
473 ID.AddInteger(Base.getAccessSpecifierAsWritten());
474 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000475}
476
Richard Trieue6caa262017-12-23 00:41:01 +0000477void ODRHash::AddFunctionDecl(const FunctionDecl *Function) {
478 assert(Function && "Expecting non-null pointer.");
479
480 // Skip hashing these kinds of function.
481 if (Function->isImplicit()) return;
482 if (Function->isDefaulted()) return;
483 if (Function->isDeleted()) return;
484 if (!Function->hasBody()) return;
485 if (!Function->getBody()) return;
486
Richard Trieuf65efae2018-02-22 05:32:25 +0000487 // TODO: Fix hashing friend functions.
Richard Trieu4eefb452018-01-12 04:42:27 +0000488 if (Function->getFriendObjectKind()) return;
Richard Trieufb598562017-12-23 01:35:32 +0000489
Richard Trieue6caa262017-12-23 00:41:01 +0000490 // Skip functions that are specializations or in specialization context.
491 const DeclContext *DC = Function;
492 while (DC) {
493 if (isa<ClassTemplateSpecializationDecl>(DC)) return;
494 if (auto *F = dyn_cast<FunctionDecl>(DC))
495 if (F->isFunctionTemplateSpecialization()) return;
496 DC = DC->getParent();
497 }
498
499 AddDecl(Function);
500
501 AddQualType(Function->getReturnType());
502
503 ID.AddInteger(Function->param_size());
504 for (auto Param : Function->parameters())
505 AddSubDecl(Param);
506
507 AddStmt(Function->getBody());
508}
509
Richard Trieue7f7ed22017-02-22 01:11:25 +0000510void ODRHash::AddDecl(const Decl *D) {
511 assert(D && "Expecting non-null pointer.");
Richard Trieuf72fb7e2017-12-21 22:38:29 +0000512 D = D->getCanonicalDecl();
Richard Trieuf65efae2018-02-22 05:32:25 +0000513
514 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
515 AddDeclarationName(ND->getDeclName());
Richard Trieue7f7ed22017-02-22 01:11:25 +0000516 return;
517 }
518
519 ID.AddInteger(D->getKind());
Richard Trieuf65efae2018-02-22 05:32:25 +0000520 // TODO: Handle non-NamedDecl here.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000521}
522
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000523namespace {
Richard Trieubcaaf962017-02-23 03:25:57 +0000524// Process a Type pointer. Add* methods call back into ODRHash while Visit*
525// methods process the relevant parts of the Type.
526class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
527 typedef TypeVisitor<ODRTypeVisitor> Inherited;
528 llvm::FoldingSetNodeID &ID;
529 ODRHash &Hash;
530
531public:
532 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
533 : ID(ID), Hash(Hash) {}
534
535 void AddStmt(Stmt *S) {
536 Hash.AddBoolean(S);
537 if (S) {
538 Hash.AddStmt(S);
539 }
540 }
541
Richard Trieu8459ddf2017-02-24 02:59:12 +0000542 void AddDecl(Decl *D) {
543 Hash.AddBoolean(D);
544 if (D) {
545 Hash.AddDecl(D);
546 }
547 }
548
Richard Trieu02552272017-05-02 23:58:52 +0000549 void AddQualType(QualType T) {
550 Hash.AddQualType(T);
551 }
552
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000553 void AddType(const Type *T) {
554 Hash.AddBoolean(T);
555 if (T) {
556 Hash.AddType(T);
557 }
558 }
559
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000560 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieu96b41642017-07-01 02:00:05 +0000561 Hash.AddBoolean(NNS);
562 if (NNS) {
563 Hash.AddNestedNameSpecifier(NNS);
564 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000565 }
566
567 void AddIdentifierInfo(const IdentifierInfo *II) {
568 Hash.AddBoolean(II);
569 if (II) {
570 Hash.AddIdentifierInfo(II);
571 }
572 }
573
Richard Trieu02552272017-05-02 23:58:52 +0000574 void VisitQualifiers(Qualifiers Quals) {
575 ID.AddInteger(Quals.getAsOpaqueValue());
576 }
577
Richard Trieubcaaf962017-02-23 03:25:57 +0000578 void Visit(const Type *T) {
579 ID.AddInteger(T->getTypeClass());
580 Inherited::Visit(T);
581 }
582
583 void VisitType(const Type *T) {}
584
Richard Trieu02552272017-05-02 23:58:52 +0000585 void VisitAdjustedType(const AdjustedType *T) {
586 AddQualType(T->getOriginalType());
587 AddQualType(T->getAdjustedType());
588 VisitType(T);
589 }
590
591 void VisitDecayedType(const DecayedType *T) {
592 AddQualType(T->getDecayedType());
593 AddQualType(T->getPointeeType());
594 VisitAdjustedType(T);
595 }
596
597 void VisitArrayType(const ArrayType *T) {
598 AddQualType(T->getElementType());
599 ID.AddInteger(T->getSizeModifier());
600 VisitQualifiers(T->getIndexTypeQualifiers());
601 VisitType(T);
602 }
603 void VisitConstantArrayType(const ConstantArrayType *T) {
604 T->getSize().Profile(ID);
605 VisitArrayType(T);
606 }
607
608 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
609 AddStmt(T->getSizeExpr());
610 VisitArrayType(T);
611 }
612
613 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
614 VisitArrayType(T);
615 }
616
617 void VisitVariableArrayType(const VariableArrayType *T) {
618 AddStmt(T->getSizeExpr());
619 VisitArrayType(T);
620 }
621
Richard Trieubcaaf962017-02-23 03:25:57 +0000622 void VisitBuiltinType(const BuiltinType *T) {
623 ID.AddInteger(T->getKind());
624 VisitType(T);
625 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000626
Richard Trieu02552272017-05-02 23:58:52 +0000627 void VisitFunctionType(const FunctionType *T) {
628 AddQualType(T->getReturnType());
629 T->getExtInfo().Profile(ID);
630 Hash.AddBoolean(T->isConst());
631 Hash.AddBoolean(T->isVolatile());
632 Hash.AddBoolean(T->isRestrict());
633 VisitType(T);
634 }
635
636 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
637 VisitFunctionType(T);
638 }
639
640 void VisitFunctionProtoType(const FunctionProtoType *T) {
641 ID.AddInteger(T->getNumParams());
642 for (auto ParamType : T->getParamTypes())
643 AddQualType(ParamType);
644
645 VisitFunctionType(T);
646 }
647
Richard Trieu8459ddf2017-02-24 02:59:12 +0000648 void VisitTypedefType(const TypedefType *T) {
649 AddDecl(T->getDecl());
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000650 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
651 VisitQualifiers(UnderlyingType.getQualifiers());
652 while (const TypedefType *Underlying =
653 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
654 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
655 }
656 AddType(UnderlyingType.getTypePtr());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000657 VisitType(T);
658 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000659
660 void VisitTagType(const TagType *T) {
661 AddDecl(T->getDecl());
662 VisitType(T);
663 }
664
665 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
666 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
667
668 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
669 ID.AddInteger(T->getKeyword());
670 VisitType(T);
671 };
672
673 void VisitDependentNameType(const DependentNameType *T) {
674 AddNestedNameSpecifier(T->getQualifier());
675 AddIdentifierInfo(T->getIdentifier());
676 VisitTypeWithKeyword(T);
677 }
678
679 void VisitDependentTemplateSpecializationType(
680 const DependentTemplateSpecializationType *T) {
681 AddIdentifierInfo(T->getIdentifier());
682 AddNestedNameSpecifier(T->getQualifier());
683 ID.AddInteger(T->getNumArgs());
684 for (const auto &TA : T->template_arguments()) {
685 Hash.AddTemplateArgument(TA);
686 }
687 VisitTypeWithKeyword(T);
688 }
689
690 void VisitElaboratedType(const ElaboratedType *T) {
691 AddNestedNameSpecifier(T->getQualifier());
692 AddQualType(T->getNamedType());
693 VisitTypeWithKeyword(T);
694 }
Richard Trieu96b49622017-05-31 00:31:58 +0000695
696 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
697 ID.AddInteger(T->getNumArgs());
698 for (const auto &TA : T->template_arguments()) {
699 Hash.AddTemplateArgument(TA);
700 }
701 Hash.AddTemplateName(T->getTemplateName());
702 VisitType(T);
703 }
Richard Trieud9201d02017-06-15 01:35:06 +0000704
705 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
706 ID.AddInteger(T->getDepth());
707 ID.AddInteger(T->getIndex());
708 Hash.AddBoolean(T->isParameterPack());
709 AddDecl(T->getDecl());
710 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000711};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000712} // namespace
Richard Trieubcaaf962017-02-23 03:25:57 +0000713
714void ODRHash::AddType(const Type *T) {
715 assert(T && "Expecting non-null pointer.");
716 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
717 ID.AddInteger(Result.first->second);
718 // On first encounter of a Type pointer, process it. Every time afterwards,
719 // only the index value is needed.
720 if (!Result.second) {
721 return;
722 }
723
724 ODRTypeVisitor(ID, *this).Visit(T);
725}
726
727void ODRHash::AddQualType(QualType T) {
728 AddBoolean(T.isNull());
729 if (T.isNull())
730 return;
731 SplitQualType split = T.split();
732 ID.AddInteger(split.Quals.getAsOpaqueValue());
733 AddType(split.Ty);
734}
735
Richard Trieue7f7ed22017-02-22 01:11:25 +0000736void ODRHash::AddBoolean(bool Value) {
737 Bools.push_back(Value);
738}