ODRDiagsEmitter.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  1. //===-- ODRDiagsEmitter.cpp - Diagnostics for ODR mismatches ----*- 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. #include "clang/AST/ODRDiagsEmitter.h"
  9. #include "clang/AST/DeclFriend.h"
  10. #include "clang/AST/DeclTemplate.h"
  11. #include "clang/AST/ODRHash.h"
  12. #include "clang/Basic/DiagnosticAST.h"
  13. #include "clang/Basic/Module.h"
  14. using namespace clang;
  15. static unsigned computeODRHash(QualType Ty) {
  16. ODRHash Hasher;
  17. Hasher.AddQualType(Ty);
  18. return Hasher.CalculateHash();
  19. }
  20. static unsigned computeODRHash(const Stmt *S) {
  21. ODRHash Hasher;
  22. Hasher.AddStmt(S);
  23. return Hasher.CalculateHash();
  24. }
  25. static unsigned computeODRHash(const Decl *D) {
  26. assert(D);
  27. ODRHash Hasher;
  28. Hasher.AddSubDecl(D);
  29. return Hasher.CalculateHash();
  30. }
  31. static unsigned computeODRHash(const TemplateArgument &TA) {
  32. ODRHash Hasher;
  33. Hasher.AddTemplateArgument(TA);
  34. return Hasher.CalculateHash();
  35. }
  36. std::string ODRDiagsEmitter::getOwningModuleNameForDiagnostic(const Decl *D) {
  37. // If we know the owning module, use it.
  38. if (Module *M = D->getImportedOwningModule())
  39. return M->getFullModuleName();
  40. // Not from a module.
  41. return {};
  42. }
  43. template <typename MethodT>
  44. static bool diagnoseSubMismatchMethodParameters(DiagnosticsEngine &Diags,
  45. const NamedDecl *FirstContainer,
  46. StringRef FirstModule,
  47. StringRef SecondModule,
  48. const MethodT *FirstMethod,
  49. const MethodT *SecondMethod) {
  50. enum DiagMethodType {
  51. DiagMethod,
  52. DiagConstructor,
  53. DiagDestructor,
  54. };
  55. auto GetDiagMethodType = [](const NamedDecl *D) {
  56. if (isa<CXXConstructorDecl>(D))
  57. return DiagConstructor;
  58. if (isa<CXXDestructorDecl>(D))
  59. return DiagDestructor;
  60. return DiagMethod;
  61. };
  62. enum ODRMethodParametersDifference {
  63. NumberParameters,
  64. ParameterType,
  65. ParameterName,
  66. };
  67. auto DiagError = [&Diags, &GetDiagMethodType, FirstContainer, FirstModule,
  68. FirstMethod](ODRMethodParametersDifference DiffType) {
  69. DeclarationName FirstName = FirstMethod->getDeclName();
  70. DiagMethodType FirstMethodType = GetDiagMethodType(FirstMethod);
  71. return Diags.Report(FirstMethod->getLocation(),
  72. diag::err_module_odr_violation_method_params)
  73. << FirstContainer << FirstModule.empty() << FirstModule
  74. << FirstMethod->getSourceRange() << DiffType << FirstMethodType
  75. << FirstName;
  76. };
  77. auto DiagNote = [&Diags, &GetDiagMethodType, SecondModule,
  78. SecondMethod](ODRMethodParametersDifference DiffType) {
  79. DeclarationName SecondName = SecondMethod->getDeclName();
  80. DiagMethodType SecondMethodType = GetDiagMethodType(SecondMethod);
  81. return Diags.Report(SecondMethod->getLocation(),
  82. diag::note_module_odr_violation_method_params)
  83. << SecondModule.empty() << SecondModule
  84. << SecondMethod->getSourceRange() << DiffType << SecondMethodType
  85. << SecondName;
  86. };
  87. const unsigned FirstNumParameters = FirstMethod->param_size();
  88. const unsigned SecondNumParameters = SecondMethod->param_size();
  89. if (FirstNumParameters != SecondNumParameters) {
  90. DiagError(NumberParameters) << FirstNumParameters;
  91. DiagNote(NumberParameters) << SecondNumParameters;
  92. return true;
  93. }
  94. for (unsigned I = 0; I < FirstNumParameters; ++I) {
  95. const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
  96. const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
  97. QualType FirstParamType = FirstParam->getType();
  98. QualType SecondParamType = SecondParam->getType();
  99. if (FirstParamType != SecondParamType &&
  100. computeODRHash(FirstParamType) != computeODRHash(SecondParamType)) {
  101. if (const DecayedType *ParamDecayedType =
  102. FirstParamType->getAs<DecayedType>()) {
  103. DiagError(ParameterType) << (I + 1) << FirstParamType << true
  104. << ParamDecayedType->getOriginalType();
  105. } else {
  106. DiagError(ParameterType) << (I + 1) << FirstParamType << false;
  107. }
  108. if (const DecayedType *ParamDecayedType =
  109. SecondParamType->getAs<DecayedType>()) {
  110. DiagNote(ParameterType) << (I + 1) << SecondParamType << true
  111. << ParamDecayedType->getOriginalType();
  112. } else {
  113. DiagNote(ParameterType) << (I + 1) << SecondParamType << false;
  114. }
  115. return true;
  116. }
  117. DeclarationName FirstParamName = FirstParam->getDeclName();
  118. DeclarationName SecondParamName = SecondParam->getDeclName();
  119. if (FirstParamName != SecondParamName) {
  120. DiagError(ParameterName) << (I + 1) << FirstParamName;
  121. DiagNote(ParameterName) << (I + 1) << SecondParamName;
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. bool ODRDiagsEmitter::diagnoseSubMismatchField(
  128. const NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
  129. const FieldDecl *FirstField, const FieldDecl *SecondField) const {
  130. enum ODRFieldDifference {
  131. FieldName,
  132. FieldTypeName,
  133. FieldSingleBitField,
  134. FieldDifferentWidthBitField,
  135. FieldSingleMutable,
  136. FieldSingleInitializer,
  137. FieldDifferentInitializers,
  138. };
  139. auto DiagError = [FirstRecord, FirstField, FirstModule,
  140. this](ODRFieldDifference DiffType) {
  141. return Diag(FirstField->getLocation(), diag::err_module_odr_violation_field)
  142. << FirstRecord << FirstModule.empty() << FirstModule
  143. << FirstField->getSourceRange() << DiffType;
  144. };
  145. auto DiagNote = [SecondField, SecondModule,
  146. this](ODRFieldDifference DiffType) {
  147. return Diag(SecondField->getLocation(),
  148. diag::note_module_odr_violation_field)
  149. << SecondModule.empty() << SecondModule << SecondField->getSourceRange() << DiffType;
  150. };
  151. IdentifierInfo *FirstII = FirstField->getIdentifier();
  152. IdentifierInfo *SecondII = SecondField->getIdentifier();
  153. if (FirstII->getName() != SecondII->getName()) {
  154. DiagError(FieldName) << FirstII;
  155. DiagNote(FieldName) << SecondII;
  156. return true;
  157. }
  158. QualType FirstType = FirstField->getType();
  159. QualType SecondType = SecondField->getType();
  160. if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
  161. DiagError(FieldTypeName) << FirstII << FirstType;
  162. DiagNote(FieldTypeName) << SecondII << SecondType;
  163. return true;
  164. }
  165. assert(Context.hasSameType(FirstField->getType(), SecondField->getType()));
  166. (void)Context;
  167. const bool IsFirstBitField = FirstField->isBitField();
  168. const bool IsSecondBitField = SecondField->isBitField();
  169. if (IsFirstBitField != IsSecondBitField) {
  170. DiagError(FieldSingleBitField) << FirstII << IsFirstBitField;
  171. DiagNote(FieldSingleBitField) << SecondII << IsSecondBitField;
  172. return true;
  173. }
  174. if (IsFirstBitField && IsSecondBitField) {
  175. unsigned FirstBitWidthHash = computeODRHash(FirstField->getBitWidth());
  176. unsigned SecondBitWidthHash = computeODRHash(SecondField->getBitWidth());
  177. if (FirstBitWidthHash != SecondBitWidthHash) {
  178. DiagError(FieldDifferentWidthBitField)
  179. << FirstII << FirstField->getBitWidth()->getSourceRange();
  180. DiagNote(FieldDifferentWidthBitField)
  181. << SecondII << SecondField->getBitWidth()->getSourceRange();
  182. return true;
  183. }
  184. }
  185. if (!LangOpts.CPlusPlus)
  186. return false;
  187. const bool IsFirstMutable = FirstField->isMutable();
  188. const bool IsSecondMutable = SecondField->isMutable();
  189. if (IsFirstMutable != IsSecondMutable) {
  190. DiagError(FieldSingleMutable) << FirstII << IsFirstMutable;
  191. DiagNote(FieldSingleMutable) << SecondII << IsSecondMutable;
  192. return true;
  193. }
  194. const Expr *FirstInitializer = FirstField->getInClassInitializer();
  195. const Expr *SecondInitializer = SecondField->getInClassInitializer();
  196. if ((!FirstInitializer && SecondInitializer) ||
  197. (FirstInitializer && !SecondInitializer)) {
  198. DiagError(FieldSingleInitializer)
  199. << FirstII << (FirstInitializer != nullptr);
  200. DiagNote(FieldSingleInitializer)
  201. << SecondII << (SecondInitializer != nullptr);
  202. return true;
  203. }
  204. if (FirstInitializer && SecondInitializer) {
  205. unsigned FirstInitHash = computeODRHash(FirstInitializer);
  206. unsigned SecondInitHash = computeODRHash(SecondInitializer);
  207. if (FirstInitHash != SecondInitHash) {
  208. DiagError(FieldDifferentInitializers)
  209. << FirstII << FirstInitializer->getSourceRange();
  210. DiagNote(FieldDifferentInitializers)
  211. << SecondII << SecondInitializer->getSourceRange();
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. bool ODRDiagsEmitter::diagnoseSubMismatchTypedef(
  218. const NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
  219. const TypedefNameDecl *FirstTD, const TypedefNameDecl *SecondTD,
  220. bool IsTypeAlias) const {
  221. enum ODRTypedefDifference {
  222. TypedefName,
  223. TypedefType,
  224. };
  225. auto DiagError = [FirstRecord, FirstTD, FirstModule,
  226. this](ODRTypedefDifference DiffType) {
  227. return Diag(FirstTD->getLocation(), diag::err_module_odr_violation_typedef)
  228. << FirstRecord << FirstModule.empty() << FirstModule
  229. << FirstTD->getSourceRange() << DiffType;
  230. };
  231. auto DiagNote = [SecondTD, SecondModule,
  232. this](ODRTypedefDifference DiffType) {
  233. return Diag(SecondTD->getLocation(),
  234. diag::note_module_odr_violation_typedef)
  235. << SecondModule << SecondTD->getSourceRange() << DiffType;
  236. };
  237. DeclarationName FirstName = FirstTD->getDeclName();
  238. DeclarationName SecondName = SecondTD->getDeclName();
  239. if (FirstName != SecondName) {
  240. DiagError(TypedefName) << IsTypeAlias << FirstName;
  241. DiagNote(TypedefName) << IsTypeAlias << SecondName;
  242. return true;
  243. }
  244. QualType FirstType = FirstTD->getUnderlyingType();
  245. QualType SecondType = SecondTD->getUnderlyingType();
  246. if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
  247. DiagError(TypedefType) << IsTypeAlias << FirstName << FirstType;
  248. DiagNote(TypedefType) << IsTypeAlias << SecondName << SecondType;
  249. return true;
  250. }
  251. return false;
  252. }
  253. bool ODRDiagsEmitter::diagnoseSubMismatchVar(const NamedDecl *FirstRecord,
  254. StringRef FirstModule,
  255. StringRef SecondModule,
  256. const VarDecl *FirstVD,
  257. const VarDecl *SecondVD) const {
  258. enum ODRVarDifference {
  259. VarName,
  260. VarType,
  261. VarSingleInitializer,
  262. VarDifferentInitializer,
  263. VarConstexpr,
  264. };
  265. auto DiagError = [FirstRecord, FirstVD, FirstModule,
  266. this](ODRVarDifference DiffType) {
  267. return Diag(FirstVD->getLocation(), diag::err_module_odr_violation_variable)
  268. << FirstRecord << FirstModule.empty() << FirstModule
  269. << FirstVD->getSourceRange() << DiffType;
  270. };
  271. auto DiagNote = [SecondVD, SecondModule, this](ODRVarDifference DiffType) {
  272. return Diag(SecondVD->getLocation(),
  273. diag::note_module_odr_violation_variable)
  274. << SecondModule << SecondVD->getSourceRange() << DiffType;
  275. };
  276. DeclarationName FirstName = FirstVD->getDeclName();
  277. DeclarationName SecondName = SecondVD->getDeclName();
  278. if (FirstName != SecondName) {
  279. DiagError(VarName) << FirstName;
  280. DiagNote(VarName) << SecondName;
  281. return true;
  282. }
  283. QualType FirstType = FirstVD->getType();
  284. QualType SecondType = SecondVD->getType();
  285. if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
  286. DiagError(VarType) << FirstName << FirstType;
  287. DiagNote(VarType) << SecondName << SecondType;
  288. return true;
  289. }
  290. if (!LangOpts.CPlusPlus)
  291. return false;
  292. const Expr *FirstInit = FirstVD->getInit();
  293. const Expr *SecondInit = SecondVD->getInit();
  294. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  295. DiagError(VarSingleInitializer)
  296. << FirstName << (FirstInit == nullptr)
  297. << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
  298. DiagNote(VarSingleInitializer)
  299. << SecondName << (SecondInit == nullptr)
  300. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  301. return true;
  302. }
  303. if (FirstInit && SecondInit &&
  304. computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
  305. DiagError(VarDifferentInitializer)
  306. << FirstName << FirstInit->getSourceRange();
  307. DiagNote(VarDifferentInitializer)
  308. << SecondName << SecondInit->getSourceRange();
  309. return true;
  310. }
  311. const bool FirstIsConstexpr = FirstVD->isConstexpr();
  312. const bool SecondIsConstexpr = SecondVD->isConstexpr();
  313. if (FirstIsConstexpr != SecondIsConstexpr) {
  314. DiagError(VarConstexpr) << FirstName << FirstIsConstexpr;
  315. DiagNote(VarConstexpr) << SecondName << SecondIsConstexpr;
  316. return true;
  317. }
  318. return false;
  319. }
  320. bool ODRDiagsEmitter::diagnoseSubMismatchProtocols(
  321. const ObjCProtocolList &FirstProtocols,
  322. const ObjCContainerDecl *FirstContainer, StringRef FirstModule,
  323. const ObjCProtocolList &SecondProtocols,
  324. const ObjCContainerDecl *SecondContainer, StringRef SecondModule) const {
  325. // Keep in sync with err_module_odr_violation_referenced_protocols.
  326. enum ODRReferencedProtocolDifference {
  327. NumProtocols,
  328. ProtocolType,
  329. };
  330. auto DiagRefProtocolError = [FirstContainer, FirstModule,
  331. this](SourceLocation Loc, SourceRange Range,
  332. ODRReferencedProtocolDifference DiffType) {
  333. return Diag(Loc, diag::err_module_odr_violation_referenced_protocols)
  334. << FirstContainer << FirstModule.empty() << FirstModule << Range
  335. << DiffType;
  336. };
  337. auto DiagRefProtocolNote = [SecondModule,
  338. this](SourceLocation Loc, SourceRange Range,
  339. ODRReferencedProtocolDifference DiffType) {
  340. return Diag(Loc, diag::note_module_odr_violation_referenced_protocols)
  341. << SecondModule.empty() << SecondModule << Range << DiffType;
  342. };
  343. auto GetProtoListSourceRange = [](const ObjCProtocolList &PL) {
  344. if (PL.empty())
  345. return SourceRange();
  346. return SourceRange(*PL.loc_begin(), *std::prev(PL.loc_end()));
  347. };
  348. if (FirstProtocols.size() != SecondProtocols.size()) {
  349. DiagRefProtocolError(FirstContainer->getLocation(),
  350. GetProtoListSourceRange(FirstProtocols), NumProtocols)
  351. << FirstProtocols.size();
  352. DiagRefProtocolNote(SecondContainer->getLocation(),
  353. GetProtoListSourceRange(SecondProtocols), NumProtocols)
  354. << SecondProtocols.size();
  355. return true;
  356. }
  357. for (unsigned I = 0, E = FirstProtocols.size(); I != E; ++I) {
  358. const ObjCProtocolDecl *FirstProtocol = FirstProtocols[I];
  359. const ObjCProtocolDecl *SecondProtocol = SecondProtocols[I];
  360. DeclarationName FirstProtocolName = FirstProtocol->getDeclName();
  361. DeclarationName SecondProtocolName = SecondProtocol->getDeclName();
  362. if (FirstProtocolName != SecondProtocolName) {
  363. SourceLocation FirstLoc = *(FirstProtocols.loc_begin() + I);
  364. SourceLocation SecondLoc = *(SecondProtocols.loc_begin() + I);
  365. SourceRange EmptyRange;
  366. DiagRefProtocolError(FirstLoc, EmptyRange, ProtocolType)
  367. << (I + 1) << FirstProtocolName;
  368. DiagRefProtocolNote(SecondLoc, EmptyRange, ProtocolType)
  369. << (I + 1) << SecondProtocolName;
  370. return true;
  371. }
  372. }
  373. return false;
  374. }
  375. bool ODRDiagsEmitter::diagnoseSubMismatchObjCMethod(
  376. const NamedDecl *FirstObjCContainer, StringRef FirstModule,
  377. StringRef SecondModule, const ObjCMethodDecl *FirstMethod,
  378. const ObjCMethodDecl *SecondMethod) const {
  379. enum ODRMethodDifference {
  380. ReturnType,
  381. InstanceOrClass,
  382. ControlLevel, // optional/required
  383. DesignatedInitializer,
  384. Directness,
  385. Name,
  386. };
  387. auto DiagError = [FirstObjCContainer, FirstModule, FirstMethod,
  388. this](ODRMethodDifference DiffType) {
  389. return Diag(FirstMethod->getLocation(),
  390. diag::err_module_odr_violation_objc_method)
  391. << FirstObjCContainer << FirstModule.empty() << FirstModule
  392. << FirstMethod->getSourceRange() << DiffType;
  393. };
  394. auto DiagNote = [SecondModule, SecondMethod,
  395. this](ODRMethodDifference DiffType) {
  396. return Diag(SecondMethod->getLocation(),
  397. diag::note_module_odr_violation_objc_method)
  398. << SecondModule.empty() << SecondModule
  399. << SecondMethod->getSourceRange() << DiffType;
  400. };
  401. if (computeODRHash(FirstMethod->getReturnType()) !=
  402. computeODRHash(SecondMethod->getReturnType())) {
  403. DiagError(ReturnType) << FirstMethod << FirstMethod->getReturnType();
  404. DiagNote(ReturnType) << SecondMethod << SecondMethod->getReturnType();
  405. return true;
  406. }
  407. if (FirstMethod->isInstanceMethod() != SecondMethod->isInstanceMethod()) {
  408. DiagError(InstanceOrClass)
  409. << FirstMethod << FirstMethod->isInstanceMethod();
  410. DiagNote(InstanceOrClass)
  411. << SecondMethod << SecondMethod->isInstanceMethod();
  412. return true;
  413. }
  414. if (FirstMethod->getImplementationControl() !=
  415. SecondMethod->getImplementationControl()) {
  416. DiagError(ControlLevel) << FirstMethod->getImplementationControl();
  417. DiagNote(ControlLevel) << SecondMethod->getImplementationControl();
  418. return true;
  419. }
  420. if (FirstMethod->isThisDeclarationADesignatedInitializer() !=
  421. SecondMethod->isThisDeclarationADesignatedInitializer()) {
  422. DiagError(DesignatedInitializer)
  423. << FirstMethod
  424. << FirstMethod->isThisDeclarationADesignatedInitializer();
  425. DiagNote(DesignatedInitializer)
  426. << SecondMethod
  427. << SecondMethod->isThisDeclarationADesignatedInitializer();
  428. return true;
  429. }
  430. if (FirstMethod->isDirectMethod() != SecondMethod->isDirectMethod()) {
  431. DiagError(Directness) << FirstMethod << FirstMethod->isDirectMethod();
  432. DiagNote(Directness) << SecondMethod << SecondMethod->isDirectMethod();
  433. return true;
  434. }
  435. if (diagnoseSubMismatchMethodParameters(Diags, FirstObjCContainer,
  436. FirstModule, SecondModule,
  437. FirstMethod, SecondMethod))
  438. return true;
  439. // Check method name *after* looking at the parameters otherwise we get a
  440. // less ideal diagnostics: a ObjCMethodName mismatch given that selectors
  441. // for different parameters are likely to be different.
  442. DeclarationName FirstName = FirstMethod->getDeclName();
  443. DeclarationName SecondName = SecondMethod->getDeclName();
  444. if (FirstName != SecondName) {
  445. DiagError(Name) << FirstName;
  446. DiagNote(Name) << SecondName;
  447. return true;
  448. }
  449. return false;
  450. }
  451. bool ODRDiagsEmitter::diagnoseSubMismatchObjCProperty(
  452. const NamedDecl *FirstObjCContainer, StringRef FirstModule,
  453. StringRef SecondModule, const ObjCPropertyDecl *FirstProp,
  454. const ObjCPropertyDecl *SecondProp) const {
  455. enum ODRPropertyDifference {
  456. Name,
  457. Type,
  458. ControlLevel, // optional/required
  459. Attribute,
  460. };
  461. auto DiagError = [FirstObjCContainer, FirstModule, FirstProp,
  462. this](SourceLocation Loc, ODRPropertyDifference DiffType) {
  463. return Diag(Loc, diag::err_module_odr_violation_objc_property)
  464. << FirstObjCContainer << FirstModule.empty() << FirstModule
  465. << FirstProp->getSourceRange() << DiffType;
  466. };
  467. auto DiagNote = [SecondModule, SecondProp,
  468. this](SourceLocation Loc, ODRPropertyDifference DiffType) {
  469. return Diag(Loc, diag::note_module_odr_violation_objc_property)
  470. << SecondModule.empty() << SecondModule
  471. << SecondProp->getSourceRange() << DiffType;
  472. };
  473. IdentifierInfo *FirstII = FirstProp->getIdentifier();
  474. IdentifierInfo *SecondII = SecondProp->getIdentifier();
  475. if (FirstII->getName() != SecondII->getName()) {
  476. DiagError(FirstProp->getLocation(), Name) << FirstII;
  477. DiagNote(SecondProp->getLocation(), Name) << SecondII;
  478. return true;
  479. }
  480. if (computeODRHash(FirstProp->getType()) !=
  481. computeODRHash(SecondProp->getType())) {
  482. DiagError(FirstProp->getLocation(), Type)
  483. << FirstII << FirstProp->getType();
  484. DiagNote(SecondProp->getLocation(), Type)
  485. << SecondII << SecondProp->getType();
  486. return true;
  487. }
  488. if (FirstProp->getPropertyImplementation() !=
  489. SecondProp->getPropertyImplementation()) {
  490. DiagError(FirstProp->getLocation(), ControlLevel)
  491. << FirstProp->getPropertyImplementation();
  492. DiagNote(SecondProp->getLocation(), ControlLevel)
  493. << SecondProp->getPropertyImplementation();
  494. return true;
  495. }
  496. // Go over the property attributes and stop at the first mismatch.
  497. unsigned FirstAttrs = (unsigned)FirstProp->getPropertyAttributes();
  498. unsigned SecondAttrs = (unsigned)SecondProp->getPropertyAttributes();
  499. if (FirstAttrs != SecondAttrs) {
  500. for (unsigned I = 0; I < NumObjCPropertyAttrsBits; ++I) {
  501. unsigned CheckedAttr = (1 << I);
  502. if ((FirstAttrs & CheckedAttr) == (SecondAttrs & CheckedAttr))
  503. continue;
  504. bool IsFirstWritten =
  505. (unsigned)FirstProp->getPropertyAttributesAsWritten() & CheckedAttr;
  506. bool IsSecondWritten =
  507. (unsigned)SecondProp->getPropertyAttributesAsWritten() & CheckedAttr;
  508. DiagError(IsFirstWritten ? FirstProp->getLParenLoc()
  509. : FirstProp->getLocation(),
  510. Attribute)
  511. << FirstII << (I + 1) << IsFirstWritten;
  512. DiagNote(IsSecondWritten ? SecondProp->getLParenLoc()
  513. : SecondProp->getLocation(),
  514. Attribute)
  515. << SecondII << (I + 1);
  516. return true;
  517. }
  518. }
  519. return false;
  520. }
  521. ODRDiagsEmitter::DiffResult
  522. ODRDiagsEmitter::FindTypeDiffs(DeclHashes &FirstHashes,
  523. DeclHashes &SecondHashes) {
  524. auto DifferenceSelector = [](const Decl *D) {
  525. assert(D && "valid Decl required");
  526. switch (D->getKind()) {
  527. default:
  528. return Other;
  529. case Decl::AccessSpec:
  530. switch (D->getAccess()) {
  531. case AS_public:
  532. return PublicSpecifer;
  533. case AS_private:
  534. return PrivateSpecifer;
  535. case AS_protected:
  536. return ProtectedSpecifer;
  537. case AS_none:
  538. break;
  539. }
  540. llvm_unreachable("Invalid access specifier");
  541. case Decl::StaticAssert:
  542. return StaticAssert;
  543. case Decl::Field:
  544. return Field;
  545. case Decl::CXXMethod:
  546. case Decl::CXXConstructor:
  547. case Decl::CXXDestructor:
  548. return CXXMethod;
  549. case Decl::TypeAlias:
  550. return TypeAlias;
  551. case Decl::Typedef:
  552. return TypeDef;
  553. case Decl::Var:
  554. return Var;
  555. case Decl::Friend:
  556. return Friend;
  557. case Decl::FunctionTemplate:
  558. return FunctionTemplate;
  559. case Decl::ObjCMethod:
  560. return ObjCMethod;
  561. case Decl::ObjCIvar:
  562. return ObjCIvar;
  563. case Decl::ObjCProperty:
  564. return ObjCProperty;
  565. }
  566. };
  567. DiffResult DR;
  568. auto FirstIt = FirstHashes.begin();
  569. auto SecondIt = SecondHashes.begin();
  570. while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
  571. if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
  572. FirstIt->second == SecondIt->second) {
  573. ++FirstIt;
  574. ++SecondIt;
  575. continue;
  576. }
  577. DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
  578. DR.SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
  579. DR.FirstDiffType =
  580. DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
  581. DR.SecondDiffType =
  582. DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
  583. return DR;
  584. }
  585. return DR;
  586. }
  587. void ODRDiagsEmitter::diagnoseSubMismatchUnexpected(
  588. DiffResult &DR, const NamedDecl *FirstRecord, StringRef FirstModule,
  589. const NamedDecl *SecondRecord, StringRef SecondModule) const {
  590. Diag(FirstRecord->getLocation(),
  591. diag::err_module_odr_violation_different_definitions)
  592. << FirstRecord << FirstModule.empty() << FirstModule;
  593. if (DR.FirstDecl) {
  594. Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
  595. << FirstRecord << DR.FirstDecl->getSourceRange();
  596. }
  597. Diag(SecondRecord->getLocation(),
  598. diag::note_module_odr_violation_different_definitions)
  599. << SecondModule;
  600. if (DR.SecondDecl) {
  601. Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
  602. << DR.SecondDecl->getSourceRange();
  603. }
  604. }
  605. void ODRDiagsEmitter::diagnoseSubMismatchDifferentDeclKinds(
  606. DiffResult &DR, const NamedDecl *FirstRecord, StringRef FirstModule,
  607. const NamedDecl *SecondRecord, StringRef SecondModule) const {
  608. auto GetMismatchedDeclLoc = [](const NamedDecl *Container,
  609. ODRMismatchDecl DiffType, const Decl *D) {
  610. SourceLocation Loc;
  611. SourceRange Range;
  612. if (DiffType == EndOfClass) {
  613. if (auto *Tag = dyn_cast<TagDecl>(Container))
  614. Loc = Tag->getBraceRange().getEnd();
  615. else if (auto *IF = dyn_cast<ObjCInterfaceDecl>(Container))
  616. Loc = IF->getAtEndRange().getBegin();
  617. else
  618. Loc = Container->getEndLoc();
  619. } else {
  620. Loc = D->getLocation();
  621. Range = D->getSourceRange();
  622. }
  623. return std::make_pair(Loc, Range);
  624. };
  625. auto FirstDiagInfo =
  626. GetMismatchedDeclLoc(FirstRecord, DR.FirstDiffType, DR.FirstDecl);
  627. Diag(FirstDiagInfo.first, diag::err_module_odr_violation_mismatch_decl)
  628. << FirstRecord << FirstModule.empty() << FirstModule
  629. << FirstDiagInfo.second << DR.FirstDiffType;
  630. auto SecondDiagInfo =
  631. GetMismatchedDeclLoc(SecondRecord, DR.SecondDiffType, DR.SecondDecl);
  632. Diag(SecondDiagInfo.first, diag::note_module_odr_violation_mismatch_decl)
  633. << SecondModule.empty() << SecondModule << SecondDiagInfo.second
  634. << DR.SecondDiffType;
  635. }
  636. bool ODRDiagsEmitter::diagnoseMismatch(
  637. const CXXRecordDecl *FirstRecord, const CXXRecordDecl *SecondRecord,
  638. const struct CXXRecordDecl::DefinitionData *SecondDD) const {
  639. // Multiple different declarations got merged together; tell the user
  640. // where they came from.
  641. if (FirstRecord == SecondRecord)
  642. return false;
  643. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
  644. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
  645. const struct CXXRecordDecl::DefinitionData *FirstDD =
  646. FirstRecord->DefinitionData;
  647. assert(FirstDD && SecondDD && "Definitions without DefinitionData");
  648. // Diagnostics from DefinitionData are emitted here.
  649. if (FirstDD != SecondDD) {
  650. // Keep in sync with err_module_odr_violation_definition_data.
  651. enum ODRDefinitionDataDifference {
  652. NumBases,
  653. NumVBases,
  654. BaseType,
  655. BaseVirtual,
  656. BaseAccess,
  657. };
  658. auto DiagBaseError = [FirstRecord, &FirstModule,
  659. this](SourceLocation Loc, SourceRange Range,
  660. ODRDefinitionDataDifference DiffType) {
  661. return Diag(Loc, diag::err_module_odr_violation_definition_data)
  662. << FirstRecord << FirstModule.empty() << FirstModule << Range
  663. << DiffType;
  664. };
  665. auto DiagBaseNote = [&SecondModule,
  666. this](SourceLocation Loc, SourceRange Range,
  667. ODRDefinitionDataDifference DiffType) {
  668. return Diag(Loc, diag::note_module_odr_violation_definition_data)
  669. << SecondModule << Range << DiffType;
  670. };
  671. auto GetSourceRange = [](const struct CXXRecordDecl::DefinitionData *DD) {
  672. unsigned NumBases = DD->NumBases;
  673. if (NumBases == 0)
  674. return SourceRange();
  675. ArrayRef<CXXBaseSpecifier> bases = DD->bases();
  676. return SourceRange(bases[0].getBeginLoc(),
  677. bases[NumBases - 1].getEndLoc());
  678. };
  679. unsigned FirstNumBases = FirstDD->NumBases;
  680. unsigned FirstNumVBases = FirstDD->NumVBases;
  681. unsigned SecondNumBases = SecondDD->NumBases;
  682. unsigned SecondNumVBases = SecondDD->NumVBases;
  683. if (FirstNumBases != SecondNumBases) {
  684. DiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
  685. NumBases)
  686. << FirstNumBases;
  687. DiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
  688. NumBases)
  689. << SecondNumBases;
  690. return true;
  691. }
  692. if (FirstNumVBases != SecondNumVBases) {
  693. DiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
  694. NumVBases)
  695. << FirstNumVBases;
  696. DiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
  697. NumVBases)
  698. << SecondNumVBases;
  699. return true;
  700. }
  701. ArrayRef<CXXBaseSpecifier> FirstBases = FirstDD->bases();
  702. ArrayRef<CXXBaseSpecifier> SecondBases = SecondDD->bases();
  703. for (unsigned I = 0; I < FirstNumBases; ++I) {
  704. const CXXBaseSpecifier FirstBase = FirstBases[I];
  705. const CXXBaseSpecifier SecondBase = SecondBases[I];
  706. if (computeODRHash(FirstBase.getType()) !=
  707. computeODRHash(SecondBase.getType())) {
  708. DiagBaseError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  709. BaseType)
  710. << (I + 1) << FirstBase.getType();
  711. DiagBaseNote(SecondRecord->getLocation(), SecondBase.getSourceRange(),
  712. BaseType)
  713. << (I + 1) << SecondBase.getType();
  714. return true;
  715. }
  716. if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
  717. DiagBaseError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  718. BaseVirtual)
  719. << (I + 1) << FirstBase.isVirtual() << FirstBase.getType();
  720. DiagBaseNote(SecondRecord->getLocation(), SecondBase.getSourceRange(),
  721. BaseVirtual)
  722. << (I + 1) << SecondBase.isVirtual() << SecondBase.getType();
  723. return true;
  724. }
  725. if (FirstBase.getAccessSpecifierAsWritten() !=
  726. SecondBase.getAccessSpecifierAsWritten()) {
  727. DiagBaseError(FirstRecord->getLocation(), FirstBase.getSourceRange(),
  728. BaseAccess)
  729. << (I + 1) << FirstBase.getType()
  730. << (int)FirstBase.getAccessSpecifierAsWritten();
  731. DiagBaseNote(SecondRecord->getLocation(), SecondBase.getSourceRange(),
  732. BaseAccess)
  733. << (I + 1) << SecondBase.getType()
  734. << (int)SecondBase.getAccessSpecifierAsWritten();
  735. return true;
  736. }
  737. }
  738. }
  739. const ClassTemplateDecl *FirstTemplate =
  740. FirstRecord->getDescribedClassTemplate();
  741. const ClassTemplateDecl *SecondTemplate =
  742. SecondRecord->getDescribedClassTemplate();
  743. assert(!FirstTemplate == !SecondTemplate &&
  744. "Both pointers should be null or non-null");
  745. if (FirstTemplate && SecondTemplate) {
  746. ArrayRef<const NamedDecl *> FirstTemplateParams =
  747. FirstTemplate->getTemplateParameters()->asArray();
  748. ArrayRef<const NamedDecl *> SecondTemplateParams =
  749. SecondTemplate->getTemplateParameters()->asArray();
  750. assert(FirstTemplateParams.size() == SecondTemplateParams.size() &&
  751. "Number of template parameters should be equal.");
  752. for (auto Pair : llvm::zip(FirstTemplateParams, SecondTemplateParams)) {
  753. const NamedDecl *FirstDecl = std::get<0>(Pair);
  754. const NamedDecl *SecondDecl = std::get<1>(Pair);
  755. if (computeODRHash(FirstDecl) == computeODRHash(SecondDecl))
  756. continue;
  757. assert(FirstDecl->getKind() == SecondDecl->getKind() &&
  758. "Parameter Decl's should be the same kind.");
  759. enum ODRTemplateDifference {
  760. ParamEmptyName,
  761. ParamName,
  762. ParamSingleDefaultArgument,
  763. ParamDifferentDefaultArgument,
  764. };
  765. auto hasDefaultArg = [](const NamedDecl *D) {
  766. if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D))
  767. return TTP->hasDefaultArgument() &&
  768. !TTP->defaultArgumentWasInherited();
  769. if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
  770. return NTTP->hasDefaultArgument() &&
  771. !NTTP->defaultArgumentWasInherited();
  772. auto *TTP = cast<TemplateTemplateParmDecl>(D);
  773. return TTP->hasDefaultArgument() && !TTP->defaultArgumentWasInherited();
  774. };
  775. bool hasFirstArg = hasDefaultArg(FirstDecl);
  776. bool hasSecondArg = hasDefaultArg(SecondDecl);
  777. ODRTemplateDifference ErrDiffType;
  778. ODRTemplateDifference NoteDiffType;
  779. DeclarationName FirstName = FirstDecl->getDeclName();
  780. DeclarationName SecondName = SecondDecl->getDeclName();
  781. if (FirstName != SecondName) {
  782. bool FirstNameEmpty =
  783. FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
  784. bool SecondNameEmpty =
  785. SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
  786. ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName;
  787. NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName;
  788. } else if (hasFirstArg == hasSecondArg)
  789. ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument;
  790. else
  791. ErrDiffType = NoteDiffType = ParamSingleDefaultArgument;
  792. Diag(FirstDecl->getLocation(),
  793. diag::err_module_odr_violation_template_parameter)
  794. << FirstRecord << FirstModule.empty() << FirstModule
  795. << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg
  796. << FirstName;
  797. Diag(SecondDecl->getLocation(),
  798. diag::note_module_odr_violation_template_parameter)
  799. << SecondModule << SecondDecl->getSourceRange() << NoteDiffType
  800. << hasSecondArg << SecondName;
  801. return true;
  802. }
  803. }
  804. auto PopulateHashes = [](DeclHashes &Hashes, const RecordDecl *Record,
  805. const DeclContext *DC) {
  806. for (const Decl *D : Record->decls()) {
  807. if (!ODRHash::isSubDeclToBeProcessed(D, DC))
  808. continue;
  809. Hashes.emplace_back(D, computeODRHash(D));
  810. }
  811. };
  812. DeclHashes FirstHashes;
  813. DeclHashes SecondHashes;
  814. const DeclContext *DC = FirstRecord;
  815. PopulateHashes(FirstHashes, FirstRecord, DC);
  816. PopulateHashes(SecondHashes, SecondRecord, DC);
  817. DiffResult DR = FindTypeDiffs(FirstHashes, SecondHashes);
  818. ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
  819. ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
  820. const Decl *FirstDecl = DR.FirstDecl;
  821. const Decl *SecondDecl = DR.SecondDecl;
  822. if (FirstDiffType == Other || SecondDiffType == Other) {
  823. diagnoseSubMismatchUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
  824. SecondModule);
  825. return true;
  826. }
  827. if (FirstDiffType != SecondDiffType) {
  828. diagnoseSubMismatchDifferentDeclKinds(DR, FirstRecord, FirstModule,
  829. SecondRecord, SecondModule);
  830. return true;
  831. }
  832. // Used with err_module_odr_violation_record and
  833. // note_module_odr_violation_record
  834. enum ODRCXXRecordDifference {
  835. StaticAssertCondition,
  836. StaticAssertMessage,
  837. StaticAssertOnlyMessage,
  838. MethodName,
  839. MethodDeleted,
  840. MethodDefaulted,
  841. MethodVirtual,
  842. MethodStatic,
  843. MethodVolatile,
  844. MethodConst,
  845. MethodInline,
  846. MethodParameterSingleDefaultArgument,
  847. MethodParameterDifferentDefaultArgument,
  848. MethodNoTemplateArguments,
  849. MethodDifferentNumberTemplateArguments,
  850. MethodDifferentTemplateArgument,
  851. MethodSingleBody,
  852. MethodDifferentBody,
  853. FriendTypeFunction,
  854. FriendType,
  855. FriendFunction,
  856. FunctionTemplateDifferentNumberParameters,
  857. FunctionTemplateParameterDifferentKind,
  858. FunctionTemplateParameterName,
  859. FunctionTemplateParameterSingleDefaultArgument,
  860. FunctionTemplateParameterDifferentDefaultArgument,
  861. FunctionTemplateParameterDifferentType,
  862. FunctionTemplatePackParameter,
  863. };
  864. auto DiagError = [FirstRecord, &FirstModule,
  865. this](SourceLocation Loc, SourceRange Range,
  866. ODRCXXRecordDifference DiffType) {
  867. return Diag(Loc, diag::err_module_odr_violation_record)
  868. << FirstRecord << FirstModule.empty() << FirstModule << Range
  869. << DiffType;
  870. };
  871. auto DiagNote = [&SecondModule, this](SourceLocation Loc, SourceRange Range,
  872. ODRCXXRecordDifference DiffType) {
  873. return Diag(Loc, diag::note_module_odr_violation_record)
  874. << SecondModule << Range << DiffType;
  875. };
  876. assert(FirstDiffType == SecondDiffType);
  877. switch (FirstDiffType) {
  878. case Other:
  879. case EndOfClass:
  880. case PublicSpecifer:
  881. case PrivateSpecifer:
  882. case ProtectedSpecifer:
  883. case ObjCMethod:
  884. case ObjCIvar:
  885. case ObjCProperty:
  886. llvm_unreachable("Invalid diff type");
  887. case StaticAssert: {
  888. const StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
  889. const StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
  890. const Expr *FirstExpr = FirstSA->getAssertExpr();
  891. const Expr *SecondExpr = SecondSA->getAssertExpr();
  892. unsigned FirstODRHash = computeODRHash(FirstExpr);
  893. unsigned SecondODRHash = computeODRHash(SecondExpr);
  894. if (FirstODRHash != SecondODRHash) {
  895. DiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(),
  896. StaticAssertCondition);
  897. DiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(),
  898. StaticAssertCondition);
  899. return true;
  900. }
  901. const StringLiteral *FirstStr = FirstSA->getMessage();
  902. const StringLiteral *SecondStr = SecondSA->getMessage();
  903. assert((FirstStr || SecondStr) && "Both messages cannot be empty");
  904. if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
  905. SourceLocation FirstLoc, SecondLoc;
  906. SourceRange FirstRange, SecondRange;
  907. if (FirstStr) {
  908. FirstLoc = FirstStr->getBeginLoc();
  909. FirstRange = FirstStr->getSourceRange();
  910. } else {
  911. FirstLoc = FirstSA->getBeginLoc();
  912. FirstRange = FirstSA->getSourceRange();
  913. }
  914. if (SecondStr) {
  915. SecondLoc = SecondStr->getBeginLoc();
  916. SecondRange = SecondStr->getSourceRange();
  917. } else {
  918. SecondLoc = SecondSA->getBeginLoc();
  919. SecondRange = SecondSA->getSourceRange();
  920. }
  921. DiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage)
  922. << (FirstStr == nullptr);
  923. DiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage)
  924. << (SecondStr == nullptr);
  925. return true;
  926. }
  927. if (FirstStr && SecondStr &&
  928. FirstStr->getString() != SecondStr->getString()) {
  929. DiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(),
  930. StaticAssertMessage);
  931. DiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(),
  932. StaticAssertMessage);
  933. return true;
  934. }
  935. break;
  936. }
  937. case Field: {
  938. if (diagnoseSubMismatchField(FirstRecord, FirstModule, SecondModule,
  939. cast<FieldDecl>(FirstDecl),
  940. cast<FieldDecl>(SecondDecl)))
  941. return true;
  942. break;
  943. }
  944. case CXXMethod: {
  945. enum {
  946. DiagMethod,
  947. DiagConstructor,
  948. DiagDestructor,
  949. } FirstMethodType,
  950. SecondMethodType;
  951. auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl *D) {
  952. if (isa<CXXConstructorDecl>(D))
  953. return DiagConstructor;
  954. if (isa<CXXDestructorDecl>(D))
  955. return DiagDestructor;
  956. return DiagMethod;
  957. };
  958. const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
  959. const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
  960. FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
  961. SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
  962. DeclarationName FirstName = FirstMethod->getDeclName();
  963. DeclarationName SecondName = SecondMethod->getDeclName();
  964. auto DiagMethodError = [&DiagError, FirstMethod, FirstMethodType,
  965. FirstName](ODRCXXRecordDifference DiffType) {
  966. return DiagError(FirstMethod->getLocation(),
  967. FirstMethod->getSourceRange(), DiffType)
  968. << FirstMethodType << FirstName;
  969. };
  970. auto DiagMethodNote = [&DiagNote, SecondMethod, SecondMethodType,
  971. SecondName](ODRCXXRecordDifference DiffType) {
  972. return DiagNote(SecondMethod->getLocation(),
  973. SecondMethod->getSourceRange(), DiffType)
  974. << SecondMethodType << SecondName;
  975. };
  976. if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
  977. DiagMethodError(MethodName);
  978. DiagMethodNote(MethodName);
  979. return true;
  980. }
  981. const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
  982. const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
  983. if (FirstDeleted != SecondDeleted) {
  984. DiagMethodError(MethodDeleted) << FirstDeleted;
  985. DiagMethodNote(MethodDeleted) << SecondDeleted;
  986. return true;
  987. }
  988. const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
  989. const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
  990. if (FirstDefaulted != SecondDefaulted) {
  991. DiagMethodError(MethodDefaulted) << FirstDefaulted;
  992. DiagMethodNote(MethodDefaulted) << SecondDefaulted;
  993. return true;
  994. }
  995. const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
  996. const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
  997. const bool FirstPure = FirstMethod->isPure();
  998. const bool SecondPure = SecondMethod->isPure();
  999. if ((FirstVirtual || SecondVirtual) &&
  1000. (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
  1001. DiagMethodError(MethodVirtual) << FirstPure << FirstVirtual;
  1002. DiagMethodNote(MethodVirtual) << SecondPure << SecondVirtual;
  1003. return true;
  1004. }
  1005. // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging,
  1006. // FirstDecl is the canonical Decl of SecondDecl, so the storage
  1007. // class needs to be checked instead.
  1008. StorageClass FirstStorage = FirstMethod->getStorageClass();
  1009. StorageClass SecondStorage = SecondMethod->getStorageClass();
  1010. const bool FirstStatic = FirstStorage == SC_Static;
  1011. const bool SecondStatic = SecondStorage == SC_Static;
  1012. if (FirstStatic != SecondStatic) {
  1013. DiagMethodError(MethodStatic) << FirstStatic;
  1014. DiagMethodNote(MethodStatic) << SecondStatic;
  1015. return true;
  1016. }
  1017. const bool FirstVolatile = FirstMethod->isVolatile();
  1018. const bool SecondVolatile = SecondMethod->isVolatile();
  1019. if (FirstVolatile != SecondVolatile) {
  1020. DiagMethodError(MethodVolatile) << FirstVolatile;
  1021. DiagMethodNote(MethodVolatile) << SecondVolatile;
  1022. return true;
  1023. }
  1024. const bool FirstConst = FirstMethod->isConst();
  1025. const bool SecondConst = SecondMethod->isConst();
  1026. if (FirstConst != SecondConst) {
  1027. DiagMethodError(MethodConst) << FirstConst;
  1028. DiagMethodNote(MethodConst) << SecondConst;
  1029. return true;
  1030. }
  1031. const bool FirstInline = FirstMethod->isInlineSpecified();
  1032. const bool SecondInline = SecondMethod->isInlineSpecified();
  1033. if (FirstInline != SecondInline) {
  1034. DiagMethodError(MethodInline) << FirstInline;
  1035. DiagMethodNote(MethodInline) << SecondInline;
  1036. return true;
  1037. }
  1038. if (diagnoseSubMismatchMethodParameters(Diags, FirstRecord,
  1039. FirstModule, SecondModule,
  1040. FirstMethod, SecondMethod))
  1041. return true;
  1042. for (unsigned I = 0, N = FirstMethod->param_size(); I < N; ++I) {
  1043. const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
  1044. const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
  1045. const Expr *FirstInit = FirstParam->getInit();
  1046. const Expr *SecondInit = SecondParam->getInit();
  1047. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  1048. DiagMethodError(MethodParameterSingleDefaultArgument)
  1049. << (I + 1) << (FirstInit == nullptr)
  1050. << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
  1051. DiagMethodNote(MethodParameterSingleDefaultArgument)
  1052. << (I + 1) << (SecondInit == nullptr)
  1053. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  1054. return true;
  1055. }
  1056. if (FirstInit && SecondInit &&
  1057. computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
  1058. DiagMethodError(MethodParameterDifferentDefaultArgument)
  1059. << (I + 1) << FirstInit->getSourceRange();
  1060. DiagMethodNote(MethodParameterDifferentDefaultArgument)
  1061. << (I + 1) << SecondInit->getSourceRange();
  1062. return true;
  1063. }
  1064. }
  1065. const TemplateArgumentList *FirstTemplateArgs =
  1066. FirstMethod->getTemplateSpecializationArgs();
  1067. const TemplateArgumentList *SecondTemplateArgs =
  1068. SecondMethod->getTemplateSpecializationArgs();
  1069. if ((FirstTemplateArgs && !SecondTemplateArgs) ||
  1070. (!FirstTemplateArgs && SecondTemplateArgs)) {
  1071. DiagMethodError(MethodNoTemplateArguments)
  1072. << (FirstTemplateArgs != nullptr);
  1073. DiagMethodNote(MethodNoTemplateArguments)
  1074. << (SecondTemplateArgs != nullptr);
  1075. return true;
  1076. }
  1077. if (FirstTemplateArgs && SecondTemplateArgs) {
  1078. // Remove pack expansions from argument list.
  1079. auto ExpandTemplateArgumentList = [](const TemplateArgumentList *TAL) {
  1080. llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
  1081. for (const TemplateArgument &TA : TAL->asArray()) {
  1082. if (TA.getKind() != TemplateArgument::Pack) {
  1083. ExpandedList.push_back(&TA);
  1084. continue;
  1085. }
  1086. llvm::append_range(ExpandedList,
  1087. llvm::make_pointer_range(TA.getPackAsArray()));
  1088. }
  1089. return ExpandedList;
  1090. };
  1091. llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
  1092. ExpandTemplateArgumentList(FirstTemplateArgs);
  1093. llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
  1094. ExpandTemplateArgumentList(SecondTemplateArgs);
  1095. if (FirstExpandedList.size() != SecondExpandedList.size()) {
  1096. DiagMethodError(MethodDifferentNumberTemplateArguments)
  1097. << (unsigned)FirstExpandedList.size();
  1098. DiagMethodNote(MethodDifferentNumberTemplateArguments)
  1099. << (unsigned)SecondExpandedList.size();
  1100. return true;
  1101. }
  1102. for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
  1103. const TemplateArgument &FirstTA = *FirstExpandedList[i],
  1104. &SecondTA = *SecondExpandedList[i];
  1105. if (computeODRHash(FirstTA) == computeODRHash(SecondTA))
  1106. continue;
  1107. DiagMethodError(MethodDifferentTemplateArgument) << FirstTA << i + 1;
  1108. DiagMethodNote(MethodDifferentTemplateArgument) << SecondTA << i + 1;
  1109. return true;
  1110. }
  1111. }
  1112. // Compute the hash of the method as if it has no body.
  1113. auto ComputeCXXMethodODRHash = [](const CXXMethodDecl *D) {
  1114. ODRHash Hasher;
  1115. Hasher.AddFunctionDecl(D, true /*SkipBody*/);
  1116. return Hasher.CalculateHash();
  1117. };
  1118. // Compare the hash generated to the hash stored. A difference means
  1119. // that a body was present in the original source. Due to merging,
  1120. // the standard way of detecting a body will not work.
  1121. const bool HasFirstBody =
  1122. ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
  1123. const bool HasSecondBody =
  1124. ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
  1125. if (HasFirstBody != HasSecondBody) {
  1126. DiagMethodError(MethodSingleBody) << HasFirstBody;
  1127. DiagMethodNote(MethodSingleBody) << HasSecondBody;
  1128. return true;
  1129. }
  1130. if (HasFirstBody && HasSecondBody) {
  1131. DiagMethodError(MethodDifferentBody);
  1132. DiagMethodNote(MethodDifferentBody);
  1133. return true;
  1134. }
  1135. break;
  1136. }
  1137. case TypeAlias:
  1138. case TypeDef: {
  1139. if (diagnoseSubMismatchTypedef(FirstRecord, FirstModule, SecondModule,
  1140. cast<TypedefNameDecl>(FirstDecl),
  1141. cast<TypedefNameDecl>(SecondDecl),
  1142. FirstDiffType == TypeAlias))
  1143. return true;
  1144. break;
  1145. }
  1146. case Var: {
  1147. if (diagnoseSubMismatchVar(FirstRecord, FirstModule, SecondModule,
  1148. cast<VarDecl>(FirstDecl),
  1149. cast<VarDecl>(SecondDecl)))
  1150. return true;
  1151. break;
  1152. }
  1153. case Friend: {
  1154. const FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
  1155. const FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
  1156. const NamedDecl *FirstND = FirstFriend->getFriendDecl();
  1157. const NamedDecl *SecondND = SecondFriend->getFriendDecl();
  1158. TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
  1159. TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
  1160. if (FirstND && SecondND) {
  1161. DiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
  1162. FriendFunction)
  1163. << FirstND;
  1164. DiagNote(SecondFriend->getFriendLoc(), SecondFriend->getSourceRange(),
  1165. FriendFunction)
  1166. << SecondND;
  1167. return true;
  1168. }
  1169. if (FirstTSI && SecondTSI) {
  1170. QualType FirstFriendType = FirstTSI->getType();
  1171. QualType SecondFriendType = SecondTSI->getType();
  1172. assert(computeODRHash(FirstFriendType) !=
  1173. computeODRHash(SecondFriendType));
  1174. DiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
  1175. FriendType)
  1176. << FirstFriendType;
  1177. DiagNote(SecondFriend->getFriendLoc(), SecondFriend->getSourceRange(),
  1178. FriendType)
  1179. << SecondFriendType;
  1180. return true;
  1181. }
  1182. DiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(),
  1183. FriendTypeFunction)
  1184. << (FirstTSI == nullptr);
  1185. DiagNote(SecondFriend->getFriendLoc(), SecondFriend->getSourceRange(),
  1186. FriendTypeFunction)
  1187. << (SecondTSI == nullptr);
  1188. return true;
  1189. }
  1190. case FunctionTemplate: {
  1191. const FunctionTemplateDecl *FirstTemplate =
  1192. cast<FunctionTemplateDecl>(FirstDecl);
  1193. const FunctionTemplateDecl *SecondTemplate =
  1194. cast<FunctionTemplateDecl>(SecondDecl);
  1195. TemplateParameterList *FirstTPL = FirstTemplate->getTemplateParameters();
  1196. TemplateParameterList *SecondTPL = SecondTemplate->getTemplateParameters();
  1197. auto DiagTemplateError = [&DiagError,
  1198. FirstTemplate](ODRCXXRecordDifference DiffType) {
  1199. return DiagError(FirstTemplate->getLocation(),
  1200. FirstTemplate->getSourceRange(), DiffType)
  1201. << FirstTemplate;
  1202. };
  1203. auto DiagTemplateNote = [&DiagNote,
  1204. SecondTemplate](ODRCXXRecordDifference DiffType) {
  1205. return DiagNote(SecondTemplate->getLocation(),
  1206. SecondTemplate->getSourceRange(), DiffType)
  1207. << SecondTemplate;
  1208. };
  1209. if (FirstTPL->size() != SecondTPL->size()) {
  1210. DiagTemplateError(FunctionTemplateDifferentNumberParameters)
  1211. << FirstTPL->size();
  1212. DiagTemplateNote(FunctionTemplateDifferentNumberParameters)
  1213. << SecondTPL->size();
  1214. return true;
  1215. }
  1216. for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
  1217. NamedDecl *FirstParam = FirstTPL->getParam(i);
  1218. NamedDecl *SecondParam = SecondTPL->getParam(i);
  1219. if (FirstParam->getKind() != SecondParam->getKind()) {
  1220. enum {
  1221. TemplateTypeParameter,
  1222. NonTypeTemplateParameter,
  1223. TemplateTemplateParameter,
  1224. };
  1225. auto GetParamType = [](NamedDecl *D) {
  1226. switch (D->getKind()) {
  1227. default:
  1228. llvm_unreachable("Unexpected template parameter type");
  1229. case Decl::TemplateTypeParm:
  1230. return TemplateTypeParameter;
  1231. case Decl::NonTypeTemplateParm:
  1232. return NonTypeTemplateParameter;
  1233. case Decl::TemplateTemplateParm:
  1234. return TemplateTemplateParameter;
  1235. }
  1236. };
  1237. DiagTemplateError(FunctionTemplateParameterDifferentKind)
  1238. << (i + 1) << GetParamType(FirstParam);
  1239. DiagTemplateNote(FunctionTemplateParameterDifferentKind)
  1240. << (i + 1) << GetParamType(SecondParam);
  1241. return true;
  1242. }
  1243. if (FirstParam->getName() != SecondParam->getName()) {
  1244. DiagTemplateError(FunctionTemplateParameterName)
  1245. << (i + 1) << (bool)FirstParam->getIdentifier() << FirstParam;
  1246. DiagTemplateNote(FunctionTemplateParameterName)
  1247. << (i + 1) << (bool)SecondParam->getIdentifier() << SecondParam;
  1248. return true;
  1249. }
  1250. if (isa<TemplateTypeParmDecl>(FirstParam) &&
  1251. isa<TemplateTypeParmDecl>(SecondParam)) {
  1252. TemplateTypeParmDecl *FirstTTPD =
  1253. cast<TemplateTypeParmDecl>(FirstParam);
  1254. TemplateTypeParmDecl *SecondTTPD =
  1255. cast<TemplateTypeParmDecl>(SecondParam);
  1256. bool HasFirstDefaultArgument =
  1257. FirstTTPD->hasDefaultArgument() &&
  1258. !FirstTTPD->defaultArgumentWasInherited();
  1259. bool HasSecondDefaultArgument =
  1260. SecondTTPD->hasDefaultArgument() &&
  1261. !SecondTTPD->defaultArgumentWasInherited();
  1262. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  1263. DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument)
  1264. << (i + 1) << HasFirstDefaultArgument;
  1265. DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument)
  1266. << (i + 1) << HasSecondDefaultArgument;
  1267. return true;
  1268. }
  1269. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  1270. QualType FirstType = FirstTTPD->getDefaultArgument();
  1271. QualType SecondType = SecondTTPD->getDefaultArgument();
  1272. if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
  1273. DiagTemplateError(FunctionTemplateParameterDifferentDefaultArgument)
  1274. << (i + 1) << FirstType;
  1275. DiagTemplateNote(FunctionTemplateParameterDifferentDefaultArgument)
  1276. << (i + 1) << SecondType;
  1277. return true;
  1278. }
  1279. }
  1280. if (FirstTTPD->isParameterPack() != SecondTTPD->isParameterPack()) {
  1281. DiagTemplateError(FunctionTemplatePackParameter)
  1282. << (i + 1) << FirstTTPD->isParameterPack();
  1283. DiagTemplateNote(FunctionTemplatePackParameter)
  1284. << (i + 1) << SecondTTPD->isParameterPack();
  1285. return true;
  1286. }
  1287. }
  1288. if (isa<TemplateTemplateParmDecl>(FirstParam) &&
  1289. isa<TemplateTemplateParmDecl>(SecondParam)) {
  1290. TemplateTemplateParmDecl *FirstTTPD =
  1291. cast<TemplateTemplateParmDecl>(FirstParam);
  1292. TemplateTemplateParmDecl *SecondTTPD =
  1293. cast<TemplateTemplateParmDecl>(SecondParam);
  1294. TemplateParameterList *FirstTPL = FirstTTPD->getTemplateParameters();
  1295. TemplateParameterList *SecondTPL = SecondTTPD->getTemplateParameters();
  1296. auto ComputeTemplateParameterListODRHash =
  1297. [](const TemplateParameterList *TPL) {
  1298. assert(TPL);
  1299. ODRHash Hasher;
  1300. Hasher.AddTemplateParameterList(TPL);
  1301. return Hasher.CalculateHash();
  1302. };
  1303. if (ComputeTemplateParameterListODRHash(FirstTPL) !=
  1304. ComputeTemplateParameterListODRHash(SecondTPL)) {
  1305. DiagTemplateError(FunctionTemplateParameterDifferentType) << (i + 1);
  1306. DiagTemplateNote(FunctionTemplateParameterDifferentType) << (i + 1);
  1307. return true;
  1308. }
  1309. bool HasFirstDefaultArgument =
  1310. FirstTTPD->hasDefaultArgument() &&
  1311. !FirstTTPD->defaultArgumentWasInherited();
  1312. bool HasSecondDefaultArgument =
  1313. SecondTTPD->hasDefaultArgument() &&
  1314. !SecondTTPD->defaultArgumentWasInherited();
  1315. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  1316. DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument)
  1317. << (i + 1) << HasFirstDefaultArgument;
  1318. DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument)
  1319. << (i + 1) << HasSecondDefaultArgument;
  1320. return true;
  1321. }
  1322. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  1323. TemplateArgument FirstTA =
  1324. FirstTTPD->getDefaultArgument().getArgument();
  1325. TemplateArgument SecondTA =
  1326. SecondTTPD->getDefaultArgument().getArgument();
  1327. if (computeODRHash(FirstTA) != computeODRHash(SecondTA)) {
  1328. DiagTemplateError(FunctionTemplateParameterDifferentDefaultArgument)
  1329. << (i + 1) << FirstTA;
  1330. DiagTemplateNote(FunctionTemplateParameterDifferentDefaultArgument)
  1331. << (i + 1) << SecondTA;
  1332. return true;
  1333. }
  1334. }
  1335. if (FirstTTPD->isParameterPack() != SecondTTPD->isParameterPack()) {
  1336. DiagTemplateError(FunctionTemplatePackParameter)
  1337. << (i + 1) << FirstTTPD->isParameterPack();
  1338. DiagTemplateNote(FunctionTemplatePackParameter)
  1339. << (i + 1) << SecondTTPD->isParameterPack();
  1340. return true;
  1341. }
  1342. }
  1343. if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
  1344. isa<NonTypeTemplateParmDecl>(SecondParam)) {
  1345. NonTypeTemplateParmDecl *FirstNTTPD =
  1346. cast<NonTypeTemplateParmDecl>(FirstParam);
  1347. NonTypeTemplateParmDecl *SecondNTTPD =
  1348. cast<NonTypeTemplateParmDecl>(SecondParam);
  1349. QualType FirstType = FirstNTTPD->getType();
  1350. QualType SecondType = SecondNTTPD->getType();
  1351. if (computeODRHash(FirstType) != computeODRHash(SecondType)) {
  1352. DiagTemplateError(FunctionTemplateParameterDifferentType) << (i + 1);
  1353. DiagTemplateNote(FunctionTemplateParameterDifferentType) << (i + 1);
  1354. return true;
  1355. }
  1356. bool HasFirstDefaultArgument =
  1357. FirstNTTPD->hasDefaultArgument() &&
  1358. !FirstNTTPD->defaultArgumentWasInherited();
  1359. bool HasSecondDefaultArgument =
  1360. SecondNTTPD->hasDefaultArgument() &&
  1361. !SecondNTTPD->defaultArgumentWasInherited();
  1362. if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
  1363. DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument)
  1364. << (i + 1) << HasFirstDefaultArgument;
  1365. DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument)
  1366. << (i + 1) << HasSecondDefaultArgument;
  1367. return true;
  1368. }
  1369. if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
  1370. Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
  1371. Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
  1372. if (computeODRHash(FirstDefaultArgument) !=
  1373. computeODRHash(SecondDefaultArgument)) {
  1374. DiagTemplateError(FunctionTemplateParameterDifferentDefaultArgument)
  1375. << (i + 1) << FirstDefaultArgument;
  1376. DiagTemplateNote(FunctionTemplateParameterDifferentDefaultArgument)
  1377. << (i + 1) << SecondDefaultArgument;
  1378. return true;
  1379. }
  1380. }
  1381. if (FirstNTTPD->isParameterPack() != SecondNTTPD->isParameterPack()) {
  1382. DiagTemplateError(FunctionTemplatePackParameter)
  1383. << (i + 1) << FirstNTTPD->isParameterPack();
  1384. DiagTemplateNote(FunctionTemplatePackParameter)
  1385. << (i + 1) << SecondNTTPD->isParameterPack();
  1386. return true;
  1387. }
  1388. }
  1389. }
  1390. break;
  1391. }
  1392. }
  1393. Diag(FirstDecl->getLocation(),
  1394. diag::err_module_odr_violation_mismatch_decl_unknown)
  1395. << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
  1396. << FirstDecl->getSourceRange();
  1397. Diag(SecondDecl->getLocation(),
  1398. diag::note_module_odr_violation_mismatch_decl_unknown)
  1399. << SecondModule.empty() << SecondModule << FirstDiffType
  1400. << SecondDecl->getSourceRange();
  1401. return true;
  1402. }
  1403. bool ODRDiagsEmitter::diagnoseMismatch(const RecordDecl *FirstRecord,
  1404. const RecordDecl *SecondRecord) const {
  1405. if (FirstRecord == SecondRecord)
  1406. return false;
  1407. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
  1408. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
  1409. auto PopulateHashes = [](DeclHashes &Hashes, const RecordDecl *Record,
  1410. const DeclContext *DC) {
  1411. for (const Decl *D : Record->decls()) {
  1412. if (!ODRHash::isSubDeclToBeProcessed(D, DC))
  1413. continue;
  1414. Hashes.emplace_back(D, computeODRHash(D));
  1415. }
  1416. };
  1417. DeclHashes FirstHashes;
  1418. DeclHashes SecondHashes;
  1419. const DeclContext *DC = FirstRecord;
  1420. PopulateHashes(FirstHashes, FirstRecord, DC);
  1421. PopulateHashes(SecondHashes, SecondRecord, DC);
  1422. DiffResult DR = FindTypeDiffs(FirstHashes, SecondHashes);
  1423. ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
  1424. ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
  1425. const Decl *FirstDecl = DR.FirstDecl;
  1426. const Decl *SecondDecl = DR.SecondDecl;
  1427. if (FirstDiffType == Other || SecondDiffType == Other) {
  1428. diagnoseSubMismatchUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
  1429. SecondModule);
  1430. return true;
  1431. }
  1432. if (FirstDiffType != SecondDiffType) {
  1433. diagnoseSubMismatchDifferentDeclKinds(DR, FirstRecord, FirstModule,
  1434. SecondRecord, SecondModule);
  1435. return true;
  1436. }
  1437. assert(FirstDiffType == SecondDiffType);
  1438. switch (FirstDiffType) {
  1439. // Already handled.
  1440. case EndOfClass:
  1441. case Other:
  1442. // C++ only, invalid in this context.
  1443. case PublicSpecifer:
  1444. case PrivateSpecifer:
  1445. case ProtectedSpecifer:
  1446. case StaticAssert:
  1447. case CXXMethod:
  1448. case TypeAlias:
  1449. case Friend:
  1450. case FunctionTemplate:
  1451. // Cannot be contained by RecordDecl, invalid in this context.
  1452. case ObjCMethod:
  1453. case ObjCIvar:
  1454. case ObjCProperty:
  1455. llvm_unreachable("Invalid diff type");
  1456. case Field: {
  1457. if (diagnoseSubMismatchField(FirstRecord, FirstModule, SecondModule,
  1458. cast<FieldDecl>(FirstDecl),
  1459. cast<FieldDecl>(SecondDecl)))
  1460. return true;
  1461. break;
  1462. }
  1463. case TypeDef: {
  1464. if (diagnoseSubMismatchTypedef(FirstRecord, FirstModule, SecondModule,
  1465. cast<TypedefNameDecl>(FirstDecl),
  1466. cast<TypedefNameDecl>(SecondDecl),
  1467. /*IsTypeAlias=*/false))
  1468. return true;
  1469. break;
  1470. }
  1471. case Var: {
  1472. if (diagnoseSubMismatchVar(FirstRecord, FirstModule, SecondModule,
  1473. cast<VarDecl>(FirstDecl),
  1474. cast<VarDecl>(SecondDecl)))
  1475. return true;
  1476. break;
  1477. }
  1478. }
  1479. Diag(FirstDecl->getLocation(),
  1480. diag::err_module_odr_violation_mismatch_decl_unknown)
  1481. << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
  1482. << FirstDecl->getSourceRange();
  1483. Diag(SecondDecl->getLocation(),
  1484. diag::note_module_odr_violation_mismatch_decl_unknown)
  1485. << SecondModule.empty() << SecondModule << FirstDiffType
  1486. << SecondDecl->getSourceRange();
  1487. return true;
  1488. }
  1489. bool ODRDiagsEmitter::diagnoseMismatch(
  1490. const FunctionDecl *FirstFunction,
  1491. const FunctionDecl *SecondFunction) const {
  1492. if (FirstFunction == SecondFunction)
  1493. return false;
  1494. // Keep in sync with select options in err_module_odr_violation_function.
  1495. enum ODRFunctionDifference {
  1496. ReturnType,
  1497. ParameterName,
  1498. ParameterType,
  1499. ParameterSingleDefaultArgument,
  1500. ParameterDifferentDefaultArgument,
  1501. FunctionBody,
  1502. };
  1503. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
  1504. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondFunction);
  1505. auto DiagError = [FirstFunction, &FirstModule,
  1506. this](SourceLocation Loc, SourceRange Range,
  1507. ODRFunctionDifference DiffType) {
  1508. return Diag(Loc, diag::err_module_odr_violation_function)
  1509. << FirstFunction << FirstModule.empty() << FirstModule << Range
  1510. << DiffType;
  1511. };
  1512. auto DiagNote = [&SecondModule, this](SourceLocation Loc, SourceRange Range,
  1513. ODRFunctionDifference DiffType) {
  1514. return Diag(Loc, diag::note_module_odr_violation_function)
  1515. << SecondModule << Range << DiffType;
  1516. };
  1517. if (computeODRHash(FirstFunction->getReturnType()) !=
  1518. computeODRHash(SecondFunction->getReturnType())) {
  1519. DiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
  1520. FirstFunction->getReturnTypeSourceRange(), ReturnType)
  1521. << FirstFunction->getReturnType();
  1522. DiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
  1523. SecondFunction->getReturnTypeSourceRange(), ReturnType)
  1524. << SecondFunction->getReturnType();
  1525. return true;
  1526. }
  1527. assert(FirstFunction->param_size() == SecondFunction->param_size() &&
  1528. "Merged functions with different number of parameters");
  1529. size_t ParamSize = FirstFunction->param_size();
  1530. for (unsigned I = 0; I < ParamSize; ++I) {
  1531. const ParmVarDecl *FirstParam = FirstFunction->getParamDecl(I);
  1532. const ParmVarDecl *SecondParam = SecondFunction->getParamDecl(I);
  1533. assert(Context.hasSameType(FirstParam->getType(), SecondParam->getType()) &&
  1534. "Merged function has different parameter types.");
  1535. if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
  1536. DiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  1537. ParameterName)
  1538. << I + 1 << FirstParam->getDeclName();
  1539. DiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  1540. ParameterName)
  1541. << I + 1 << SecondParam->getDeclName();
  1542. return true;
  1543. };
  1544. QualType FirstParamType = FirstParam->getType();
  1545. QualType SecondParamType = SecondParam->getType();
  1546. if (FirstParamType != SecondParamType &&
  1547. computeODRHash(FirstParamType) != computeODRHash(SecondParamType)) {
  1548. if (const DecayedType *ParamDecayedType =
  1549. FirstParamType->getAs<DecayedType>()) {
  1550. DiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  1551. ParameterType)
  1552. << (I + 1) << FirstParamType << true
  1553. << ParamDecayedType->getOriginalType();
  1554. } else {
  1555. DiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  1556. ParameterType)
  1557. << (I + 1) << FirstParamType << false;
  1558. }
  1559. if (const DecayedType *ParamDecayedType =
  1560. SecondParamType->getAs<DecayedType>()) {
  1561. DiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  1562. ParameterType)
  1563. << (I + 1) << SecondParamType << true
  1564. << ParamDecayedType->getOriginalType();
  1565. } else {
  1566. DiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  1567. ParameterType)
  1568. << (I + 1) << SecondParamType << false;
  1569. }
  1570. return true;
  1571. }
  1572. const Expr *FirstInit = FirstParam->getInit();
  1573. const Expr *SecondInit = SecondParam->getInit();
  1574. if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
  1575. DiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  1576. ParameterSingleDefaultArgument)
  1577. << (I + 1) << (FirstInit == nullptr)
  1578. << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
  1579. DiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  1580. ParameterSingleDefaultArgument)
  1581. << (I + 1) << (SecondInit == nullptr)
  1582. << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
  1583. return true;
  1584. }
  1585. if (FirstInit && SecondInit &&
  1586. computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
  1587. DiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
  1588. ParameterDifferentDefaultArgument)
  1589. << (I + 1) << FirstInit->getSourceRange();
  1590. DiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
  1591. ParameterDifferentDefaultArgument)
  1592. << (I + 1) << SecondInit->getSourceRange();
  1593. return true;
  1594. }
  1595. assert(computeODRHash(FirstParam) == computeODRHash(SecondParam) &&
  1596. "Undiagnosed parameter difference.");
  1597. }
  1598. // If no error has been generated before now, assume the problem is in
  1599. // the body and generate a message.
  1600. DiagError(FirstFunction->getLocation(), FirstFunction->getSourceRange(),
  1601. FunctionBody);
  1602. DiagNote(SecondFunction->getLocation(), SecondFunction->getSourceRange(),
  1603. FunctionBody);
  1604. return true;
  1605. }
  1606. bool ODRDiagsEmitter::diagnoseMismatch(const EnumDecl *FirstEnum,
  1607. const EnumDecl *SecondEnum) const {
  1608. if (FirstEnum == SecondEnum)
  1609. return false;
  1610. // Keep in sync with select options in err_module_odr_violation_enum.
  1611. enum ODREnumDifference {
  1612. SingleScopedEnum,
  1613. EnumTagKeywordMismatch,
  1614. SingleSpecifiedType,
  1615. DifferentSpecifiedTypes,
  1616. DifferentNumberEnumConstants,
  1617. EnumConstantName,
  1618. EnumConstantSingleInitializer,
  1619. EnumConstantDifferentInitializer,
  1620. };
  1621. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
  1622. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondEnum);
  1623. auto DiagError = [FirstEnum, &FirstModule, this](const auto *DiagAnchor,
  1624. ODREnumDifference DiffType) {
  1625. return Diag(DiagAnchor->getLocation(), diag::err_module_odr_violation_enum)
  1626. << FirstEnum << FirstModule.empty() << FirstModule
  1627. << DiagAnchor->getSourceRange() << DiffType;
  1628. };
  1629. auto DiagNote = [&SecondModule, this](const auto *DiagAnchor,
  1630. ODREnumDifference DiffType) {
  1631. return Diag(DiagAnchor->getLocation(), diag::note_module_odr_violation_enum)
  1632. << SecondModule << DiagAnchor->getSourceRange() << DiffType;
  1633. };
  1634. if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
  1635. DiagError(FirstEnum, SingleScopedEnum) << FirstEnum->isScoped();
  1636. DiagNote(SecondEnum, SingleScopedEnum) << SecondEnum->isScoped();
  1637. return true;
  1638. }
  1639. if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
  1640. if (FirstEnum->isScopedUsingClassTag() !=
  1641. SecondEnum->isScopedUsingClassTag()) {
  1642. DiagError(FirstEnum, EnumTagKeywordMismatch)
  1643. << FirstEnum->isScopedUsingClassTag();
  1644. DiagNote(SecondEnum, EnumTagKeywordMismatch)
  1645. << SecondEnum->isScopedUsingClassTag();
  1646. return true;
  1647. }
  1648. }
  1649. QualType FirstUnderlyingType =
  1650. FirstEnum->getIntegerTypeSourceInfo()
  1651. ? FirstEnum->getIntegerTypeSourceInfo()->getType()
  1652. : QualType();
  1653. QualType SecondUnderlyingType =
  1654. SecondEnum->getIntegerTypeSourceInfo()
  1655. ? SecondEnum->getIntegerTypeSourceInfo()->getType()
  1656. : QualType();
  1657. if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
  1658. DiagError(FirstEnum, SingleSpecifiedType) << !FirstUnderlyingType.isNull();
  1659. DiagNote(SecondEnum, SingleSpecifiedType) << !SecondUnderlyingType.isNull();
  1660. return true;
  1661. }
  1662. if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
  1663. if (computeODRHash(FirstUnderlyingType) !=
  1664. computeODRHash(SecondUnderlyingType)) {
  1665. DiagError(FirstEnum, DifferentSpecifiedTypes) << FirstUnderlyingType;
  1666. DiagNote(SecondEnum, DifferentSpecifiedTypes) << SecondUnderlyingType;
  1667. return true;
  1668. }
  1669. }
  1670. // Compare enum constants.
  1671. using DeclHashes =
  1672. llvm::SmallVector<std::pair<const EnumConstantDecl *, unsigned>, 4>;
  1673. auto PopulateHashes = [FirstEnum](DeclHashes &Hashes, const EnumDecl *Enum) {
  1674. for (const Decl *D : Enum->decls()) {
  1675. // Due to decl merging, the first EnumDecl is the parent of
  1676. // Decls in both records.
  1677. if (!ODRHash::isSubDeclToBeProcessed(D, FirstEnum))
  1678. continue;
  1679. assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
  1680. Hashes.emplace_back(cast<EnumConstantDecl>(D), computeODRHash(D));
  1681. }
  1682. };
  1683. DeclHashes FirstHashes;
  1684. PopulateHashes(FirstHashes, FirstEnum);
  1685. DeclHashes SecondHashes;
  1686. PopulateHashes(SecondHashes, SecondEnum);
  1687. if (FirstHashes.size() != SecondHashes.size()) {
  1688. DiagError(FirstEnum, DifferentNumberEnumConstants)
  1689. << (int)FirstHashes.size();
  1690. DiagNote(SecondEnum, DifferentNumberEnumConstants)
  1691. << (int)SecondHashes.size();
  1692. return true;
  1693. }
  1694. for (unsigned I = 0, N = FirstHashes.size(); I < N; ++I) {
  1695. if (FirstHashes[I].second == SecondHashes[I].second)
  1696. continue;
  1697. const EnumConstantDecl *FirstConstant = FirstHashes[I].first;
  1698. const EnumConstantDecl *SecondConstant = SecondHashes[I].first;
  1699. if (FirstConstant->getDeclName() != SecondConstant->getDeclName()) {
  1700. DiagError(FirstConstant, EnumConstantName) << I + 1 << FirstConstant;
  1701. DiagNote(SecondConstant, EnumConstantName) << I + 1 << SecondConstant;
  1702. return true;
  1703. }
  1704. const Expr *FirstInit = FirstConstant->getInitExpr();
  1705. const Expr *SecondInit = SecondConstant->getInitExpr();
  1706. if (!FirstInit && !SecondInit)
  1707. continue;
  1708. if (!FirstInit || !SecondInit) {
  1709. DiagError(FirstConstant, EnumConstantSingleInitializer)
  1710. << I + 1 << FirstConstant << (FirstInit != nullptr);
  1711. DiagNote(SecondConstant, EnumConstantSingleInitializer)
  1712. << I + 1 << SecondConstant << (SecondInit != nullptr);
  1713. return true;
  1714. }
  1715. if (computeODRHash(FirstInit) != computeODRHash(SecondInit)) {
  1716. DiagError(FirstConstant, EnumConstantDifferentInitializer)
  1717. << I + 1 << FirstConstant;
  1718. DiagNote(SecondConstant, EnumConstantDifferentInitializer)
  1719. << I + 1 << SecondConstant;
  1720. return true;
  1721. }
  1722. }
  1723. return false;
  1724. }
  1725. bool ODRDiagsEmitter::diagnoseMismatch(
  1726. const ObjCInterfaceDecl *FirstID, const ObjCInterfaceDecl *SecondID,
  1727. const struct ObjCInterfaceDecl::DefinitionData *SecondDD) const {
  1728. // Multiple different declarations got merged together; tell the user
  1729. // where they came from.
  1730. if (FirstID == SecondID)
  1731. return false;
  1732. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstID);
  1733. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondID);
  1734. // Keep in sync with err_module_odr_violation_objc_interface.
  1735. enum ODRInterfaceDifference {
  1736. SuperClassType,
  1737. IVarAccess,
  1738. };
  1739. auto DiagError = [FirstID, &FirstModule,
  1740. this](SourceLocation Loc, SourceRange Range,
  1741. ODRInterfaceDifference DiffType) {
  1742. return Diag(Loc, diag::err_module_odr_violation_objc_interface)
  1743. << FirstID << FirstModule.empty() << FirstModule << Range
  1744. << DiffType;
  1745. };
  1746. auto DiagNote = [&SecondModule, this](SourceLocation Loc, SourceRange Range,
  1747. ODRInterfaceDifference DiffType) {
  1748. return Diag(Loc, diag::note_module_odr_violation_objc_interface)
  1749. << SecondModule.empty() << SecondModule << Range << DiffType;
  1750. };
  1751. const struct ObjCInterfaceDecl::DefinitionData *FirstDD = &FirstID->data();
  1752. assert(FirstDD && SecondDD && "Definitions without DefinitionData");
  1753. if (FirstDD != SecondDD) {
  1754. // Check for matching super class.
  1755. auto GetSuperClassSourceRange = [](const TypeSourceInfo *SuperInfo,
  1756. const ObjCInterfaceDecl *ID) {
  1757. if (!SuperInfo)
  1758. return ID->getSourceRange();
  1759. TypeLoc Loc = SuperInfo->getTypeLoc();
  1760. return SourceRange(Loc.getBeginLoc(), Loc.getEndLoc());
  1761. };
  1762. ObjCInterfaceDecl *FirstSuperClass = FirstID->getSuperClass();
  1763. ObjCInterfaceDecl *SecondSuperClass = nullptr;
  1764. const TypeSourceInfo *FirstSuperInfo = FirstID->getSuperClassTInfo();
  1765. const TypeSourceInfo *SecondSuperInfo = SecondDD->SuperClassTInfo;
  1766. if (SecondSuperInfo)
  1767. SecondSuperClass =
  1768. SecondSuperInfo->getType()->castAs<ObjCObjectType>()->getInterface();
  1769. if ((FirstSuperClass && SecondSuperClass &&
  1770. FirstSuperClass->getODRHash() != SecondSuperClass->getODRHash()) ||
  1771. (FirstSuperClass && !SecondSuperClass) ||
  1772. (!FirstSuperClass && SecondSuperClass)) {
  1773. QualType FirstType;
  1774. if (FirstSuperInfo)
  1775. FirstType = FirstSuperInfo->getType();
  1776. DiagError(FirstID->getLocation(),
  1777. GetSuperClassSourceRange(FirstSuperInfo, FirstID),
  1778. SuperClassType)
  1779. << (bool)FirstSuperInfo << FirstType;
  1780. QualType SecondType;
  1781. if (SecondSuperInfo)
  1782. SecondType = SecondSuperInfo->getType();
  1783. DiagNote(SecondID->getLocation(),
  1784. GetSuperClassSourceRange(SecondSuperInfo, SecondID),
  1785. SuperClassType)
  1786. << (bool)SecondSuperInfo << SecondType;
  1787. return true;
  1788. }
  1789. // Check both interfaces reference the same protocols.
  1790. auto &FirstProtos = FirstID->getReferencedProtocols();
  1791. auto &SecondProtos = SecondDD->ReferencedProtocols;
  1792. if (diagnoseSubMismatchProtocols(FirstProtos, FirstID, FirstModule,
  1793. SecondProtos, SecondID, SecondModule))
  1794. return true;
  1795. }
  1796. auto PopulateHashes = [](DeclHashes &Hashes, const ObjCInterfaceDecl *ID,
  1797. const DeclContext *DC) {
  1798. for (auto *D : ID->decls()) {
  1799. if (!ODRHash::isSubDeclToBeProcessed(D, DC))
  1800. continue;
  1801. Hashes.emplace_back(D, computeODRHash(D));
  1802. }
  1803. };
  1804. DeclHashes FirstHashes;
  1805. DeclHashes SecondHashes;
  1806. // Use definition as DeclContext because definitions are merged when
  1807. // DeclContexts are merged and separate when DeclContexts are separate.
  1808. PopulateHashes(FirstHashes, FirstID, FirstID->getDefinition());
  1809. PopulateHashes(SecondHashes, SecondID, SecondID->getDefinition());
  1810. DiffResult DR = FindTypeDiffs(FirstHashes, SecondHashes);
  1811. ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
  1812. ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
  1813. const Decl *FirstDecl = DR.FirstDecl;
  1814. const Decl *SecondDecl = DR.SecondDecl;
  1815. if (FirstDiffType == Other || SecondDiffType == Other) {
  1816. diagnoseSubMismatchUnexpected(DR, FirstID, FirstModule, SecondID,
  1817. SecondModule);
  1818. return true;
  1819. }
  1820. if (FirstDiffType != SecondDiffType) {
  1821. diagnoseSubMismatchDifferentDeclKinds(DR, FirstID, FirstModule, SecondID,
  1822. SecondModule);
  1823. return true;
  1824. }
  1825. assert(FirstDiffType == SecondDiffType);
  1826. switch (FirstDiffType) {
  1827. // Already handled.
  1828. case EndOfClass:
  1829. case Other:
  1830. // Cannot be contained by ObjCInterfaceDecl, invalid in this context.
  1831. case Field:
  1832. case TypeDef:
  1833. case Var:
  1834. // C++ only, invalid in this context.
  1835. case PublicSpecifer:
  1836. case PrivateSpecifer:
  1837. case ProtectedSpecifer:
  1838. case StaticAssert:
  1839. case CXXMethod:
  1840. case TypeAlias:
  1841. case Friend:
  1842. case FunctionTemplate:
  1843. llvm_unreachable("Invalid diff type");
  1844. case ObjCMethod: {
  1845. if (diagnoseSubMismatchObjCMethod(FirstID, FirstModule, SecondModule,
  1846. cast<ObjCMethodDecl>(FirstDecl),
  1847. cast<ObjCMethodDecl>(SecondDecl)))
  1848. return true;
  1849. break;
  1850. }
  1851. case ObjCIvar: {
  1852. if (diagnoseSubMismatchField(FirstID, FirstModule, SecondModule,
  1853. cast<FieldDecl>(FirstDecl),
  1854. cast<FieldDecl>(SecondDecl)))
  1855. return true;
  1856. // Check if the access match.
  1857. const ObjCIvarDecl *FirstIvar = cast<ObjCIvarDecl>(FirstDecl);
  1858. const ObjCIvarDecl *SecondIvar = cast<ObjCIvarDecl>(SecondDecl);
  1859. if (FirstIvar->getCanonicalAccessControl() !=
  1860. SecondIvar->getCanonicalAccessControl()) {
  1861. DiagError(FirstIvar->getLocation(), FirstIvar->getSourceRange(),
  1862. IVarAccess)
  1863. << FirstIvar->getName()
  1864. << (int)FirstIvar->getCanonicalAccessControl();
  1865. DiagNote(SecondIvar->getLocation(), SecondIvar->getSourceRange(),
  1866. IVarAccess)
  1867. << SecondIvar->getName()
  1868. << (int)SecondIvar->getCanonicalAccessControl();
  1869. return true;
  1870. }
  1871. break;
  1872. }
  1873. case ObjCProperty: {
  1874. if (diagnoseSubMismatchObjCProperty(FirstID, FirstModule, SecondModule,
  1875. cast<ObjCPropertyDecl>(FirstDecl),
  1876. cast<ObjCPropertyDecl>(SecondDecl)))
  1877. return true;
  1878. break;
  1879. }
  1880. }
  1881. Diag(FirstDecl->getLocation(),
  1882. diag::err_module_odr_violation_mismatch_decl_unknown)
  1883. << FirstID << FirstModule.empty() << FirstModule << FirstDiffType
  1884. << FirstDecl->getSourceRange();
  1885. Diag(SecondDecl->getLocation(),
  1886. diag::note_module_odr_violation_mismatch_decl_unknown)
  1887. << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
  1888. return true;
  1889. }
  1890. bool ODRDiagsEmitter::diagnoseMismatch(
  1891. const ObjCProtocolDecl *FirstProtocol,
  1892. const ObjCProtocolDecl *SecondProtocol,
  1893. const struct ObjCProtocolDecl::DefinitionData *SecondDD) const {
  1894. if (FirstProtocol == SecondProtocol)
  1895. return false;
  1896. std::string FirstModule = getOwningModuleNameForDiagnostic(FirstProtocol);
  1897. std::string SecondModule = getOwningModuleNameForDiagnostic(SecondProtocol);
  1898. const ObjCProtocolDecl::DefinitionData *FirstDD = &FirstProtocol->data();
  1899. assert(FirstDD && SecondDD && "Definitions without DefinitionData");
  1900. // Diagnostics from ObjCProtocol DefinitionData are emitted here.
  1901. if (FirstDD != SecondDD) {
  1902. // Check both protocols reference the same protocols.
  1903. const ObjCProtocolList &FirstProtocols =
  1904. FirstProtocol->getReferencedProtocols();
  1905. const ObjCProtocolList &SecondProtocols = SecondDD->ReferencedProtocols;
  1906. if (diagnoseSubMismatchProtocols(FirstProtocols, FirstProtocol, FirstModule,
  1907. SecondProtocols, SecondProtocol,
  1908. SecondModule))
  1909. return true;
  1910. }
  1911. auto PopulateHashes = [](DeclHashes &Hashes, const ObjCProtocolDecl *ID,
  1912. const DeclContext *DC) {
  1913. for (const Decl *D : ID->decls()) {
  1914. if (!ODRHash::isSubDeclToBeProcessed(D, DC))
  1915. continue;
  1916. Hashes.emplace_back(D, computeODRHash(D));
  1917. }
  1918. };
  1919. DeclHashes FirstHashes;
  1920. DeclHashes SecondHashes;
  1921. // Use definition as DeclContext because definitions are merged when
  1922. // DeclContexts are merged and separate when DeclContexts are separate.
  1923. PopulateHashes(FirstHashes, FirstProtocol, FirstProtocol->getDefinition());
  1924. PopulateHashes(SecondHashes, SecondProtocol, SecondProtocol->getDefinition());
  1925. DiffResult DR = FindTypeDiffs(FirstHashes, SecondHashes);
  1926. ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
  1927. ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
  1928. const Decl *FirstDecl = DR.FirstDecl;
  1929. const Decl *SecondDecl = DR.SecondDecl;
  1930. if (FirstDiffType == Other || SecondDiffType == Other) {
  1931. diagnoseSubMismatchUnexpected(DR, FirstProtocol, FirstModule,
  1932. SecondProtocol, SecondModule);
  1933. return true;
  1934. }
  1935. if (FirstDiffType != SecondDiffType) {
  1936. diagnoseSubMismatchDifferentDeclKinds(DR, FirstProtocol, FirstModule,
  1937. SecondProtocol, SecondModule);
  1938. return true;
  1939. }
  1940. assert(FirstDiffType == SecondDiffType);
  1941. switch (FirstDiffType) {
  1942. // Already handled.
  1943. case EndOfClass:
  1944. case Other:
  1945. // Cannot be contained by ObjCProtocolDecl, invalid in this context.
  1946. case Field:
  1947. case TypeDef:
  1948. case Var:
  1949. case ObjCIvar:
  1950. // C++ only, invalid in this context.
  1951. case PublicSpecifer:
  1952. case PrivateSpecifer:
  1953. case ProtectedSpecifer:
  1954. case StaticAssert:
  1955. case CXXMethod:
  1956. case TypeAlias:
  1957. case Friend:
  1958. case FunctionTemplate:
  1959. llvm_unreachable("Invalid diff type");
  1960. case ObjCMethod: {
  1961. if (diagnoseSubMismatchObjCMethod(FirstProtocol, FirstModule, SecondModule,
  1962. cast<ObjCMethodDecl>(FirstDecl),
  1963. cast<ObjCMethodDecl>(SecondDecl)))
  1964. return true;
  1965. break;
  1966. }
  1967. case ObjCProperty: {
  1968. if (diagnoseSubMismatchObjCProperty(FirstProtocol, FirstModule,
  1969. SecondModule,
  1970. cast<ObjCPropertyDecl>(FirstDecl),
  1971. cast<ObjCPropertyDecl>(SecondDecl)))
  1972. return true;
  1973. break;
  1974. }
  1975. }
  1976. Diag(FirstDecl->getLocation(),
  1977. diag::err_module_odr_violation_mismatch_decl_unknown)
  1978. << FirstProtocol << FirstModule.empty() << FirstModule << FirstDiffType
  1979. << FirstDecl->getSourceRange();
  1980. Diag(SecondDecl->getLocation(),
  1981. diag::note_module_odr_violation_mismatch_decl_unknown)
  1982. << SecondModule.empty() << SecondModule << FirstDiffType
  1983. << SecondDecl->getSourceRange();
  1984. return true;
  1985. }