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