ODRHash.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. const TypeConstraint *TC = D->getTypeConstraint();
  332. Hash.AddBoolean(TC != nullptr);
  333. if (TC)
  334. AddStmt(TC->getImmediatelyDeclaredConstraint());
  335. Inherited::VisitTemplateTypeParmDecl(D);
  336. }
  337. void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
  338. // Only care about default arguments as part of the definition.
  339. const bool hasDefaultArgument =
  340. D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
  341. Hash.AddBoolean(hasDefaultArgument);
  342. if (hasDefaultArgument) {
  343. AddStmt(D->getDefaultArgument());
  344. }
  345. Hash.AddBoolean(D->isParameterPack());
  346. Inherited::VisitNonTypeTemplateParmDecl(D);
  347. }
  348. void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
  349. // Only care about default arguments as part of the definition.
  350. const bool hasDefaultArgument =
  351. D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
  352. Hash.AddBoolean(hasDefaultArgument);
  353. if (hasDefaultArgument) {
  354. AddTemplateArgument(D->getDefaultArgument().getArgument());
  355. }
  356. Hash.AddBoolean(D->isParameterPack());
  357. Inherited::VisitTemplateTemplateParmDecl(D);
  358. }
  359. void VisitTemplateDecl(const TemplateDecl *D) {
  360. Hash.AddTemplateParameterList(D->getTemplateParameters());
  361. Inherited::VisitTemplateDecl(D);
  362. }
  363. void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
  364. Hash.AddBoolean(D->isMemberSpecialization());
  365. Inherited::VisitRedeclarableTemplateDecl(D);
  366. }
  367. void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
  368. AddDecl(D->getTemplatedDecl());
  369. ID.AddInteger(D->getTemplatedDecl()->getODRHash());
  370. Inherited::VisitFunctionTemplateDecl(D);
  371. }
  372. void VisitEnumConstantDecl(const EnumConstantDecl *D) {
  373. AddStmt(D->getInitExpr());
  374. Inherited::VisitEnumConstantDecl(D);
  375. }
  376. };
  377. } // namespace
  378. // Only allow a small portion of Decl's to be processed. Remove this once
  379. // all Decl's can be handled.
  380. bool ODRHash::isDeclToBeProcessed(const Decl *D, const DeclContext *Parent) {
  381. if (D->isImplicit()) return false;
  382. if (D->getDeclContext() != Parent) return false;
  383. switch (D->getKind()) {
  384. default:
  385. return false;
  386. case Decl::AccessSpec:
  387. case Decl::CXXConstructor:
  388. case Decl::CXXDestructor:
  389. case Decl::CXXMethod:
  390. case Decl::EnumConstant: // Only found in EnumDecl's.
  391. case Decl::Field:
  392. case Decl::Friend:
  393. case Decl::FunctionTemplate:
  394. case Decl::StaticAssert:
  395. case Decl::TypeAlias:
  396. case Decl::Typedef:
  397. case Decl::Var:
  398. return true;
  399. }
  400. }
  401. void ODRHash::AddSubDecl(const Decl *D) {
  402. assert(D && "Expecting non-null pointer.");
  403. ODRDeclVisitor(ID, *this).Visit(D);
  404. }
  405. void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
  406. assert(Record && Record->hasDefinition() &&
  407. "Expected non-null record to be a definition.");
  408. const DeclContext *DC = Record;
  409. while (DC) {
  410. if (isa<ClassTemplateSpecializationDecl>(DC)) {
  411. return;
  412. }
  413. DC = DC->getParent();
  414. }
  415. AddDecl(Record);
  416. // Filter out sub-Decls which will not be processed in order to get an
  417. // accurate count of Decl's.
  418. llvm::SmallVector<const Decl *, 16> Decls;
  419. for (Decl *SubDecl : Record->decls()) {
  420. if (isDeclToBeProcessed(SubDecl, Record)) {
  421. Decls.push_back(SubDecl);
  422. if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
  423. // Compute/Preload ODRHash into FunctionDecl.
  424. Function->getODRHash();
  425. }
  426. }
  427. }
  428. ID.AddInteger(Decls.size());
  429. for (auto SubDecl : Decls) {
  430. AddSubDecl(SubDecl);
  431. }
  432. const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
  433. AddBoolean(TD);
  434. if (TD) {
  435. AddTemplateParameterList(TD->getTemplateParameters());
  436. }
  437. ID.AddInteger(Record->getNumBases());
  438. auto Bases = Record->bases();
  439. for (auto Base : Bases) {
  440. AddQualType(Base.getType());
  441. ID.AddInteger(Base.isVirtual());
  442. ID.AddInteger(Base.getAccessSpecifierAsWritten());
  443. }
  444. }
  445. void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
  446. bool SkipBody) {
  447. assert(Function && "Expecting non-null pointer.");
  448. // Skip functions that are specializations or in specialization context.
  449. const DeclContext *DC = Function;
  450. while (DC) {
  451. if (isa<ClassTemplateSpecializationDecl>(DC)) return;
  452. if (auto *F = dyn_cast<FunctionDecl>(DC)) {
  453. if (F->isFunctionTemplateSpecialization()) {
  454. if (!isa<CXXMethodDecl>(DC)) return;
  455. if (DC->getLexicalParent()->isFileContext()) return;
  456. // Inline method specializations are the only supported
  457. // specialization for now.
  458. }
  459. }
  460. DC = DC->getParent();
  461. }
  462. ID.AddInteger(Function->getDeclKind());
  463. const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
  464. AddBoolean(SpecializationArgs);
  465. if (SpecializationArgs) {
  466. ID.AddInteger(SpecializationArgs->size());
  467. for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
  468. AddTemplateArgument(TA);
  469. }
  470. }
  471. if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
  472. AddBoolean(Method->isConst());
  473. AddBoolean(Method->isVolatile());
  474. }
  475. ID.AddInteger(Function->getStorageClass());
  476. AddBoolean(Function->isInlineSpecified());
  477. AddBoolean(Function->isVirtualAsWritten());
  478. AddBoolean(Function->isPure());
  479. AddBoolean(Function->isDeletedAsWritten());
  480. AddBoolean(Function->isExplicitlyDefaulted());
  481. AddDecl(Function);
  482. AddQualType(Function->getReturnType());
  483. ID.AddInteger(Function->param_size());
  484. for (auto Param : Function->parameters())
  485. AddSubDecl(Param);
  486. if (SkipBody) {
  487. AddBoolean(false);
  488. return;
  489. }
  490. const bool HasBody = Function->isThisDeclarationADefinition() &&
  491. !Function->isDefaulted() && !Function->isDeleted() &&
  492. !Function->isLateTemplateParsed();
  493. AddBoolean(HasBody);
  494. if (!HasBody) {
  495. return;
  496. }
  497. auto *Body = Function->getBody();
  498. AddBoolean(Body);
  499. if (Body)
  500. AddStmt(Body);
  501. // Filter out sub-Decls which will not be processed in order to get an
  502. // accurate count of Decl's.
  503. llvm::SmallVector<const Decl *, 16> Decls;
  504. for (Decl *SubDecl : Function->decls()) {
  505. if (isDeclToBeProcessed(SubDecl, Function)) {
  506. Decls.push_back(SubDecl);
  507. }
  508. }
  509. ID.AddInteger(Decls.size());
  510. for (auto SubDecl : Decls) {
  511. AddSubDecl(SubDecl);
  512. }
  513. }
  514. void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
  515. assert(Enum);
  516. AddDeclarationName(Enum->getDeclName());
  517. AddBoolean(Enum->isScoped());
  518. if (Enum->isScoped())
  519. AddBoolean(Enum->isScopedUsingClassTag());
  520. if (Enum->getIntegerTypeSourceInfo())
  521. AddQualType(Enum->getIntegerType());
  522. // Filter out sub-Decls which will not be processed in order to get an
  523. // accurate count of Decl's.
  524. llvm::SmallVector<const Decl *, 16> Decls;
  525. for (Decl *SubDecl : Enum->decls()) {
  526. if (isDeclToBeProcessed(SubDecl, Enum)) {
  527. assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
  528. Decls.push_back(SubDecl);
  529. }
  530. }
  531. ID.AddInteger(Decls.size());
  532. for (auto SubDecl : Decls) {
  533. AddSubDecl(SubDecl);
  534. }
  535. }
  536. void ODRHash::AddDecl(const Decl *D) {
  537. assert(D && "Expecting non-null pointer.");
  538. D = D->getCanonicalDecl();
  539. const NamedDecl *ND = dyn_cast<NamedDecl>(D);
  540. AddBoolean(ND);
  541. if (!ND) {
  542. ID.AddInteger(D->getKind());
  543. return;
  544. }
  545. AddDeclarationName(ND->getDeclName());
  546. const auto *Specialization =
  547. dyn_cast<ClassTemplateSpecializationDecl>(D);
  548. AddBoolean(Specialization);
  549. if (Specialization) {
  550. const TemplateArgumentList &List = Specialization->getTemplateArgs();
  551. ID.AddInteger(List.size());
  552. for (const TemplateArgument &TA : List.asArray())
  553. AddTemplateArgument(TA);
  554. }
  555. }
  556. namespace {
  557. // Process a Type pointer. Add* methods call back into ODRHash while Visit*
  558. // methods process the relevant parts of the Type.
  559. class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
  560. typedef TypeVisitor<ODRTypeVisitor> Inherited;
  561. llvm::FoldingSetNodeID &ID;
  562. ODRHash &Hash;
  563. public:
  564. ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
  565. : ID(ID), Hash(Hash) {}
  566. void AddStmt(Stmt *S) {
  567. Hash.AddBoolean(S);
  568. if (S) {
  569. Hash.AddStmt(S);
  570. }
  571. }
  572. void AddDecl(Decl *D) {
  573. Hash.AddBoolean(D);
  574. if (D) {
  575. Hash.AddDecl(D);
  576. }
  577. }
  578. void AddQualType(QualType T) {
  579. Hash.AddQualType(T);
  580. }
  581. void AddType(const Type *T) {
  582. Hash.AddBoolean(T);
  583. if (T) {
  584. Hash.AddType(T);
  585. }
  586. }
  587. void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
  588. Hash.AddBoolean(NNS);
  589. if (NNS) {
  590. Hash.AddNestedNameSpecifier(NNS);
  591. }
  592. }
  593. void AddIdentifierInfo(const IdentifierInfo *II) {
  594. Hash.AddBoolean(II);
  595. if (II) {
  596. Hash.AddIdentifierInfo(II);
  597. }
  598. }
  599. void VisitQualifiers(Qualifiers Quals) {
  600. ID.AddInteger(Quals.getAsOpaqueValue());
  601. }
  602. // Return the RecordType if the typedef only strips away a keyword.
  603. // Otherwise, return the original type.
  604. static const Type *RemoveTypedef(const Type *T) {
  605. const auto *TypedefT = dyn_cast<TypedefType>(T);
  606. if (!TypedefT) {
  607. return T;
  608. }
  609. const TypedefNameDecl *D = TypedefT->getDecl();
  610. QualType UnderlyingType = D->getUnderlyingType();
  611. if (UnderlyingType.hasLocalQualifiers()) {
  612. return T;
  613. }
  614. const auto *ElaboratedT = dyn_cast<ElaboratedType>(UnderlyingType);
  615. if (!ElaboratedT) {
  616. return T;
  617. }
  618. if (ElaboratedT->getQualifier() != nullptr) {
  619. return T;
  620. }
  621. QualType NamedType = ElaboratedT->getNamedType();
  622. if (NamedType.hasLocalQualifiers()) {
  623. return T;
  624. }
  625. const auto *RecordT = dyn_cast<RecordType>(NamedType);
  626. if (!RecordT) {
  627. return T;
  628. }
  629. const IdentifierInfo *TypedefII = TypedefT->getDecl()->getIdentifier();
  630. const IdentifierInfo *RecordII = RecordT->getDecl()->getIdentifier();
  631. if (!TypedefII || !RecordII ||
  632. TypedefII->getName() != RecordII->getName()) {
  633. return T;
  634. }
  635. return RecordT;
  636. }
  637. void Visit(const Type *T) {
  638. T = RemoveTypedef(T);
  639. ID.AddInteger(T->getTypeClass());
  640. Inherited::Visit(T);
  641. }
  642. void VisitType(const Type *T) {}
  643. void VisitAdjustedType(const AdjustedType *T) {
  644. QualType Original = T->getOriginalType();
  645. QualType Adjusted = T->getAdjustedType();
  646. // The original type and pointee type can be the same, as in the case of
  647. // function pointers decaying to themselves. Set a bool and only process
  648. // the type once, to prevent doubling the work.
  649. SplitQualType split = Adjusted.split();
  650. if (auto Pointer = dyn_cast<PointerType>(split.Ty)) {
  651. if (Pointer->getPointeeType() == Original) {
  652. Hash.AddBoolean(true);
  653. ID.AddInteger(split.Quals.getAsOpaqueValue());
  654. AddQualType(Original);
  655. VisitType(T);
  656. return;
  657. }
  658. }
  659. // The original type and pointee type are different, such as in the case
  660. // of a array decaying to an element pointer. Set a bool to false and
  661. // process both types.
  662. Hash.AddBoolean(false);
  663. AddQualType(Original);
  664. AddQualType(Adjusted);
  665. VisitType(T);
  666. }
  667. void VisitDecayedType(const DecayedType *T) {
  668. // getDecayedType and getPointeeType are derived from getAdjustedType
  669. // and don't need to be separately processed.
  670. VisitAdjustedType(T);
  671. }
  672. void VisitArrayType(const ArrayType *T) {
  673. AddQualType(T->getElementType());
  674. ID.AddInteger(T->getSizeModifier());
  675. VisitQualifiers(T->getIndexTypeQualifiers());
  676. VisitType(T);
  677. }
  678. void VisitConstantArrayType(const ConstantArrayType *T) {
  679. T->getSize().Profile(ID);
  680. VisitArrayType(T);
  681. }
  682. void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
  683. AddStmt(T->getSizeExpr());
  684. VisitArrayType(T);
  685. }
  686. void VisitIncompleteArrayType(const IncompleteArrayType *T) {
  687. VisitArrayType(T);
  688. }
  689. void VisitVariableArrayType(const VariableArrayType *T) {
  690. AddStmt(T->getSizeExpr());
  691. VisitArrayType(T);
  692. }
  693. void VisitAttributedType(const AttributedType *T) {
  694. ID.AddInteger(T->getAttrKind());
  695. AddQualType(T->getModifiedType());
  696. AddQualType(T->getEquivalentType());
  697. VisitType(T);
  698. }
  699. void VisitBlockPointerType(const BlockPointerType *T) {
  700. AddQualType(T->getPointeeType());
  701. VisitType(T);
  702. }
  703. void VisitBuiltinType(const BuiltinType *T) {
  704. ID.AddInteger(T->getKind());
  705. VisitType(T);
  706. }
  707. void VisitComplexType(const ComplexType *T) {
  708. AddQualType(T->getElementType());
  709. VisitType(T);
  710. }
  711. void VisitDecltypeType(const DecltypeType *T) {
  712. AddStmt(T->getUnderlyingExpr());
  713. AddQualType(T->getUnderlyingType());
  714. VisitType(T);
  715. }
  716. void VisitDependentDecltypeType(const DependentDecltypeType *T) {
  717. VisitDecltypeType(T);
  718. }
  719. void VisitDeducedType(const DeducedType *T) {
  720. AddQualType(T->getDeducedType());
  721. VisitType(T);
  722. }
  723. void VisitAutoType(const AutoType *T) {
  724. ID.AddInteger((unsigned)T->getKeyword());
  725. ID.AddInteger(T->isConstrained());
  726. if (T->isConstrained()) {
  727. AddDecl(T->getTypeConstraintConcept());
  728. ID.AddInteger(T->getNumArgs());
  729. for (const auto &TA : T->getTypeConstraintArguments())
  730. Hash.AddTemplateArgument(TA);
  731. }
  732. VisitDeducedType(T);
  733. }
  734. void VisitDeducedTemplateSpecializationType(
  735. const DeducedTemplateSpecializationType *T) {
  736. Hash.AddTemplateName(T->getTemplateName());
  737. VisitDeducedType(T);
  738. }
  739. void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) {
  740. AddQualType(T->getPointeeType());
  741. AddStmt(T->getAddrSpaceExpr());
  742. VisitType(T);
  743. }
  744. void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
  745. AddQualType(T->getElementType());
  746. AddStmt(T->getSizeExpr());
  747. VisitType(T);
  748. }
  749. void VisitFunctionType(const FunctionType *T) {
  750. AddQualType(T->getReturnType());
  751. T->getExtInfo().Profile(ID);
  752. Hash.AddBoolean(T->isConst());
  753. Hash.AddBoolean(T->isVolatile());
  754. Hash.AddBoolean(T->isRestrict());
  755. VisitType(T);
  756. }
  757. void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
  758. VisitFunctionType(T);
  759. }
  760. void VisitFunctionProtoType(const FunctionProtoType *T) {
  761. ID.AddInteger(T->getNumParams());
  762. for (auto ParamType : T->getParamTypes())
  763. AddQualType(ParamType);
  764. VisitFunctionType(T);
  765. }
  766. void VisitInjectedClassNameType(const InjectedClassNameType *T) {
  767. AddDecl(T->getDecl());
  768. VisitType(T);
  769. }
  770. void VisitMemberPointerType(const MemberPointerType *T) {
  771. AddQualType(T->getPointeeType());
  772. AddType(T->getClass());
  773. VisitType(T);
  774. }
  775. void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
  776. AddQualType(T->getPointeeType());
  777. VisitType(T);
  778. }
  779. void VisitObjCObjectType(const ObjCObjectType *T) {
  780. AddDecl(T->getInterface());
  781. auto TypeArgs = T->getTypeArgsAsWritten();
  782. ID.AddInteger(TypeArgs.size());
  783. for (auto Arg : TypeArgs) {
  784. AddQualType(Arg);
  785. }
  786. auto Protocols = T->getProtocols();
  787. ID.AddInteger(Protocols.size());
  788. for (auto Protocol : Protocols) {
  789. AddDecl(Protocol);
  790. }
  791. Hash.AddBoolean(T->isKindOfType());
  792. VisitType(T);
  793. }
  794. void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
  795. // This type is handled by the parent type ObjCObjectType.
  796. VisitObjCObjectType(T);
  797. }
  798. void VisitObjCTypeParamType(const ObjCTypeParamType *T) {
  799. AddDecl(T->getDecl());
  800. auto Protocols = T->getProtocols();
  801. ID.AddInteger(Protocols.size());
  802. for (auto Protocol : Protocols) {
  803. AddDecl(Protocol);
  804. }
  805. VisitType(T);
  806. }
  807. void VisitPackExpansionType(const PackExpansionType *T) {
  808. AddQualType(T->getPattern());
  809. VisitType(T);
  810. }
  811. void VisitParenType(const ParenType *T) {
  812. AddQualType(T->getInnerType());
  813. VisitType(T);
  814. }
  815. void VisitPipeType(const PipeType *T) {
  816. AddQualType(T->getElementType());
  817. Hash.AddBoolean(T->isReadOnly());
  818. VisitType(T);
  819. }
  820. void VisitPointerType(const PointerType *T) {
  821. AddQualType(T->getPointeeType());
  822. VisitType(T);
  823. }
  824. void VisitReferenceType(const ReferenceType *T) {
  825. AddQualType(T->getPointeeTypeAsWritten());
  826. VisitType(T);
  827. }
  828. void VisitLValueReferenceType(const LValueReferenceType *T) {
  829. VisitReferenceType(T);
  830. }
  831. void VisitRValueReferenceType(const RValueReferenceType *T) {
  832. VisitReferenceType(T);
  833. }
  834. void
  835. VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
  836. AddType(T->getReplacedParameter());
  837. Hash.AddTemplateArgument(T->getArgumentPack());
  838. VisitType(T);
  839. }
  840. void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
  841. AddType(T->getReplacedParameter());
  842. AddQualType(T->getReplacementType());
  843. VisitType(T);
  844. }
  845. void VisitTagType(const TagType *T) {
  846. AddDecl(T->getDecl());
  847. VisitType(T);
  848. }
  849. void VisitRecordType(const RecordType *T) { VisitTagType(T); }
  850. void VisitEnumType(const EnumType *T) { VisitTagType(T); }
  851. void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
  852. ID.AddInteger(T->getNumArgs());
  853. for (const auto &TA : T->template_arguments()) {
  854. Hash.AddTemplateArgument(TA);
  855. }
  856. Hash.AddTemplateName(T->getTemplateName());
  857. VisitType(T);
  858. }
  859. void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
  860. ID.AddInteger(T->getDepth());
  861. ID.AddInteger(T->getIndex());
  862. Hash.AddBoolean(T->isParameterPack());
  863. AddDecl(T->getDecl());
  864. }
  865. void VisitTypedefType(const TypedefType *T) {
  866. AddDecl(T->getDecl());
  867. QualType UnderlyingType = T->getDecl()->getUnderlyingType();
  868. VisitQualifiers(UnderlyingType.getQualifiers());
  869. while (true) {
  870. if (const TypedefType *Underlying =
  871. dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
  872. UnderlyingType = Underlying->getDecl()->getUnderlyingType();
  873. continue;
  874. }
  875. if (const ElaboratedType *Underlying =
  876. dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
  877. UnderlyingType = Underlying->getNamedType();
  878. continue;
  879. }
  880. break;
  881. }
  882. AddType(UnderlyingType.getTypePtr());
  883. VisitType(T);
  884. }
  885. void VisitTypeOfExprType(const TypeOfExprType *T) {
  886. AddStmt(T->getUnderlyingExpr());
  887. Hash.AddBoolean(T->isSugared());
  888. if (T->isSugared())
  889. AddQualType(T->desugar());
  890. VisitType(T);
  891. }
  892. void VisitTypeOfType(const TypeOfType *T) {
  893. AddQualType(T->getUnderlyingType());
  894. VisitType(T);
  895. }
  896. void VisitTypeWithKeyword(const TypeWithKeyword *T) {
  897. ID.AddInteger(T->getKeyword());
  898. VisitType(T);
  899. };
  900. void VisitDependentNameType(const DependentNameType *T) {
  901. AddNestedNameSpecifier(T->getQualifier());
  902. AddIdentifierInfo(T->getIdentifier());
  903. VisitTypeWithKeyword(T);
  904. }
  905. void VisitDependentTemplateSpecializationType(
  906. const DependentTemplateSpecializationType *T) {
  907. AddIdentifierInfo(T->getIdentifier());
  908. AddNestedNameSpecifier(T->getQualifier());
  909. ID.AddInteger(T->getNumArgs());
  910. for (const auto &TA : T->template_arguments()) {
  911. Hash.AddTemplateArgument(TA);
  912. }
  913. VisitTypeWithKeyword(T);
  914. }
  915. void VisitElaboratedType(const ElaboratedType *T) {
  916. AddNestedNameSpecifier(T->getQualifier());
  917. AddQualType(T->getNamedType());
  918. VisitTypeWithKeyword(T);
  919. }
  920. void VisitUnaryTransformType(const UnaryTransformType *T) {
  921. AddQualType(T->getUnderlyingType());
  922. AddQualType(T->getBaseType());
  923. VisitType(T);
  924. }
  925. void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
  926. AddDecl(T->getDecl());
  927. VisitType(T);
  928. }
  929. void VisitVectorType(const VectorType *T) {
  930. AddQualType(T->getElementType());
  931. ID.AddInteger(T->getNumElements());
  932. ID.AddInteger(T->getVectorKind());
  933. VisitType(T);
  934. }
  935. void VisitExtVectorType(const ExtVectorType * T) {
  936. VisitVectorType(T);
  937. }
  938. };
  939. } // namespace
  940. void ODRHash::AddType(const Type *T) {
  941. assert(T && "Expecting non-null pointer.");
  942. ODRTypeVisitor(ID, *this).Visit(T);
  943. }
  944. void ODRHash::AddQualType(QualType T) {
  945. AddBoolean(T.isNull());
  946. if (T.isNull())
  947. return;
  948. SplitQualType split = T.split();
  949. ID.AddInteger(split.Quals.getAsOpaqueValue());
  950. AddType(split.Ty);
  951. }
  952. void ODRHash::AddBoolean(bool Value) {
  953. Bools.push_back(Value);
  954. }