clang 18.0.0git
DelayedDiagnostic.h
Go to the documentation of this file.
1//===- DelayedDiagnostic.h - Delayed declarator diagnostics -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Defines the classes clang::DelayedDiagnostic and
11/// clang::AccessedEntity.
12///
13/// DelayedDiangostic is used to record diagnostics that are being
14/// conditionally produced during declarator parsing. Certain kinds of
15/// diagnostics -- notably deprecation and access control -- are suppressed
16/// based on semantic properties of the parsed declaration that aren't known
17/// until it is fully parsed.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
22#define LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
23
25#include "clang/AST/DeclBase.h"
26#include "clang/AST/DeclCXX.h"
27#include "clang/AST/Type.h"
28#include "clang/Basic/LLVM.h"
32#include "clang/Sema/Sema.h"
33#include "llvm/ADT/ArrayRef.h"
34#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/StringRef.h"
36#include "llvm/Support/Casting.h"
37#include <cassert>
38#include <cstddef>
39#include <utility>
40
41namespace clang {
42
43class ObjCInterfaceDecl;
44class ObjCPropertyDecl;
45
46namespace sema {
47
48/// A declaration being accessed, together with information about how
49/// it was accessed.
51public:
52 /// A member declaration found through lookup. The target is the
53 /// member.
55
56 /// A hierarchy (base-to-derived or derived-to-base) conversion.
57 /// The target is the base class.
58 enum BaseNonce { Base };
59
61 MemberNonce _, CXXRecordDecl *NamingClass,
62 DeclAccessPair FoundDecl, QualType BaseObjectType)
63 : Access(FoundDecl.getAccess()), IsMember(true),
64 Target(FoundDecl.getDecl()), NamingClass(NamingClass),
65 BaseObjectType(BaseObjectType), Diag(0, Allocator) {}
66
68 BaseNonce _, CXXRecordDecl *BaseClass,
69 CXXRecordDecl *DerivedClass, AccessSpecifier Access)
70 : Access(Access), IsMember(false), Target(BaseClass),
71 NamingClass(DerivedClass), Diag(0, Allocator) {}
72
73 bool isMemberAccess() const { return IsMember; }
74
75 bool isQuiet() const { return Diag.getDiagID() == 0; }
76
77 AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
78
79 // These apply to member decls...
80 NamedDecl *getTargetDecl() const { return Target; }
81 CXXRecordDecl *getNamingClass() const { return NamingClass; }
82
83 // ...and these apply to hierarchy conversions.
85 assert(!IsMember); return cast<CXXRecordDecl>(Target);
86 }
87 CXXRecordDecl *getDerivedClass() const { return NamingClass; }
88
89 /// Retrieves the base object type, important when accessing
90 /// an instance member.
91 QualType getBaseObjectType() const { return BaseObjectType; }
92
93 /// Sets a diagnostic to be performed. The diagnostic is given
94 /// four (additional) arguments:
95 /// %0 - 0 if the entity was private, 1 if protected
96 /// %1 - the DeclarationName of the entity
97 /// %2 - the TypeDecl type of the naming class
98 /// %3 - the TypeDecl type of the declaring class
99 void setDiag(const PartialDiagnostic &PDiag) {
100 assert(isQuiet() && "partial diagnostic already defined");
101 Diag = PDiag;
102 }
103 PartialDiagnostic &setDiag(unsigned DiagID) {
104 assert(isQuiet() && "partial diagnostic already defined");
105 assert(DiagID && "creating null diagnostic");
106 Diag.Reset(DiagID);
107 return Diag;
108 }
109 const PartialDiagnostic &getDiag() const {
110 return Diag;
111 }
112
113private:
114 unsigned Access : 2;
115 unsigned IsMember : 1;
116 NamedDecl *Target;
117 CXXRecordDecl *NamingClass;
118 QualType BaseObjectType;
120};
121
122/// A diagnostic message which has been conditionally emitted pending
123/// the complete parsing of the current declaration.
125public:
126 enum DDKind : unsigned char { Availability, Access, ForbiddenType };
127
130
132
133 void Destroy();
134
137 const NamedDecl *ReferringDecl,
138 const NamedDecl *OffendingDecl,
139 const ObjCInterfaceDecl *UnknownObjCClass,
140 const ObjCPropertyDecl *ObjCProperty,
141 StringRef Msg,
142 bool ObjCPropertyAccess);
143
145 const AccessedEntity &Entity) {
147 DD.Kind = Access;
148 DD.Triggered = false;
149 DD.Loc = Loc;
150 new (&DD.getAccessData()) AccessedEntity(Entity);
151 return DD;
152 }
153
155 unsigned diagnostic,
157 unsigned argument) {
159 DD.Kind = ForbiddenType;
160 DD.Triggered = false;
161 DD.Loc = loc;
162 DD.ForbiddenTypeData.Diagnostic = diagnostic;
163 DD.ForbiddenTypeData.OperandType = type.getAsOpaquePtr();
164 DD.ForbiddenTypeData.Argument = argument;
165 return DD;
166 }
167
169 assert(Kind == Access && "Not an access diagnostic.");
170 return *reinterpret_cast<AccessedEntity*>(AccessData);
171 }
173 assert(Kind == Access && "Not an access diagnostic.");
174 return *reinterpret_cast<const AccessedEntity*>(AccessData);
175 }
176
178 assert(Kind == Availability && "Not an availability diagnostic.");
179 return AvailabilityData.ReferringDecl;
180 }
181
183 return AvailabilityData.OffendingDecl;
184 }
185
186 StringRef getAvailabilityMessage() const {
187 assert(Kind == Availability && "Not an availability diagnostic.");
188 return StringRef(AvailabilityData.Message, AvailabilityData.MessageLen);
189 }
190
192 assert(Kind == Availability && "Not an availability diagnostic.");
193 return llvm::ArrayRef(AvailabilityData.SelectorLocs,
194 AvailabilityData.NumSelectorLocs);
195 }
196
198 assert(Kind == Availability && "Not an availability diagnostic.");
199 return AvailabilityData.AR;
200 }
201
202 /// The diagnostic ID to emit. Used like so:
203 /// Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
204 /// << diag.getForbiddenTypeOperand()
205 /// << diag.getForbiddenTypeArgument();
206 unsigned getForbiddenTypeDiagnostic() const {
207 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
208 return ForbiddenTypeData.Diagnostic;
209 }
210
211 unsigned getForbiddenTypeArgument() const {
212 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
213 return ForbiddenTypeData.Argument;
214 }
215
217 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
219 }
220
222 return AvailabilityData.UnknownObjCClass;
223 }
224
226 return AvailabilityData.ObjCProperty;
227 }
228
230 return AvailabilityData.ObjCPropertyAccess;
231 }
232
233private:
234 struct AD {
235 const NamedDecl *ReferringDecl;
236 const NamedDecl *OffendingDecl;
237 const ObjCInterfaceDecl *UnknownObjCClass;
238 const ObjCPropertyDecl *ObjCProperty;
239 const char *Message;
240 size_t MessageLen;
241 SourceLocation *SelectorLocs;
242 size_t NumSelectorLocs;
244 bool ObjCPropertyAccess;
245 };
246
247 struct FTD {
248 unsigned Diagnostic;
249 unsigned Argument;
250 void *OperandType;
251 };
252
253 union {
256
257 /// Access control.
259 };
260};
261
262/// A collection of diagnostics which were delayed.
264 const DelayedDiagnosticPool *Parent;
266
267public:
269
272
274 : Parent(Other.Parent), Diagnostics(std::move(Other.Diagnostics)) {
275 Other.Diagnostics.clear();
276 }
277
279 Parent = Other.Parent;
280 Diagnostics = std::move(Other.Diagnostics);
281 Other.Diagnostics.clear();
282 return *this;
283 }
284
287 i = Diagnostics.begin(), e = Diagnostics.end(); i != e; ++i)
288 i->Destroy();
289 }
290
291 const DelayedDiagnosticPool *getParent() const { return Parent; }
292
293 /// Does this pool, or any of its ancestors, contain any diagnostics?
294 bool empty() const {
295 return (Diagnostics.empty() && (!Parent || Parent->empty()));
296 }
297
298 /// Add a diagnostic to this pool.
299 void add(const DelayedDiagnostic &diag) {
300 Diagnostics.push_back(diag);
301 }
302
303 /// Steal the diagnostics from the given pool.
305 if (pool.Diagnostics.empty()) return;
306
307 if (Diagnostics.empty()) {
308 Diagnostics = std::move(pool.Diagnostics);
309 } else {
310 Diagnostics.append(pool.pool_begin(), pool.pool_end());
311 }
312 pool.Diagnostics.clear();
313 }
314
316
317 pool_iterator pool_begin() const { return Diagnostics.begin(); }
318 pool_iterator pool_end() const { return Diagnostics.end(); }
319 bool pool_empty() const { return Diagnostics.empty(); }
320};
321
322} // namespace clang
323
324/// Add a diagnostic to the current delay pool.
326 assert(shouldDelayDiagnostics() && "trying to delay without pool");
327 CurPool->add(diag);
328}
329
330} // namespace clang
331
332#endif // LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
NodeId Parent
Definition: ASTDiff.cpp:191
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Implements a partial diagnostic that can be emitted anwyhere in a DiagnosticBuilder stream.
Defines the clang::SourceLocation class and associated facilities.
Defines various enumerations that describe declaration and type specifiers.
C Language Family Type Representation.
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
A POD class for pairing a NamedDecl* with an access specifier.
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1566
This represents a decl that may have a name.
Definition: Decl.h:247
Represents an ObjC class declaration.
Definition: DeclObjC.h:1147
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:729
void Reset(unsigned DiagID=0)
Clear out this partial diagnostic, giving it a new diagnostic ID and removing all of its arguments,...
A (possibly-)qualified type.
Definition: Type.h:736
static QualType getFromOpaquePtr(const void *Ptr)
Definition: Type.h:785
bool shouldDelayDiagnostics()
Determines whether diagnostics should be delayed.
Definition: Sema.h:976
void add(const sema::DelayedDiagnostic &diag)
Adds a delayed diagnostic.
Encodes a location in the source.
An allocator for DiagnosticStorage objects, which uses a small cache to objects, used to reduce mallo...
Definition: Diagnostic.h:1114
A declaration being accessed, together with information about how it was accessed.
PartialDiagnostic & setDiag(unsigned DiagID)
void setDiag(const PartialDiagnostic &PDiag)
Sets a diagnostic to be performed.
CXXRecordDecl * getNamingClass() const
BaseNonce
A hierarchy (base-to-derived or derived-to-base) conversion.
AccessedEntity(PartialDiagnostic::DiagStorageAllocator &Allocator, MemberNonce _, CXXRecordDecl *NamingClass, DeclAccessPair FoundDecl, QualType BaseObjectType)
CXXRecordDecl * getBaseClass() const
const PartialDiagnostic & getDiag() const
CXXRecordDecl * getDerivedClass() const
NamedDecl * getTargetDecl() const
AccessedEntity(PartialDiagnostic::DiagStorageAllocator &Allocator, BaseNonce _, CXXRecordDecl *BaseClass, CXXRecordDecl *DerivedClass, AccessSpecifier Access)
QualType getBaseObjectType() const
Retrieves the base object type, important when accessing an instance member.
AccessSpecifier getAccess() const
MemberNonce
A member declaration found through lookup.
A collection of diagnostics which were delayed.
const DelayedDiagnosticPool * getParent() const
DelayedDiagnosticPool(DelayedDiagnosticPool &&Other)
void steal(DelayedDiagnosticPool &pool)
Steal the diagnostics from the given pool.
DelayedDiagnosticPool(const DelayedDiagnosticPool &)=delete
void add(const DelayedDiagnostic &diag)
Add a diagnostic to this pool.
DelayedDiagnosticPool & operator=(DelayedDiagnosticPool &&Other)
bool empty() const
Does this pool, or any of its ancestors, contain any diagnostics?
SmallVectorImpl< DelayedDiagnostic >::const_iterator pool_iterator
DelayedDiagnosticPool(const DelayedDiagnosticPool *parent)
DelayedDiagnosticPool & operator=(const DelayedDiagnosticPool &)=delete
A diagnostic message which has been conditionally emitted pending the complete parsing of the current...
static DelayedDiagnostic makeAvailability(AvailabilityResult AR, ArrayRef< SourceLocation > Locs, const NamedDecl *ReferringDecl, const NamedDecl *OffendingDecl, const ObjCInterfaceDecl *UnknownObjCClass, const ObjCPropertyDecl *ObjCProperty, StringRef Msg, bool ObjCPropertyAccess)
QualType getForbiddenTypeOperand() const
const ObjCInterfaceDecl * getUnknownObjCClass() const
static DelayedDiagnostic makeForbiddenType(SourceLocation loc, unsigned diagnostic, QualType type, unsigned argument)
const NamedDecl * getAvailabilityOffendingDecl() const
static DelayedDiagnostic makeAccess(SourceLocation Loc, const AccessedEntity &Entity)
unsigned getForbiddenTypeDiagnostic() const
The diagnostic ID to emit.
char AccessData[sizeof(AccessedEntity)]
Access control.
const ObjCPropertyDecl * getObjCProperty() const
StringRef getAvailabilityMessage() const
ArrayRef< SourceLocation > getAvailabilitySelectorLocs() const
AvailabilityResult getAvailabilityResult() const
const NamedDecl * getAvailabilityReferringDecl() const
unsigned getForbiddenTypeArgument() const
const AccessedEntity & getAccessData() const
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
AvailabilityResult
Captures the result of checking the availability of a declaration.
Definition: DeclBase.h:69
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:114
Definition: Format.h:5078
#define true
Definition: stdbool.h:21
#define false
Definition: stdbool.h:22