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 //===----------------------------------------------------------------------===//
10 // This file defines the DelayedDiagnostic class, which is used to
11 // record diagnostics that are being conditionally produced during
12 // declarator parsing. Certain kinds of diagnostics --- notably
13 // deprecation and access control --- are suppressed based on
14 // semantic properties of the parsed declaration that aren't known
15 // until it is fully parsed.
17 // This file also defines AccessedEntity.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CLANG_SEMA_DELAYED_DIAGNOSTIC_H
22 #define LLVM_CLANG_SEMA_DELAYED_DIAGNOSTIC_H
24 #include "clang/Sema/Sema.h"
29 /// A declaration being accessed, together with information about how
31 class AccessedEntity {
33 /// A member declaration found through lookup. The target is the
35 enum MemberNonce { Member };
37 /// A hierarchy (base-to-derived or derived-to-base) conversion.
38 /// The target is the base class.
39 enum BaseNonce { Base };
41 bool isMemberAccess() const { return IsMember; }
43 AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator,
45 CXXRecordDecl *NamingClass,
46 DeclAccessPair FoundDecl,
47 QualType BaseObjectType)
48 : Access(FoundDecl.getAccess()), IsMember(true),
49 Target(FoundDecl.getDecl()), NamingClass(NamingClass),
50 BaseObjectType(BaseObjectType), Diag(0, Allocator) {
53 AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator,
55 CXXRecordDecl *BaseClass,
56 CXXRecordDecl *DerivedClass,
57 AccessSpecifier Access)
58 : Access(Access), IsMember(false),
60 NamingClass(DerivedClass),
64 bool isQuiet() const { return Diag.getDiagID() == 0; }
66 AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
68 // These apply to member decls...
69 NamedDecl *getTargetDecl() const { return Target; }
70 CXXRecordDecl *getNamingClass() const { return NamingClass; }
72 // ...and these apply to hierarchy conversions.
73 CXXRecordDecl *getBaseClass() const {
74 assert(!IsMember); return cast<CXXRecordDecl>(Target);
76 CXXRecordDecl *getDerivedClass() const { return NamingClass; }
78 /// Retrieves the base object type, important when accessing
79 /// an instance member.
80 QualType getBaseObjectType() const { return BaseObjectType; }
82 /// Sets a diagnostic to be performed. The diagnostic is given
83 /// four (additional) arguments:
84 /// %0 - 0 if the entity was private, 1 if protected
85 /// %1 - the DeclarationName of the entity
86 /// %2 - the TypeDecl type of the naming class
87 /// %3 - the TypeDecl type of the declaring class
88 void setDiag(const PartialDiagnostic &PDiag) {
89 assert(isQuiet() && "partial diagnostic already defined");
92 PartialDiagnostic &setDiag(unsigned DiagID) {
93 assert(isQuiet() && "partial diagnostic already defined");
94 assert(DiagID && "creating null diagnostic");
98 const PartialDiagnostic &getDiag() const {
104 unsigned IsMember : 1;
106 CXXRecordDecl *NamingClass;
107 QualType BaseObjectType;
108 PartialDiagnostic Diag;
111 /// A diagnostic message which has been conditionally emitted pending
112 /// the complete parsing of the current declaration.
113 class DelayedDiagnostic {
115 enum DDKind { Deprecation, Access, ForbiddenType };
117 unsigned char Kind; // actually a DDKind
124 static DelayedDiagnostic makeDeprecation(SourceLocation Loc,
126 const ObjCInterfaceDecl *UnknownObjCClass,
129 static DelayedDiagnostic makeAccess(SourceLocation Loc,
130 const AccessedEntity &Entity) {
131 DelayedDiagnostic DD;
133 DD.Triggered = false;
135 new (&DD.getAccessData()) AccessedEntity(Entity);
139 static DelayedDiagnostic makeForbiddenType(SourceLocation loc,
143 DelayedDiagnostic DD;
144 DD.Kind = ForbiddenType;
145 DD.Triggered = false;
147 DD.ForbiddenTypeData.Diagnostic = diagnostic;
148 DD.ForbiddenTypeData.OperandType = type.getAsOpaquePtr();
149 DD.ForbiddenTypeData.Argument = argument;
153 AccessedEntity &getAccessData() {
154 assert(Kind == Access && "Not an access diagnostic.");
155 return *reinterpret_cast<AccessedEntity*>(AccessData);
157 const AccessedEntity &getAccessData() const {
158 assert(Kind == Access && "Not an access diagnostic.");
159 return *reinterpret_cast<const AccessedEntity*>(AccessData);
162 const NamedDecl *getDeprecationDecl() const {
163 assert(Kind == Deprecation && "Not a deprecation diagnostic.");
164 return DeprecationData.Decl;
167 StringRef getDeprecationMessage() const {
168 assert(Kind == Deprecation && "Not a deprecation diagnostic.");
169 return StringRef(DeprecationData.Message,
170 DeprecationData.MessageLen);
173 /// The diagnostic ID to emit. Used like so:
174 /// Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
175 /// << diag.getForbiddenTypeOperand()
176 /// << diag.getForbiddenTypeArgument();
177 unsigned getForbiddenTypeDiagnostic() const {
178 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
179 return ForbiddenTypeData.Diagnostic;
182 unsigned getForbiddenTypeArgument() const {
183 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
184 return ForbiddenTypeData.Argument;
187 QualType getForbiddenTypeOperand() const {
188 assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
189 return QualType::getFromOpaquePtr(ForbiddenTypeData.OperandType);
192 const ObjCInterfaceDecl *getUnknownObjCClass() const {
193 return DeprecationData.UnknownObjCClass;
200 const NamedDecl *Decl;
201 const ObjCInterfaceDecl *UnknownObjCClass;
213 char AccessData[sizeof(AccessedEntity)];
217 /// DelayedDiagnosticPool - A collection of diagnostics which were
219 class DelayedDiagnosticPool {
220 const DelayedDiagnosticPool *Parent;
221 llvm::SmallVector<DelayedDiagnostic, 4> Diagnostics;
223 DelayedDiagnosticPool(const DelayedDiagnosticPool &) LLVM_DELETED_FUNCTION;
224 void operator=(const DelayedDiagnosticPool &) LLVM_DELETED_FUNCTION;
226 DelayedDiagnosticPool(const DelayedDiagnosticPool *parent) : Parent(parent) {}
227 ~DelayedDiagnosticPool() {
228 for (llvm::SmallVectorImpl<DelayedDiagnostic>::iterator
229 i = Diagnostics.begin(), e = Diagnostics.end(); i != e; ++i)
233 const DelayedDiagnosticPool *getParent() const { return Parent; }
235 /// Does this pool, or any of its ancestors, contain any diagnostics?
237 return (Diagnostics.empty() && (Parent == NULL || Parent->empty()));
240 /// Add a diagnostic to this pool.
241 void add(const DelayedDiagnostic &diag) {
242 Diagnostics.push_back(diag);
245 /// Steal the diagnostics from the given pool.
246 void steal(DelayedDiagnosticPool &pool) {
247 if (pool.Diagnostics.empty()) return;
249 if (Diagnostics.empty()) {
250 Diagnostics = llvm_move(pool.Diagnostics);
252 Diagnostics.append(pool.pool_begin(), pool.pool_end());
254 pool.Diagnostics.clear();
257 typedef llvm::SmallVectorImpl<DelayedDiagnostic>::const_iterator
259 pool_iterator pool_begin() const { return Diagnostics.begin(); }
260 pool_iterator pool_end() const { return Diagnostics.end(); }
261 bool pool_empty() const { return Diagnostics.empty(); }
266 /// Add a diagnostic to the current delay pool.
267 inline void Sema::DelayedDiagnostics::add(const sema::DelayedDiagnostic &diag) {
268 assert(shouldDelayDiagnostics() && "trying to delay without pool");