blob: 4f4ea67d32f1f6333e1468ee35bd61fbdd8b4a6c [file] [log] [blame]
Richard Trieue7f7ed22017-02-22 01:11:25 +00001//===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Richard Trieue7f7ed22017-02-22 01:11:25 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the ODRHash class, which calculates a hash based
11/// on AST nodes, which is stable across different runs.
12///
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/ODRHash.h"
16
17#include "clang/AST/DeclVisitor.h"
18#include "clang/AST/NestedNameSpecifier.h"
19#include "clang/AST/StmtVisitor.h"
20#include "clang/AST/TypeVisitor.h"
21
22using namespace clang;
23
Richard Trieu639d7b62017-02-22 22:22:42 +000024void ODRHash::AddStmt(const Stmt *S) {
25 assert(S && "Expecting non-null pointer.");
26 S->ProcessODRHash(ID, *this);
27}
Richard Trieud0786092017-02-23 00:23:01 +000028
29void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
30 assert(II && "Expecting non-null pointer.");
31 ID.AddString(II->getName());
32}
33
Richard Trieu22ddc282018-09-04 22:53:19 +000034void ODRHash::AddDeclarationName(DeclarationName Name, bool TreatAsDecl) {
35 if (TreatAsDecl)
Richard Trieud8673902018-09-14 01:15:28 +000036 // Matches the NamedDecl check in AddDecl
Richard Trieu22ddc282018-09-04 22:53:19 +000037 AddBoolean(true);
38
Richard Trieud8673902018-09-14 01:15:28 +000039 AddDeclarationNameImpl(Name);
40
41 if (TreatAsDecl)
42 // Matches the ClassTemplateSpecializationDecl check in AddDecl
43 AddBoolean(false);
44}
45
46void ODRHash::AddDeclarationNameImpl(DeclarationName Name) {
Richard Trieuf65efae2018-02-22 05:32:25 +000047 // Index all DeclarationName and use index numbers to refer to them.
48 auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
49 ID.AddInteger(Result.first->second);
50 if (!Result.second) {
Raphael Isemannb23ccec2018-12-10 12:37:46 +000051 // If found in map, the DeclarationName has previously been processed.
Richard Trieuf65efae2018-02-22 05:32:25 +000052 return;
53 }
54
55 // First time processing each DeclarationName, also process its details.
Richard Trieu8459ddf2017-02-24 02:59:12 +000056 AddBoolean(Name.isEmpty());
57 if (Name.isEmpty())
58 return;
59
60 auto Kind = Name.getNameKind();
61 ID.AddInteger(Kind);
62 switch (Kind) {
63 case DeclarationName::Identifier:
64 AddIdentifierInfo(Name.getAsIdentifierInfo());
65 break;
66 case DeclarationName::ObjCZeroArgSelector:
67 case DeclarationName::ObjCOneArgSelector:
68 case DeclarationName::ObjCMultiArgSelector: {
69 Selector S = Name.getObjCSelector();
70 AddBoolean(S.isNull());
71 AddBoolean(S.isKeywordSelector());
72 AddBoolean(S.isUnarySelector());
73 unsigned NumArgs = S.getNumArgs();
74 for (unsigned i = 0; i < NumArgs; ++i) {
75 AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
76 }
77 break;
78 }
79 case DeclarationName::CXXConstructorName:
80 case DeclarationName::CXXDestructorName:
81 AddQualType(Name.getCXXNameType());
82 break;
83 case DeclarationName::CXXOperatorName:
84 ID.AddInteger(Name.getCXXOverloadedOperator());
85 break;
86 case DeclarationName::CXXLiteralOperatorName:
87 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
88 break;
89 case DeclarationName::CXXConversionFunctionName:
90 AddQualType(Name.getCXXNameType());
91 break;
92 case DeclarationName::CXXUsingDirective:
93 break;
94 case DeclarationName::CXXDeductionGuideName: {
95 auto *Template = Name.getCXXDeductionGuideTemplate();
96 AddBoolean(Template);
97 if (Template) {
98 AddDecl(Template);
99 }
100 }
101 }
102}
103
Richard Trieuce81b192017-05-17 03:23:35 +0000104void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieu96b41642017-07-01 02:00:05 +0000105 assert(NNS && "Expecting non-null pointer.");
106 const auto *Prefix = NNS->getPrefix();
107 AddBoolean(Prefix);
108 if (Prefix) {
109 AddNestedNameSpecifier(Prefix);
Richard Trieuce81b192017-05-17 03:23:35 +0000110 }
111 auto Kind = NNS->getKind();
112 ID.AddInteger(Kind);
113 switch (Kind) {
114 case NestedNameSpecifier::Identifier:
115 AddIdentifierInfo(NNS->getAsIdentifier());
116 break;
117 case NestedNameSpecifier::Namespace:
118 AddDecl(NNS->getAsNamespace());
119 break;
120 case NestedNameSpecifier::NamespaceAlias:
121 AddDecl(NNS->getAsNamespaceAlias());
122 break;
123 case NestedNameSpecifier::TypeSpec:
124 case NestedNameSpecifier::TypeSpecWithTemplate:
125 AddType(NNS->getAsType());
126 break;
127 case NestedNameSpecifier::Global:
128 case NestedNameSpecifier::Super:
129 break;
130 }
131}
132
Richard Trieu96b49622017-05-31 00:31:58 +0000133void ODRHash::AddTemplateName(TemplateName Name) {
134 auto Kind = Name.getKind();
135 ID.AddInteger(Kind);
136
137 switch (Kind) {
138 case TemplateName::Template:
139 AddDecl(Name.getAsTemplateDecl());
140 break;
141 // TODO: Support these cases.
142 case TemplateName::OverloadedTemplate:
143 case TemplateName::QualifiedTemplate:
144 case TemplateName::DependentTemplate:
145 case TemplateName::SubstTemplateTemplateParm:
146 case TemplateName::SubstTemplateTemplateParmPack:
147 break;
148 }
149}
150
Richard Trieu3b261bb72017-06-13 22:21:18 +0000151void ODRHash::AddTemplateArgument(TemplateArgument TA) {
152 const auto Kind = TA.getKind();
153 ID.AddInteger(Kind);
Richard Trieu1dcb4052017-06-14 01:28:00 +0000154
155 switch (Kind) {
156 case TemplateArgument::Null:
Richard Trieu8844c522017-06-30 22:40:33 +0000157 llvm_unreachable("Expected valid TemplateArgument");
Richard Trieu1dcb4052017-06-14 01:28:00 +0000158 case TemplateArgument::Type:
Richard Trieu8844c522017-06-30 22:40:33 +0000159 AddQualType(TA.getAsType());
160 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000161 case TemplateArgument::Declaration:
Richard Trieu7282d322018-04-25 00:31:15 +0000162 AddDecl(TA.getAsDecl());
163 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000164 case TemplateArgument::NullPtr:
165 case TemplateArgument::Integral:
Richard Trieuee132d62017-06-14 03:17:26 +0000166 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000167 case TemplateArgument::Template:
168 case TemplateArgument::TemplateExpansion:
Richard Trieuee132d62017-06-14 03:17:26 +0000169 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
Richard Trieu1dcb4052017-06-14 01:28:00 +0000170 break;
171 case TemplateArgument::Expression:
172 AddStmt(TA.getAsExpr());
173 break;
174 case TemplateArgument::Pack:
Richard Trieud9201d02017-06-15 01:35:06 +0000175 ID.AddInteger(TA.pack_size());
176 for (auto SubTA : TA.pack_elements()) {
177 AddTemplateArgument(SubTA);
178 }
Richard Trieu1dcb4052017-06-14 01:28:00 +0000179 break;
180 }
Richard Trieu3b261bb72017-06-13 22:21:18 +0000181}
182
Richard Trieu498117b2017-08-23 02:43:59 +0000183void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
184 assert(TPL && "Expecting non-null pointer.");
185
186 ID.AddInteger(TPL->size());
187 for (auto *ND : TPL->asArray()) {
188 AddSubDecl(ND);
189 }
190}
Richard Trieue7f7ed22017-02-22 01:11:25 +0000191
192void ODRHash::clear() {
Richard Trieuf65efae2018-02-22 05:32:25 +0000193 DeclNameMap.clear();
Richard Trieue7f7ed22017-02-22 01:11:25 +0000194 Bools.clear();
195 ID.clear();
196}
197
198unsigned ODRHash::CalculateHash() {
199 // Append the bools to the end of the data segment backwards. This allows
200 // for the bools data to be compressed 32 times smaller compared to using
201 // ID.AddBoolean
202 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
203 const unsigned size = Bools.size();
204 const unsigned remainder = size % unsigned_bits;
205 const unsigned loops = size / unsigned_bits;
206 auto I = Bools.rbegin();
207 unsigned value = 0;
208 for (unsigned i = 0; i < remainder; ++i) {
209 value <<= 1;
210 value |= *I;
211 ++I;
212 }
213 ID.AddInteger(value);
214
215 for (unsigned i = 0; i < loops; ++i) {
216 value = 0;
217 for (unsigned j = 0; j < unsigned_bits; ++j) {
218 value <<= 1;
219 value |= *I;
220 ++I;
221 }
222 ID.AddInteger(value);
223 }
224
225 assert(I == Bools.rend());
226 Bools.clear();
227 return ID.ComputeHash();
228}
229
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000230namespace {
Richard Trieue7f7ed22017-02-22 01:11:25 +0000231// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
232// methods process the relevant parts of the Decl.
233class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
234 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
235 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000236 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000237
238public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000239 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
240 : ID(ID), Hash(Hash) {}
241
242 void AddStmt(const Stmt *S) {
243 Hash.AddBoolean(S);
244 if (S) {
245 Hash.AddStmt(S);
246 }
247 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000248
Richard Trieud0786092017-02-23 00:23:01 +0000249 void AddIdentifierInfo(const IdentifierInfo *II) {
250 Hash.AddBoolean(II);
251 if (II) {
252 Hash.AddIdentifierInfo(II);
253 }
254 }
255
Richard Trieubcaaf962017-02-23 03:25:57 +0000256 void AddQualType(QualType T) {
257 Hash.AddQualType(T);
258 }
259
Richard Trieuac6a1b62017-07-08 02:04:42 +0000260 void AddDecl(const Decl *D) {
261 Hash.AddBoolean(D);
262 if (D) {
263 Hash.AddDecl(D);
264 }
265 }
266
Richard Trieu498117b2017-08-23 02:43:59 +0000267 void AddTemplateArgument(TemplateArgument TA) {
268 Hash.AddTemplateArgument(TA);
269 }
270
Richard Trieue7f7ed22017-02-22 01:11:25 +0000271 void Visit(const Decl *D) {
272 ID.AddInteger(D->getKind());
273 Inherited::Visit(D);
274 }
275
Richard Trieud0786092017-02-23 00:23:01 +0000276 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000277 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000278 Inherited::VisitNamedDecl(D);
279 }
280
Richard Trieubcaaf962017-02-23 03:25:57 +0000281 void VisitValueDecl(const ValueDecl *D) {
Richard Trieu9747a7c2017-07-14 01:36:41 +0000282 if (!isa<FunctionDecl>(D)) {
283 AddQualType(D->getType());
284 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000285 Inherited::VisitValueDecl(D);
286 }
287
Richard Trieu6e13ff32017-06-16 02:44:29 +0000288 void VisitVarDecl(const VarDecl *D) {
289 Hash.AddBoolean(D->isStaticLocal());
290 Hash.AddBoolean(D->isConstexpr());
291 const bool HasInit = D->hasInit();
292 Hash.AddBoolean(HasInit);
293 if (HasInit) {
294 AddStmt(D->getInit());
295 }
296 Inherited::VisitVarDecl(D);
297 }
298
Richard Trieu02552272017-05-02 23:58:52 +0000299 void VisitParmVarDecl(const ParmVarDecl *D) {
300 // TODO: Handle default arguments.
301 Inherited::VisitParmVarDecl(D);
302 }
303
Richard Trieue7f7ed22017-02-22 01:11:25 +0000304 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
305 ID.AddInteger(D->getAccess());
306 Inherited::VisitAccessSpecDecl(D);
307 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000308
309 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
310 AddStmt(D->getAssertExpr());
311 AddStmt(D->getMessage());
312
313 Inherited::VisitStaticAssertDecl(D);
314 }
Richard Trieud0786092017-02-23 00:23:01 +0000315
316 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000317 const bool IsBitfield = D->isBitField();
318 Hash.AddBoolean(IsBitfield);
319
320 if (IsBitfield) {
321 AddStmt(D->getBitWidth());
322 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000323
324 Hash.AddBoolean(D->isMutable());
325 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000326
327 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000328 }
Richard Trieu48143742017-02-28 21:24:38 +0000329
330 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000331 // Handled by the ODRHash for FunctionDecl
332 ID.AddInteger(D->getODRHash());
Richard Trieu7282d322018-04-25 00:31:15 +0000333
Richard Trieu48143742017-02-28 21:24:38 +0000334 Inherited::VisitFunctionDecl(D);
335 }
336
337 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000338 // Handled by the ODRHash for FunctionDecl
Richard Trieu583e2c12017-03-04 00:08:58 +0000339
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 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000375 Hash.AddBoolean(D->isParameterPack());
Richard Trieu498117b2017-08-23 02:43:59 +0000376
377 Inherited::VisitTemplateTypeParmDecl(D);
378 }
379
380 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
381 // Only care about default arguments as part of the definition.
382 const bool hasDefaultArgument =
383 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
384 Hash.AddBoolean(hasDefaultArgument);
385 if (hasDefaultArgument) {
386 AddStmt(D->getDefaultArgument());
387 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000388 Hash.AddBoolean(D->isParameterPack());
Richard Trieu498117b2017-08-23 02:43:59 +0000389
390 Inherited::VisitNonTypeTemplateParmDecl(D);
391 }
392
393 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
394 // Only care about default arguments as part of the definition.
395 const bool hasDefaultArgument =
396 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
397 Hash.AddBoolean(hasDefaultArgument);
398 if (hasDefaultArgument) {
399 AddTemplateArgument(D->getDefaultArgument().getArgument());
400 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000401 Hash.AddBoolean(D->isParameterPack());
Richard Trieu498117b2017-08-23 02:43:59 +0000402
403 Inherited::VisitTemplateTemplateParmDecl(D);
404 }
Richard Trieu9359e8f2018-05-30 01:12:26 +0000405
406 void VisitTemplateDecl(const TemplateDecl *D) {
407 Hash.AddTemplateParameterList(D->getTemplateParameters());
408
409 Inherited::VisitTemplateDecl(D);
410 }
411
412 void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
413 Hash.AddBoolean(D->isMemberSpecialization());
414 Inherited::VisitRedeclarableTemplateDecl(D);
415 }
416
417 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
Richard Trieu5d014062018-06-07 00:20:58 +0000418 AddDecl(D->getTemplatedDecl());
Richard Trieu22ddc282018-09-04 22:53:19 +0000419 ID.AddInteger(D->getTemplatedDecl()->getODRHash());
Richard Trieu9359e8f2018-05-30 01:12:26 +0000420 Inherited::VisitFunctionTemplateDecl(D);
421 }
Richard Trieuab4d7302018-07-25 22:52:05 +0000422
423 void VisitEnumConstantDecl(const EnumConstantDecl *D) {
424 AddStmt(D->getInitExpr());
425 Inherited::VisitEnumConstantDecl(D);
426 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000427};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000428} // namespace
Richard Trieue7f7ed22017-02-22 01:11:25 +0000429
430// Only allow a small portion of Decl's to be processed. Remove this once
431// all Decl's can be handled.
Richard Trieuab4d7302018-07-25 22:52:05 +0000432bool ODRHash::isWhitelistedDecl(const Decl *D, const DeclContext *Parent) {
Richard Trieue7f7ed22017-02-22 01:11:25 +0000433 if (D->isImplicit()) return false;
434 if (D->getDeclContext() != Parent) return false;
435
436 switch (D->getKind()) {
437 default:
438 return false;
439 case Decl::AccessSpec:
Richard Trieu1c71d512017-07-15 02:55:13 +0000440 case Decl::CXXConstructor:
441 case Decl::CXXDestructor:
Richard Trieu48143742017-02-28 21:24:38 +0000442 case Decl::CXXMethod:
Richard Trieuab4d7302018-07-25 22:52:05 +0000443 case Decl::EnumConstant: // Only found in EnumDecl's.
Richard Trieud0786092017-02-23 00:23:01 +0000444 case Decl::Field:
Richard Trieuac6a1b62017-07-08 02:04:42 +0000445 case Decl::Friend:
Richard Trieu9359e8f2018-05-30 01:12:26 +0000446 case Decl::FunctionTemplate:
Richard Trieu639d7b62017-02-22 22:22:42 +0000447 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000448 case Decl::TypeAlias:
449 case Decl::Typedef:
Richard Trieu6e13ff32017-06-16 02:44:29 +0000450 case Decl::Var:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000451 return true;
452 }
453}
454
455void ODRHash::AddSubDecl(const Decl *D) {
456 assert(D && "Expecting non-null pointer.");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000457
Richard Trieu639d7b62017-02-22 22:22:42 +0000458 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000459}
460
461void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
462 assert(Record && Record->hasDefinition() &&
463 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000464
Richard Trieu5fb82ef2017-08-05 00:54:19 +0000465 const DeclContext *DC = Record;
466 while (DC) {
467 if (isa<ClassTemplateSpecializationDecl>(DC)) {
468 return;
469 }
470 DC = DC->getParent();
Richard Trieu02552272017-05-02 23:58:52 +0000471 }
472
Richard Trieue7f7ed22017-02-22 01:11:25 +0000473 AddDecl(Record);
474
475 // Filter out sub-Decls which will not be processed in order to get an
476 // accurate count of Decl's.
477 llvm::SmallVector<const Decl *, 16> Decls;
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000478 for (Decl *SubDecl : Record->decls()) {
Richard Trieue7f7ed22017-02-22 01:11:25 +0000479 if (isWhitelistedDecl(SubDecl, Record)) {
480 Decls.push_back(SubDecl);
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000481 if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
482 // Compute/Preload ODRHash into FunctionDecl.
483 Function->getODRHash();
484 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000485 }
486 }
487
488 ID.AddInteger(Decls.size());
489 for (auto SubDecl : Decls) {
490 AddSubDecl(SubDecl);
491 }
Richard Trieu498117b2017-08-23 02:43:59 +0000492
493 const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
494 AddBoolean(TD);
495 if (TD) {
496 AddTemplateParameterList(TD->getTemplateParameters());
497 }
Richard Trieue13eabe2017-09-30 02:19:17 +0000498
499 ID.AddInteger(Record->getNumBases());
500 auto Bases = Record->bases();
501 for (auto Base : Bases) {
502 AddQualType(Base.getType());
503 ID.AddInteger(Base.isVirtual());
504 ID.AddInteger(Base.getAccessSpecifierAsWritten());
505 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000506}
507
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000508void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
509 bool SkipBody) {
Richard Trieue6caa262017-12-23 00:41:01 +0000510 assert(Function && "Expecting non-null pointer.");
511
Richard Trieue6caa262017-12-23 00:41:01 +0000512 // Skip functions that are specializations or in specialization context.
513 const DeclContext *DC = Function;
514 while (DC) {
515 if (isa<ClassTemplateSpecializationDecl>(DC)) return;
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000516 if (auto *F = dyn_cast<FunctionDecl>(DC)) {
517 if (F->isFunctionTemplateSpecialization()) {
518 if (!isa<CXXMethodDecl>(DC)) return;
519 if (DC->getLexicalParent()->isFileContext()) return;
520 // Inline method specializations are the only supported
521 // specialization for now.
522 }
523 }
Richard Trieue6caa262017-12-23 00:41:01 +0000524 DC = DC->getParent();
525 }
526
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000527 ID.AddInteger(Function->getDeclKind());
528
529 const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
530 AddBoolean(SpecializationArgs);
531 if (SpecializationArgs) {
532 ID.AddInteger(SpecializationArgs->size());
533 for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
534 AddTemplateArgument(TA);
535 }
536 }
537
538 if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
539 AddBoolean(Method->isConst());
540 AddBoolean(Method->isVolatile());
541 }
542
543 ID.AddInteger(Function->getStorageClass());
544 AddBoolean(Function->isInlineSpecified());
545 AddBoolean(Function->isVirtualAsWritten());
546 AddBoolean(Function->isPure());
547 AddBoolean(Function->isDeletedAsWritten());
548 AddBoolean(Function->isExplicitlyDefaulted());
549
Richard Trieue6caa262017-12-23 00:41:01 +0000550 AddDecl(Function);
551
552 AddQualType(Function->getReturnType());
553
554 ID.AddInteger(Function->param_size());
555 for (auto Param : Function->parameters())
556 AddSubDecl(Param);
557
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000558 if (SkipBody) {
559 AddBoolean(false);
560 return;
561 }
562
563 const bool HasBody = Function->isThisDeclarationADefinition() &&
564 !Function->isDefaulted() && !Function->isDeleted() &&
565 !Function->isLateTemplateParsed();
566 AddBoolean(HasBody);
Richard Trieu22ddc282018-09-04 22:53:19 +0000567 if (!HasBody) {
568 return;
569 }
570
571 auto *Body = Function->getBody();
572 AddBoolean(Body);
573 if (Body)
574 AddStmt(Body);
575
576 // Filter out sub-Decls which will not be processed in order to get an
577 // accurate count of Decl's.
578 llvm::SmallVector<const Decl *, 16> Decls;
579 for (Decl *SubDecl : Function->decls()) {
580 if (isWhitelistedDecl(SubDecl, Function)) {
581 Decls.push_back(SubDecl);
582 }
583 }
584
585 ID.AddInteger(Decls.size());
586 for (auto SubDecl : Decls) {
587 AddSubDecl(SubDecl);
Richard Trieu27c1b1a2018-07-10 01:40:50 +0000588 }
Richard Trieue6caa262017-12-23 00:41:01 +0000589}
590
Richard Trieuab4d7302018-07-25 22:52:05 +0000591void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
592 assert(Enum);
593 AddDeclarationName(Enum->getDeclName());
594
595 AddBoolean(Enum->isScoped());
596 if (Enum->isScoped())
597 AddBoolean(Enum->isScopedUsingClassTag());
598
599 if (Enum->getIntegerTypeSourceInfo())
600 AddQualType(Enum->getIntegerType());
601
602 // Filter out sub-Decls which will not be processed in order to get an
603 // accurate count of Decl's.
604 llvm::SmallVector<const Decl *, 16> Decls;
605 for (Decl *SubDecl : Enum->decls()) {
606 if (isWhitelistedDecl(SubDecl, Enum)) {
607 assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
608 Decls.push_back(SubDecl);
609 }
610 }
611
612 ID.AddInteger(Decls.size());
613 for (auto SubDecl : Decls) {
614 AddSubDecl(SubDecl);
615 }
616
617}
618
Richard Trieue7f7ed22017-02-22 01:11:25 +0000619void ODRHash::AddDecl(const Decl *D) {
620 assert(D && "Expecting non-null pointer.");
Richard Trieuf72fb7e2017-12-21 22:38:29 +0000621 D = D->getCanonicalDecl();
Richard Trieuf65efae2018-02-22 05:32:25 +0000622
Richard Trieu22ddc282018-09-04 22:53:19 +0000623 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
624 AddBoolean(ND);
625 if (!ND) {
626 ID.AddInteger(D->getKind());
Richard Trieue7f7ed22017-02-22 01:11:25 +0000627 return;
628 }
629
Richard Trieu22ddc282018-09-04 22:53:19 +0000630 AddDeclarationName(ND->getDeclName());
631
632 const auto *Specialization =
633 dyn_cast<ClassTemplateSpecializationDecl>(D);
634 AddBoolean(Specialization);
635 if (Specialization) {
636 const TemplateArgumentList &List = Specialization->getTemplateArgs();
637 ID.AddInteger(List.size());
638 for (const TemplateArgument &TA : List.asArray())
639 AddTemplateArgument(TA);
640 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000641}
642
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +0000643namespace {
Richard Trieubcaaf962017-02-23 03:25:57 +0000644// Process a Type pointer. Add* methods call back into ODRHash while Visit*
645// methods process the relevant parts of the Type.
646class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
647 typedef TypeVisitor<ODRTypeVisitor> Inherited;
648 llvm::FoldingSetNodeID &ID;
649 ODRHash &Hash;
650
651public:
652 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
653 : ID(ID), Hash(Hash) {}
654
655 void AddStmt(Stmt *S) {
656 Hash.AddBoolean(S);
657 if (S) {
658 Hash.AddStmt(S);
659 }
660 }
661
Richard Trieu8459ddf2017-02-24 02:59:12 +0000662 void AddDecl(Decl *D) {
663 Hash.AddBoolean(D);
664 if (D) {
665 Hash.AddDecl(D);
666 }
667 }
668
Richard Trieu02552272017-05-02 23:58:52 +0000669 void AddQualType(QualType T) {
670 Hash.AddQualType(T);
671 }
672
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000673 void AddType(const Type *T) {
674 Hash.AddBoolean(T);
675 if (T) {
676 Hash.AddType(T);
677 }
678 }
679
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000680 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieu96b41642017-07-01 02:00:05 +0000681 Hash.AddBoolean(NNS);
682 if (NNS) {
683 Hash.AddNestedNameSpecifier(NNS);
684 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000685 }
686
687 void AddIdentifierInfo(const IdentifierInfo *II) {
688 Hash.AddBoolean(II);
689 if (II) {
690 Hash.AddIdentifierInfo(II);
691 }
692 }
693
Richard Trieu02552272017-05-02 23:58:52 +0000694 void VisitQualifiers(Qualifiers Quals) {
695 ID.AddInteger(Quals.getAsOpaqueValue());
696 }
697
Richard Trieubcaaf962017-02-23 03:25:57 +0000698 void Visit(const Type *T) {
699 ID.AddInteger(T->getTypeClass());
700 Inherited::Visit(T);
701 }
702
703 void VisitType(const Type *T) {}
704
Richard Trieu02552272017-05-02 23:58:52 +0000705 void VisitAdjustedType(const AdjustedType *T) {
706 AddQualType(T->getOriginalType());
707 AddQualType(T->getAdjustedType());
708 VisitType(T);
709 }
710
711 void VisitDecayedType(const DecayedType *T) {
712 AddQualType(T->getDecayedType());
713 AddQualType(T->getPointeeType());
714 VisitAdjustedType(T);
715 }
716
717 void VisitArrayType(const ArrayType *T) {
718 AddQualType(T->getElementType());
719 ID.AddInteger(T->getSizeModifier());
720 VisitQualifiers(T->getIndexTypeQualifiers());
721 VisitType(T);
722 }
723 void VisitConstantArrayType(const ConstantArrayType *T) {
724 T->getSize().Profile(ID);
725 VisitArrayType(T);
726 }
727
728 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
729 AddStmt(T->getSizeExpr());
730 VisitArrayType(T);
731 }
732
733 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
734 VisitArrayType(T);
735 }
736
737 void VisitVariableArrayType(const VariableArrayType *T) {
738 AddStmt(T->getSizeExpr());
739 VisitArrayType(T);
740 }
741
Richard Trieu22ddc282018-09-04 22:53:19 +0000742 void VisitAttributedType(const AttributedType *T) {
743 ID.AddInteger(T->getAttrKind());
744 AddQualType(T->getModifiedType());
745 AddQualType(T->getEquivalentType());
746
747 VisitType(T);
748 }
749
750 void VisitBlockPointerType(const BlockPointerType *T) {
751 AddQualType(T->getPointeeType());
752 VisitType(T);
753 }
754
Richard Trieubcaaf962017-02-23 03:25:57 +0000755 void VisitBuiltinType(const BuiltinType *T) {
756 ID.AddInteger(T->getKind());
757 VisitType(T);
758 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000759
Richard Trieu22ddc282018-09-04 22:53:19 +0000760 void VisitComplexType(const ComplexType *T) {
761 AddQualType(T->getElementType());
762 VisitType(T);
763 }
764
765 void VisitDecltypeType(const DecltypeType *T) {
766 AddStmt(T->getUnderlyingExpr());
767 AddQualType(T->getUnderlyingType());
768 VisitType(T);
769 }
770
771 void VisitDependentDecltypeType(const DependentDecltypeType *T) {
772 VisitDecltypeType(T);
773 }
774
775 void VisitDeducedType(const DeducedType *T) {
776 AddQualType(T->getDeducedType());
777 VisitType(T);
778 }
779
780 void VisitAutoType(const AutoType *T) {
781 ID.AddInteger((unsigned)T->getKeyword());
782 VisitDeducedType(T);
783 }
784
785 void VisitDeducedTemplateSpecializationType(
786 const DeducedTemplateSpecializationType *T) {
787 Hash.AddTemplateName(T->getTemplateName());
788 VisitDeducedType(T);
789 }
790
791 void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) {
792 AddQualType(T->getPointeeType());
793 AddStmt(T->getAddrSpaceExpr());
794 VisitType(T);
795 }
796
797 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
798 AddQualType(T->getElementType());
799 AddStmt(T->getSizeExpr());
800 VisitType(T);
801 }
802
Richard Trieu02552272017-05-02 23:58:52 +0000803 void VisitFunctionType(const FunctionType *T) {
804 AddQualType(T->getReturnType());
805 T->getExtInfo().Profile(ID);
806 Hash.AddBoolean(T->isConst());
807 Hash.AddBoolean(T->isVolatile());
808 Hash.AddBoolean(T->isRestrict());
809 VisitType(T);
810 }
811
812 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
813 VisitFunctionType(T);
814 }
815
816 void VisitFunctionProtoType(const FunctionProtoType *T) {
817 ID.AddInteger(T->getNumParams());
818 for (auto ParamType : T->getParamTypes())
819 AddQualType(ParamType);
820
821 VisitFunctionType(T);
822 }
823
Richard Trieu22ddc282018-09-04 22:53:19 +0000824 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
825 AddDecl(T->getDecl());
826 VisitType(T);
827 }
828
829 void VisitMemberPointerType(const MemberPointerType *T) {
830 AddQualType(T->getPointeeType());
831 AddType(T->getClass());
832 VisitType(T);
833 }
834
835 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
836 AddQualType(T->getPointeeType());
837 VisitType(T);
838 }
839
840 void VisitObjCObjectType(const ObjCObjectType *T) {
841 AddDecl(T->getInterface());
842
843 auto TypeArgs = T->getTypeArgsAsWritten();
844 ID.AddInteger(TypeArgs.size());
845 for (auto Arg : TypeArgs) {
846 AddQualType(Arg);
847 }
848
849 auto Protocols = T->getProtocols();
850 ID.AddInteger(Protocols.size());
851 for (auto Protocol : Protocols) {
852 AddDecl(Protocol);
853 }
854
855 Hash.AddBoolean(T->isKindOfType());
856
857 VisitType(T);
858 }
859
860 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
861 // This type is handled by the parent type ObjCObjectType.
862 VisitObjCObjectType(T);
863 }
864
865 void VisitObjCTypeParamType(const ObjCTypeParamType *T) {
866 AddDecl(T->getDecl());
867 auto Protocols = T->getProtocols();
868 ID.AddInteger(Protocols.size());
869 for (auto Protocol : Protocols) {
870 AddDecl(Protocol);
871 }
872
873 VisitType(T);
874 }
875
876 void VisitPackExpansionType(const PackExpansionType *T) {
877 AddQualType(T->getPattern());
878 VisitType(T);
879 }
880
881 void VisitParenType(const ParenType *T) {
882 AddQualType(T->getInnerType());
883 VisitType(T);
884 }
885
886 void VisitPipeType(const PipeType *T) {
887 AddQualType(T->getElementType());
888 Hash.AddBoolean(T->isReadOnly());
889 VisitType(T);
890 }
891
Richard Trieu15970af2018-04-13 22:34:43 +0000892 void VisitPointerType(const PointerType *T) {
893 AddQualType(T->getPointeeType());
894 VisitType(T);
895 }
896
897 void VisitReferenceType(const ReferenceType *T) {
898 AddQualType(T->getPointeeTypeAsWritten());
899 VisitType(T);
900 }
901
902 void VisitLValueReferenceType(const LValueReferenceType *T) {
903 VisitReferenceType(T);
904 }
905
906 void VisitRValueReferenceType(const RValueReferenceType *T) {
907 VisitReferenceType(T);
908 }
909
Richard Trieu22ddc282018-09-04 22:53:19 +0000910 void
911 VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
912 AddType(T->getReplacedParameter());
913 Hash.AddTemplateArgument(T->getArgumentPack());
914 VisitType(T);
915 }
916
917 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
918 AddType(T->getReplacedParameter());
919 AddQualType(T->getReplacementType());
920 VisitType(T);
921 }
922
923 void VisitTagType(const TagType *T) {
924 AddDecl(T->getDecl());
925 VisitType(T);
926 }
927
928 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
929 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
930
931 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
932 ID.AddInteger(T->getNumArgs());
933 for (const auto &TA : T->template_arguments()) {
934 Hash.AddTemplateArgument(TA);
935 }
936 Hash.AddTemplateName(T->getTemplateName());
937 VisitType(T);
938 }
939
940 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
941 ID.AddInteger(T->getDepth());
942 ID.AddInteger(T->getIndex());
943 Hash.AddBoolean(T->isParameterPack());
944 AddDecl(T->getDecl());
945 }
946
Richard Trieu8459ddf2017-02-24 02:59:12 +0000947 void VisitTypedefType(const TypedefType *T) {
948 AddDecl(T->getDecl());
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000949 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
950 VisitQualifiers(UnderlyingType.getQualifiers());
Richard Trieucaaccee2018-04-12 02:26:49 +0000951 while (true) {
952 if (const TypedefType *Underlying =
953 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
954 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
955 continue;
956 }
957 if (const ElaboratedType *Underlying =
958 dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
959 UnderlyingType = Underlying->getNamedType();
960 continue;
961 }
962
963 break;
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000964 }
965 AddType(UnderlyingType.getTypePtr());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000966 VisitType(T);
967 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000968
Richard Trieu22ddc282018-09-04 22:53:19 +0000969 void VisitTypeOfExprType(const TypeOfExprType *T) {
970 AddStmt(T->getUnderlyingExpr());
971 Hash.AddBoolean(T->isSugared());
972 if (T->isSugared())
973 AddQualType(T->desugar());
974
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000975 VisitType(T);
976 }
Richard Trieu22ddc282018-09-04 22:53:19 +0000977 void VisitTypeOfType(const TypeOfType *T) {
978 AddQualType(T->getUnderlyingType());
979 VisitType(T);
980 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000981
982 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
983 ID.AddInteger(T->getKeyword());
984 VisitType(T);
985 };
986
987 void VisitDependentNameType(const DependentNameType *T) {
988 AddNestedNameSpecifier(T->getQualifier());
989 AddIdentifierInfo(T->getIdentifier());
990 VisitTypeWithKeyword(T);
991 }
992
993 void VisitDependentTemplateSpecializationType(
994 const DependentTemplateSpecializationType *T) {
995 AddIdentifierInfo(T->getIdentifier());
996 AddNestedNameSpecifier(T->getQualifier());
997 ID.AddInteger(T->getNumArgs());
998 for (const auto &TA : T->template_arguments()) {
999 Hash.AddTemplateArgument(TA);
1000 }
1001 VisitTypeWithKeyword(T);
1002 }
1003
1004 void VisitElaboratedType(const ElaboratedType *T) {
1005 AddNestedNameSpecifier(T->getQualifier());
1006 AddQualType(T->getNamedType());
1007 VisitTypeWithKeyword(T);
1008 }
Richard Trieu96b49622017-05-31 00:31:58 +00001009
Richard Trieu22ddc282018-09-04 22:53:19 +00001010 void VisitUnaryTransformType(const UnaryTransformType *T) {
1011 AddQualType(T->getUnderlyingType());
1012 AddQualType(T->getBaseType());
Richard Trieu96b49622017-05-31 00:31:58 +00001013 VisitType(T);
1014 }
Richard Trieud9201d02017-06-15 01:35:06 +00001015
Richard Trieu22ddc282018-09-04 22:53:19 +00001016 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Trieud9201d02017-06-15 01:35:06 +00001017 AddDecl(T->getDecl());
Richard Trieu22ddc282018-09-04 22:53:19 +00001018 VisitType(T);
1019 }
1020
1021 void VisitVectorType(const VectorType *T) {
1022 AddQualType(T->getElementType());
1023 ID.AddInteger(T->getNumElements());
1024 ID.AddInteger(T->getVectorKind());
1025 VisitType(T);
1026 }
1027
1028 void VisitExtVectorType(const ExtVectorType * T) {
1029 VisitVectorType(T);
Richard Trieud9201d02017-06-15 01:35:06 +00001030 }
Richard Trieubcaaf962017-02-23 03:25:57 +00001031};
Benjamin Kramerbffdf4c2017-08-20 13:02:57 +00001032} // namespace
Richard Trieubcaaf962017-02-23 03:25:57 +00001033
1034void ODRHash::AddType(const Type *T) {
1035 assert(T && "Expecting non-null pointer.");
Richard Trieubcaaf962017-02-23 03:25:57 +00001036 ODRTypeVisitor(ID, *this).Visit(T);
1037}
1038
1039void ODRHash::AddQualType(QualType T) {
1040 AddBoolean(T.isNull());
1041 if (T.isNull())
1042 return;
1043 SplitQualType split = T.split();
1044 ID.AddInteger(split.Quals.getAsOpaqueValue());
1045 AddType(split.Ty);
1046}
1047
Richard Trieue7f7ed22017-02-22 01:11:25 +00001048void ODRHash::AddBoolean(bool Value) {
1049 Bools.push_back(Value);
1050}