ODRHash.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. //===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file implements the ODRHash class, which calculates a hash based
  11. /// on AST nodes, which is stable across different runs.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ODRHash.h"
  15. #include "clang/AST/DeclVisitor.h"
  16. #include "clang/AST/NestedNameSpecifier.h"
  17. #include "clang/AST/StmtVisitor.h"
  18. #include "clang/AST/TypeVisitor.h"
  19. using namespace clang;
  20. void ODRHash::AddStmt(const Stmt *S) {
  21. assert(S && "Expecting non-null pointer.");
  22. S->ProcessODRHash(ID, *this);
  23. }
  24. void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
  25. assert(II && "Expecting non-null pointer.");
  26. ID.AddString(II->getName());
  27. }
  28. void ODRHash::AddDeclarationName(DeclarationName Name, bool TreatAsDecl) {
  29. if (TreatAsDecl)
  30. // Matches the NamedDecl check in AddDecl
  31. AddBoolean(true);
  32. AddDeclarationNameImpl(Name);
  33. if (TreatAsDecl)
  34. // Matches the ClassTemplateSpecializationDecl check in AddDecl
  35. AddBoolean(false);
  36. }
  37. void ODRHash::AddDeclarationNameImpl(DeclarationName Name) {
  38. // Index all DeclarationName and use index numbers to refer to them.
  39. auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
  40. ID.AddInteger(Result.first->second);
  41. if (!Result.second) {
  42. // If found in map, the DeclarationName has previously been processed.
  43. return;
  44. }
  45. // First time processing each DeclarationName, also process its details.
  46. AddBoolean(Name.isEmpty());
  47. if (Name.isEmpty())
  48. return;
  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. ID.AddInteger(NumArgs);
  64. for (unsigned i = 0; i < NumArgs; ++i) {
  65. const IdentifierInfo *II = S.getIdentifierInfoForSlot(i);
  66. AddBoolean(II);
  67. if (II) {
  68. AddIdentifierInfo(II);
  69. }
  70. }
  71. break;
  72. }
  73. case DeclarationName::CXXConstructorName:
  74. case DeclarationName::CXXDestructorName:
  75. AddQualType(Name.getCXXNameType());
  76. break;
  77. case DeclarationName::CXXOperatorName:
  78. ID.AddInteger(Name.getCXXOverloadedOperator());
  79. break;
  80. case DeclarationName::CXXLiteralOperatorName:
  81. AddIdentifierInfo(Name.getCXXLiteralIdentifier());
  82. break;
  83. case DeclarationName::CXXConversionFunctionName:
  84. AddQualType(Name.getCXXNameType());
  85. break;
  86. case DeclarationName::CXXUsingDirective:
  87. break;
  88. case DeclarationName::CXXDeductionGuideName: {
  89. auto *Template = Name.getCXXDeductionGuideTemplate();
  90. AddBoolean(Template);
  91. if (Template) {
  92. AddDecl(Template);
  93. }
  94. }
  95. }
  96. }
  97. void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
  98. assert(NNS && "Expecting non-null pointer.");
  99. const auto *Prefix = NNS->getPrefix();
  100. AddBoolean(Prefix);
  101. if (Prefix) {
  102. AddNestedNameSpecifier(Prefix);
  103. }
  104. auto Kind = NNS->getKind();
  105. ID.AddInteger(Kind);
  106. switch (Kind) {
  107. case NestedNameSpecifier::Identifier:
  108. AddIdentifierInfo(NNS->getAsIdentifier());
  109. break;
  110. case NestedNameSpecifier::Namespace:
  111. AddDecl(NNS->getAsNamespace());
  112. break;
  113. case NestedNameSpecifier::NamespaceAlias:
  114. AddDecl(NNS->getAsNamespaceAlias());
  115. break;
  116. case NestedNameSpecifier::TypeSpec:
  117. case NestedNameSpecifier::TypeSpecWithTemplate:
  118. AddType(NNS->getAsType());
  119. break;
  120. case NestedNameSpecifier::Global:
  121. case NestedNameSpecifier::Super:
  122. break;
  123. }
  124. }
  125. void ODRHash::AddTemplateName(TemplateName Name) {
  126. auto Kind = Name.getKind();
  127. ID.AddInteger(Kind);
  128. switch (Kind) {
  129. case TemplateName::Template:
  130. AddDecl(Name.getAsTemplateDecl());
  131. break;
  132. // TODO: Support these cases.
  133. case TemplateName::OverloadedTemplate:
  134. case TemplateName::AssumedTemplate:
  135. case TemplateName::QualifiedTemplate:
  136. case TemplateName::DependentTemplate:
  137. case TemplateName::SubstTemplateTemplateParm:
  138. case TemplateName::SubstTemplateTemplateParmPack:
  139. break;
  140. }
  141. }
  142. void ODRHash::AddTemplateArgument(TemplateArgument TA) {
  143. const auto Kind = TA.getKind();
  144. ID.AddInteger(Kind);
  145. switch (Kind) {
  146. case TemplateArgument::Null:
  147. llvm_unreachable("Expected valid TemplateArgument");
  148. case TemplateArgument::Type:
  149. AddQualType(TA.getAsType());
  150. break;
  151. case TemplateArgument::Declaration:
  152. AddDecl(TA.getAsDecl());
  153. break;
  154. case TemplateArgument::NullPtr:
  155. case TemplateArgument::Integral:
  156. break;
  157. case TemplateArgument::Template:
  158. case TemplateArgument::TemplateExpansion:
  159. AddTemplateName(TA.getAsTemplateOrTemplatePattern());
  160. break;
  161. case TemplateArgument::Expression:
  162. AddStmt(TA.getAsExpr());
  163. break;
  164. case TemplateArgument::Pack:
  165. ID.AddInteger(TA.pack_size());
  166. for (auto SubTA : TA.pack_elements()) {
  167. AddTemplateArgument(SubTA);
  168. }
  169. break;
  170. }
  171. }
  172. void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
  173. assert(TPL && "Expecting non-null pointer.");
  174. ID.AddInteger(TPL->size());
  175. for (auto *ND : TPL->asArray()) {
  176. AddSubDecl(ND);
  177. }
  178. }
  179. void ODRHash::clear() {
  180. DeclNameMap.clear();
  181. Bools.clear();
  182. ID.clear();
  183. }
  184. unsigned ODRHash::CalculateHash() {
  185. // Append the bools to the end of the data segment backwards. This allows
  186. // for the bools data to be compressed 32 times smaller compared to using
  187. // ID.AddBoolean
  188. const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
  189. const unsigned size = Bools.size();
  190. const unsigned remainder = size % unsigned_bits;
  191. const unsigned loops = size / unsigned_bits;
  192. auto I = Bools.rbegin();
  193. unsigned value = 0;
  194. for (unsigned i = 0; i < remainder; ++i) {
  195. value <<= 1;
  196. value |= *I;
  197. ++I;
  198. }
  199. ID.AddInteger(value);
  200. for (unsigned i = 0; i < loops; ++i) {
  201. value = 0;
  202. for (unsigned j = 0; j < unsigned_bits; ++j) {
  203. value <<= 1;
  204. value |= *I;
  205. ++I;
  206. }
  207. ID.AddInteger(value);
  208. }
  209. assert(I == Bools.rend());
  210. Bools.clear();
  211. return ID.ComputeHash();
  212. }
  213. namespace {
  214. // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
  215. // methods process the relevant parts of the Decl.
  216. class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
  217. typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
  218. llvm::FoldingSetNodeID &ID;
  219. ODRHash &Hash;
  220. public:
  221. ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
  222. : ID(ID), Hash(Hash) {}
  223. void AddStmt(const Stmt *S) {
  224. Hash.AddBoolean(S);
  225. if (S) {
  226. Hash.AddStmt(S);
  227. }
  228. }
  229. void AddIdentifierInfo(const IdentifierInfo *II) {
  230. Hash.AddBoolean(II);
  231. if (II) {
  232. Hash.AddIdentifierInfo(II);
  233. }
  234. }
  235. void AddQualType(QualType T) {
  236. Hash.AddQualType(T);
  237. }
  238. void AddDecl(const Decl *D) {
  239. Hash.AddBoolean(D);
  240. if (D) {
  241. Hash.AddDecl(D);
  242. }
  243. }
  244. void AddTemplateArgument(TemplateArgument TA) {
  245. Hash.AddTemplateArgument(TA);
  246. }
  247. void Visit(const Decl *D) {
  248. ID.AddInteger(D->getKind());
  249. Inherited::Visit(D);
  250. }
  251. void VisitNamedDecl(const NamedDecl *D) {
  252. Hash.AddDeclarationName(D->getDeclName());
  253. Inherited::VisitNamedDecl(D);
  254. }
  255. void VisitValueDecl(const ValueDecl *D) {
  256. if (!isa<FunctionDecl>(D)) {
  257. AddQualType(D->getType());
  258. }
  259. Inherited::VisitValueDecl(D);
  260. }
  261. void VisitVarDecl(const VarDecl *D) {
  262. Hash.AddBoolean(D->isStaticLocal());
  263. Hash.AddBoolean(D->isConstexpr());
  264. const bool HasInit = D->hasInit();
  265. Hash.AddBoolean(HasInit);
  266. if (HasInit) {
  267. AddStmt(D->getInit());
  268. }
  269. Inherited::VisitVarDecl(D);
  270. }
  271. void VisitParmVarDecl(const ParmVarDecl *D) {
  272. // TODO: Handle default arguments.
  273. Inherited::VisitParmVarDecl(D);
  274. }
  275. void VisitAccessSpecDecl(const AccessSpecDecl *D) {
  276. ID.AddInteger(D->getAccess());
  277. Inherited::VisitAccessSpecDecl(D);
  278. }
  279. void VisitStaticAssertDecl(const StaticAssertDecl *D) {
  280. AddStmt(D->getAssertExpr());
  281. AddStmt(D->getMessage());
  282. Inherited::VisitStaticAssertDecl(D);
  283. }
  284. void VisitFieldDecl(const FieldDecl *D) {
  285. const bool IsBitfield = D->isBitField();
  286. Hash.AddBoolean(IsBitfield);
  287. if (IsBitfield) {
  288. AddStmt(D->getBitWidth());
  289. }
  290. Hash.AddBoolean(D->isMutable());
  291. AddStmt(D->getInClassInitializer());
  292. Inherited::VisitFieldDecl(D);
  293. }
  294. void VisitFunctionDecl(const FunctionDecl *D) {
  295. // Handled by the ODRHash for FunctionDecl
  296. ID.AddInteger(D->getODRHash());
  297. Inherited::VisitFunctionDecl(D);
  298. }
  299. void VisitCXXMethodDecl(const CXXMethodDecl *D) {
  300. // Handled by the ODRHash for FunctionDecl
  301. Inherited::VisitCXXMethodDecl(D);
  302. }
  303. void VisitTypedefNameDecl(const TypedefNameDecl *D) {
  304. AddQualType(D->getUnderlyingType());
  305. Inherited::VisitTypedefNameDecl(D);
  306. }
  307. void VisitTypedefDecl(const TypedefDecl *D) {
  308. Inherited::VisitTypedefDecl(D);
  309. }
  310. void VisitTypeAliasDecl(const TypeAliasDecl *D) {
  311. Inherited::VisitTypeAliasDecl(D);
  312. }
  313. void VisitFriendDecl(const FriendDecl *D) {
  314. TypeSourceInfo *TSI = D->getFriendType();
  315. Hash.AddBoolean(TSI);
  316. if (TSI) {
  317. AddQualType(TSI->getType());
  318. } else {
  319. AddDecl(D->getFriendDecl());
  320. }
  321. }
  322. void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
  323. // Only care about default arguments as part of the definition.
  324. const bool hasDefaultArgument =
  325. D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
  326. Hash.AddBoolean(hasDefaultArgument);
  327. if (hasDefaultArgument) {
  328. AddTemplateArgument(D->getDefaultArgument());
  329. }
  330. Hash.AddBoolean(D->isParameterPack());
  331. Inherited::VisitTemplateTypeParmDecl(D);
  332. }
  333. void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
  334. // Only care about default arguments as part of the definition.
  335. const bool hasDefaultArgument =
  336. D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
  337. Hash.AddBoolean(hasDefaultArgument);
  338. if (hasDefaultArgument) {
  339. AddStmt(D->getDefaultArgument());
  340. }
  341. Hash.AddBoolean(D->isParameterPack());
  342. Inherited::VisitNonTypeTemplateParmDecl(D);
  343. }
  344. void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
  345. // Only care about default arguments as part of the definition.
  346. const bool hasDefaultArgument =
  347. D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
  348. Hash.AddBoolean(hasDefaultArgument);
  349. if (hasDefaultArgument) {
  350. AddTemplateArgument(D->getDefaultArgument().getArgument());
  351. }
  352. Hash.AddBoolean(D->isParameterPack());
  353. Inherited::VisitTemplateTemplateParmDecl(D);
  354. }
  355. void VisitTemplateDecl(const TemplateDecl *D) {
  356. Hash.AddTemplateParameterList(D->getTemplateParameters());
  357. Inherited::VisitTemplateDecl(D);
  358. }
  359. void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
  360. Hash.AddBoolean(D->isMemberSpecialization());
  361. Inherited::VisitRedeclarableTemplateDecl(D);
  362. }
  363. void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
  364. AddDecl(D->getTemplatedDecl());
  365. ID.AddInteger(D->getTemplatedDecl()->getODRHash());
  366. Inherited::VisitFunctionTemplateDecl(D);
  367. }
  368. void VisitEnumConstantDecl(const EnumConstantDecl *D) {
  369. AddStmt(D->getInitExpr());
  370. Inherited::VisitEnumConstantDecl(D);
  371. }
  372. };
  373. } // namespace
  374. // Only allow a small portion of Decl's to be processed. Remove this once
  375. // all Decl's can be handled.
  376. bool ODRHash::isWhitelistedDecl(const Decl *D, const DeclContext *Parent) {
  377. if (D->isImplicit()) return false;
  378. if (D->getDeclContext() != Parent) return false;
  379. switch (D->getKind()) {
  380. default:
  381. return false;
  382. case Decl::AccessSpec:
  383. case Decl::CXXConstructor:
  384. case Decl::CXXDestructor:
  385. case Decl::CXXMethod:
  386. case Decl::EnumConstant: // Only found in EnumDecl's.
  387. case Decl::Field:
  388. case Decl::Friend:
  389. case Decl::FunctionTemplate:
  390. case Decl::StaticAssert:
  391. case Decl::TypeAlias:
  392. case Decl::Typedef:
  393. case Decl::Var:
  394. return true;
  395. }
  396. }
  397. void ODRHash::AddSubDecl(const Decl *D) {
  398. assert(D && "Expecting non-null pointer.");
  399. ODRDeclVisitor(ID, *this).Visit(D);
  400. }
  401. void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
  402. assert(Record && Record->hasDefinition() &&
  403. "Expected non-null record to be a definition.");
  404. const DeclContext *DC = Record;
  405. while (DC) {
  406. if (isa<ClassTemplateSpecializationDecl>(DC)) {
  407. return;
  408. }
  409. DC = DC->getParent();
  410. }
  411. AddDecl(Record);
  412. // Filter out sub-Decls which will not be processed in order to get an
  413. // accurate count of Decl's.
  414. llvm::SmallVector<const Decl *, 16> Decls;
  415. for (Decl *SubDecl : Record->decls()) {
  416. if (isWhitelistedDecl(SubDecl, Record)) {
  417. Decls.push_back(SubDecl);
  418. if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
  419. // Compute/Preload ODRHash into FunctionDecl.
  420. Function->getODRHash();
  421. }
  422. }
  423. }
  424. ID.AddInteger(Decls.size());
  425. for (auto SubDecl : Decls) {
  426. AddSubDecl(SubDecl);
  427. }
  428. const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
  429. AddBoolean(TD);
  430. if (TD) {
  431. AddTemplateParameterList(TD->getTemplateParameters());
  432. }
  433. ID.AddInteger(Record->getNumBases());
  434. auto Bases = Record->bases();
  435. for (auto Base : Bases) {
  436. AddQualType(Base.getType());
  437. ID.AddInteger(Base.isVirtual());
  438. ID.AddInteger(Base.getAccessSpecifierAsWritten());
  439. }
  440. }
  441. void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
  442. bool SkipBody) {
  443. assert(Function && "Expecting non-null pointer.");
  444. // Skip functions that are specializations or in specialization context.
  445. const DeclContext *DC = Function;
  446. while (DC) {
  447. if (isa<ClassTemplateSpecializationDecl>(DC)) return;
  448. if (auto *F = dyn_cast<FunctionDecl>(DC)) {
  449. if (F->isFunctionTemplateSpecialization()) {
  450. if (!isa<CXXMethodDecl>(DC)) return;
  451. if (DC->getLexicalParent()->isFileContext()) return;
  452. // Inline method specializations are the only supported
  453. // specialization for now.
  454. }
  455. }
  456. DC = DC->getParent();
  457. }
  458. ID.AddInteger(Function->getDeclKind());
  459. const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
  460. AddBoolean(SpecializationArgs);
  461. if (SpecializationArgs) {
  462. ID.AddInteger(SpecializationArgs->size());
  463. for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
  464. AddTemplateArgument(TA);
  465. }
  466. }
  467. if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
  468. AddBoolean(Method->isConst());
  469. AddBoolean(Method->isVolatile());
  470. }
  471. ID.AddInteger(Function->getStorageClass());
  472. AddBoolean(Function->isInlineSpecified());
  473. AddBoolean(Function->isVirtualAsWritten());
  474. AddBoolean(Function->isPure());
  475. AddBoolean(Function->isDeletedAsWritten());
  476. AddBoolean(Function->isExplicitlyDefaulted());
  477. AddDecl(Function);
  478. AddQualType(Function->getReturnType());
  479. ID.AddInteger(Function->param_size());
  480. for (auto Param : Function->parameters())
  481. AddSubDecl(Param);
  482. if (SkipBody) {
  483. AddBoolean(false);
  484. return;
  485. }
  486. const bool HasBody = Function->isThisDeclarationADefinition() &&
  487. !Function->isDefaulted() && !Function->isDeleted() &&
  488. !Function->isLateTemplateParsed();
  489. AddBoolean(HasBody);
  490. if (!HasBody) {
  491. return;
  492. }
  493. auto *Body = Function->getBody();
  494. AddBoolean(Body);
  495. if (Body)
  496. AddStmt(Body);
  497. // Filter out sub-Decls which will not be processed in order to get an
  498. // accurate count of Decl's.
  499. llvm::SmallVector<const Decl *, 16> Decls;
  500. for (Decl *SubDecl : Function->decls()) {
  501. if (isWhitelistedDecl(SubDecl, Function)) {
  502. Decls.push_back(SubDecl);
  503. }
  504. }
  505. ID.AddInteger(Decls.size());
  506. for (auto SubDecl : Decls) {
  507. AddSubDecl(SubDecl);
  508. }
  509. }
  510. void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
  511. assert(Enum);
  512. AddDeclarationName(Enum->getDeclName());
  513. AddBoolean(Enum->isScoped());
  514. if (Enum->isScoped())
  515. AddBoolean(Enum->isScopedUsingClassTag());
  516. if (Enum->getIntegerTypeSourceInfo())
  517. AddQualType(Enum->getIntegerType());
  518. // Filter out sub-Decls which will not be processed in order to get an
  519. // accurate count of Decl's.
  520. llvm::SmallVector<const Decl *, 16> Decls;
  521. for (Decl *SubDecl : Enum->decls()) {
  522. if (isWhitelistedDecl(SubDecl, Enum)) {
  523. assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
  524. Decls.push_back(SubDecl);
  525. }
  526. }
  527. ID.AddInteger(Decls.size());
  528. for (auto SubDecl : Decls) {
  529. AddSubDecl(SubDecl);
  530. }
  531. }
  532. void ODRHash::AddDecl(const Decl *D) {
  533. assert(D && "Expecting non-null pointer.");
  534. D = D->getCanonicalDecl();
  535. const NamedDecl *ND = dyn_cast<NamedDecl>(D);
  536. AddBoolean(ND);
  537. if (!ND) {
  538. ID.AddInteger(D->getKind());
  539. return;
  540. }
  541. AddDeclarationName(ND->getDeclName());
  542. const auto *Specialization =
  543. dyn_cast<ClassTemplateSpecializationDecl>(D);
  544. AddBoolean(Specialization);
  545. if (Specialization) {
  546. const TemplateArgumentList &List = Specialization->getTemplateArgs();
  547. ID.AddInteger(List.size());
  548. for (const TemplateArgument &TA : List.asArray())
  549. AddTemplateArgument(TA);
  550. }
  551. }
  552. namespace {
  553. // Process a Type pointer. Add* methods call back into ODRHash while Visit*
  554. // methods process the relevant parts of the Type.
  555. class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
  556. typedef TypeVisitor<ODRTypeVisitor> Inherited;
  557. llvm::FoldingSetNodeID &ID;
  558. ODRHash &Hash;
  559. public:
  560. ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
  561. : ID(ID), Hash(Hash) {}
  562. void AddStmt(Stmt *S) {
  563. Hash.AddBoolean(S);
  564. if (S) {
  565. Hash.AddStmt(S);
  566. }
  567. }
  568. void AddDecl(Decl *D) {
  569. Hash.AddBoolean(D);
  570. if (D) {
  571. Hash.AddDecl(D);
  572. }
  573. }
  574. void AddQualType(QualType T) {
  575. Hash.AddQualType(T);
  576. }
  577. void AddType(const Type *T) {
  578. Hash.AddBoolean(T);
  579. if (T) {
  580. Hash.AddType(T);
  581. }
  582. }
  583. void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
  584. Hash.AddBoolean(NNS);
  585. if (NNS) {
  586. Hash.AddNestedNameSpecifier(NNS);
  587. }
  588. }
  589. void AddIdentifierInfo(const IdentifierInfo *II) {
  590. Hash.AddBoolean(II);
  591. if (II) {
  592. Hash.AddIdentifierInfo(II);
  593. }
  594. }
  595. void VisitQualifiers(Qualifiers Quals) {
  596. ID.AddInteger(Quals.getAsOpaqueValue());
  597. }
  598. // Return the RecordType if the typedef only strips away a keyword.
  599. // Otherwise, return the original type.
  600. static const Type *RemoveTypedef(const Type *T) {
  601. const auto *TypedefT = dyn_cast<TypedefType>(T);
  602. if (!TypedefT) {
  603. return T;
  604. }
  605. const TypedefNameDecl *D = TypedefT->getDecl();
  606. QualType UnderlyingType = D->getUnderlyingType();
  607. if (UnderlyingType.hasLocalQualifiers()) {
  608. return T;
  609. }
  610. const auto *ElaboratedT = dyn_cast<ElaboratedType>(UnderlyingType);
  611. if (!ElaboratedT) {
  612. return T;
  613. }
  614. if (ElaboratedT->getQualifier() != nullptr) {
  615. return T;
  616. }
  617. QualType NamedType = ElaboratedT->getNamedType();
  618. if (NamedType.hasLocalQualifiers()) {
  619. return T;
  620. }
  621. const auto *RecordT = dyn_cast<RecordType>(NamedType);
  622. if (!RecordT) {
  623. return T;
  624. }
  625. const IdentifierInfo *TypedefII = TypedefT->getDecl()->getIdentifier();
  626. const IdentifierInfo *RecordII = RecordT->getDecl()->getIdentifier();
  627. if (!TypedefII || !RecordII ||
  628. TypedefII->getName() != RecordII->getName()) {
  629. return T;
  630. }
  631. return RecordT;
  632. }
  633. void Visit(const Type *T) {
  634. T = RemoveTypedef(T);
  635. ID.AddInteger(T->getTypeClass());
  636. Inherited::Visit(T);
  637. }
  638. void VisitType(const Type *T) {}
  639. void VisitAdjustedType(const AdjustedType *T) {
  640. QualType Original = T->getOriginalType();
  641. QualType Adjusted = T->getAdjustedType();
  642. // The original type and pointee type can be the same, as in the case of
  643. // function pointers decaying to themselves. Set a bool and only process
  644. // the type once, to prevent doubling the work.
  645. SplitQualType split = Adjusted.split();
  646. if (auto Pointer = dyn_cast<PointerType>(split.Ty)) {
  647. if (Pointer->getPointeeType() == Original) {
  648. Hash.AddBoolean(true);
  649. ID.AddInteger(split.Quals.getAsOpaqueValue());
  650. AddQualType(Original);
  651. VisitType(T);
  652. return;
  653. }
  654. }
  655. // The original type and pointee type are different, such as in the case
  656. // of a array decaying to an element pointer. Set a bool to false and
  657. // process both types.
  658. Hash.AddBoolean(false);
  659. AddQualType(Original);
  660. AddQualType(Adjusted);
  661. VisitType(T);
  662. }
  663. void VisitDecayedType(const DecayedType *T) {
  664. // getDecayedType and getPointeeType are derived from getAdjustedType
  665. // and don't need to be separately processed.
  666. VisitAdjustedType(T);
  667. }
  668. void VisitArrayType(const ArrayType *T) {
  669. AddQualType(T->getElementType());
  670. ID.AddInteger(T->getSizeModifier());
  671. VisitQualifiers(T->getIndexTypeQualifiers());
  672. VisitType(T);
  673. }
  674. void VisitConstantArrayType(const ConstantArrayType *T) {
  675. T->getSize().Profile(ID);
  676. VisitArrayType(T);
  677. }
  678. void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
  679. AddStmt(T->getSizeExpr());
  680. VisitArrayType(T);
  681. }
  682. void VisitIncompleteArrayType(const IncompleteArrayType *T) {
  683. VisitArrayType(T);
  684. }
  685. void VisitVariableArrayType(const VariableArrayType *T) {
  686. AddStmt(T->getSizeExpr());
  687. VisitArrayType(T);
  688. }
  689. void VisitAttributedType(const AttributedType *T) {
  690. ID.AddInteger(T->getAttrKind());
  691. AddQualType(T->getModifiedType());
  692. AddQualType(T->getEquivalentType());
  693. VisitType(T);
  694. }
  695. void VisitBlockPointerType(const BlockPointerType *T) {
  696. AddQualType(T->getPointeeType());
  697. VisitType(T);
  698. }
  699. void VisitBuiltinType(const BuiltinType *T) {
  700. ID.AddInteger(T->getKind());
  701. VisitType(T);
  702. }
  703. void VisitComplexType(const ComplexType *T) {
  704. AddQualType(T->getElementType());
  705. VisitType(T);
  706. }
  707. void VisitDecltypeType(const DecltypeType *T) {
  708. AddStmt(T->getUnderlyingExpr());
  709. AddQualType(T->getUnderlyingType());
  710. VisitType(T);
  711. }
  712. void VisitDependentDecltypeType(const DependentDecltypeType *T) {
  713. VisitDecltypeType(T);
  714. }
  715. void VisitDeducedType(const DeducedType *T) {
  716. AddQualType(T->getDeducedType());
  717. VisitType(T);
  718. }
  719. void VisitAutoType(const AutoType *T) {
  720. ID.AddInteger((unsigned)T->getKeyword());
  721. VisitDeducedType(T);
  722. }
  723. void VisitDeducedTemplateSpecializationType(
  724. const DeducedTemplateSpecializationType *T) {
  725. Hash.AddTemplateName(T->getTemplateName());
  726. VisitDeducedType(T);
  727. }
  728. void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) {
  729. AddQualType(T->getPointeeType());
  730. AddStmt(T->getAddrSpaceExpr());
  731. VisitType(T);
  732. }
  733. void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
  734. AddQualType(T->getElementType());
  735. AddStmt(T->getSizeExpr());
  736. VisitType(T);
  737. }
  738. void VisitFunctionType(const FunctionType *T) {
  739. AddQualType(T->getReturnType());
  740. T->getExtInfo().Profile(ID);
  741. Hash.AddBoolean(T->isConst());
  742. Hash.AddBoolean(T->isVolatile());
  743. Hash.AddBoolean(T->isRestrict());
  744. VisitType(T);
  745. }
  746. void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
  747. VisitFunctionType(T);
  748. }
  749. void VisitFunctionProtoType(const FunctionProtoType *T) {
  750. ID.AddInteger(T->getNumParams());
  751. for (auto ParamType : T->getParamTypes())
  752. AddQualType(ParamType);
  753. VisitFunctionType(T);
  754. }
  755. void VisitInjectedClassNameType(const InjectedClassNameType *T) {
  756. AddDecl(T->getDecl());
  757. VisitType(T);
  758. }
  759. void VisitMemberPointerType(const MemberPointerType *T) {
  760. AddQualType(T->getPointeeType());
  761. AddType(T->getClass());
  762. VisitType(T);
  763. }
  764. void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
  765. AddQualType(T->getPointeeType());
  766. VisitType(T);
  767. }
  768. void VisitObjCObjectType(const ObjCObjectType *T) {
  769. AddDecl(T->getInterface());
  770. auto TypeArgs = T->getTypeArgsAsWritten();
  771. ID.AddInteger(TypeArgs.size());
  772. for (auto Arg : TypeArgs) {
  773. AddQualType(Arg);
  774. }
  775. auto Protocols = T->getProtocols();
  776. ID.AddInteger(Protocols.size());
  777. for (auto Protocol : Protocols) {
  778. AddDecl(Protocol);
  779. }
  780. Hash.AddBoolean(T->isKindOfType());
  781. VisitType(T);
  782. }
  783. void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
  784. // This type is handled by the parent type ObjCObjectType.
  785. VisitObjCObjectType(T);
  786. }
  787. void VisitObjCTypeParamType(const ObjCTypeParamType *T) {
  788. AddDecl(T->getDecl());
  789. auto Protocols = T->getProtocols();
  790. ID.AddInteger(Protocols.size());
  791. for (auto Protocol : Protocols) {
  792. AddDecl(Protocol);
  793. }
  794. VisitType(T);
  795. }
  796. void VisitPackExpansionType(const PackExpansionType *T) {
  797. AddQualType(T->getPattern());
  798. VisitType(T);
  799. }
  800. void VisitParenType(const ParenType *T) {
  801. AddQualType(T->getInnerType());
  802. VisitType(T);
  803. }
  804. void VisitPipeType(const PipeType *T) {
  805. AddQualType(T->getElementType());
  806. Hash.AddBoolean(T->isReadOnly());
  807. VisitType(T);
  808. }
  809. void VisitPointerType(const PointerType *T) {
  810. AddQualType(T->getPointeeType());
  811. VisitType(T);
  812. }
  813. void VisitReferenceType(const ReferenceType *T) {
  814. AddQualType(T->getPointeeTypeAsWritten());
  815. VisitType(T);
  816. }
  817. void VisitLValueReferenceType(const LValueReferenceType *T) {
  818. VisitReferenceType(T);
  819. }
  820. void VisitRValueReferenceType(const RValueReferenceType *T) {
  821. VisitReferenceType(T);
  822. }
  823. void
  824. VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
  825. AddType(T->getReplacedParameter());
  826. Hash.AddTemplateArgument(T->getArgumentPack());
  827. VisitType(T);
  828. }
  829. void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
  830. AddType(T->getReplacedParameter());
  831. AddQualType(T->getReplacementType());
  832. VisitType(T);
  833. }
  834. void VisitTagType(const TagType *T) {
  835. AddDecl(T->getDecl());
  836. VisitType(T);
  837. }
  838. void VisitRecordType(const RecordType *T) { VisitTagType(T); }
  839. void VisitEnumType(const EnumType *T) { VisitTagType(T); }
  840. void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
  841. ID.AddInteger(T->getNumArgs());
  842. for (const auto &TA : T->template_arguments()) {
  843. Hash.AddTemplateArgument(TA);
  844. }
  845. Hash.AddTemplateName(T->getTemplateName());
  846. VisitType(T);
  847. }
  848. void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
  849. ID.AddInteger(T->getDepth());
  850. ID.AddInteger(T->getIndex());
  851. Hash.AddBoolean(T->isParameterPack());
  852. AddDecl(T->getDecl());
  853. }
  854. void VisitTypedefType(const TypedefType *T) {
  855. AddDecl(T->getDecl());
  856. QualType UnderlyingType = T->getDecl()->getUnderlyingType();
  857. VisitQualifiers(UnderlyingType.getQualifiers());
  858. while (true) {
  859. if (const TypedefType *Underlying =
  860. dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
  861. UnderlyingType = Underlying->getDecl()->getUnderlyingType();
  862. continue;
  863. }
  864. if (const ElaboratedType *Underlying =
  865. dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
  866. UnderlyingType = Underlying->getNamedType();
  867. continue;
  868. }
  869. break;
  870. }
  871. AddType(UnderlyingType.getTypePtr());
  872. VisitType(T);
  873. }
  874. void VisitTypeOfExprType(const TypeOfExprType *T) {
  875. AddStmt(T->getUnderlyingExpr());
  876. Hash.AddBoolean(T->isSugared());
  877. if (T->isSugared())
  878. AddQualType(T->desugar());
  879. VisitType(T);
  880. }
  881. void VisitTypeOfType(const TypeOfType *T) {
  882. AddQualType(T->getUnderlyingType());
  883. VisitType(T);
  884. }
  885. void VisitTypeWithKeyword(const TypeWithKeyword *T) {
  886. ID.AddInteger(T->getKeyword());
  887. VisitType(T);
  888. };
  889. void VisitDependentNameType(const DependentNameType *T) {
  890. AddNestedNameSpecifier(T->getQualifier());
  891. AddIdentifierInfo(T->getIdentifier());
  892. VisitTypeWithKeyword(T);
  893. }
  894. void VisitDependentTemplateSpecializationType(
  895. const DependentTemplateSpecializationType *T) {
  896. AddIdentifierInfo(T->getIdentifier());
  897. AddNestedNameSpecifier(T->getQualifier());
  898. ID.AddInteger(T->getNumArgs());
  899. for (const auto &TA : T->template_arguments()) {
  900. Hash.AddTemplateArgument(TA);
  901. }
  902. VisitTypeWithKeyword(T);
  903. }
  904. void VisitElaboratedType(const ElaboratedType *T) {
  905. AddNestedNameSpecifier(T->getQualifier());
  906. AddQualType(T->getNamedType());
  907. VisitTypeWithKeyword(T);
  908. }
  909. void VisitUnaryTransformType(const UnaryTransformType *T) {
  910. AddQualType(T->getUnderlyingType());
  911. AddQualType(T->getBaseType());
  912. VisitType(T);
  913. }
  914. void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
  915. AddDecl(T->getDecl());
  916. VisitType(T);
  917. }
  918. void VisitVectorType(const VectorType *T) {
  919. AddQualType(T->getElementType());
  920. ID.AddInteger(T->getNumElements());
  921. ID.AddInteger(T->getVectorKind());
  922. VisitType(T);
  923. }
  924. void VisitExtVectorType(const ExtVectorType * T) {
  925. VisitVectorType(T);
  926. }
  927. };
  928. } // namespace
  929. void ODRHash::AddType(const Type *T) {
  930. assert(T && "Expecting non-null pointer.");
  931. ODRTypeVisitor(ID, *this).Visit(T);
  932. }
  933. void ODRHash::AddQualType(QualType T) {
  934. AddBoolean(T.isNull());
  935. if (T.isNull())
  936. return;
  937. SplitQualType split = T.split();
  938. ID.AddInteger(split.Quals.getAsOpaqueValue());
  939. AddType(split.Ty);
  940. }
  941. void ODRHash::AddBoolean(bool Value) {
  942. Bools.push_back(Value);
  943. }