blob: a0b870f4dacce85a285ea0baca50af1b6db65160 [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 Trieue7f7ed22017-02-22 01:11:25 +0000172 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
173 ID.AddInteger(D->getAccess());
174 Inherited::VisitAccessSpecDecl(D);
175 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000176
177 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
178 AddStmt(D->getAssertExpr());
179 AddStmt(D->getMessage());
180
181 Inherited::VisitStaticAssertDecl(D);
182 }
Richard Trieud0786092017-02-23 00:23:01 +0000183
184 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000185 const bool IsBitfield = D->isBitField();
186 Hash.AddBoolean(IsBitfield);
187
188 if (IsBitfield) {
189 AddStmt(D->getBitWidth());
190 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000191
192 Hash.AddBoolean(D->isMutable());
193 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000194
195 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000196 }
Richard Trieu48143742017-02-28 21:24:38 +0000197
198 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000199 ID.AddInteger(D->getStorageClass());
200 Hash.AddBoolean(D->isInlineSpecified());
201 Hash.AddBoolean(D->isVirtualAsWritten());
202 Hash.AddBoolean(D->isPure());
203 Hash.AddBoolean(D->isDeletedAsWritten());
204
Richard Trieu48143742017-02-28 21:24:38 +0000205 Inherited::VisitFunctionDecl(D);
206 }
207
208 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000209 Hash.AddBoolean(D->isConst());
210 Hash.AddBoolean(D->isVolatile());
211
Richard Trieu48143742017-02-28 21:24:38 +0000212 Inherited::VisitCXXMethodDecl(D);
213 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000214};
215
216// Only allow a small portion of Decl's to be processed. Remove this once
217// all Decl's can be handled.
218bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
219 if (D->isImplicit()) return false;
220 if (D->getDeclContext() != Parent) return false;
221
222 switch (D->getKind()) {
223 default:
224 return false;
225 case Decl::AccessSpec:
Richard Trieu48143742017-02-28 21:24:38 +0000226 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000227 case Decl::Field:
Richard Trieu639d7b62017-02-22 22:22:42 +0000228 case Decl::StaticAssert:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000229 return true;
230 }
231}
232
233void ODRHash::AddSubDecl(const Decl *D) {
234 assert(D && "Expecting non-null pointer.");
235 AddDecl(D);
236
Richard Trieu639d7b62017-02-22 22:22:42 +0000237 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000238}
239
240void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
241 assert(Record && Record->hasDefinition() &&
242 "Expected non-null record to be a definition.");
243 AddDecl(Record);
244
245 // Filter out sub-Decls which will not be processed in order to get an
246 // accurate count of Decl's.
247 llvm::SmallVector<const Decl *, 16> Decls;
248 for (const Decl *SubDecl : Record->decls()) {
249 if (isWhitelistedDecl(SubDecl, Record)) {
250 Decls.push_back(SubDecl);
251 }
252 }
253
254 ID.AddInteger(Decls.size());
255 for (auto SubDecl : Decls) {
256 AddSubDecl(SubDecl);
257 }
258}
259
260void ODRHash::AddDecl(const Decl *D) {
261 assert(D && "Expecting non-null pointer.");
262 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
263 ID.AddInteger(Result.first->second);
264 // On first encounter of a Decl pointer, process it. Every time afterwards,
265 // only the index value is needed.
266 if (!Result.second) {
267 return;
268 }
269
270 ID.AddInteger(D->getKind());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000271
272 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
273 AddDeclarationName(ND->getDeclName());
274 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000275}
276
Richard Trieubcaaf962017-02-23 03:25:57 +0000277// Process a Type pointer. Add* methods call back into ODRHash while Visit*
278// methods process the relevant parts of the Type.
279class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
280 typedef TypeVisitor<ODRTypeVisitor> Inherited;
281 llvm::FoldingSetNodeID &ID;
282 ODRHash &Hash;
283
284public:
285 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
286 : ID(ID), Hash(Hash) {}
287
288 void AddStmt(Stmt *S) {
289 Hash.AddBoolean(S);
290 if (S) {
291 Hash.AddStmt(S);
292 }
293 }
294
Richard Trieu8459ddf2017-02-24 02:59:12 +0000295 void AddDecl(Decl *D) {
296 Hash.AddBoolean(D);
297 if (D) {
298 Hash.AddDecl(D);
299 }
300 }
301
Richard Trieubcaaf962017-02-23 03:25:57 +0000302 void Visit(const Type *T) {
303 ID.AddInteger(T->getTypeClass());
304 Inherited::Visit(T);
305 }
306
307 void VisitType(const Type *T) {}
308
309 void VisitBuiltinType(const BuiltinType *T) {
310 ID.AddInteger(T->getKind());
311 VisitType(T);
312 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000313
314 void VisitTypedefType(const TypedefType *T) {
315 AddDecl(T->getDecl());
316 VisitType(T);
317 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000318};
319
320void ODRHash::AddType(const Type *T) {
321 assert(T && "Expecting non-null pointer.");
322 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
323 ID.AddInteger(Result.first->second);
324 // On first encounter of a Type pointer, process it. Every time afterwards,
325 // only the index value is needed.
326 if (!Result.second) {
327 return;
328 }
329
330 ODRTypeVisitor(ID, *this).Visit(T);
331}
332
333void ODRHash::AddQualType(QualType T) {
334 AddBoolean(T.isNull());
335 if (T.isNull())
336 return;
337 SplitQualType split = T.split();
338 ID.AddInteger(split.Quals.getAsOpaqueValue());
339 AddType(split.Ty);
340}
341
Richard Trieue7f7ed22017-02-22 01:11:25 +0000342void ODRHash::AddBoolean(bool Value) {
343 Bools.push_back(Value);
344}