blob: 0c42acd121842cb4569d97d344a2b60cab0df62a [file] [log] [blame]
Richard Trieue7f7ed22017-02-22 01:11:25 +00001//===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// This file implements the ODRHash class, which calculates a hash based
12/// on AST nodes, which is stable across different runs.
13///
14//===----------------------------------------------------------------------===//
15
16#include "clang/AST/ODRHash.h"
17
18#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/NestedNameSpecifier.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/TypeVisitor.h"
22
23using namespace clang;
24
Richard Trieu639d7b62017-02-22 22:22:42 +000025void ODRHash::AddStmt(const Stmt *S) {
26 assert(S && "Expecting non-null pointer.");
27 S->ProcessODRHash(ID, *this);
28}
Richard Trieud0786092017-02-23 00:23:01 +000029
30void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
31 assert(II && "Expecting non-null pointer.");
32 ID.AddString(II->getName());
33}
34
Richard Trieu8459ddf2017-02-24 02:59:12 +000035void ODRHash::AddDeclarationName(DeclarationName Name) {
36 AddBoolean(Name.isEmpty());
37 if (Name.isEmpty())
38 return;
39
40 auto Kind = Name.getNameKind();
41 ID.AddInteger(Kind);
42 switch (Kind) {
43 case DeclarationName::Identifier:
44 AddIdentifierInfo(Name.getAsIdentifierInfo());
45 break;
46 case DeclarationName::ObjCZeroArgSelector:
47 case DeclarationName::ObjCOneArgSelector:
48 case DeclarationName::ObjCMultiArgSelector: {
49 Selector S = Name.getObjCSelector();
50 AddBoolean(S.isNull());
51 AddBoolean(S.isKeywordSelector());
52 AddBoolean(S.isUnarySelector());
53 unsigned NumArgs = S.getNumArgs();
54 for (unsigned i = 0; i < NumArgs; ++i) {
55 AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
56 }
57 break;
58 }
59 case DeclarationName::CXXConstructorName:
60 case DeclarationName::CXXDestructorName:
61 AddQualType(Name.getCXXNameType());
62 break;
63 case DeclarationName::CXXOperatorName:
64 ID.AddInteger(Name.getCXXOverloadedOperator());
65 break;
66 case DeclarationName::CXXLiteralOperatorName:
67 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
68 break;
69 case DeclarationName::CXXConversionFunctionName:
70 AddQualType(Name.getCXXNameType());
71 break;
72 case DeclarationName::CXXUsingDirective:
73 break;
74 case DeclarationName::CXXDeductionGuideName: {
75 auto *Template = Name.getCXXDeductionGuideTemplate();
76 AddBoolean(Template);
77 if (Template) {
78 AddDecl(Template);
79 }
80 }
81 }
82}
83
Richard Trieue7f7ed22017-02-22 01:11:25 +000084void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {}
85void ODRHash::AddTemplateName(TemplateName Name) {}
Richard Trieue7f7ed22017-02-22 01:11:25 +000086void ODRHash::AddTemplateArgument(TemplateArgument TA) {}
87void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
88
89void ODRHash::clear() {
90 DeclMap.clear();
91 TypeMap.clear();
92 Bools.clear();
93 ID.clear();
94}
95
96unsigned ODRHash::CalculateHash() {
97 // Append the bools to the end of the data segment backwards. This allows
98 // for the bools data to be compressed 32 times smaller compared to using
99 // ID.AddBoolean
100 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
101 const unsigned size = Bools.size();
102 const unsigned remainder = size % unsigned_bits;
103 const unsigned loops = size / unsigned_bits;
104 auto I = Bools.rbegin();
105 unsigned value = 0;
106 for (unsigned i = 0; i < remainder; ++i) {
107 value <<= 1;
108 value |= *I;
109 ++I;
110 }
111 ID.AddInteger(value);
112
113 for (unsigned i = 0; i < loops; ++i) {
114 value = 0;
115 for (unsigned j = 0; j < unsigned_bits; ++j) {
116 value <<= 1;
117 value |= *I;
118 ++I;
119 }
120 ID.AddInteger(value);
121 }
122
123 assert(I == Bools.rend());
124 Bools.clear();
125 return ID.ComputeHash();
126}
127
128// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
129// methods process the relevant parts of the Decl.
130class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
131 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
132 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000133 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000134
135public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000136 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
137 : ID(ID), Hash(Hash) {}
138
139 void AddStmt(const Stmt *S) {
140 Hash.AddBoolean(S);
141 if (S) {
142 Hash.AddStmt(S);
143 }
144 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000145
Richard Trieud0786092017-02-23 00:23:01 +0000146 void AddIdentifierInfo(const IdentifierInfo *II) {
147 Hash.AddBoolean(II);
148 if (II) {
149 Hash.AddIdentifierInfo(II);
150 }
151 }
152
Richard Trieubcaaf962017-02-23 03:25:57 +0000153 void AddQualType(QualType T) {
154 Hash.AddQualType(T);
155 }
156
Richard Trieue7f7ed22017-02-22 01:11:25 +0000157 void Visit(const Decl *D) {
158 ID.AddInteger(D->getKind());
159 Inherited::Visit(D);
160 }
161
Richard Trieud0786092017-02-23 00:23:01 +0000162 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000163 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000164 Inherited::VisitNamedDecl(D);
165 }
166
Richard Trieubcaaf962017-02-23 03:25:57 +0000167 void VisitValueDecl(const ValueDecl *D) {
168 AddQualType(D->getType());
169 Inherited::VisitValueDecl(D);
170 }
171
Richard Trieu02552272017-05-02 23:58:52 +0000172 void VisitParmVarDecl(const ParmVarDecl *D) {
173 // TODO: Handle default arguments.
174 Inherited::VisitParmVarDecl(D);
175 }
176
Richard Trieue7f7ed22017-02-22 01:11:25 +0000177 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
178 ID.AddInteger(D->getAccess());
179 Inherited::VisitAccessSpecDecl(D);
180 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000181
182 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
183 AddStmt(D->getAssertExpr());
184 AddStmt(D->getMessage());
185
186 Inherited::VisitStaticAssertDecl(D);
187 }
Richard Trieud0786092017-02-23 00:23:01 +0000188
189 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000190 const bool IsBitfield = D->isBitField();
191 Hash.AddBoolean(IsBitfield);
192
193 if (IsBitfield) {
194 AddStmt(D->getBitWidth());
195 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000196
197 Hash.AddBoolean(D->isMutable());
198 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000199
200 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000201 }
Richard Trieu48143742017-02-28 21:24:38 +0000202
203 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000204 ID.AddInteger(D->getStorageClass());
205 Hash.AddBoolean(D->isInlineSpecified());
206 Hash.AddBoolean(D->isVirtualAsWritten());
207 Hash.AddBoolean(D->isPure());
208 Hash.AddBoolean(D->isDeletedAsWritten());
209
Richard Trieu02552272017-05-02 23:58:52 +0000210 ID.AddInteger(D->param_size());
211
212 for (auto *Param : D->parameters()) {
213 Hash.AddSubDecl(Param);
214 }
215
Richard Trieu48143742017-02-28 21:24:38 +0000216 Inherited::VisitFunctionDecl(D);
217 }
218
219 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000220 Hash.AddBoolean(D->isConst());
221 Hash.AddBoolean(D->isVolatile());
222
Richard Trieu48143742017-02-28 21:24:38 +0000223 Inherited::VisitCXXMethodDecl(D);
224 }
Richard Trieu33562c22017-03-08 00:13:19 +0000225
226 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
227 AddQualType(D->getUnderlyingType());
228
229 Inherited::VisitTypedefNameDecl(D);
230 }
231
232 void VisitTypedefDecl(const TypedefDecl *D) {
233 Inherited::VisitTypedefDecl(D);
234 }
235
236 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
237 Inherited::VisitTypeAliasDecl(D);
238 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000239};
240
241// Only allow a small portion of Decl's to be processed. Remove this once
242// all Decl's can be handled.
243bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
244 if (D->isImplicit()) return false;
245 if (D->getDeclContext() != Parent) return false;
246
247 switch (D->getKind()) {
248 default:
249 return false;
250 case Decl::AccessSpec:
Richard Trieu48143742017-02-28 21:24:38 +0000251 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000252 case Decl::Field:
Richard Trieu639d7b62017-02-22 22:22:42 +0000253 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000254 case Decl::TypeAlias:
255 case Decl::Typedef:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000256 return true;
257 }
258}
259
260void ODRHash::AddSubDecl(const Decl *D) {
261 assert(D && "Expecting non-null pointer.");
262 AddDecl(D);
263
Richard Trieu639d7b62017-02-22 22:22:42 +0000264 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000265}
266
267void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
268 assert(Record && Record->hasDefinition() &&
269 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000270
271 if (isa<ClassTemplateSpecializationDecl>(Record)) {
272 return;
273 }
274
Richard Trieue7f7ed22017-02-22 01:11:25 +0000275 AddDecl(Record);
276
277 // Filter out sub-Decls which will not be processed in order to get an
278 // accurate count of Decl's.
279 llvm::SmallVector<const Decl *, 16> Decls;
280 for (const Decl *SubDecl : Record->decls()) {
281 if (isWhitelistedDecl(SubDecl, Record)) {
282 Decls.push_back(SubDecl);
283 }
284 }
285
286 ID.AddInteger(Decls.size());
287 for (auto SubDecl : Decls) {
288 AddSubDecl(SubDecl);
289 }
290}
291
292void ODRHash::AddDecl(const Decl *D) {
293 assert(D && "Expecting non-null pointer.");
294 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
295 ID.AddInteger(Result.first->second);
296 // On first encounter of a Decl pointer, process it. Every time afterwards,
297 // only the index value is needed.
298 if (!Result.second) {
299 return;
300 }
301
302 ID.AddInteger(D->getKind());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000303
304 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
305 AddDeclarationName(ND->getDeclName());
306 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000307}
308
Richard Trieubcaaf962017-02-23 03:25:57 +0000309// Process a Type pointer. Add* methods call back into ODRHash while Visit*
310// methods process the relevant parts of the Type.
311class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
312 typedef TypeVisitor<ODRTypeVisitor> Inherited;
313 llvm::FoldingSetNodeID &ID;
314 ODRHash &Hash;
315
316public:
317 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
318 : ID(ID), Hash(Hash) {}
319
320 void AddStmt(Stmt *S) {
321 Hash.AddBoolean(S);
322 if (S) {
323 Hash.AddStmt(S);
324 }
325 }
326
Richard Trieu8459ddf2017-02-24 02:59:12 +0000327 void AddDecl(Decl *D) {
328 Hash.AddBoolean(D);
329 if (D) {
330 Hash.AddDecl(D);
331 }
332 }
333
Richard Trieu02552272017-05-02 23:58:52 +0000334 void AddQualType(QualType T) {
335 Hash.AddQualType(T);
336 }
337
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000338 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
339 Hash.AddBoolean(NNS);
340 if (NNS) {
341 Hash.AddNestedNameSpecifier(NNS);
342 }
343 }
344
345 void AddIdentifierInfo(const IdentifierInfo *II) {
346 Hash.AddBoolean(II);
347 if (II) {
348 Hash.AddIdentifierInfo(II);
349 }
350 }
351
Richard Trieu02552272017-05-02 23:58:52 +0000352 void VisitQualifiers(Qualifiers Quals) {
353 ID.AddInteger(Quals.getAsOpaqueValue());
354 }
355
Richard Trieubcaaf962017-02-23 03:25:57 +0000356 void Visit(const Type *T) {
357 ID.AddInteger(T->getTypeClass());
358 Inherited::Visit(T);
359 }
360
361 void VisitType(const Type *T) {}
362
Richard Trieu02552272017-05-02 23:58:52 +0000363 void VisitAdjustedType(const AdjustedType *T) {
364 AddQualType(T->getOriginalType());
365 AddQualType(T->getAdjustedType());
366 VisitType(T);
367 }
368
369 void VisitDecayedType(const DecayedType *T) {
370 AddQualType(T->getDecayedType());
371 AddQualType(T->getPointeeType());
372 VisitAdjustedType(T);
373 }
374
375 void VisitArrayType(const ArrayType *T) {
376 AddQualType(T->getElementType());
377 ID.AddInteger(T->getSizeModifier());
378 VisitQualifiers(T->getIndexTypeQualifiers());
379 VisitType(T);
380 }
381 void VisitConstantArrayType(const ConstantArrayType *T) {
382 T->getSize().Profile(ID);
383 VisitArrayType(T);
384 }
385
386 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
387 AddStmt(T->getSizeExpr());
388 VisitArrayType(T);
389 }
390
391 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
392 VisitArrayType(T);
393 }
394
395 void VisitVariableArrayType(const VariableArrayType *T) {
396 AddStmt(T->getSizeExpr());
397 VisitArrayType(T);
398 }
399
Richard Trieubcaaf962017-02-23 03:25:57 +0000400 void VisitBuiltinType(const BuiltinType *T) {
401 ID.AddInteger(T->getKind());
402 VisitType(T);
403 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000404
Richard Trieu02552272017-05-02 23:58:52 +0000405 void VisitFunctionType(const FunctionType *T) {
406 AddQualType(T->getReturnType());
407 T->getExtInfo().Profile(ID);
408 Hash.AddBoolean(T->isConst());
409 Hash.AddBoolean(T->isVolatile());
410 Hash.AddBoolean(T->isRestrict());
411 VisitType(T);
412 }
413
414 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
415 VisitFunctionType(T);
416 }
417
418 void VisitFunctionProtoType(const FunctionProtoType *T) {
419 ID.AddInteger(T->getNumParams());
420 for (auto ParamType : T->getParamTypes())
421 AddQualType(ParamType);
422
423 VisitFunctionType(T);
424 }
425
Richard Trieu8459ddf2017-02-24 02:59:12 +0000426 void VisitTypedefType(const TypedefType *T) {
427 AddDecl(T->getDecl());
Richard Trieub35ef2a2017-05-09 03:24:34 +0000428 AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000429 VisitType(T);
430 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000431
432 void VisitTagType(const TagType *T) {
433 AddDecl(T->getDecl());
434 VisitType(T);
435 }
436
437 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
438 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
439
440 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
441 ID.AddInteger(T->getKeyword());
442 VisitType(T);
443 };
444
445 void VisitDependentNameType(const DependentNameType *T) {
446 AddNestedNameSpecifier(T->getQualifier());
447 AddIdentifierInfo(T->getIdentifier());
448 VisitTypeWithKeyword(T);
449 }
450
451 void VisitDependentTemplateSpecializationType(
452 const DependentTemplateSpecializationType *T) {
453 AddIdentifierInfo(T->getIdentifier());
454 AddNestedNameSpecifier(T->getQualifier());
455 ID.AddInteger(T->getNumArgs());
456 for (const auto &TA : T->template_arguments()) {
457 Hash.AddTemplateArgument(TA);
458 }
459 VisitTypeWithKeyword(T);
460 }
461
462 void VisitElaboratedType(const ElaboratedType *T) {
463 AddNestedNameSpecifier(T->getQualifier());
464 AddQualType(T->getNamedType());
465 VisitTypeWithKeyword(T);
466 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000467};
468
469void ODRHash::AddType(const Type *T) {
470 assert(T && "Expecting non-null pointer.");
471 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
472 ID.AddInteger(Result.first->second);
473 // On first encounter of a Type pointer, process it. Every time afterwards,
474 // only the index value is needed.
475 if (!Result.second) {
476 return;
477 }
478
479 ODRTypeVisitor(ID, *this).Visit(T);
480}
481
482void ODRHash::AddQualType(QualType T) {
483 AddBoolean(T.isNull());
484 if (T.isNull())
485 return;
486 SplitQualType split = T.split();
487 ID.AddInteger(split.Quals.getAsOpaqueValue());
488 AddType(split.Ty);
489}
490
Richard Trieue7f7ed22017-02-22 01:11:25 +0000491void ODRHash::AddBoolean(bool Value) {
492 Bools.push_back(Value);
493}