1 //===- DelayedDiagnostic.h - Delayed declarator diagnostics -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Defines the classes clang::DelayedDiagnostic and
12 /// clang::AccessedEntity.
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.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
23 #define LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
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"
44 class ObjCInterfaceDecl;
45 class ObjCPropertyDecl;
49 /// A declaration being accessed, together with information about how
51 class AccessedEntity {
53 /// A member declaration found through lookup. The target is the
55 enum MemberNonce { Member };
57 /// A hierarchy (base-to-derived or derived-to-base) conversion.
58 /// The target is the base class.
59 enum BaseNonce { Base };
61 AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator,
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) {
71 AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator,
73 CXXRecordDecl *BaseClass,
74 CXXRecordDecl *DerivedClass,
75 AccessSpecifier Access)
76 : Access(Access), IsMember(false), Target(BaseClass),
77 NamingClass(DerivedClass), Diag(0, Allocator) {}
79 bool isMemberAccess() const { return IsMember; }
81 bool isQuiet() const { return Diag.getDiagID() == 0; }
83 AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
85 // These apply to member decls...
86 NamedDecl *getTargetDecl() const { return Target; }
87 CXXRecordDecl *getNamingClass() const { return NamingClass; }
89 // ...and these apply to hierarchy conversions.
90 CXXRecordDecl *getBaseClass() const {
91 assert(!IsMember); return cast<CXXRecordDecl>(Target);
93 CXXRecordDecl *getDerivedClass() const { return NamingClass; }
95 /// Retrieves the base object type, important when accessing
96 /// an instance member.
97 QualType getBaseObjectType() const { return BaseObjectType; }
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");
109 PartialDiagnostic &setDiag(unsigned DiagID) {
110 assert(isQuiet() && "partial diagnostic already defined");
111 assert(DiagID && "creating null diagnostic");
115 const PartialDiagnostic &getDiag() const {
121 unsigned IsMember : 1;
123 CXXRecordDecl *NamingClass;
124 QualType BaseObjectType;
125 PartialDiagnostic Diag;
128 /// A diagnostic message which has been conditionally emitted pending
129 /// the complete parsing of the current declaration.
130 class DelayedDiagnostic {
132 enum DDKind : unsigned char { Availability, Access, ForbiddenType };
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,
148 bool ObjCPropertyAccess);
150 static DelayedDiagnostic makeAccess(SourceLocation Loc,
151 const AccessedEntity &Entity) {
152 DelayedDiagnostic DD;
154 DD.Triggered = false;
156 new (&DD.getAccessData()) AccessedEntity(Entity);
160 static DelayedDiagnostic makeForbiddenType(SourceLocation loc,
164 DelayedDiagnostic DD;
165 DD.Kind = ForbiddenType;
166 DD.Triggered = false;
168 DD.ForbiddenTypeData.Diagnostic = diagnostic;
169 DD.ForbiddenTypeData.OperandType = type.getAsOpaquePtr();
170 DD.ForbiddenTypeData.Argument = argument;
174 AccessedEntity &getAccessData() {
175 assert(Kind == Access && "Not an access diagnostic.");
176 return *reinterpret_cast<AccessedEntity*>(AccessData);
178 const AccessedEntity &getAccessData() const {
179 assert(Kind == Access && "Not an access diagnostic.");
180 return *reinterpret_cast<const AccessedEntity*>(AccessData);
183 const NamedDecl *getAvailabilityReferringDecl() const {
184 assert(Kind == Availability && "Not an availability diagnostic.");
185 return AvailabilityData.ReferringDecl;
188 const NamedDecl *getAvailabilityOffendingDecl() const {
189 return AvailabilityData.OffendingDecl;
192 StringRef getAvailabilityMessage() const {
193 assert(Kind == Availability && "Not an availability diagnostic.");
194 return StringRef(AvailabilityData.Message, AvailabilityData.MessageLen);
197 ArrayRef<SourceLocation> getAvailabilitySelectorLocs() const {
198 assert(Kind == Availability && "Not an availability diagnostic.");
199 return llvm::makeArrayRef(AvailabilityData.SelectorLocs,
200 AvailabilityData.NumSelectorLocs);
203 AvailabilityResult getAvailabilityResult() const {
204 assert(Kind == Availability && "Not an availability diagnostic.");
205 return AvailabilityData.AR;
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;
217 unsigned getForbiddenTypeArgument() const {
218 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
219 return ForbiddenTypeData.Argument;
222 QualType getForbiddenTypeOperand() const {
223 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
224 return QualType::getFromOpaquePtr(ForbiddenTypeData.OperandType);
227 const ObjCInterfaceDecl *getUnknownObjCClass() const {
228 return AvailabilityData.UnknownObjCClass;
231 const ObjCPropertyDecl *getObjCProperty() const {
232 return AvailabilityData.ObjCProperty;
235 bool getObjCPropertyAccess() const {
236 return AvailabilityData.ObjCPropertyAccess;
241 const NamedDecl *ReferringDecl;
242 const NamedDecl *OffendingDecl;
243 const ObjCInterfaceDecl *UnknownObjCClass;
244 const ObjCPropertyDecl *ObjCProperty;
247 SourceLocation *SelectorLocs;
248 size_t NumSelectorLocs;
249 AvailabilityResult AR;
250 bool ObjCPropertyAccess;
260 struct AD AvailabilityData;
261 struct FTD ForbiddenTypeData;
264 char AccessData[sizeof(AccessedEntity)];
268 /// A collection of diagnostics which were delayed.
269 class DelayedDiagnosticPool {
270 const DelayedDiagnosticPool *Parent;
271 SmallVector<DelayedDiagnostic, 4> Diagnostics;
274 DelayedDiagnosticPool(const DelayedDiagnosticPool *parent) : Parent(parent) {}
276 DelayedDiagnosticPool(const DelayedDiagnosticPool &) = delete;
277 DelayedDiagnosticPool &operator=(const DelayedDiagnosticPool &) = delete;
279 DelayedDiagnosticPool(DelayedDiagnosticPool &&Other)
280 : Parent(Other.Parent), Diagnostics(std::move(Other.Diagnostics)) {
281 Other.Diagnostics.clear();
284 DelayedDiagnosticPool &operator=(DelayedDiagnosticPool &&Other) {
285 Parent = Other.Parent;
286 Diagnostics = std::move(Other.Diagnostics);
287 Other.Diagnostics.clear();
291 ~DelayedDiagnosticPool() {
292 for (SmallVectorImpl<DelayedDiagnostic>::iterator
293 i = Diagnostics.begin(), e = Diagnostics.end(); i != e; ++i)
297 const DelayedDiagnosticPool *getParent() const { return Parent; }
299 /// Does this pool, or any of its ancestors, contain any diagnostics?
301 return (Diagnostics.empty() && (!Parent || Parent->empty()));
304 /// Add a diagnostic to this pool.
305 void add(const DelayedDiagnostic &diag) {
306 Diagnostics.push_back(diag);
309 /// Steal the diagnostics from the given pool.
310 void steal(DelayedDiagnosticPool &pool) {
311 if (pool.Diagnostics.empty()) return;
313 if (Diagnostics.empty()) {
314 Diagnostics = std::move(pool.Diagnostics);
316 Diagnostics.append(pool.pool_begin(), pool.pool_end());
318 pool.Diagnostics.clear();
321 using pool_iterator = SmallVectorImpl<DelayedDiagnostic>::const_iterator;
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(); }
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");
338 #endif // LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H