clang  7.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 /// 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 
26 #include "clang/AST/DeclBase.h"
27 #include "clang/AST/DeclCXX.h"
28 #include "clang/AST/Type.h"
29 #include "clang/Basic/LLVM.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.
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 
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 
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.
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.
131 public:
132  enum DDKind : unsigned char { Availability, Access, ForbiddenType };
133 
135  bool Triggered;
136 
138 
139  void Destroy();
140 
141  static DelayedDiagnostic makeAvailability(AvailabilityResult AR,
143  const NamedDecl *ReferringDecl,
144  const NamedDecl *OffendingDecl,
145  const ObjCInterfaceDecl *UnknownObjCClass,
146  const ObjCPropertyDecl *ObjCProperty,
147  StringRef Msg,
148  bool ObjCPropertyAccess);
149 
151  const AccessedEntity &Entity) {
153  DD.Kind = Access;
154  DD.Triggered = false;
155  DD.Loc = Loc;
156  new (&DD.getAccessData()) AccessedEntity(Entity);
157  return DD;
158  }
159 
161  unsigned diagnostic,
162  QualType type,
163  unsigned argument) {
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 
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 
184  assert(Kind == Availability && "Not an availability diagnostic.");
185  return AvailabilityData.ReferringDecl;
186  }
187 
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 
198  assert(Kind == Availability && "Not an availability diagnostic.");
199  return llvm::makeArrayRef(AvailabilityData.SelectorLocs,
200  AvailabilityData.NumSelectorLocs);
201  }
202 
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 
223  assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
224  return QualType::getFromOpaquePtr(ForbiddenTypeData.OperandType);
225  }
226 
228  return AvailabilityData.UnknownObjCClass;
229  }
230 
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;
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.
272 
273 public:
274  DelayedDiagnosticPool(const DelayedDiagnosticPool *parent) : Parent(parent) {}
275 
277  DelayedDiagnosticPool &operator=(const DelayedDiagnosticPool &) = delete;
278 
280  : Parent(Other.Parent), Diagnostics(std::move(Other.Diagnostics)) {
281  Other.Diagnostics.clear();
282  }
283 
285  Parent = Other.Parent;
286  Diagnostics = std::move(Other.Diagnostics);
287  Other.Diagnostics.clear();
288  return *this;
289  }
290 
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.
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 
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.
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
DelayedDiagnosticPool(const DelayedDiagnosticPool *parent)
DelayedDiagnosticPool(DelayedDiagnosticPool &&Other)
A (possibly-)qualified type.
Definition: Type.h:655
NamedDecl * getTargetDecl() const
C Language Family Type Representation.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:98
const ObjCPropertyDecl * getObjCProperty() const
const NamedDecl * getAvailabilityReferringDecl() const
const AccessedEntity & getAccessData() const
Definition: Format.h:2022
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
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
CXXRecordDecl * getNamingClass() const
CXXRecordDecl * getDerivedClass() const
void steal(DelayedDiagnosticPool &pool)
Steal the diagnostics from the given pool.
void * getAsOpaquePtr() const
Definition: Type.h:700
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()...
ArrayRef< SourceLocation > getAvailabilitySelectorLocs() const
#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:702
BaseNonce
A hierarchy (base-to-derived or derived-to-base) conversion.
Defines various enumerations that describe declaration and type specifiers.
A POD class for pairing a NamedDecl* with an access specifier.
Dataflow Directional Tag Classes.
QualType getForbiddenTypeOperand() const
SmallVectorImpl< DelayedDiagnostic >::const_iterator pool_iterator
void setDiag(const PartialDiagnostic &PDiag)
Sets a diagnostic to be performed.
void add(const sema::DelayedDiagnostic &diag)
Adds a delayed diagnostic.
Implements a partial diagnostic that can be emitted anwyhere in a DiagnosticBuilder stream...
const DelayedDiagnosticPool * getParent() const
AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator, MemberNonce _, CXXRecordDecl *NamingClass, DeclAccessPair FoundDecl, QualType BaseObjectType)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Defines the clang::SourceLocation class and associated facilities.
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
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:1315
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.
This represents a decl that may have a name.
Definition: Decl.h:248
const PartialDiagnostic & getDiag() const
StringRef getAvailabilityMessage() const
static DelayedDiagnostic makeAccess(SourceLocation Loc, const AccessedEntity &Entity)