ODRDiagsEmitter.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ODRDiagsEmitter.h - Emits diagnostic for ODR mismatches --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_AST_ODRDIAGSEMITTER_H
  14. #define LLVM_CLANG_AST_ODRDIAGSEMITTER_H
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/DeclCXX.h"
  17. #include "clang/AST/DeclObjC.h"
  18. #include "clang/Basic/Diagnostic.h"
  19. #include "clang/Basic/LangOptions.h"
  20. namespace clang {
  21. class ODRDiagsEmitter {
  22. public:
  23. ODRDiagsEmitter(DiagnosticsEngine &Diags, const ASTContext &Context,
  24. const LangOptions &LangOpts)
  25. : Diags(Diags), Context(Context), LangOpts(LangOpts) {}
  26. /// Diagnose ODR mismatch between 2 FunctionDecl.
  27. ///
  28. /// Returns true if found a mismatch and diagnosed it.
  29. bool diagnoseMismatch(const FunctionDecl *FirstFunction,
  30. const FunctionDecl *SecondFunction) const;
  31. /// Diagnose ODR mismatch between 2 EnumDecl.
  32. ///
  33. /// Returns true if found a mismatch and diagnosed it.
  34. bool diagnoseMismatch(const EnumDecl *FirstEnum,
  35. const EnumDecl *SecondEnum) const;
  36. /// Diagnose ODR mismatch between 2 CXXRecordDecl.
  37. ///
  38. /// Returns true if found a mismatch and diagnosed it.
  39. /// To compare 2 declarations with merged and identical definition data
  40. /// you need to provide pre-merge definition data in \p SecondDD.
  41. bool
  42. diagnoseMismatch(const CXXRecordDecl *FirstRecord,
  43. const CXXRecordDecl *SecondRecord,
  44. const struct CXXRecordDecl::DefinitionData *SecondDD) const;
  45. /// Diagnose ODR mismatch between 2 RecordDecl that are not CXXRecordDecl.
  46. ///
  47. /// Returns true if found a mismatch and diagnosed it.
  48. bool diagnoseMismatch(const RecordDecl *FirstRecord,
  49. const RecordDecl *SecondRecord) const;
  50. /// Diagnose ODR mismatch between 2 ObjCInterfaceDecl.
  51. ///
  52. /// Returns true if found a mismatch and diagnosed it.
  53. bool diagnoseMismatch(
  54. const ObjCInterfaceDecl *FirstID, const ObjCInterfaceDecl *SecondID,
  55. const struct ObjCInterfaceDecl::DefinitionData *SecondDD) const;
  56. /// Diagnose ODR mismatch between ObjCInterfaceDecl with different
  57. /// definitions.
  58. bool diagnoseMismatch(const ObjCInterfaceDecl *FirstID,
  59. const ObjCInterfaceDecl *SecondID) const {
  60. assert(FirstID->data().Definition != SecondID->data().Definition &&
  61. "Don't diagnose differences when definitions are merged already");
  62. return diagnoseMismatch(FirstID, SecondID, &SecondID->data());
  63. }
  64. /// Diagnose ODR mismatch between 2 ObjCProtocolDecl.
  65. ///
  66. /// Returns true if found a mismatch and diagnosed it.
  67. /// To compare 2 declarations with merged and identical definition data
  68. /// you need to provide pre-merge definition data in \p SecondDD.
  69. bool diagnoseMismatch(
  70. const ObjCProtocolDecl *FirstProtocol,
  71. const ObjCProtocolDecl *SecondProtocol,
  72. const struct ObjCProtocolDecl::DefinitionData *SecondDD) const;
  73. /// Diagnose ODR mismatch between ObjCProtocolDecl with different definitions.
  74. bool diagnoseMismatch(const ObjCProtocolDecl *FirstProtocol,
  75. const ObjCProtocolDecl *SecondProtocol) const {
  76. assert(FirstProtocol->data().Definition !=
  77. SecondProtocol->data().Definition &&
  78. "Don't diagnose differences when definitions are merged already");
  79. return diagnoseMismatch(FirstProtocol, SecondProtocol,
  80. &SecondProtocol->data());
  81. }
  82. /// Get the best name we know for the module that owns the given
  83. /// declaration, or an empty string if the declaration is not from a module.
  84. static std::string getOwningModuleNameForDiagnostic(const Decl *D);
  85. private:
  86. using DeclHashes = llvm::SmallVector<std::pair<const Decl *, unsigned>, 4>;
  87. // Used with err_module_odr_violation_mismatch_decl,
  88. // note_module_odr_violation_mismatch_decl,
  89. // err_module_odr_violation_mismatch_decl_unknown,
  90. // and note_module_odr_violation_mismatch_decl_unknown
  91. // This list should be the same Decl's as in ODRHash::isSubDeclToBeProcessed
  92. enum ODRMismatchDecl {
  93. EndOfClass,
  94. PublicSpecifer,
  95. PrivateSpecifer,
  96. ProtectedSpecifer,
  97. StaticAssert,
  98. Field,
  99. CXXMethod,
  100. TypeAlias,
  101. TypeDef,
  102. Var,
  103. Friend,
  104. FunctionTemplate,
  105. ObjCMethod,
  106. ObjCIvar,
  107. ObjCProperty,
  108. Other
  109. };
  110. struct DiffResult {
  111. const Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
  112. ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
  113. };
  114. // If there is a diagnoseable difference, FirstDiffType and
  115. // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
  116. // filled in if not EndOfClass.
  117. static DiffResult FindTypeDiffs(DeclHashes &FirstHashes,
  118. DeclHashes &SecondHashes);
  119. DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
  120. return Diags.Report(Loc, DiagID);
  121. }
  122. // Use this to diagnose that an unexpected Decl was encountered
  123. // or no difference was detected. This causes a generic error
  124. // message to be emitted.
  125. void diagnoseSubMismatchUnexpected(DiffResult &DR,
  126. const NamedDecl *FirstRecord,
  127. StringRef FirstModule,
  128. const NamedDecl *SecondRecord,
  129. StringRef SecondModule) const;
  130. void diagnoseSubMismatchDifferentDeclKinds(DiffResult &DR,
  131. const NamedDecl *FirstRecord,
  132. StringRef FirstModule,
  133. const NamedDecl *SecondRecord,
  134. StringRef SecondModule) const;
  135. bool diagnoseSubMismatchField(const NamedDecl *FirstRecord,
  136. StringRef FirstModule, StringRef SecondModule,
  137. const FieldDecl *FirstField,
  138. const FieldDecl *SecondField) const;
  139. bool diagnoseSubMismatchTypedef(const NamedDecl *FirstRecord,
  140. StringRef FirstModule, StringRef SecondModule,
  141. const TypedefNameDecl *FirstTD,
  142. const TypedefNameDecl *SecondTD,
  143. bool IsTypeAlias) const;
  144. bool diagnoseSubMismatchVar(const NamedDecl *FirstRecord,
  145. StringRef FirstModule, StringRef SecondModule,
  146. const VarDecl *FirstVD,
  147. const VarDecl *SecondVD) const;
  148. /// Check if protocol lists are the same and diagnose if they are different.
  149. ///
  150. /// Returns true if found a mismatch and diagnosed it.
  151. bool diagnoseSubMismatchProtocols(const ObjCProtocolList &FirstProtocols,
  152. const ObjCContainerDecl *FirstContainer,
  153. StringRef FirstModule,
  154. const ObjCProtocolList &SecondProtocols,
  155. const ObjCContainerDecl *SecondContainer,
  156. StringRef SecondModule) const;
  157. /// Check if Objective-C methods are the same and diagnose if different.
  158. ///
  159. /// Returns true if found a mismatch and diagnosed it.
  160. bool diagnoseSubMismatchObjCMethod(const NamedDecl *FirstObjCContainer,
  161. StringRef FirstModule,
  162. StringRef SecondModule,
  163. const ObjCMethodDecl *FirstMethod,
  164. const ObjCMethodDecl *SecondMethod) const;
  165. /// Check if Objective-C properties are the same and diagnose if different.
  166. ///
  167. /// Returns true if found a mismatch and diagnosed it.
  168. bool
  169. diagnoseSubMismatchObjCProperty(const NamedDecl *FirstObjCContainer,
  170. StringRef FirstModule, StringRef SecondModule,
  171. const ObjCPropertyDecl *FirstProp,
  172. const ObjCPropertyDecl *SecondProp) const;
  173. private:
  174. DiagnosticsEngine &Diags;
  175. const ASTContext &Context;
  176. const LangOptions &LangOpts;
  177. };
  178. } // namespace clang
  179. #endif
  180. #ifdef __GNUC__
  181. #pragma GCC diagnostic pop
  182. #endif