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