clang  16.0.0git
ODRDiagsEmitter.h
Go to the documentation of this file.
1 //===- ODRDiagsEmitter.h - Emits diagnostic 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 #ifndef LLVM_CLANG_AST_ODRDIAGSEMITTER_H
10 #define LLVM_CLANG_AST_ODRDIAGSEMITTER_H
11 
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/DeclCXX.h"
14 #include "clang/AST/DeclObjC.h"
15 #include "clang/Basic/Diagnostic.h"
17 
18 namespace clang {
19 
21 public:
23  const LangOptions &LangOpts)
24  : Diags(Diags), Context(Context), LangOpts(LangOpts) {}
25 
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 
32  /// Diagnose ODR mismatch between 2 EnumDecl.
33  ///
34  /// Returns true if found a mismatch and diagnosed it.
35  bool diagnoseMismatch(const EnumDecl *FirstEnum,
36  const EnumDecl *SecondEnum) const;
37 
38  /// Diagnose ODR mismatch between 2 CXXRecordDecl.
39  ///
40  /// Returns true if found a mismatch and diagnosed it.
41  /// To compare 2 declarations with merged and identical definition data
42  /// you need to provide pre-merge definition data in \p SecondDD.
43  bool
44  diagnoseMismatch(const CXXRecordDecl *FirstRecord,
45  const CXXRecordDecl *SecondRecord,
46  const struct CXXRecordDecl::DefinitionData *SecondDD) const;
47 
48  /// Diagnose ODR mismatch between 2 ObjCProtocolDecl.
49  ///
50  /// Returns true if found a mismatch and diagnosed it.
51  /// To compare 2 declarations with merged and identical definition data
52  /// you need to provide pre-merge definition data in \p SecondDD.
53  bool diagnoseMismatch(
54  const ObjCProtocolDecl *FirstProtocol,
55  const ObjCProtocolDecl *SecondProtocol,
56  const struct ObjCProtocolDecl::DefinitionData *SecondDD) const;
57 
58  /// Diagnose ODR mismatch between ObjCProtocolDecl with different definitions.
59  bool diagnoseMismatch(const ObjCProtocolDecl *FirstProtocol,
60  const ObjCProtocolDecl *SecondProtocol) const {
61  assert(FirstProtocol->data().Definition !=
62  SecondProtocol->data().Definition &&
63  "Don't diagnose differences when definitions are merged already");
64  return diagnoseMismatch(FirstProtocol, SecondProtocol,
65  &SecondProtocol->data());
66  }
67 
68  /// Get the best name we know for the module that owns the given
69  /// declaration, or an empty string if the declaration is not from a module.
71 
72 private:
74 
75  // Used with err_module_odr_violation_mismatch_decl,
76  // note_module_odr_violation_mismatch_decl,
77  // err_module_odr_violation_mismatch_decl_unknown,
78  // and note_module_odr_violation_mismatch_decl_unknown
79  // This list should be the same Decl's as in ODRHash::isSubDeclToBeProcessed
80  enum ODRMismatchDecl {
81  EndOfClass,
82  PublicSpecifer,
83  PrivateSpecifer,
84  ProtectedSpecifer,
85  StaticAssert,
86  Field,
87  CXXMethod,
88  TypeAlias,
89  TypeDef,
90  Var,
91  Friend,
92  FunctionTemplate,
93  ObjCMethod,
94  ObjCProperty,
95  Other
96  };
97 
98  struct DiffResult {
99  const Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
100  ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
101  };
102 
103  // If there is a diagnoseable difference, FirstDiffType and
104  // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
105  // filled in if not EndOfClass.
106  static DiffResult FindTypeDiffs(DeclHashes &FirstHashes,
107  DeclHashes &SecondHashes);
108 
109  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
110  return Diags.Report(Loc, DiagID);
111  }
112 
113  // Use this to diagnose that an unexpected Decl was encountered
114  // or no difference was detected. This causes a generic error
115  // message to be emitted.
116  void diagnoseSubMismatchUnexpected(DiffResult &DR,
117  const NamedDecl *FirstRecord,
118  StringRef FirstModule,
119  const NamedDecl *SecondRecord,
120  StringRef SecondModule) const;
121 
122  void diagnoseSubMismatchDifferentDeclKinds(DiffResult &DR,
123  const NamedDecl *FirstRecord,
124  StringRef FirstModule,
125  const NamedDecl *SecondRecord,
126  StringRef SecondModule) const;
127 
128  bool diagnoseSubMismatchField(const NamedDecl *FirstRecord,
129  StringRef FirstModule, StringRef SecondModule,
130  const FieldDecl *FirstField,
131  const FieldDecl *SecondField) const;
132 
133  bool diagnoseSubMismatchTypedef(const NamedDecl *FirstRecord,
134  StringRef FirstModule, StringRef SecondModule,
135  const TypedefNameDecl *FirstTD,
136  const TypedefNameDecl *SecondTD,
137  bool IsTypeAlias) const;
138 
139  bool diagnoseSubMismatchVar(const NamedDecl *FirstRecord,
140  StringRef FirstModule, StringRef SecondModule,
141  const VarDecl *FirstVD,
142  const VarDecl *SecondVD) const;
143 
144  /// Check if protocol lists are the same and diagnose if they are different.
145  ///
146  /// Returns true if found a mismatch and diagnosed it.
147  bool diagnoseSubMismatchProtocols(const ObjCProtocolList &FirstProtocols,
148  const ObjCContainerDecl *FirstContainer,
149  StringRef FirstModule,
150  const ObjCProtocolList &SecondProtocols,
151  const ObjCContainerDecl *SecondContainer,
152  StringRef SecondModule) const;
153 
154  /// Check if Objective-C methods are the same and diagnose if different.
155  ///
156  /// Returns true if found a mismatch and diagnosed it.
157  bool diagnoseSubMismatchObjCMethod(const NamedDecl *FirstObjCContainer,
158  StringRef FirstModule,
159  StringRef SecondModule,
160  const ObjCMethodDecl *FirstMethod,
161  const ObjCMethodDecl *SecondMethod) const;
162 
163  /// Check if Objective-C properties are the same and diagnose if different.
164  ///
165  /// Returns true if found a mismatch and diagnosed it.
166  bool
167  diagnoseSubMismatchObjCProperty(const NamedDecl *FirstObjCContainer,
168  StringRef FirstModule, StringRef SecondModule,
169  const ObjCPropertyDecl *FirstProp,
170  const ObjCPropertyDecl *SecondProp) const;
171 
172 private:
173  DiagnosticsEngine &Diags;
174  const ASTContext &Context;
175  const LangOptions &LangOpts;
176 };
177 
178 } // namespace clang
179 
180 #endif
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
llvm::SmallVector
Definition: LLVM.h:38
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
DeclCXX.h
clang::ODRDiagsEmitter::ODRDiagsEmitter
ODRDiagsEmitter(DiagnosticsEngine &Diags, const ASTContext &Context, const LangOptions &LangOpts)
Definition: ODRDiagsEmitter.h:22
clang::EnumDecl
Represents an enum.
Definition: Decl.h:3705
clang::ODRDiagsEmitter::getOwningModuleNameForDiagnostic
static std::string getOwningModuleNameForDiagnostic(const Decl *D)
Get the best name we know for the module that owns the given declaration, or an empty string if the d...
Definition: ODRDiagsEmitter.cpp:43
DeclObjC.h
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
LangOptions.h
Diagnostic.h
ASTContext.h
clang::ODRDiagsEmitter::diagnoseMismatch
bool diagnoseMismatch(const FunctionDecl *FirstFunction, const FunctionDecl *SecondFunction) const
Diagnose ODR mismatch between 2 FunctionDecl.
Definition: ODRDiagsEmitter.cpp:1550
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:81
clang
Definition: CalledOnceCheck.h:17
clang::ObjCProtocolDecl
Represents an Objective-C protocol declaration.
Definition: DeclObjC.h:2050
clang::ODRDiagsEmitter::diagnoseMismatch
bool diagnoseMismatch(const ObjCProtocolDecl *FirstProtocol, const ObjCProtocolDecl *SecondProtocol) const
Diagnose ODR mismatch between ObjCProtocolDecl with different definitions.
Definition: ODRDiagsEmitter.h:59
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1904
clang::ODRDiagsEmitter
Definition: ODRDiagsEmitter.h:20
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1537