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 /// \brief 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/Sema/Sema.h"
30 /// A declaration being accessed, together with information about how
32 class AccessedEntity {
34 /// A member declaration found through lookup. The target is the
36 enum MemberNonce { Member };
38 /// A hierarchy (base-to-derived or derived-to-base) conversion.
39 /// The target is the base class.
40 enum BaseNonce { Base };
42 bool isMemberAccess() const { return IsMember; }
44 AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator,
46 CXXRecordDecl *NamingClass,
47 DeclAccessPair FoundDecl,
48 QualType BaseObjectType)
49 : Access(FoundDecl.getAccess()), IsMember(true),
50 Target(FoundDecl.getDecl()), NamingClass(NamingClass),
51 BaseObjectType(BaseObjectType), Diag(0, Allocator) {
54 AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator,
56 CXXRecordDecl *BaseClass,
57 CXXRecordDecl *DerivedClass,
58 AccessSpecifier Access)
59 : Access(Access), IsMember(false),
61 NamingClass(DerivedClass),
65 bool isQuiet() const { return Diag.getDiagID() == 0; }
67 AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
69 // These apply to member decls...
70 NamedDecl *getTargetDecl() const { return Target; }
71 CXXRecordDecl *getNamingClass() const { return NamingClass; }
73 // ...and these apply to hierarchy conversions.
74 CXXRecordDecl *getBaseClass() const {
75 assert(!IsMember); return cast<CXXRecordDecl>(Target);
77 CXXRecordDecl *getDerivedClass() const { return NamingClass; }
79 /// Retrieves the base object type, important when accessing
80 /// an instance member.
81 QualType getBaseObjectType() const { return BaseObjectType; }
83 /// Sets a diagnostic to be performed. The diagnostic is given
84 /// four (additional) arguments:
85 /// %0 - 0 if the entity was private, 1 if protected
86 /// %1 - the DeclarationName of the entity
87 /// %2 - the TypeDecl type of the naming class
88 /// %3 - the TypeDecl type of the declaring class
89 void setDiag(const PartialDiagnostic &PDiag) {
90 assert(isQuiet() && "partial diagnostic already defined");
93 PartialDiagnostic &setDiag(unsigned DiagID) {
94 assert(isQuiet() && "partial diagnostic already defined");
95 assert(DiagID && "creating null diagnostic");
99 const PartialDiagnostic &getDiag() const {
105 unsigned IsMember : 1;
107 CXXRecordDecl *NamingClass;
108 QualType BaseObjectType;
109 PartialDiagnostic Diag;
112 /// A diagnostic message which has been conditionally emitted pending
113 /// the complete parsing of the current declaration.
114 class DelayedDiagnostic {
116 enum DDKind : unsigned char { Availability, Access, ForbiddenType };
125 static DelayedDiagnostic makeAvailability(AvailabilityResult AR,
127 const NamedDecl *ReferringDecl,
128 const NamedDecl *OffendingDecl,
129 const ObjCInterfaceDecl *UnknownObjCClass,
130 const ObjCPropertyDecl *ObjCProperty,
132 bool ObjCPropertyAccess);
135 static DelayedDiagnostic makeAccess(SourceLocation Loc,
136 const AccessedEntity &Entity) {
137 DelayedDiagnostic DD;
139 DD.Triggered = false;
141 new (&DD.getAccessData()) AccessedEntity(Entity);
145 static DelayedDiagnostic makeForbiddenType(SourceLocation loc,
149 DelayedDiagnostic DD;
150 DD.Kind = ForbiddenType;
151 DD.Triggered = false;
153 DD.ForbiddenTypeData.Diagnostic = diagnostic;
154 DD.ForbiddenTypeData.OperandType = type.getAsOpaquePtr();
155 DD.ForbiddenTypeData.Argument = argument;
159 AccessedEntity &getAccessData() {
160 assert(Kind == Access && "Not an access diagnostic.");
161 return *reinterpret_cast<AccessedEntity*>(AccessData);
163 const AccessedEntity &getAccessData() const {
164 assert(Kind == Access && "Not an access diagnostic.");
165 return *reinterpret_cast<const AccessedEntity*>(AccessData);
168 const NamedDecl *getAvailabilityReferringDecl() const {
169 assert(Kind == Availability && "Not an availability diagnostic.");
170 return AvailabilityData.ReferringDecl;
173 const NamedDecl *getAvailabilityOffendingDecl() const {
174 return AvailabilityData.OffendingDecl;
177 StringRef getAvailabilityMessage() const {
178 assert(Kind == Availability && "Not an availability diagnostic.");
179 return StringRef(AvailabilityData.Message, AvailabilityData.MessageLen);
182 AvailabilityResult getAvailabilityResult() const {
183 assert(Kind == Availability && "Not an availability diagnostic.");
184 return AvailabilityData.AR;
187 /// The diagnostic ID to emit. Used like so:
188 /// Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
189 /// << diag.getForbiddenTypeOperand()
190 /// << diag.getForbiddenTypeArgument();
191 unsigned getForbiddenTypeDiagnostic() const {
192 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
193 return ForbiddenTypeData.Diagnostic;
196 unsigned getForbiddenTypeArgument() const {
197 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
198 return ForbiddenTypeData.Argument;
201 QualType getForbiddenTypeOperand() const {
202 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
203 return QualType::getFromOpaquePtr(ForbiddenTypeData.OperandType);
206 const ObjCInterfaceDecl *getUnknownObjCClass() const {
207 return AvailabilityData.UnknownObjCClass;
210 const ObjCPropertyDecl *getObjCProperty() const {
211 return AvailabilityData.ObjCProperty;
214 bool getObjCPropertyAccess() const {
215 return AvailabilityData.ObjCPropertyAccess;
221 const NamedDecl *ReferringDecl;
222 const NamedDecl *OffendingDecl;
223 const ObjCInterfaceDecl *UnknownObjCClass;
224 const ObjCPropertyDecl *ObjCProperty;
227 AvailabilityResult AR;
228 bool ObjCPropertyAccess;
238 struct AD AvailabilityData;
239 struct FTD ForbiddenTypeData;
242 char AccessData[sizeof(AccessedEntity)];
246 /// \brief A collection of diagnostics which were delayed.
247 class DelayedDiagnosticPool {
248 const DelayedDiagnosticPool *Parent;
249 SmallVector<DelayedDiagnostic, 4> Diagnostics;
251 DelayedDiagnosticPool(const DelayedDiagnosticPool &) = delete;
252 void operator=(const DelayedDiagnosticPool &) = delete;
254 DelayedDiagnosticPool(const DelayedDiagnosticPool *parent) : Parent(parent) {}
255 ~DelayedDiagnosticPool() {
256 for (SmallVectorImpl<DelayedDiagnostic>::iterator
257 i = Diagnostics.begin(), e = Diagnostics.end(); i != e; ++i)
261 DelayedDiagnosticPool(DelayedDiagnosticPool &&Other)
262 : Parent(Other.Parent), Diagnostics(std::move(Other.Diagnostics)) {
263 Other.Diagnostics.clear();
265 DelayedDiagnosticPool &operator=(DelayedDiagnosticPool &&Other) {
266 Parent = Other.Parent;
267 Diagnostics = std::move(Other.Diagnostics);
268 Other.Diagnostics.clear();
272 const DelayedDiagnosticPool *getParent() const { return Parent; }
274 /// Does this pool, or any of its ancestors, contain any diagnostics?
276 return (Diagnostics.empty() && (!Parent || Parent->empty()));
279 /// Add a diagnostic to this pool.
280 void add(const DelayedDiagnostic &diag) {
281 Diagnostics.push_back(diag);
284 /// Steal the diagnostics from the given pool.
285 void steal(DelayedDiagnosticPool &pool) {
286 if (pool.Diagnostics.empty()) return;
288 if (Diagnostics.empty()) {
289 Diagnostics = std::move(pool.Diagnostics);
291 Diagnostics.append(pool.pool_begin(), pool.pool_end());
293 pool.Diagnostics.clear();
296 typedef SmallVectorImpl<DelayedDiagnostic>::const_iterator pool_iterator;
297 pool_iterator pool_begin() const { return Diagnostics.begin(); }
298 pool_iterator pool_end() const { return Diagnostics.end(); }
299 bool pool_empty() const { return Diagnostics.empty(); }
304 /// Add a diagnostic to the current delay pool.
305 inline void Sema::DelayedDiagnostics::add(const sema::DelayedDiagnostic &diag) {
306 assert(shouldDelayDiagnostics() && "trying to delay without pool");