/src/llvm-project/clang/lib/AST/ODRHash.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===// |
2 | | // |
3 | | // 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 |
6 | | // |
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 | | |
22 | | using namespace clang; |
23 | | |
24 | 0 | void ODRHash::AddStmt(const Stmt *S) { |
25 | 0 | assert(S && "Expecting non-null pointer."); |
26 | 0 | S->ProcessODRHash(ID, *this); |
27 | 0 | } |
28 | | |
29 | 0 | void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) { |
30 | 0 | assert(II && "Expecting non-null pointer."); |
31 | 0 | ID.AddString(II->getName()); |
32 | 0 | } |
33 | | |
34 | 0 | void ODRHash::AddDeclarationName(DeclarationName Name, bool TreatAsDecl) { |
35 | 0 | if (TreatAsDecl) |
36 | | // Matches the NamedDecl check in AddDecl |
37 | 0 | AddBoolean(true); |
38 | |
|
39 | 0 | AddDeclarationNameImpl(Name); |
40 | |
|
41 | 0 | if (TreatAsDecl) |
42 | | // Matches the ClassTemplateSpecializationDecl check in AddDecl |
43 | 0 | AddBoolean(false); |
44 | 0 | } |
45 | | |
46 | 0 | void ODRHash::AddDeclarationNameImpl(DeclarationName Name) { |
47 | | // Index all DeclarationName and use index numbers to refer to them. |
48 | 0 | auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size())); |
49 | 0 | ID.AddInteger(Result.first->second); |
50 | 0 | if (!Result.second) { |
51 | | // If found in map, the DeclarationName has previously been processed. |
52 | 0 | return; |
53 | 0 | } |
54 | | |
55 | | // First time processing each DeclarationName, also process its details. |
56 | 0 | AddBoolean(Name.isEmpty()); |
57 | 0 | if (Name.isEmpty()) |
58 | 0 | return; |
59 | | |
60 | 0 | auto Kind = Name.getNameKind(); |
61 | 0 | ID.AddInteger(Kind); |
62 | 0 | switch (Kind) { |
63 | 0 | case DeclarationName::Identifier: |
64 | 0 | AddIdentifierInfo(Name.getAsIdentifierInfo()); |
65 | 0 | break; |
66 | 0 | case DeclarationName::ObjCZeroArgSelector: |
67 | 0 | case DeclarationName::ObjCOneArgSelector: |
68 | 0 | case DeclarationName::ObjCMultiArgSelector: { |
69 | 0 | Selector S = Name.getObjCSelector(); |
70 | 0 | AddBoolean(S.isNull()); |
71 | 0 | AddBoolean(S.isKeywordSelector()); |
72 | 0 | AddBoolean(S.isUnarySelector()); |
73 | 0 | unsigned NumArgs = S.getNumArgs(); |
74 | 0 | ID.AddInteger(NumArgs); |
75 | | // Compare all selector slots. For selectors with arguments it means all arg |
76 | | // slots. And if there are no arguments, compare the first-and-only slot. |
77 | 0 | unsigned SlotsToCheck = NumArgs > 0 ? NumArgs : 1; |
78 | 0 | for (unsigned i = 0; i < SlotsToCheck; ++i) { |
79 | 0 | const IdentifierInfo *II = S.getIdentifierInfoForSlot(i); |
80 | 0 | AddBoolean(II); |
81 | 0 | if (II) { |
82 | 0 | AddIdentifierInfo(II); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | break; |
86 | 0 | } |
87 | 0 | case DeclarationName::CXXConstructorName: |
88 | 0 | case DeclarationName::CXXDestructorName: |
89 | 0 | AddQualType(Name.getCXXNameType()); |
90 | 0 | break; |
91 | 0 | case DeclarationName::CXXOperatorName: |
92 | 0 | ID.AddInteger(Name.getCXXOverloadedOperator()); |
93 | 0 | break; |
94 | 0 | case DeclarationName::CXXLiteralOperatorName: |
95 | 0 | AddIdentifierInfo(Name.getCXXLiteralIdentifier()); |
96 | 0 | break; |
97 | 0 | case DeclarationName::CXXConversionFunctionName: |
98 | 0 | AddQualType(Name.getCXXNameType()); |
99 | 0 | break; |
100 | 0 | case DeclarationName::CXXUsingDirective: |
101 | 0 | break; |
102 | 0 | case DeclarationName::CXXDeductionGuideName: { |
103 | 0 | auto *Template = Name.getCXXDeductionGuideTemplate(); |
104 | 0 | AddBoolean(Template); |
105 | 0 | if (Template) { |
106 | 0 | AddDecl(Template); |
107 | 0 | } |
108 | 0 | } |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | 0 | void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) { |
113 | 0 | assert(NNS && "Expecting non-null pointer."); |
114 | 0 | const auto *Prefix = NNS->getPrefix(); |
115 | 0 | AddBoolean(Prefix); |
116 | 0 | if (Prefix) { |
117 | 0 | AddNestedNameSpecifier(Prefix); |
118 | 0 | } |
119 | 0 | auto Kind = NNS->getKind(); |
120 | 0 | ID.AddInteger(Kind); |
121 | 0 | switch (Kind) { |
122 | 0 | case NestedNameSpecifier::Identifier: |
123 | 0 | AddIdentifierInfo(NNS->getAsIdentifier()); |
124 | 0 | break; |
125 | 0 | case NestedNameSpecifier::Namespace: |
126 | 0 | AddDecl(NNS->getAsNamespace()); |
127 | 0 | break; |
128 | 0 | case NestedNameSpecifier::NamespaceAlias: |
129 | 0 | AddDecl(NNS->getAsNamespaceAlias()); |
130 | 0 | break; |
131 | 0 | case NestedNameSpecifier::TypeSpec: |
132 | 0 | case NestedNameSpecifier::TypeSpecWithTemplate: |
133 | 0 | AddType(NNS->getAsType()); |
134 | 0 | break; |
135 | 0 | case NestedNameSpecifier::Global: |
136 | 0 | case NestedNameSpecifier::Super: |
137 | 0 | break; |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | 0 | void ODRHash::AddTemplateName(TemplateName Name) { |
142 | 0 | auto Kind = Name.getKind(); |
143 | 0 | ID.AddInteger(Kind); |
144 | |
|
145 | 0 | switch (Kind) { |
146 | 0 | case TemplateName::Template: |
147 | 0 | AddDecl(Name.getAsTemplateDecl()); |
148 | 0 | break; |
149 | | // TODO: Support these cases. |
150 | 0 | case TemplateName::OverloadedTemplate: |
151 | 0 | case TemplateName::AssumedTemplate: |
152 | 0 | case TemplateName::QualifiedTemplate: |
153 | 0 | case TemplateName::DependentTemplate: |
154 | 0 | case TemplateName::SubstTemplateTemplateParm: |
155 | 0 | case TemplateName::SubstTemplateTemplateParmPack: |
156 | 0 | case TemplateName::UsingTemplate: |
157 | 0 | break; |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | 0 | void ODRHash::AddTemplateArgument(TemplateArgument TA) { |
162 | 0 | const auto Kind = TA.getKind(); |
163 | 0 | ID.AddInteger(Kind); |
164 | |
|
165 | 0 | switch (Kind) { |
166 | 0 | case TemplateArgument::Null: |
167 | 0 | llvm_unreachable("Expected valid TemplateArgument"); |
168 | 0 | case TemplateArgument::Type: |
169 | 0 | AddQualType(TA.getAsType()); |
170 | 0 | break; |
171 | 0 | case TemplateArgument::Declaration: |
172 | 0 | AddDecl(TA.getAsDecl()); |
173 | 0 | break; |
174 | 0 | case TemplateArgument::NullPtr: |
175 | 0 | ID.AddPointer(nullptr); |
176 | 0 | break; |
177 | 0 | case TemplateArgument::Integral: { |
178 | | // There are integrals (e.g.: _BitInt(128)) that cannot be represented as |
179 | | // any builtin integral type, so we use the hash of APSInt instead. |
180 | 0 | TA.getAsIntegral().Profile(ID); |
181 | 0 | break; |
182 | 0 | } |
183 | 0 | case TemplateArgument::Template: |
184 | 0 | case TemplateArgument::TemplateExpansion: |
185 | 0 | AddTemplateName(TA.getAsTemplateOrTemplatePattern()); |
186 | 0 | break; |
187 | 0 | case TemplateArgument::Expression: |
188 | 0 | AddStmt(TA.getAsExpr()); |
189 | 0 | break; |
190 | 0 | case TemplateArgument::Pack: |
191 | 0 | ID.AddInteger(TA.pack_size()); |
192 | 0 | for (auto SubTA : TA.pack_elements()) { |
193 | 0 | AddTemplateArgument(SubTA); |
194 | 0 | } |
195 | 0 | break; |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | 0 | void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) { |
200 | 0 | assert(TPL && "Expecting non-null pointer."); |
201 | | |
202 | 0 | ID.AddInteger(TPL->size()); |
203 | 0 | for (auto *ND : TPL->asArray()) { |
204 | 0 | AddSubDecl(ND); |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | 0 | void ODRHash::clear() { |
209 | 0 | DeclNameMap.clear(); |
210 | 0 | Bools.clear(); |
211 | 0 | ID.clear(); |
212 | 0 | } |
213 | | |
214 | 0 | unsigned ODRHash::CalculateHash() { |
215 | | // Append the bools to the end of the data segment backwards. This allows |
216 | | // for the bools data to be compressed 32 times smaller compared to using |
217 | | // ID.AddBoolean |
218 | 0 | const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT; |
219 | 0 | const unsigned size = Bools.size(); |
220 | 0 | const unsigned remainder = size % unsigned_bits; |
221 | 0 | const unsigned loops = size / unsigned_bits; |
222 | 0 | auto I = Bools.rbegin(); |
223 | 0 | unsigned value = 0; |
224 | 0 | for (unsigned i = 0; i < remainder; ++i) { |
225 | 0 | value <<= 1; |
226 | 0 | value |= *I; |
227 | 0 | ++I; |
228 | 0 | } |
229 | 0 | ID.AddInteger(value); |
230 | |
|
231 | 0 | for (unsigned i = 0; i < loops; ++i) { |
232 | 0 | value = 0; |
233 | 0 | for (unsigned j = 0; j < unsigned_bits; ++j) { |
234 | 0 | value <<= 1; |
235 | 0 | value |= *I; |
236 | 0 | ++I; |
237 | 0 | } |
238 | 0 | ID.AddInteger(value); |
239 | 0 | } |
240 | |
|
241 | 0 | assert(I == Bools.rend()); |
242 | 0 | Bools.clear(); |
243 | 0 | return ID.ComputeHash(); |
244 | 0 | } |
245 | | |
246 | | namespace { |
247 | | // Process a Decl pointer. Add* methods call back into ODRHash while Visit* |
248 | | // methods process the relevant parts of the Decl. |
249 | | class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> { |
250 | | typedef ConstDeclVisitor<ODRDeclVisitor> Inherited; |
251 | | llvm::FoldingSetNodeID &ID; |
252 | | ODRHash &Hash; |
253 | | |
254 | | public: |
255 | | ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash) |
256 | 0 | : ID(ID), Hash(Hash) {} |
257 | | |
258 | 0 | void AddStmt(const Stmt *S) { |
259 | 0 | Hash.AddBoolean(S); |
260 | 0 | if (S) { |
261 | 0 | Hash.AddStmt(S); |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | 0 | void AddIdentifierInfo(const IdentifierInfo *II) { |
266 | 0 | Hash.AddBoolean(II); |
267 | 0 | if (II) { |
268 | 0 | Hash.AddIdentifierInfo(II); |
269 | 0 | } |
270 | 0 | } |
271 | | |
272 | 0 | void AddQualType(QualType T) { |
273 | 0 | Hash.AddQualType(T); |
274 | 0 | } |
275 | | |
276 | 0 | void AddDecl(const Decl *D) { |
277 | 0 | Hash.AddBoolean(D); |
278 | 0 | if (D) { |
279 | 0 | Hash.AddDecl(D); |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | 0 | void AddTemplateArgument(TemplateArgument TA) { |
284 | 0 | Hash.AddTemplateArgument(TA); |
285 | 0 | } |
286 | | |
287 | 0 | void Visit(const Decl *D) { |
288 | 0 | ID.AddInteger(D->getKind()); |
289 | 0 | Inherited::Visit(D); |
290 | 0 | } |
291 | | |
292 | 0 | void VisitNamedDecl(const NamedDecl *D) { |
293 | 0 | Hash.AddDeclarationName(D->getDeclName()); |
294 | 0 | Inherited::VisitNamedDecl(D); |
295 | 0 | } |
296 | | |
297 | 0 | void VisitValueDecl(const ValueDecl *D) { |
298 | 0 | if (auto *DD = dyn_cast<DeclaratorDecl>(D); DD && DD->getTypeSourceInfo()) |
299 | 0 | AddQualType(DD->getTypeSourceInfo()->getType()); |
300 | |
|
301 | 0 | Inherited::VisitValueDecl(D); |
302 | 0 | } |
303 | | |
304 | 0 | void VisitVarDecl(const VarDecl *D) { |
305 | 0 | Hash.AddBoolean(D->isStaticLocal()); |
306 | 0 | Hash.AddBoolean(D->isConstexpr()); |
307 | 0 | const bool HasInit = D->hasInit(); |
308 | 0 | Hash.AddBoolean(HasInit); |
309 | 0 | if (HasInit) { |
310 | 0 | AddStmt(D->getInit()); |
311 | 0 | } |
312 | 0 | Inherited::VisitVarDecl(D); |
313 | 0 | } |
314 | | |
315 | 0 | void VisitParmVarDecl(const ParmVarDecl *D) { |
316 | | // TODO: Handle default arguments. |
317 | 0 | Inherited::VisitParmVarDecl(D); |
318 | 0 | } |
319 | | |
320 | 0 | void VisitAccessSpecDecl(const AccessSpecDecl *D) { |
321 | 0 | ID.AddInteger(D->getAccess()); |
322 | 0 | Inherited::VisitAccessSpecDecl(D); |
323 | 0 | } |
324 | | |
325 | 0 | void VisitStaticAssertDecl(const StaticAssertDecl *D) { |
326 | 0 | AddStmt(D->getAssertExpr()); |
327 | 0 | AddStmt(D->getMessage()); |
328 | |
|
329 | 0 | Inherited::VisitStaticAssertDecl(D); |
330 | 0 | } |
331 | | |
332 | 0 | void VisitFieldDecl(const FieldDecl *D) { |
333 | 0 | const bool IsBitfield = D->isBitField(); |
334 | 0 | Hash.AddBoolean(IsBitfield); |
335 | |
|
336 | 0 | if (IsBitfield) { |
337 | 0 | AddStmt(D->getBitWidth()); |
338 | 0 | } |
339 | |
|
340 | 0 | Hash.AddBoolean(D->isMutable()); |
341 | 0 | AddStmt(D->getInClassInitializer()); |
342 | |
|
343 | 0 | Inherited::VisitFieldDecl(D); |
344 | 0 | } |
345 | | |
346 | 0 | void VisitObjCIvarDecl(const ObjCIvarDecl *D) { |
347 | 0 | ID.AddInteger(D->getCanonicalAccessControl()); |
348 | 0 | Inherited::VisitObjCIvarDecl(D); |
349 | 0 | } |
350 | | |
351 | 0 | void VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { |
352 | 0 | ID.AddInteger(D->getPropertyAttributes()); |
353 | 0 | ID.AddInteger(D->getPropertyImplementation()); |
354 | 0 | AddQualType(D->getTypeSourceInfo()->getType()); |
355 | 0 | AddDecl(D); |
356 | |
|
357 | 0 | Inherited::VisitObjCPropertyDecl(D); |
358 | 0 | } |
359 | | |
360 | 0 | void VisitFunctionDecl(const FunctionDecl *D) { |
361 | | // Handled by the ODRHash for FunctionDecl |
362 | 0 | ID.AddInteger(D->getODRHash()); |
363 | |
|
364 | 0 | Inherited::VisitFunctionDecl(D); |
365 | 0 | } |
366 | | |
367 | 0 | void VisitCXXMethodDecl(const CXXMethodDecl *D) { |
368 | | // Handled by the ODRHash for FunctionDecl |
369 | |
|
370 | 0 | Inherited::VisitCXXMethodDecl(D); |
371 | 0 | } |
372 | | |
373 | 0 | void VisitObjCMethodDecl(const ObjCMethodDecl *Method) { |
374 | 0 | ID.AddInteger(Method->getDeclKind()); |
375 | 0 | Hash.AddBoolean(Method->isInstanceMethod()); // false if class method |
376 | 0 | Hash.AddBoolean(Method->isVariadic()); |
377 | 0 | Hash.AddBoolean(Method->isSynthesizedAccessorStub()); |
378 | 0 | Hash.AddBoolean(Method->isDefined()); |
379 | 0 | Hash.AddBoolean(Method->isDirectMethod()); |
380 | 0 | Hash.AddBoolean(Method->isThisDeclarationADesignatedInitializer()); |
381 | 0 | Hash.AddBoolean(Method->hasSkippedBody()); |
382 | |
|
383 | 0 | ID.AddInteger(llvm::to_underlying(Method->getImplementationControl())); |
384 | 0 | ID.AddInteger(Method->getMethodFamily()); |
385 | 0 | ImplicitParamDecl *Cmd = Method->getCmdDecl(); |
386 | 0 | Hash.AddBoolean(Cmd); |
387 | 0 | if (Cmd) |
388 | 0 | ID.AddInteger(llvm::to_underlying(Cmd->getParameterKind())); |
389 | |
|
390 | 0 | ImplicitParamDecl *Self = Method->getSelfDecl(); |
391 | 0 | Hash.AddBoolean(Self); |
392 | 0 | if (Self) |
393 | 0 | ID.AddInteger(llvm::to_underlying(Self->getParameterKind())); |
394 | |
|
395 | 0 | AddDecl(Method); |
396 | |
|
397 | 0 | if (Method->getReturnTypeSourceInfo()) |
398 | 0 | AddQualType(Method->getReturnTypeSourceInfo()->getType()); |
399 | |
|
400 | 0 | ID.AddInteger(Method->param_size()); |
401 | 0 | for (auto Param : Method->parameters()) |
402 | 0 | Hash.AddSubDecl(Param); |
403 | |
|
404 | 0 | if (Method->hasBody()) { |
405 | 0 | const bool IsDefinition = Method->isThisDeclarationADefinition(); |
406 | 0 | Hash.AddBoolean(IsDefinition); |
407 | 0 | if (IsDefinition) { |
408 | 0 | Stmt *Body = Method->getBody(); |
409 | 0 | Hash.AddBoolean(Body); |
410 | 0 | if (Body) |
411 | 0 | AddStmt(Body); |
412 | | |
413 | | // Filter out sub-Decls which will not be processed in order to get an |
414 | | // accurate count of Decl's. |
415 | 0 | llvm::SmallVector<const Decl *, 16> Decls; |
416 | 0 | for (Decl *SubDecl : Method->decls()) |
417 | 0 | if (ODRHash::isSubDeclToBeProcessed(SubDecl, Method)) |
418 | 0 | Decls.push_back(SubDecl); |
419 | |
|
420 | 0 | ID.AddInteger(Decls.size()); |
421 | 0 | for (auto SubDecl : Decls) |
422 | 0 | Hash.AddSubDecl(SubDecl); |
423 | 0 | } |
424 | 0 | } else { |
425 | 0 | Hash.AddBoolean(false); |
426 | 0 | } |
427 | |
|
428 | 0 | Inherited::VisitObjCMethodDecl(Method); |
429 | 0 | } |
430 | | |
431 | 0 | void VisitTypedefNameDecl(const TypedefNameDecl *D) { |
432 | 0 | AddQualType(D->getUnderlyingType()); |
433 | |
|
434 | 0 | Inherited::VisitTypedefNameDecl(D); |
435 | 0 | } |
436 | | |
437 | 0 | void VisitTypedefDecl(const TypedefDecl *D) { |
438 | 0 | Inherited::VisitTypedefDecl(D); |
439 | 0 | } |
440 | | |
441 | 0 | void VisitTypeAliasDecl(const TypeAliasDecl *D) { |
442 | 0 | Inherited::VisitTypeAliasDecl(D); |
443 | 0 | } |
444 | | |
445 | 0 | void VisitFriendDecl(const FriendDecl *D) { |
446 | 0 | TypeSourceInfo *TSI = D->getFriendType(); |
447 | 0 | Hash.AddBoolean(TSI); |
448 | 0 | if (TSI) { |
449 | 0 | AddQualType(TSI->getType()); |
450 | 0 | } else { |
451 | 0 | AddDecl(D->getFriendDecl()); |
452 | 0 | } |
453 | 0 | } |
454 | | |
455 | 0 | void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { |
456 | | // Only care about default arguments as part of the definition. |
457 | 0 | const bool hasDefaultArgument = |
458 | 0 | D->hasDefaultArgument() && !D->defaultArgumentWasInherited(); |
459 | 0 | Hash.AddBoolean(hasDefaultArgument); |
460 | 0 | if (hasDefaultArgument) { |
461 | 0 | AddTemplateArgument(D->getDefaultArgument()); |
462 | 0 | } |
463 | 0 | Hash.AddBoolean(D->isParameterPack()); |
464 | |
|
465 | 0 | const TypeConstraint *TC = D->getTypeConstraint(); |
466 | 0 | Hash.AddBoolean(TC != nullptr); |
467 | 0 | if (TC) |
468 | 0 | AddStmt(TC->getImmediatelyDeclaredConstraint()); |
469 | |
|
470 | 0 | Inherited::VisitTemplateTypeParmDecl(D); |
471 | 0 | } |
472 | | |
473 | 0 | void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) { |
474 | | // Only care about default arguments as part of the definition. |
475 | 0 | const bool hasDefaultArgument = |
476 | 0 | D->hasDefaultArgument() && !D->defaultArgumentWasInherited(); |
477 | 0 | Hash.AddBoolean(hasDefaultArgument); |
478 | 0 | if (hasDefaultArgument) { |
479 | 0 | AddStmt(D->getDefaultArgument()); |
480 | 0 | } |
481 | 0 | Hash.AddBoolean(D->isParameterPack()); |
482 | |
|
483 | 0 | Inherited::VisitNonTypeTemplateParmDecl(D); |
484 | 0 | } |
485 | | |
486 | 0 | void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) { |
487 | | // Only care about default arguments as part of the definition. |
488 | 0 | const bool hasDefaultArgument = |
489 | 0 | D->hasDefaultArgument() && !D->defaultArgumentWasInherited(); |
490 | 0 | Hash.AddBoolean(hasDefaultArgument); |
491 | 0 | if (hasDefaultArgument) { |
492 | 0 | AddTemplateArgument(D->getDefaultArgument().getArgument()); |
493 | 0 | } |
494 | 0 | Hash.AddBoolean(D->isParameterPack()); |
495 | |
|
496 | 0 | Inherited::VisitTemplateTemplateParmDecl(D); |
497 | 0 | } |
498 | | |
499 | 0 | void VisitTemplateDecl(const TemplateDecl *D) { |
500 | 0 | Hash.AddTemplateParameterList(D->getTemplateParameters()); |
501 | |
|
502 | 0 | Inherited::VisitTemplateDecl(D); |
503 | 0 | } |
504 | | |
505 | 0 | void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) { |
506 | 0 | Hash.AddBoolean(D->isMemberSpecialization()); |
507 | 0 | Inherited::VisitRedeclarableTemplateDecl(D); |
508 | 0 | } |
509 | | |
510 | 0 | void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { |
511 | 0 | AddDecl(D->getTemplatedDecl()); |
512 | 0 | ID.AddInteger(D->getTemplatedDecl()->getODRHash()); |
513 | 0 | Inherited::VisitFunctionTemplateDecl(D); |
514 | 0 | } |
515 | | |
516 | 0 | void VisitEnumConstantDecl(const EnumConstantDecl *D) { |
517 | 0 | AddStmt(D->getInitExpr()); |
518 | 0 | Inherited::VisitEnumConstantDecl(D); |
519 | 0 | } |
520 | | }; |
521 | | } // namespace |
522 | | |
523 | | // Only allow a small portion of Decl's to be processed. Remove this once |
524 | | // all Decl's can be handled. |
525 | 0 | bool ODRHash::isSubDeclToBeProcessed(const Decl *D, const DeclContext *Parent) { |
526 | 0 | if (D->isImplicit()) return false; |
527 | 0 | if (D->getDeclContext() != Parent) return false; |
528 | | |
529 | 0 | switch (D->getKind()) { |
530 | 0 | default: |
531 | 0 | return false; |
532 | 0 | case Decl::AccessSpec: |
533 | 0 | case Decl::CXXConstructor: |
534 | 0 | case Decl::CXXDestructor: |
535 | 0 | case Decl::CXXMethod: |
536 | 0 | case Decl::EnumConstant: // Only found in EnumDecl's. |
537 | 0 | case Decl::Field: |
538 | 0 | case Decl::Friend: |
539 | 0 | case Decl::FunctionTemplate: |
540 | 0 | case Decl::StaticAssert: |
541 | 0 | case Decl::TypeAlias: |
542 | 0 | case Decl::Typedef: |
543 | 0 | case Decl::Var: |
544 | 0 | case Decl::ObjCMethod: |
545 | 0 | case Decl::ObjCIvar: |
546 | 0 | case Decl::ObjCProperty: |
547 | 0 | return true; |
548 | 0 | } |
549 | 0 | } |
550 | | |
551 | 0 | void ODRHash::AddSubDecl(const Decl *D) { |
552 | 0 | assert(D && "Expecting non-null pointer."); |
553 | | |
554 | 0 | ODRDeclVisitor(ID, *this).Visit(D); |
555 | 0 | } |
556 | | |
557 | 0 | void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) { |
558 | 0 | assert(Record && Record->hasDefinition() && |
559 | 0 | "Expected non-null record to be a definition."); |
560 | | |
561 | 0 | const DeclContext *DC = Record; |
562 | 0 | while (DC) { |
563 | 0 | if (isa<ClassTemplateSpecializationDecl>(DC)) { |
564 | 0 | return; |
565 | 0 | } |
566 | 0 | DC = DC->getParent(); |
567 | 0 | } |
568 | | |
569 | 0 | AddDecl(Record); |
570 | | |
571 | | // Filter out sub-Decls which will not be processed in order to get an |
572 | | // accurate count of Decl's. |
573 | 0 | llvm::SmallVector<const Decl *, 16> Decls; |
574 | 0 | for (Decl *SubDecl : Record->decls()) { |
575 | 0 | if (isSubDeclToBeProcessed(SubDecl, Record)) { |
576 | 0 | Decls.push_back(SubDecl); |
577 | 0 | if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) { |
578 | | // Compute/Preload ODRHash into FunctionDecl. |
579 | 0 | Function->getODRHash(); |
580 | 0 | } |
581 | 0 | } |
582 | 0 | } |
583 | |
|
584 | 0 | ID.AddInteger(Decls.size()); |
585 | 0 | for (auto SubDecl : Decls) { |
586 | 0 | AddSubDecl(SubDecl); |
587 | 0 | } |
588 | |
|
589 | 0 | const ClassTemplateDecl *TD = Record->getDescribedClassTemplate(); |
590 | 0 | AddBoolean(TD); |
591 | 0 | if (TD) { |
592 | 0 | AddTemplateParameterList(TD->getTemplateParameters()); |
593 | 0 | } |
594 | |
|
595 | 0 | ID.AddInteger(Record->getNumBases()); |
596 | 0 | auto Bases = Record->bases(); |
597 | 0 | for (const auto &Base : Bases) { |
598 | 0 | AddQualType(Base.getTypeSourceInfo()->getType()); |
599 | 0 | ID.AddInteger(Base.isVirtual()); |
600 | 0 | ID.AddInteger(Base.getAccessSpecifierAsWritten()); |
601 | 0 | } |
602 | 0 | } |
603 | | |
604 | 0 | void ODRHash::AddRecordDecl(const RecordDecl *Record) { |
605 | 0 | assert(!isa<CXXRecordDecl>(Record) && |
606 | 0 | "For CXXRecordDecl should call AddCXXRecordDecl."); |
607 | 0 | AddDecl(Record); |
608 | | |
609 | | // Filter out sub-Decls which will not be processed in order to get an |
610 | | // accurate count of Decl's. |
611 | 0 | llvm::SmallVector<const Decl *, 16> Decls; |
612 | 0 | for (Decl *SubDecl : Record->decls()) { |
613 | 0 | if (isSubDeclToBeProcessed(SubDecl, Record)) |
614 | 0 | Decls.push_back(SubDecl); |
615 | 0 | } |
616 | |
|
617 | 0 | ID.AddInteger(Decls.size()); |
618 | 0 | for (const Decl *SubDecl : Decls) |
619 | 0 | AddSubDecl(SubDecl); |
620 | 0 | } |
621 | | |
622 | 0 | void ODRHash::AddObjCInterfaceDecl(const ObjCInterfaceDecl *IF) { |
623 | 0 | AddDecl(IF); |
624 | |
|
625 | 0 | auto *SuperClass = IF->getSuperClass(); |
626 | 0 | AddBoolean(SuperClass); |
627 | 0 | if (SuperClass) |
628 | 0 | ID.AddInteger(SuperClass->getODRHash()); |
629 | | |
630 | | // Hash referenced protocols. |
631 | 0 | ID.AddInteger(IF->getReferencedProtocols().size()); |
632 | 0 | for (const ObjCProtocolDecl *RefP : IF->protocols()) { |
633 | | // Hash the name only as a referenced protocol can be a forward declaration. |
634 | 0 | AddDeclarationName(RefP->getDeclName()); |
635 | 0 | } |
636 | | |
637 | | // Filter out sub-Decls which will not be processed in order to get an |
638 | | // accurate count of Decl's. |
639 | 0 | llvm::SmallVector<const Decl *, 16> Decls; |
640 | 0 | for (Decl *SubDecl : IF->decls()) |
641 | 0 | if (isSubDeclToBeProcessed(SubDecl, IF)) |
642 | 0 | Decls.push_back(SubDecl); |
643 | |
|
644 | 0 | ID.AddInteger(Decls.size()); |
645 | 0 | for (auto *SubDecl : Decls) |
646 | 0 | AddSubDecl(SubDecl); |
647 | 0 | } |
648 | | |
649 | | void ODRHash::AddFunctionDecl(const FunctionDecl *Function, |
650 | 0 | bool SkipBody) { |
651 | 0 | assert(Function && "Expecting non-null pointer."); |
652 | | |
653 | | // Skip functions that are specializations or in specialization context. |
654 | 0 | const DeclContext *DC = Function; |
655 | 0 | while (DC) { |
656 | 0 | if (isa<ClassTemplateSpecializationDecl>(DC)) return; |
657 | 0 | if (auto *F = dyn_cast<FunctionDecl>(DC)) { |
658 | 0 | if (F->isFunctionTemplateSpecialization()) { |
659 | 0 | if (!isa<CXXMethodDecl>(DC)) return; |
660 | 0 | if (DC->getLexicalParent()->isFileContext()) return; |
661 | | // Skip class scope explicit function template specializations, |
662 | | // as they have not yet been instantiated. |
663 | 0 | if (F->getDependentSpecializationInfo()) |
664 | 0 | return; |
665 | | // Inline method specializations are the only supported |
666 | | // specialization for now. |
667 | 0 | } |
668 | 0 | } |
669 | 0 | DC = DC->getParent(); |
670 | 0 | } |
671 | | |
672 | 0 | ID.AddInteger(Function->getDeclKind()); |
673 | |
|
674 | 0 | const auto *SpecializationArgs = Function->getTemplateSpecializationArgs(); |
675 | 0 | AddBoolean(SpecializationArgs); |
676 | 0 | if (SpecializationArgs) { |
677 | 0 | ID.AddInteger(SpecializationArgs->size()); |
678 | 0 | for (const TemplateArgument &TA : SpecializationArgs->asArray()) { |
679 | 0 | AddTemplateArgument(TA); |
680 | 0 | } |
681 | 0 | } |
682 | |
|
683 | 0 | if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) { |
684 | 0 | AddBoolean(Method->isConst()); |
685 | 0 | AddBoolean(Method->isVolatile()); |
686 | 0 | } |
687 | |
|
688 | 0 | ID.AddInteger(Function->getStorageClass()); |
689 | 0 | AddBoolean(Function->isInlineSpecified()); |
690 | 0 | AddBoolean(Function->isVirtualAsWritten()); |
691 | 0 | AddBoolean(Function->isPure()); |
692 | 0 | AddBoolean(Function->isDeletedAsWritten()); |
693 | 0 | AddBoolean(Function->isExplicitlyDefaulted()); |
694 | |
|
695 | 0 | AddDecl(Function); |
696 | |
|
697 | 0 | AddQualType(Function->getReturnType()); |
698 | |
|
699 | 0 | ID.AddInteger(Function->param_size()); |
700 | 0 | for (auto *Param : Function->parameters()) |
701 | 0 | AddSubDecl(Param); |
702 | |
|
703 | 0 | if (SkipBody) { |
704 | 0 | AddBoolean(false); |
705 | 0 | return; |
706 | 0 | } |
707 | | |
708 | 0 | const bool HasBody = Function->isThisDeclarationADefinition() && |
709 | 0 | !Function->isDefaulted() && !Function->isDeleted() && |
710 | 0 | !Function->isLateTemplateParsed(); |
711 | 0 | AddBoolean(HasBody); |
712 | 0 | if (!HasBody) { |
713 | 0 | return; |
714 | 0 | } |
715 | | |
716 | 0 | auto *Body = Function->getBody(); |
717 | 0 | AddBoolean(Body); |
718 | 0 | if (Body) |
719 | 0 | AddStmt(Body); |
720 | | |
721 | | // Filter out sub-Decls which will not be processed in order to get an |
722 | | // accurate count of Decl's. |
723 | 0 | llvm::SmallVector<const Decl *, 16> Decls; |
724 | 0 | for (Decl *SubDecl : Function->decls()) { |
725 | 0 | if (isSubDeclToBeProcessed(SubDecl, Function)) { |
726 | 0 | Decls.push_back(SubDecl); |
727 | 0 | } |
728 | 0 | } |
729 | |
|
730 | 0 | ID.AddInteger(Decls.size()); |
731 | 0 | for (auto SubDecl : Decls) { |
732 | 0 | AddSubDecl(SubDecl); |
733 | 0 | } |
734 | 0 | } |
735 | | |
736 | 0 | void ODRHash::AddEnumDecl(const EnumDecl *Enum) { |
737 | 0 | assert(Enum); |
738 | 0 | AddDeclarationName(Enum->getDeclName()); |
739 | |
|
740 | 0 | AddBoolean(Enum->isScoped()); |
741 | 0 | if (Enum->isScoped()) |
742 | 0 | AddBoolean(Enum->isScopedUsingClassTag()); |
743 | |
|
744 | 0 | if (Enum->getIntegerTypeSourceInfo()) |
745 | 0 | AddQualType(Enum->getIntegerType()); |
746 | | |
747 | | // Filter out sub-Decls which will not be processed in order to get an |
748 | | // accurate count of Decl's. |
749 | 0 | llvm::SmallVector<const Decl *, 16> Decls; |
750 | 0 | for (Decl *SubDecl : Enum->decls()) { |
751 | 0 | if (isSubDeclToBeProcessed(SubDecl, Enum)) { |
752 | 0 | assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl"); |
753 | 0 | Decls.push_back(SubDecl); |
754 | 0 | } |
755 | 0 | } |
756 | |
|
757 | 0 | ID.AddInteger(Decls.size()); |
758 | 0 | for (auto SubDecl : Decls) { |
759 | 0 | AddSubDecl(SubDecl); |
760 | 0 | } |
761 | |
|
762 | 0 | } |
763 | | |
764 | 0 | void ODRHash::AddObjCProtocolDecl(const ObjCProtocolDecl *P) { |
765 | 0 | AddDecl(P); |
766 | | |
767 | | // Hash referenced protocols. |
768 | 0 | ID.AddInteger(P->getReferencedProtocols().size()); |
769 | 0 | for (const ObjCProtocolDecl *RefP : P->protocols()) { |
770 | | // Hash the name only as a referenced protocol can be a forward declaration. |
771 | 0 | AddDeclarationName(RefP->getDeclName()); |
772 | 0 | } |
773 | | |
774 | | // Filter out sub-Decls which will not be processed in order to get an |
775 | | // accurate count of Decl's. |
776 | 0 | llvm::SmallVector<const Decl *, 16> Decls; |
777 | 0 | for (Decl *SubDecl : P->decls()) { |
778 | 0 | if (isSubDeclToBeProcessed(SubDecl, P)) { |
779 | 0 | Decls.push_back(SubDecl); |
780 | 0 | } |
781 | 0 | } |
782 | |
|
783 | 0 | ID.AddInteger(Decls.size()); |
784 | 0 | for (auto *SubDecl : Decls) { |
785 | 0 | AddSubDecl(SubDecl); |
786 | 0 | } |
787 | 0 | } |
788 | | |
789 | 0 | void ODRHash::AddDecl(const Decl *D) { |
790 | 0 | assert(D && "Expecting non-null pointer."); |
791 | 0 | D = D->getCanonicalDecl(); |
792 | |
|
793 | 0 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
794 | 0 | AddBoolean(ND); |
795 | 0 | if (!ND) { |
796 | 0 | ID.AddInteger(D->getKind()); |
797 | 0 | return; |
798 | 0 | } |
799 | | |
800 | 0 | AddDeclarationName(ND->getDeclName()); |
801 | |
|
802 | 0 | const auto *Specialization = |
803 | 0 | dyn_cast<ClassTemplateSpecializationDecl>(D); |
804 | 0 | AddBoolean(Specialization); |
805 | 0 | if (Specialization) { |
806 | 0 | const TemplateArgumentList &List = Specialization->getTemplateArgs(); |
807 | 0 | ID.AddInteger(List.size()); |
808 | 0 | for (const TemplateArgument &TA : List.asArray()) |
809 | 0 | AddTemplateArgument(TA); |
810 | 0 | } |
811 | 0 | } |
812 | | |
813 | | namespace { |
814 | | // Process a Type pointer. Add* methods call back into ODRHash while Visit* |
815 | | // methods process the relevant parts of the Type. |
816 | | class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> { |
817 | | typedef TypeVisitor<ODRTypeVisitor> Inherited; |
818 | | llvm::FoldingSetNodeID &ID; |
819 | | ODRHash &Hash; |
820 | | |
821 | | public: |
822 | | ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash) |
823 | 0 | : ID(ID), Hash(Hash) {} |
824 | | |
825 | 0 | void AddStmt(Stmt *S) { |
826 | 0 | Hash.AddBoolean(S); |
827 | 0 | if (S) { |
828 | 0 | Hash.AddStmt(S); |
829 | 0 | } |
830 | 0 | } |
831 | | |
832 | 0 | void AddDecl(const Decl *D) { |
833 | 0 | Hash.AddBoolean(D); |
834 | 0 | if (D) { |
835 | 0 | Hash.AddDecl(D); |
836 | 0 | } |
837 | 0 | } |
838 | | |
839 | 0 | void AddQualType(QualType T) { |
840 | 0 | Hash.AddQualType(T); |
841 | 0 | } |
842 | | |
843 | 0 | void AddType(const Type *T) { |
844 | 0 | Hash.AddBoolean(T); |
845 | 0 | if (T) { |
846 | 0 | Hash.AddType(T); |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | 0 | void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) { |
851 | 0 | Hash.AddBoolean(NNS); |
852 | 0 | if (NNS) { |
853 | 0 | Hash.AddNestedNameSpecifier(NNS); |
854 | 0 | } |
855 | 0 | } |
856 | | |
857 | 0 | void AddIdentifierInfo(const IdentifierInfo *II) { |
858 | 0 | Hash.AddBoolean(II); |
859 | 0 | if (II) { |
860 | 0 | Hash.AddIdentifierInfo(II); |
861 | 0 | } |
862 | 0 | } |
863 | | |
864 | 0 | void VisitQualifiers(Qualifiers Quals) { |
865 | 0 | ID.AddInteger(Quals.getAsOpaqueValue()); |
866 | 0 | } |
867 | | |
868 | | // Return the RecordType if the typedef only strips away a keyword. |
869 | | // Otherwise, return the original type. |
870 | 0 | static const Type *RemoveTypedef(const Type *T) { |
871 | 0 | const auto *TypedefT = dyn_cast<TypedefType>(T); |
872 | 0 | if (!TypedefT) { |
873 | 0 | return T; |
874 | 0 | } |
875 | | |
876 | 0 | const TypedefNameDecl *D = TypedefT->getDecl(); |
877 | 0 | QualType UnderlyingType = D->getUnderlyingType(); |
878 | |
|
879 | 0 | if (UnderlyingType.hasLocalQualifiers()) { |
880 | 0 | return T; |
881 | 0 | } |
882 | | |
883 | 0 | const auto *ElaboratedT = dyn_cast<ElaboratedType>(UnderlyingType); |
884 | 0 | if (!ElaboratedT) { |
885 | 0 | return T; |
886 | 0 | } |
887 | | |
888 | 0 | if (ElaboratedT->getQualifier() != nullptr) { |
889 | 0 | return T; |
890 | 0 | } |
891 | | |
892 | 0 | QualType NamedType = ElaboratedT->getNamedType(); |
893 | 0 | if (NamedType.hasLocalQualifiers()) { |
894 | 0 | return T; |
895 | 0 | } |
896 | | |
897 | 0 | const auto *RecordT = dyn_cast<RecordType>(NamedType); |
898 | 0 | if (!RecordT) { |
899 | 0 | return T; |
900 | 0 | } |
901 | | |
902 | 0 | const IdentifierInfo *TypedefII = TypedefT->getDecl()->getIdentifier(); |
903 | 0 | const IdentifierInfo *RecordII = RecordT->getDecl()->getIdentifier(); |
904 | 0 | if (!TypedefII || !RecordII || |
905 | 0 | TypedefII->getName() != RecordII->getName()) { |
906 | 0 | return T; |
907 | 0 | } |
908 | | |
909 | 0 | return RecordT; |
910 | 0 | } |
911 | | |
912 | 0 | void Visit(const Type *T) { |
913 | 0 | T = RemoveTypedef(T); |
914 | 0 | ID.AddInteger(T->getTypeClass()); |
915 | 0 | Inherited::Visit(T); |
916 | 0 | } |
917 | | |
918 | 0 | void VisitType(const Type *T) {} |
919 | | |
920 | 0 | void VisitAdjustedType(const AdjustedType *T) { |
921 | 0 | AddQualType(T->getOriginalType()); |
922 | |
|
923 | 0 | VisitType(T); |
924 | 0 | } |
925 | | |
926 | 0 | void VisitDecayedType(const DecayedType *T) { |
927 | | // getDecayedType and getPointeeType are derived from getAdjustedType |
928 | | // and don't need to be separately processed. |
929 | 0 | VisitAdjustedType(T); |
930 | 0 | } |
931 | | |
932 | 0 | void VisitArrayType(const ArrayType *T) { |
933 | 0 | AddQualType(T->getElementType()); |
934 | 0 | ID.AddInteger(llvm::to_underlying(T->getSizeModifier())); |
935 | 0 | VisitQualifiers(T->getIndexTypeQualifiers()); |
936 | 0 | VisitType(T); |
937 | 0 | } |
938 | 0 | void VisitConstantArrayType(const ConstantArrayType *T) { |
939 | 0 | T->getSize().Profile(ID); |
940 | 0 | VisitArrayType(T); |
941 | 0 | } |
942 | | |
943 | 0 | void VisitDependentSizedArrayType(const DependentSizedArrayType *T) { |
944 | 0 | AddStmt(T->getSizeExpr()); |
945 | 0 | VisitArrayType(T); |
946 | 0 | } |
947 | | |
948 | 0 | void VisitIncompleteArrayType(const IncompleteArrayType *T) { |
949 | 0 | VisitArrayType(T); |
950 | 0 | } |
951 | | |
952 | 0 | void VisitVariableArrayType(const VariableArrayType *T) { |
953 | 0 | AddStmt(T->getSizeExpr()); |
954 | 0 | VisitArrayType(T); |
955 | 0 | } |
956 | | |
957 | 0 | void VisitAttributedType(const AttributedType *T) { |
958 | 0 | ID.AddInteger(T->getAttrKind()); |
959 | 0 | AddQualType(T->getModifiedType()); |
960 | |
|
961 | 0 | VisitType(T); |
962 | 0 | } |
963 | | |
964 | 0 | void VisitBlockPointerType(const BlockPointerType *T) { |
965 | 0 | AddQualType(T->getPointeeType()); |
966 | 0 | VisitType(T); |
967 | 0 | } |
968 | | |
969 | 0 | void VisitBuiltinType(const BuiltinType *T) { |
970 | 0 | ID.AddInteger(T->getKind()); |
971 | 0 | VisitType(T); |
972 | 0 | } |
973 | | |
974 | 0 | void VisitComplexType(const ComplexType *T) { |
975 | 0 | AddQualType(T->getElementType()); |
976 | 0 | VisitType(T); |
977 | 0 | } |
978 | | |
979 | 0 | void VisitDecltypeType(const DecltypeType *T) { |
980 | 0 | AddStmt(T->getUnderlyingExpr()); |
981 | 0 | VisitType(T); |
982 | 0 | } |
983 | | |
984 | 0 | void VisitDependentDecltypeType(const DependentDecltypeType *T) { |
985 | 0 | VisitDecltypeType(T); |
986 | 0 | } |
987 | | |
988 | 0 | void VisitDeducedType(const DeducedType *T) { |
989 | 0 | AddQualType(T->getDeducedType()); |
990 | 0 | VisitType(T); |
991 | 0 | } |
992 | | |
993 | 0 | void VisitAutoType(const AutoType *T) { |
994 | 0 | ID.AddInteger((unsigned)T->getKeyword()); |
995 | 0 | ID.AddInteger(T->isConstrained()); |
996 | 0 | if (T->isConstrained()) { |
997 | 0 | AddDecl(T->getTypeConstraintConcept()); |
998 | 0 | ID.AddInteger(T->getTypeConstraintArguments().size()); |
999 | 0 | for (const auto &TA : T->getTypeConstraintArguments()) |
1000 | 0 | Hash.AddTemplateArgument(TA); |
1001 | 0 | } |
1002 | 0 | VisitDeducedType(T); |
1003 | 0 | } |
1004 | | |
1005 | | void VisitDeducedTemplateSpecializationType( |
1006 | 0 | const DeducedTemplateSpecializationType *T) { |
1007 | 0 | Hash.AddTemplateName(T->getTemplateName()); |
1008 | 0 | VisitDeducedType(T); |
1009 | 0 | } |
1010 | | |
1011 | 0 | void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) { |
1012 | 0 | AddQualType(T->getPointeeType()); |
1013 | 0 | AddStmt(T->getAddrSpaceExpr()); |
1014 | 0 | VisitType(T); |
1015 | 0 | } |
1016 | | |
1017 | 0 | void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) { |
1018 | 0 | AddQualType(T->getElementType()); |
1019 | 0 | AddStmt(T->getSizeExpr()); |
1020 | 0 | VisitType(T); |
1021 | 0 | } |
1022 | | |
1023 | 0 | void VisitFunctionType(const FunctionType *T) { |
1024 | 0 | AddQualType(T->getReturnType()); |
1025 | 0 | T->getExtInfo().Profile(ID); |
1026 | 0 | Hash.AddBoolean(T->isConst()); |
1027 | 0 | Hash.AddBoolean(T->isVolatile()); |
1028 | 0 | Hash.AddBoolean(T->isRestrict()); |
1029 | 0 | VisitType(T); |
1030 | 0 | } |
1031 | | |
1032 | 0 | void VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
1033 | 0 | VisitFunctionType(T); |
1034 | 0 | } |
1035 | | |
1036 | 0 | void VisitFunctionProtoType(const FunctionProtoType *T) { |
1037 | 0 | ID.AddInteger(T->getNumParams()); |
1038 | 0 | for (auto ParamType : T->getParamTypes()) |
1039 | 0 | AddQualType(ParamType); |
1040 | |
|
1041 | 0 | VisitFunctionType(T); |
1042 | 0 | } |
1043 | | |
1044 | 0 | void VisitInjectedClassNameType(const InjectedClassNameType *T) { |
1045 | 0 | AddDecl(T->getDecl()); |
1046 | 0 | VisitType(T); |
1047 | 0 | } |
1048 | | |
1049 | 0 | void VisitMemberPointerType(const MemberPointerType *T) { |
1050 | 0 | AddQualType(T->getPointeeType()); |
1051 | 0 | AddType(T->getClass()); |
1052 | 0 | VisitType(T); |
1053 | 0 | } |
1054 | | |
1055 | 0 | void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
1056 | 0 | AddQualType(T->getPointeeType()); |
1057 | 0 | VisitType(T); |
1058 | 0 | } |
1059 | | |
1060 | 0 | void VisitObjCObjectType(const ObjCObjectType *T) { |
1061 | 0 | AddDecl(T->getInterface()); |
1062 | |
|
1063 | 0 | auto TypeArgs = T->getTypeArgsAsWritten(); |
1064 | 0 | ID.AddInteger(TypeArgs.size()); |
1065 | 0 | for (auto Arg : TypeArgs) { |
1066 | 0 | AddQualType(Arg); |
1067 | 0 | } |
1068 | |
|
1069 | 0 | auto Protocols = T->getProtocols(); |
1070 | 0 | ID.AddInteger(Protocols.size()); |
1071 | 0 | for (auto *Protocol : Protocols) { |
1072 | 0 | AddDecl(Protocol); |
1073 | 0 | } |
1074 | |
|
1075 | 0 | Hash.AddBoolean(T->isKindOfType()); |
1076 | |
|
1077 | 0 | VisitType(T); |
1078 | 0 | } |
1079 | | |
1080 | 0 | void VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
1081 | | // This type is handled by the parent type ObjCObjectType. |
1082 | 0 | VisitObjCObjectType(T); |
1083 | 0 | } |
1084 | | |
1085 | 0 | void VisitObjCTypeParamType(const ObjCTypeParamType *T) { |
1086 | 0 | AddDecl(T->getDecl()); |
1087 | 0 | auto Protocols = T->getProtocols(); |
1088 | 0 | ID.AddInteger(Protocols.size()); |
1089 | 0 | for (auto *Protocol : Protocols) { |
1090 | 0 | AddDecl(Protocol); |
1091 | 0 | } |
1092 | |
|
1093 | 0 | VisitType(T); |
1094 | 0 | } |
1095 | | |
1096 | 0 | void VisitPackExpansionType(const PackExpansionType *T) { |
1097 | 0 | AddQualType(T->getPattern()); |
1098 | 0 | VisitType(T); |
1099 | 0 | } |
1100 | | |
1101 | 0 | void VisitParenType(const ParenType *T) { |
1102 | 0 | AddQualType(T->getInnerType()); |
1103 | 0 | VisitType(T); |
1104 | 0 | } |
1105 | | |
1106 | 0 | void VisitPipeType(const PipeType *T) { |
1107 | 0 | AddQualType(T->getElementType()); |
1108 | 0 | Hash.AddBoolean(T->isReadOnly()); |
1109 | 0 | VisitType(T); |
1110 | 0 | } |
1111 | | |
1112 | 0 | void VisitPointerType(const PointerType *T) { |
1113 | 0 | AddQualType(T->getPointeeType()); |
1114 | 0 | VisitType(T); |
1115 | 0 | } |
1116 | | |
1117 | 0 | void VisitReferenceType(const ReferenceType *T) { |
1118 | 0 | AddQualType(T->getPointeeTypeAsWritten()); |
1119 | 0 | VisitType(T); |
1120 | 0 | } |
1121 | | |
1122 | 0 | void VisitLValueReferenceType(const LValueReferenceType *T) { |
1123 | 0 | VisitReferenceType(T); |
1124 | 0 | } |
1125 | | |
1126 | 0 | void VisitRValueReferenceType(const RValueReferenceType *T) { |
1127 | 0 | VisitReferenceType(T); |
1128 | 0 | } |
1129 | | |
1130 | | void |
1131 | 0 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
1132 | 0 | AddDecl(T->getAssociatedDecl()); |
1133 | 0 | Hash.AddTemplateArgument(T->getArgumentPack()); |
1134 | 0 | VisitType(T); |
1135 | 0 | } |
1136 | | |
1137 | 0 | void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
1138 | 0 | AddDecl(T->getAssociatedDecl()); |
1139 | 0 | AddQualType(T->getReplacementType()); |
1140 | 0 | VisitType(T); |
1141 | 0 | } |
1142 | | |
1143 | 0 | void VisitTagType(const TagType *T) { |
1144 | 0 | AddDecl(T->getDecl()); |
1145 | 0 | VisitType(T); |
1146 | 0 | } |
1147 | | |
1148 | 0 | void VisitRecordType(const RecordType *T) { VisitTagType(T); } |
1149 | 0 | void VisitEnumType(const EnumType *T) { VisitTagType(T); } |
1150 | | |
1151 | 0 | void VisitTemplateSpecializationType(const TemplateSpecializationType *T) { |
1152 | 0 | ID.AddInteger(T->template_arguments().size()); |
1153 | 0 | for (const auto &TA : T->template_arguments()) { |
1154 | 0 | Hash.AddTemplateArgument(TA); |
1155 | 0 | } |
1156 | 0 | Hash.AddTemplateName(T->getTemplateName()); |
1157 | 0 | VisitType(T); |
1158 | 0 | } |
1159 | | |
1160 | 0 | void VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
1161 | 0 | ID.AddInteger(T->getDepth()); |
1162 | 0 | ID.AddInteger(T->getIndex()); |
1163 | 0 | Hash.AddBoolean(T->isParameterPack()); |
1164 | 0 | AddDecl(T->getDecl()); |
1165 | 0 | } |
1166 | | |
1167 | 0 | void VisitTypedefType(const TypedefType *T) { |
1168 | 0 | AddDecl(T->getDecl()); |
1169 | 0 | VisitType(T); |
1170 | 0 | } |
1171 | | |
1172 | 0 | void VisitTypeOfExprType(const TypeOfExprType *T) { |
1173 | 0 | AddStmt(T->getUnderlyingExpr()); |
1174 | 0 | Hash.AddBoolean(T->isSugared()); |
1175 | |
|
1176 | 0 | VisitType(T); |
1177 | 0 | } |
1178 | 0 | void VisitTypeOfType(const TypeOfType *T) { |
1179 | 0 | AddQualType(T->getUnmodifiedType()); |
1180 | 0 | VisitType(T); |
1181 | 0 | } |
1182 | | |
1183 | 0 | void VisitTypeWithKeyword(const TypeWithKeyword *T) { |
1184 | 0 | ID.AddInteger(llvm::to_underlying(T->getKeyword())); |
1185 | 0 | VisitType(T); |
1186 | 0 | }; |
1187 | | |
1188 | 0 | void VisitDependentNameType(const DependentNameType *T) { |
1189 | 0 | AddNestedNameSpecifier(T->getQualifier()); |
1190 | 0 | AddIdentifierInfo(T->getIdentifier()); |
1191 | 0 | VisitTypeWithKeyword(T); |
1192 | 0 | } |
1193 | | |
1194 | | void VisitDependentTemplateSpecializationType( |
1195 | 0 | const DependentTemplateSpecializationType *T) { |
1196 | 0 | AddIdentifierInfo(T->getIdentifier()); |
1197 | 0 | AddNestedNameSpecifier(T->getQualifier()); |
1198 | 0 | ID.AddInteger(T->template_arguments().size()); |
1199 | 0 | for (const auto &TA : T->template_arguments()) { |
1200 | 0 | Hash.AddTemplateArgument(TA); |
1201 | 0 | } |
1202 | 0 | VisitTypeWithKeyword(T); |
1203 | 0 | } |
1204 | | |
1205 | 0 | void VisitElaboratedType(const ElaboratedType *T) { |
1206 | 0 | AddNestedNameSpecifier(T->getQualifier()); |
1207 | 0 | AddQualType(T->getNamedType()); |
1208 | 0 | VisitTypeWithKeyword(T); |
1209 | 0 | } |
1210 | | |
1211 | 0 | void VisitUnaryTransformType(const UnaryTransformType *T) { |
1212 | 0 | AddQualType(T->getUnderlyingType()); |
1213 | 0 | AddQualType(T->getBaseType()); |
1214 | 0 | VisitType(T); |
1215 | 0 | } |
1216 | | |
1217 | 0 | void VisitUnresolvedUsingType(const UnresolvedUsingType *T) { |
1218 | 0 | AddDecl(T->getDecl()); |
1219 | 0 | VisitType(T); |
1220 | 0 | } |
1221 | | |
1222 | 0 | void VisitVectorType(const VectorType *T) { |
1223 | 0 | AddQualType(T->getElementType()); |
1224 | 0 | ID.AddInteger(T->getNumElements()); |
1225 | 0 | ID.AddInteger(llvm::to_underlying(T->getVectorKind())); |
1226 | 0 | VisitType(T); |
1227 | 0 | } |
1228 | | |
1229 | 0 | void VisitExtVectorType(const ExtVectorType * T) { |
1230 | 0 | VisitVectorType(T); |
1231 | 0 | } |
1232 | | }; |
1233 | | } // namespace |
1234 | | |
1235 | 0 | void ODRHash::AddType(const Type *T) { |
1236 | 0 | assert(T && "Expecting non-null pointer."); |
1237 | 0 | ODRTypeVisitor(ID, *this).Visit(T); |
1238 | 0 | } |
1239 | | |
1240 | 0 | void ODRHash::AddQualType(QualType T) { |
1241 | 0 | AddBoolean(T.isNull()); |
1242 | 0 | if (T.isNull()) |
1243 | 0 | return; |
1244 | 0 | SplitQualType split = T.split(); |
1245 | 0 | ID.AddInteger(split.Quals.getAsOpaqueValue()); |
1246 | 0 | AddType(split.Ty); |
1247 | 0 | } |
1248 | | |
1249 | 0 | void ODRHash::AddBoolean(bool Value) { |
1250 | 0 | Bools.push_back(Value); |
1251 | 0 | } |