]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Sema/DelayedDiagnostic.h
Merge llvm trunk r338150 (just before the 7.0.0 branch point), and
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Sema / DelayedDiagnostic.h
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