clang  6.0.0svn
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 /// \brief 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/Sema/Sema.h"
26 
27 namespace clang {
28 namespace sema {
29 
30 /// A declaration being accessed, together with information about how
31 /// it was accessed.
33 public:
34  /// A member declaration found through lookup. The target is the
35  /// member.
36  enum MemberNonce { Member };
37 
38  /// A hierarchy (base-to-derived or derived-to-base) conversion.
39  /// The target is the base class.
40  enum BaseNonce { Base };
41 
42  bool isMemberAccess() const { return IsMember; }
43 
45  MemberNonce _,
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) {
52  }
53 
55  BaseNonce _,
56  CXXRecordDecl *BaseClass,
57  CXXRecordDecl *DerivedClass,
58  AccessSpecifier Access)
59  : Access(Access), IsMember(false),
60  Target(BaseClass),
61  NamingClass(DerivedClass),
62  Diag(0, Allocator) {
63  }
64 
65  bool isQuiet() const { return Diag.getDiagID() == 0; }
66 
67  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
68 
69  // These apply to member decls...
70  NamedDecl *getTargetDecl() const { return Target; }
71  CXXRecordDecl *getNamingClass() const { return NamingClass; }
72 
73  // ...and these apply to hierarchy conversions.
75  assert(!IsMember); return cast<CXXRecordDecl>(Target);
76  }
77  CXXRecordDecl *getDerivedClass() const { return NamingClass; }
78 
79  /// Retrieves the base object type, important when accessing
80  /// an instance member.
81  QualType getBaseObjectType() const { return BaseObjectType; }
82 
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");
91  Diag = PDiag;
92  }
93  PartialDiagnostic &setDiag(unsigned DiagID) {
94  assert(isQuiet() && "partial diagnostic already defined");
95  assert(DiagID && "creating null diagnostic");
96  Diag.Reset(DiagID);
97  return Diag;
98  }
99  const PartialDiagnostic &getDiag() const {
100  return Diag;
101  }
102 
103 private:
104  unsigned Access : 2;
105  unsigned IsMember : 1;
106  NamedDecl *Target;
107  CXXRecordDecl *NamingClass;
108  QualType BaseObjectType;
109  PartialDiagnostic Diag;
110 };
111 
112 /// A diagnostic message which has been conditionally emitted pending
113 /// the complete parsing of the current declaration.
115 public:
116  enum DDKind : unsigned char { Availability, Access, ForbiddenType };
117 
119  bool Triggered;
120 
122 
123  void Destroy();
124 
125  static DelayedDiagnostic makeAvailability(AvailabilityResult AR,
126  SourceLocation Loc,
127  const NamedDecl *ReferringDecl,
128  const NamedDecl *OffendingDecl,
129  const ObjCInterfaceDecl *UnknownObjCClass,
130  const ObjCPropertyDecl *ObjCProperty,
131  StringRef Msg,
132  bool ObjCPropertyAccess);
133 
134 
136  const AccessedEntity &Entity) {
138  DD.Kind = Access;
139  DD.Triggered = false;
140  DD.Loc = Loc;
141  new (&DD.getAccessData()) AccessedEntity(Entity);
142  return DD;
143  }
144 
146  unsigned diagnostic,
147  QualType type,
148  unsigned argument) {
150  DD.Kind = ForbiddenType;
151  DD.Triggered = false;
152  DD.Loc = loc;
153  DD.ForbiddenTypeData.Diagnostic = diagnostic;
154  DD.ForbiddenTypeData.OperandType = type.getAsOpaquePtr();
155  DD.ForbiddenTypeData.Argument = argument;
156  return DD;
157  }
158 
160  assert(Kind == Access && "Not an access diagnostic.");
161  return *reinterpret_cast<AccessedEntity*>(AccessData);
162  }
163  const AccessedEntity &getAccessData() const {
164  assert(Kind == Access && "Not an access diagnostic.");
165  return *reinterpret_cast<const AccessedEntity*>(AccessData);
166  }
167 
169  assert(Kind == Availability && "Not an availability diagnostic.");
170  return AvailabilityData.ReferringDecl;
171  }
172 
174  return AvailabilityData.OffendingDecl;
175  }
176 
177  StringRef getAvailabilityMessage() const {
178  assert(Kind == Availability && "Not an availability diagnostic.");
179  return StringRef(AvailabilityData.Message, AvailabilityData.MessageLen);
180  }
181 
183  assert(Kind == Availability && "Not an availability diagnostic.");
184  return AvailabilityData.AR;
185  }
186 
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;
194  }
195 
196  unsigned getForbiddenTypeArgument() const {
197  assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
198  return ForbiddenTypeData.Argument;
199  }
200 
202  assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
203  return QualType::getFromOpaquePtr(ForbiddenTypeData.OperandType);
204  }
205 
207  return AvailabilityData.UnknownObjCClass;
208  }
209 
211  return AvailabilityData.ObjCProperty;
212  }
213 
214  bool getObjCPropertyAccess() const {
215  return AvailabilityData.ObjCPropertyAccess;
216  }
217 
218 private:
219 
220  struct AD {
221  const NamedDecl *ReferringDecl;
222  const NamedDecl *OffendingDecl;
223  const ObjCInterfaceDecl *UnknownObjCClass;
224  const ObjCPropertyDecl *ObjCProperty;
225  const char *Message;
226  size_t MessageLen;
228  bool ObjCPropertyAccess;
229  };
230 
231  struct FTD {
232  unsigned Diagnostic;
233  unsigned Argument;
234  void *OperandType;
235  };
236 
237  union {
238  struct AD AvailabilityData;
239  struct FTD ForbiddenTypeData;
240 
241  /// Access control.
242  char AccessData[sizeof(AccessedEntity)];
243  };
244 };
245 
246 /// \brief A collection of diagnostics which were delayed.
250 
252  void operator=(const DelayedDiagnosticPool &) = delete;
253 public:
254  DelayedDiagnosticPool(const DelayedDiagnosticPool *parent) : Parent(parent) {}
257  i = Diagnostics.begin(), e = Diagnostics.end(); i != e; ++i)
258  i->Destroy();
259  }
260 
262  : Parent(Other.Parent), Diagnostics(std::move(Other.Diagnostics)) {
263  Other.Diagnostics.clear();
264  }
266  Parent = Other.Parent;
267  Diagnostics = std::move(Other.Diagnostics);
268  Other.Diagnostics.clear();
269  return *this;
270  }
271 
272  const DelayedDiagnosticPool *getParent() const { return Parent; }
273 
274  /// Does this pool, or any of its ancestors, contain any diagnostics?
275  bool empty() const {
276  return (Diagnostics.empty() && (!Parent || Parent->empty()));
277  }
278 
279  /// Add a diagnostic to this pool.
280  void add(const DelayedDiagnostic &diag) {
281  Diagnostics.push_back(diag);
282  }
283 
284  /// Steal the diagnostics from the given pool.
286  if (pool.Diagnostics.empty()) return;
287 
288  if (Diagnostics.empty()) {
289  Diagnostics = std::move(pool.Diagnostics);
290  } else {
291  Diagnostics.append(pool.pool_begin(), pool.pool_end());
292  }
293  pool.Diagnostics.clear();
294  }
295 
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(); }
300 };
301 
302 }
303 
304 /// Add a diagnostic to the current delay pool.
306  assert(shouldDelayDiagnostics() && "trying to delay without pool");
307  CurPool->add(diag);
308 }
309 
310 
311 }
312 
313 #endif
DelayedDiagnosticPool(const DelayedDiagnosticPool *parent)
DelayedDiagnosticPool(DelayedDiagnosticPool &&Other)
A (possibly-)qualified type.
Definition: Type.h:653
NamedDecl * getTargetDecl() const
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:95
const ObjCPropertyDecl * getObjCProperty() const
const NamedDecl * getAvailabilityReferringDecl() const
const AccessedEntity & getAccessData() const
Definition: Format.h:1900
void add(const DelayedDiagnostic &diag)
Add a diagnostic to this pool.
void Reset(unsigned DiagID=0)
Clear out this partial diagnostic, giving it a new diagnostic ID and removing all of its arguments...
CXXRecordDecl * getBaseClass() const
const NamedDecl * getAvailabilityOffendingDecl() const
CXXRecordDecl * getNamingClass() const
CXXRecordDecl * getDerivedClass() const
void steal(DelayedDiagnosticPool &pool)
Steal the diagnostics from the given pool.
void * getAsOpaquePtr() const
Definition: Type.h:699
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
AvailabilityResult
Captures the result of checking the availability of a declaration.
Definition: DeclBase.h:72
NodeId Parent
Definition: ASTDiff.cpp:192
const ObjCInterfaceDecl * getUnknownObjCClass() const
A declaration being accessed, together with information about how it was accessed.
static DelayedDiagnostic makeForbiddenType(SourceLocation loc, unsigned diagnostic, QualType type, unsigned argument)
An allocator for Storage objects, which uses a small cache to objects, used to reduce malloc()/free()...
#define false
Definition: stdbool.h:33
AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator, BaseNonce _, CXXRecordDecl *BaseClass, CXXRecordDecl *DerivedClass, AccessSpecifier Access)
unsigned getForbiddenTypeDiagnostic() const
The diagnostic ID to emit.
Encodes a location in the source.
unsigned getForbiddenTypeArgument() const
MemberNonce
A member declaration found through lookup.
AccessSpecifier getAccess() const
PartialDiagnostic & setDiag(unsigned DiagID)
bool empty() const
Does this pool, or any of its ancestors, contain any diagnostics?
AvailabilityResult getAvailabilityResult() const
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:746
DelayedDiagnosticPool & operator=(DelayedDiagnosticPool &&Other)
static QualType getFromOpaquePtr(const void *Ptr)
Definition: Type.h:701
BaseNonce
A hierarchy (base-to-derived or derived-to-base) conversion.
A POD class for pairing a NamedDecl* with an access specifier.
Dataflow Directional Tag Classes.
QualType getForbiddenTypeOperand() const
void setDiag(const PartialDiagnostic &PDiag)
Sets a diagnostic to be performed.
void add(const sema::DelayedDiagnostic &diag)
Adds a delayed diagnostic.
const DelayedDiagnosticPool * getParent() const
AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator, MemberNonce _, CXXRecordDecl *NamingClass, DeclAccessPair FoundDecl, QualType BaseObjectType)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
A diagnostic message which has been conditionally emitted pending the complete parsing of the current...
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1227
A collection of diagnostics which were delayed.
#define true
Definition: stdbool.h:32
QualType getBaseObjectType() const
Retrieves the base object type, important when accessing an instance member.
NamedDecl - This represents a decl with a name.
Definition: Decl.h:245
const PartialDiagnostic & getDiag() const
StringRef getAvailabilityMessage() const
static DelayedDiagnostic makeAccess(SourceLocation Loc, const AccessedEntity &Entity)
SmallVectorImpl< DelayedDiagnostic >::const_iterator pool_iterator