Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 1 | //===-- 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 | |
| 23 | using namespace clang; |
| 24 | |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame] | 25 | void ODRHash::AddStmt(const Stmt *S) { |
| 26 | assert(S && "Expecting non-null pointer."); |
| 27 | S->ProcessODRHash(ID, *this); |
| 28 | } |
Richard Trieu | d078609 | 2017-02-23 00:23:01 +0000 | [diff] [blame] | 29 | |
| 30 | void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) { |
| 31 | assert(II && "Expecting non-null pointer."); |
| 32 | ID.AddString(II->getName()); |
| 33 | } |
| 34 | |
Richard Trieu | 8459ddf | 2017-02-24 02:59:12 +0000 | [diff] [blame] | 35 | void 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 Trieu | ce81b19 | 2017-05-17 03:23:35 +0000 | [diff] [blame] | 84 | void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) { |
Richard Trieu | f21b803 | 2017-06-09 20:11:51 +0000 | [diff] [blame] | 85 | // Unlike the other pointer handling functions, allow null pointers here. |
| 86 | if (!NNS) { |
| 87 | AddBoolean(false); |
| 88 | return; |
Richard Trieu | ce81b19 | 2017-05-17 03:23:35 +0000 | [diff] [blame] | 89 | } |
Richard Trieu | f21b803 | 2017-06-09 20:11:51 +0000 | [diff] [blame] | 90 | |
| 91 | // Skip inlined namespaces. |
Richard Trieu | ce81b19 | 2017-05-17 03:23:35 +0000 | [diff] [blame] | 92 | auto Kind = NNS->getKind(); |
Richard Trieu | f21b803 | 2017-06-09 20:11:51 +0000 | [diff] [blame] | 93 | if (Kind == NestedNameSpecifier::Namespace) { |
| 94 | if (NNS->getAsNamespace()->isInline()) { |
| 95 | return AddNestedNameSpecifier(NNS->getPrefix()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | AddBoolean(true); |
| 100 | |
| 101 | // Process prefix |
| 102 | AddNestedNameSpecifier(NNS->getPrefix()); |
| 103 | |
Richard Trieu | ce81b19 | 2017-05-17 03:23:35 +0000 | [diff] [blame] | 104 | ID.AddInteger(Kind); |
| 105 | switch (Kind) { |
| 106 | case NestedNameSpecifier::Identifier: |
| 107 | AddIdentifierInfo(NNS->getAsIdentifier()); |
| 108 | break; |
| 109 | case NestedNameSpecifier::Namespace: |
| 110 | AddDecl(NNS->getAsNamespace()); |
| 111 | break; |
| 112 | case NestedNameSpecifier::NamespaceAlias: |
| 113 | AddDecl(NNS->getAsNamespaceAlias()); |
| 114 | break; |
| 115 | case NestedNameSpecifier::TypeSpec: |
| 116 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 117 | AddType(NNS->getAsType()); |
| 118 | break; |
| 119 | case NestedNameSpecifier::Global: |
| 120 | case NestedNameSpecifier::Super: |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
Richard Trieu | 96b4962 | 2017-05-31 00:31:58 +0000 | [diff] [blame] | 125 | void ODRHash::AddTemplateName(TemplateName Name) { |
| 126 | auto Kind = Name.getKind(); |
| 127 | ID.AddInteger(Kind); |
| 128 | |
| 129 | switch (Kind) { |
| 130 | case TemplateName::Template: |
| 131 | AddDecl(Name.getAsTemplateDecl()); |
| 132 | break; |
| 133 | // TODO: Support these cases. |
| 134 | case TemplateName::OverloadedTemplate: |
| 135 | case TemplateName::QualifiedTemplate: |
| 136 | case TemplateName::DependentTemplate: |
| 137 | case TemplateName::SubstTemplateTemplateParm: |
| 138 | case TemplateName::SubstTemplateTemplateParmPack: |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
Richard Trieu | 3b261bb7 | 2017-06-13 22:21:18 +0000 | [diff] [blame] | 143 | void ODRHash::AddTemplateArgument(TemplateArgument TA) { |
| 144 | const auto Kind = TA.getKind(); |
| 145 | ID.AddInteger(Kind); |
| 146 | } |
| 147 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 148 | void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {} |
| 149 | |
| 150 | void ODRHash::clear() { |
| 151 | DeclMap.clear(); |
| 152 | TypeMap.clear(); |
| 153 | Bools.clear(); |
| 154 | ID.clear(); |
| 155 | } |
| 156 | |
| 157 | unsigned ODRHash::CalculateHash() { |
| 158 | // Append the bools to the end of the data segment backwards. This allows |
| 159 | // for the bools data to be compressed 32 times smaller compared to using |
| 160 | // ID.AddBoolean |
| 161 | const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT; |
| 162 | const unsigned size = Bools.size(); |
| 163 | const unsigned remainder = size % unsigned_bits; |
| 164 | const unsigned loops = size / unsigned_bits; |
| 165 | auto I = Bools.rbegin(); |
| 166 | unsigned value = 0; |
| 167 | for (unsigned i = 0; i < remainder; ++i) { |
| 168 | value <<= 1; |
| 169 | value |= *I; |
| 170 | ++I; |
| 171 | } |
| 172 | ID.AddInteger(value); |
| 173 | |
| 174 | for (unsigned i = 0; i < loops; ++i) { |
| 175 | value = 0; |
| 176 | for (unsigned j = 0; j < unsigned_bits; ++j) { |
| 177 | value <<= 1; |
| 178 | value |= *I; |
| 179 | ++I; |
| 180 | } |
| 181 | ID.AddInteger(value); |
| 182 | } |
| 183 | |
| 184 | assert(I == Bools.rend()); |
| 185 | Bools.clear(); |
| 186 | return ID.ComputeHash(); |
| 187 | } |
| 188 | |
| 189 | // Process a Decl pointer. Add* methods call back into ODRHash while Visit* |
| 190 | // methods process the relevant parts of the Decl. |
| 191 | class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> { |
| 192 | typedef ConstDeclVisitor<ODRDeclVisitor> Inherited; |
| 193 | llvm::FoldingSetNodeID &ID; |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame] | 194 | ODRHash &Hash; |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 195 | |
| 196 | public: |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame] | 197 | ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash) |
| 198 | : ID(ID), Hash(Hash) {} |
| 199 | |
| 200 | void AddStmt(const Stmt *S) { |
| 201 | Hash.AddBoolean(S); |
| 202 | if (S) { |
| 203 | Hash.AddStmt(S); |
| 204 | } |
| 205 | } |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 206 | |
Richard Trieu | d078609 | 2017-02-23 00:23:01 +0000 | [diff] [blame] | 207 | void AddIdentifierInfo(const IdentifierInfo *II) { |
| 208 | Hash.AddBoolean(II); |
| 209 | if (II) { |
| 210 | Hash.AddIdentifierInfo(II); |
| 211 | } |
| 212 | } |
| 213 | |
Richard Trieu | bcaaf96 | 2017-02-23 03:25:57 +0000 | [diff] [blame] | 214 | void AddQualType(QualType T) { |
| 215 | Hash.AddQualType(T); |
| 216 | } |
| 217 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 218 | void Visit(const Decl *D) { |
| 219 | ID.AddInteger(D->getKind()); |
| 220 | Inherited::Visit(D); |
| 221 | } |
| 222 | |
Richard Trieu | d078609 | 2017-02-23 00:23:01 +0000 | [diff] [blame] | 223 | void VisitNamedDecl(const NamedDecl *D) { |
Richard Trieu | 583e2c1 | 2017-03-04 00:08:58 +0000 | [diff] [blame] | 224 | Hash.AddDeclarationName(D->getDeclName()); |
Richard Trieu | d078609 | 2017-02-23 00:23:01 +0000 | [diff] [blame] | 225 | Inherited::VisitNamedDecl(D); |
| 226 | } |
| 227 | |
Richard Trieu | bcaaf96 | 2017-02-23 03:25:57 +0000 | [diff] [blame] | 228 | void VisitValueDecl(const ValueDecl *D) { |
| 229 | AddQualType(D->getType()); |
| 230 | Inherited::VisitValueDecl(D); |
| 231 | } |
| 232 | |
Richard Trieu | 0255227 | 2017-05-02 23:58:52 +0000 | [diff] [blame] | 233 | void VisitParmVarDecl(const ParmVarDecl *D) { |
| 234 | // TODO: Handle default arguments. |
| 235 | Inherited::VisitParmVarDecl(D); |
| 236 | } |
| 237 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 238 | void VisitAccessSpecDecl(const AccessSpecDecl *D) { |
| 239 | ID.AddInteger(D->getAccess()); |
| 240 | Inherited::VisitAccessSpecDecl(D); |
| 241 | } |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame] | 242 | |
| 243 | void VisitStaticAssertDecl(const StaticAssertDecl *D) { |
| 244 | AddStmt(D->getAssertExpr()); |
| 245 | AddStmt(D->getMessage()); |
| 246 | |
| 247 | Inherited::VisitStaticAssertDecl(D); |
| 248 | } |
Richard Trieu | d078609 | 2017-02-23 00:23:01 +0000 | [diff] [blame] | 249 | |
| 250 | void VisitFieldDecl(const FieldDecl *D) { |
Richard Trieu | 93772fc | 2017-02-24 20:59:28 +0000 | [diff] [blame] | 251 | const bool IsBitfield = D->isBitField(); |
| 252 | Hash.AddBoolean(IsBitfield); |
| 253 | |
| 254 | if (IsBitfield) { |
| 255 | AddStmt(D->getBitWidth()); |
| 256 | } |
Richard Trieu | 8d543e2 | 2017-02-24 23:35:37 +0000 | [diff] [blame] | 257 | |
| 258 | Hash.AddBoolean(D->isMutable()); |
| 259 | AddStmt(D->getInClassInitializer()); |
Richard Trieu | ff60e0f | 2017-02-25 01:29:34 +0000 | [diff] [blame] | 260 | |
| 261 | Inherited::VisitFieldDecl(D); |
Richard Trieu | d078609 | 2017-02-23 00:23:01 +0000 | [diff] [blame] | 262 | } |
Richard Trieu | 4814374 | 2017-02-28 21:24:38 +0000 | [diff] [blame] | 263 | |
| 264 | void VisitFunctionDecl(const FunctionDecl *D) { |
Richard Trieu | 583e2c1 | 2017-03-04 00:08:58 +0000 | [diff] [blame] | 265 | ID.AddInteger(D->getStorageClass()); |
| 266 | Hash.AddBoolean(D->isInlineSpecified()); |
| 267 | Hash.AddBoolean(D->isVirtualAsWritten()); |
| 268 | Hash.AddBoolean(D->isPure()); |
| 269 | Hash.AddBoolean(D->isDeletedAsWritten()); |
| 270 | |
Richard Trieu | 0255227 | 2017-05-02 23:58:52 +0000 | [diff] [blame] | 271 | ID.AddInteger(D->param_size()); |
| 272 | |
| 273 | for (auto *Param : D->parameters()) { |
| 274 | Hash.AddSubDecl(Param); |
| 275 | } |
| 276 | |
Richard Trieu | 4814374 | 2017-02-28 21:24:38 +0000 | [diff] [blame] | 277 | Inherited::VisitFunctionDecl(D); |
| 278 | } |
| 279 | |
| 280 | void VisitCXXMethodDecl(const CXXMethodDecl *D) { |
Richard Trieu | 583e2c1 | 2017-03-04 00:08:58 +0000 | [diff] [blame] | 281 | Hash.AddBoolean(D->isConst()); |
| 282 | Hash.AddBoolean(D->isVolatile()); |
| 283 | |
Richard Trieu | 4814374 | 2017-02-28 21:24:38 +0000 | [diff] [blame] | 284 | Inherited::VisitCXXMethodDecl(D); |
| 285 | } |
Richard Trieu | 33562c2 | 2017-03-08 00:13:19 +0000 | [diff] [blame] | 286 | |
| 287 | void VisitTypedefNameDecl(const TypedefNameDecl *D) { |
| 288 | AddQualType(D->getUnderlyingType()); |
| 289 | |
| 290 | Inherited::VisitTypedefNameDecl(D); |
| 291 | } |
| 292 | |
| 293 | void VisitTypedefDecl(const TypedefDecl *D) { |
| 294 | Inherited::VisitTypedefDecl(D); |
| 295 | } |
| 296 | |
| 297 | void VisitTypeAliasDecl(const TypeAliasDecl *D) { |
| 298 | Inherited::VisitTypeAliasDecl(D); |
| 299 | } |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | // Only allow a small portion of Decl's to be processed. Remove this once |
| 303 | // all Decl's can be handled. |
| 304 | bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) { |
| 305 | if (D->isImplicit()) return false; |
| 306 | if (D->getDeclContext() != Parent) return false; |
| 307 | |
| 308 | switch (D->getKind()) { |
| 309 | default: |
| 310 | return false; |
| 311 | case Decl::AccessSpec: |
Richard Trieu | 4814374 | 2017-02-28 21:24:38 +0000 | [diff] [blame] | 312 | case Decl::CXXMethod: |
Richard Trieu | d078609 | 2017-02-23 00:23:01 +0000 | [diff] [blame] | 313 | case Decl::Field: |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame] | 314 | case Decl::StaticAssert: |
Richard Trieu | 33562c2 | 2017-03-08 00:13:19 +0000 | [diff] [blame] | 315 | case Decl::TypeAlias: |
| 316 | case Decl::Typedef: |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void ODRHash::AddSubDecl(const Decl *D) { |
| 322 | assert(D && "Expecting non-null pointer."); |
| 323 | AddDecl(D); |
| 324 | |
Richard Trieu | 639d7b6 | 2017-02-22 22:22:42 +0000 | [diff] [blame] | 325 | ODRDeclVisitor(ID, *this).Visit(D); |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) { |
| 329 | assert(Record && Record->hasDefinition() && |
| 330 | "Expected non-null record to be a definition."); |
Richard Trieu | 0255227 | 2017-05-02 23:58:52 +0000 | [diff] [blame] | 331 | |
| 332 | if (isa<ClassTemplateSpecializationDecl>(Record)) { |
| 333 | return; |
| 334 | } |
| 335 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 336 | AddDecl(Record); |
| 337 | |
| 338 | // Filter out sub-Decls which will not be processed in order to get an |
| 339 | // accurate count of Decl's. |
| 340 | llvm::SmallVector<const Decl *, 16> Decls; |
| 341 | for (const Decl *SubDecl : Record->decls()) { |
| 342 | if (isWhitelistedDecl(SubDecl, Record)) { |
| 343 | Decls.push_back(SubDecl); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | ID.AddInteger(Decls.size()); |
| 348 | for (auto SubDecl : Decls) { |
| 349 | AddSubDecl(SubDecl); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void ODRHash::AddDecl(const Decl *D) { |
| 354 | assert(D && "Expecting non-null pointer."); |
| 355 | auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size())); |
| 356 | ID.AddInteger(Result.first->second); |
| 357 | // On first encounter of a Decl pointer, process it. Every time afterwards, |
| 358 | // only the index value is needed. |
| 359 | if (!Result.second) { |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | ID.AddInteger(D->getKind()); |
Richard Trieu | 8459ddf | 2017-02-24 02:59:12 +0000 | [diff] [blame] | 364 | |
| 365 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 366 | AddDeclarationName(ND->getDeclName()); |
| 367 | } |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Richard Trieu | bcaaf96 | 2017-02-23 03:25:57 +0000 | [diff] [blame] | 370 | // Process a Type pointer. Add* methods call back into ODRHash while Visit* |
| 371 | // methods process the relevant parts of the Type. |
| 372 | class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> { |
| 373 | typedef TypeVisitor<ODRTypeVisitor> Inherited; |
| 374 | llvm::FoldingSetNodeID &ID; |
| 375 | ODRHash &Hash; |
| 376 | |
| 377 | public: |
| 378 | ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash) |
| 379 | : ID(ID), Hash(Hash) {} |
| 380 | |
| 381 | void AddStmt(Stmt *S) { |
| 382 | Hash.AddBoolean(S); |
| 383 | if (S) { |
| 384 | Hash.AddStmt(S); |
| 385 | } |
| 386 | } |
| 387 | |
Richard Trieu | 8459ddf | 2017-02-24 02:59:12 +0000 | [diff] [blame] | 388 | void AddDecl(Decl *D) { |
| 389 | Hash.AddBoolean(D); |
| 390 | if (D) { |
| 391 | Hash.AddDecl(D); |
| 392 | } |
| 393 | } |
| 394 | |
Richard Trieu | 0255227 | 2017-05-02 23:58:52 +0000 | [diff] [blame] | 395 | void AddQualType(QualType T) { |
| 396 | Hash.AddQualType(T); |
| 397 | } |
| 398 | |
Richard Trieu | 58bb7bd | 2017-05-17 02:29:02 +0000 | [diff] [blame] | 399 | void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) { |
Richard Trieu | f21b803 | 2017-06-09 20:11:51 +0000 | [diff] [blame] | 400 | Hash.AddNestedNameSpecifier(NNS); |
Richard Trieu | 58bb7bd | 2017-05-17 02:29:02 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void AddIdentifierInfo(const IdentifierInfo *II) { |
| 404 | Hash.AddBoolean(II); |
| 405 | if (II) { |
| 406 | Hash.AddIdentifierInfo(II); |
| 407 | } |
| 408 | } |
| 409 | |
Richard Trieu | 0255227 | 2017-05-02 23:58:52 +0000 | [diff] [blame] | 410 | void VisitQualifiers(Qualifiers Quals) { |
| 411 | ID.AddInteger(Quals.getAsOpaqueValue()); |
| 412 | } |
| 413 | |
Richard Trieu | bcaaf96 | 2017-02-23 03:25:57 +0000 | [diff] [blame] | 414 | void Visit(const Type *T) { |
| 415 | ID.AddInteger(T->getTypeClass()); |
| 416 | Inherited::Visit(T); |
| 417 | } |
| 418 | |
| 419 | void VisitType(const Type *T) {} |
| 420 | |
Richard Trieu | 0255227 | 2017-05-02 23:58:52 +0000 | [diff] [blame] | 421 | void VisitAdjustedType(const AdjustedType *T) { |
| 422 | AddQualType(T->getOriginalType()); |
| 423 | AddQualType(T->getAdjustedType()); |
| 424 | VisitType(T); |
| 425 | } |
| 426 | |
| 427 | void VisitDecayedType(const DecayedType *T) { |
| 428 | AddQualType(T->getDecayedType()); |
| 429 | AddQualType(T->getPointeeType()); |
| 430 | VisitAdjustedType(T); |
| 431 | } |
| 432 | |
| 433 | void VisitArrayType(const ArrayType *T) { |
| 434 | AddQualType(T->getElementType()); |
| 435 | ID.AddInteger(T->getSizeModifier()); |
| 436 | VisitQualifiers(T->getIndexTypeQualifiers()); |
| 437 | VisitType(T); |
| 438 | } |
| 439 | void VisitConstantArrayType(const ConstantArrayType *T) { |
| 440 | T->getSize().Profile(ID); |
| 441 | VisitArrayType(T); |
| 442 | } |
| 443 | |
| 444 | void VisitDependentSizedArrayType(const DependentSizedArrayType *T) { |
| 445 | AddStmt(T->getSizeExpr()); |
| 446 | VisitArrayType(T); |
| 447 | } |
| 448 | |
| 449 | void VisitIncompleteArrayType(const IncompleteArrayType *T) { |
| 450 | VisitArrayType(T); |
| 451 | } |
| 452 | |
| 453 | void VisitVariableArrayType(const VariableArrayType *T) { |
| 454 | AddStmt(T->getSizeExpr()); |
| 455 | VisitArrayType(T); |
| 456 | } |
| 457 | |
Richard Trieu | bcaaf96 | 2017-02-23 03:25:57 +0000 | [diff] [blame] | 458 | void VisitBuiltinType(const BuiltinType *T) { |
| 459 | ID.AddInteger(T->getKind()); |
| 460 | VisitType(T); |
| 461 | } |
Richard Trieu | 8459ddf | 2017-02-24 02:59:12 +0000 | [diff] [blame] | 462 | |
Richard Trieu | 0255227 | 2017-05-02 23:58:52 +0000 | [diff] [blame] | 463 | void VisitFunctionType(const FunctionType *T) { |
| 464 | AddQualType(T->getReturnType()); |
| 465 | T->getExtInfo().Profile(ID); |
| 466 | Hash.AddBoolean(T->isConst()); |
| 467 | Hash.AddBoolean(T->isVolatile()); |
| 468 | Hash.AddBoolean(T->isRestrict()); |
| 469 | VisitType(T); |
| 470 | } |
| 471 | |
| 472 | void VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
| 473 | VisitFunctionType(T); |
| 474 | } |
| 475 | |
| 476 | void VisitFunctionProtoType(const FunctionProtoType *T) { |
| 477 | ID.AddInteger(T->getNumParams()); |
| 478 | for (auto ParamType : T->getParamTypes()) |
| 479 | AddQualType(ParamType); |
| 480 | |
| 481 | VisitFunctionType(T); |
| 482 | } |
| 483 | |
Richard Trieu | 8459ddf | 2017-02-24 02:59:12 +0000 | [diff] [blame] | 484 | void VisitTypedefType(const TypedefType *T) { |
| 485 | AddDecl(T->getDecl()); |
Richard Trieu | b35ef2a | 2017-05-09 03:24:34 +0000 | [diff] [blame] | 486 | AddQualType(T->getDecl()->getUnderlyingType().getCanonicalType()); |
Richard Trieu | 8459ddf | 2017-02-24 02:59:12 +0000 | [diff] [blame] | 487 | VisitType(T); |
| 488 | } |
Richard Trieu | 58bb7bd | 2017-05-17 02:29:02 +0000 | [diff] [blame] | 489 | |
| 490 | void VisitTagType(const TagType *T) { |
| 491 | AddDecl(T->getDecl()); |
| 492 | VisitType(T); |
| 493 | } |
| 494 | |
| 495 | void VisitRecordType(const RecordType *T) { VisitTagType(T); } |
| 496 | void VisitEnumType(const EnumType *T) { VisitTagType(T); } |
| 497 | |
| 498 | void VisitTypeWithKeyword(const TypeWithKeyword *T) { |
| 499 | ID.AddInteger(T->getKeyword()); |
| 500 | VisitType(T); |
| 501 | }; |
| 502 | |
| 503 | void VisitDependentNameType(const DependentNameType *T) { |
| 504 | AddNestedNameSpecifier(T->getQualifier()); |
| 505 | AddIdentifierInfo(T->getIdentifier()); |
| 506 | VisitTypeWithKeyword(T); |
| 507 | } |
| 508 | |
| 509 | void VisitDependentTemplateSpecializationType( |
| 510 | const DependentTemplateSpecializationType *T) { |
| 511 | AddIdentifierInfo(T->getIdentifier()); |
| 512 | AddNestedNameSpecifier(T->getQualifier()); |
| 513 | ID.AddInteger(T->getNumArgs()); |
| 514 | for (const auto &TA : T->template_arguments()) { |
| 515 | Hash.AddTemplateArgument(TA); |
| 516 | } |
| 517 | VisitTypeWithKeyword(T); |
| 518 | } |
| 519 | |
| 520 | void VisitElaboratedType(const ElaboratedType *T) { |
| 521 | AddNestedNameSpecifier(T->getQualifier()); |
| 522 | AddQualType(T->getNamedType()); |
| 523 | VisitTypeWithKeyword(T); |
| 524 | } |
Richard Trieu | 96b4962 | 2017-05-31 00:31:58 +0000 | [diff] [blame] | 525 | |
| 526 | void VisitTemplateSpecializationType(const TemplateSpecializationType *T) { |
| 527 | ID.AddInteger(T->getNumArgs()); |
| 528 | for (const auto &TA : T->template_arguments()) { |
| 529 | Hash.AddTemplateArgument(TA); |
| 530 | } |
| 531 | Hash.AddTemplateName(T->getTemplateName()); |
| 532 | VisitType(T); |
| 533 | } |
Richard Trieu | bcaaf96 | 2017-02-23 03:25:57 +0000 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | void ODRHash::AddType(const Type *T) { |
| 537 | assert(T && "Expecting non-null pointer."); |
| 538 | auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size())); |
| 539 | ID.AddInteger(Result.first->second); |
| 540 | // On first encounter of a Type pointer, process it. Every time afterwards, |
| 541 | // only the index value is needed. |
| 542 | if (!Result.second) { |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | ODRTypeVisitor(ID, *this).Visit(T); |
| 547 | } |
| 548 | |
| 549 | void ODRHash::AddQualType(QualType T) { |
| 550 | AddBoolean(T.isNull()); |
| 551 | if (T.isNull()) |
| 552 | return; |
| 553 | SplitQualType split = T.split(); |
| 554 | ID.AddInteger(split.Quals.getAsOpaqueValue()); |
| 555 | AddType(split.Ty); |
| 556 | } |
| 557 | |
Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 558 | void ODRHash::AddBoolean(bool Value) { |
| 559 | Bools.push_back(Value); |
| 560 | } |