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