clang 8.0.0

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