clang  12.0.0git
DelayedDiagnostic.h
Go to the documentation of this file.
1 //===- DelayedDiagnostic.h - Delayed declarator diagnostics -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Defines the classes clang::DelayedDiagnostic and
11 /// clang::AccessedEntity.
12 ///
13 /// DelayedDiangostic is used to record diagnostics that are being
14 /// conditionally produced during declarator parsing. Certain kinds of
15 /// diagnostics -- notably deprecation and access control -- are suppressed
16 /// based on semantic properties of the parsed declaration that aren't known
17 /// until it is fully parsed.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
22 #define LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
23 
25 #include "clang/AST/DeclBase.h"
26 #include "clang/AST/DeclCXX.h"
27 #include "clang/AST/Type.h"
28 #include "clang/Basic/LLVM.h"
31 #include "clang/Basic/Specifiers.h"
32 #include "clang/Sema/Sema.h"
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/Support/Casting.h"
37 #include <cassert>
38 #include <cstddef>
39 #include <utility>
40 
41 namespace clang {
42 
43 class ObjCInterfaceDecl;
44 class ObjCPropertyDecl;
45 
46 namespace sema {
47 
48 /// A declaration being accessed, together with information about how
49 /// it was accessed.
51 public:
52  /// A member declaration found through lookup. The target is the
53  /// member.
54  enum MemberNonce { Member };
55 
56  /// A hierarchy (base-to-derived or derived-to-base) conversion.
57  /// The target is the base class.
58  enum BaseNonce { Base };
59 
61  MemberNonce _, CXXRecordDecl *NamingClass,
62  DeclAccessPair FoundDecl, QualType BaseObjectType)
63  : Access(FoundDecl.getAccess()), IsMember(true),
64  Target(FoundDecl.getDecl()), NamingClass(NamingClass),
65  BaseObjectType(BaseObjectType), Diag(0, Allocator) {}
66 
68  BaseNonce _, CXXRecordDecl *BaseClass,
69  CXXRecordDecl *DerivedClass, AccessSpecifier Access)
70  : Access(Access), IsMember(false), Target(BaseClass),
71  NamingClass(DerivedClass), Diag(0, Allocator) {}
72 
73  bool isMemberAccess() const { return IsMember; }
74 
75  bool isQuiet() const { return Diag.getDiagID() == 0; }
76 
77  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
78 
79  // These apply to member decls...
80  NamedDecl *getTargetDecl() const { return Target; }
81  CXXRecordDecl *getNamingClass() const { return NamingClass; }
82 
83  // ...and these apply to hierarchy conversions.
85  assert(!IsMember); return cast<CXXRecordDecl>(Target);
86  }
87  CXXRecordDecl *getDerivedClass() const { return NamingClass; }
88 
89  /// Retrieves the base object type, important when accessing
90  /// an instance member.
91  QualType getBaseObjectType() const { return BaseObjectType; }
92 
93  /// Sets a diagnostic to be performed. The diagnostic is given
94  /// four (additional) arguments:
95  /// %0 - 0 if the entity was private, 1 if protected
96  /// %1 - the DeclarationName of the entity
97  /// %2 - the TypeDecl type of the naming class
98  /// %3 - the TypeDecl type of the declaring class
99  void setDiag(const PartialDiagnostic &PDiag) {
100  assert(isQuiet() && "partial diagnostic already defined");
101  Diag = PDiag;
102  }
103  PartialDiagnostic &setDiag(unsigned DiagID) {
104  assert(isQuiet() && "partial diagnostic already defined");
105  assert(DiagID && "creating null diagnostic");
106  Diag.Reset(DiagID);
107  return Diag;
108  }
109  const PartialDiagnostic &getDiag() const {
110  return Diag;
111  }
112 
113 private:
114  unsigned Access : 2;
115  unsigned IsMember : 1;
116  NamedDecl *Target;
117  CXXRecordDecl *NamingClass;
118  QualType BaseObjectType;
119  PartialDiagnostic Diag;
120 };
121 
122 /// A diagnostic message which has been conditionally emitted pending
123 /// the complete parsing of the current declaration.
125 public:
126  enum DDKind : unsigned char { Availability, Access, ForbiddenType };
127 
129  bool Triggered;
130 
132 
133  void Destroy();
134 
137  const NamedDecl *ReferringDecl,
138  const NamedDecl *OffendingDecl,
139  const ObjCInterfaceDecl *UnknownObjCClass,
140  const ObjCPropertyDecl *ObjCProperty,
141  StringRef Msg,
142  bool ObjCPropertyAccess);
143 
145  const AccessedEntity &Entity) {
147  DD.Kind = Access;
148  DD.Triggered = false;
149  DD.Loc = Loc;
150  new (&DD.getAccessData()) AccessedEntity(Entity);
151  return DD;
152  }
153 
155  unsigned diagnostic,
156  QualType type,
157  unsigned argument) {
159  DD.Kind = ForbiddenType;
160  DD.Triggered = false;
161  DD.Loc = loc;
162  DD.ForbiddenTypeData.Diagnostic = diagnostic;
163  DD.ForbiddenTypeData.OperandType = type.getAsOpaquePtr();
164  DD.ForbiddenTypeData.Argument = argument;
165  return DD;
166  }
167 
169  assert(Kind == Access && "Not an access diagnostic.");
170  return *reinterpret_cast<AccessedEntity*>(AccessData);
171  }
172  const AccessedEntity &getAccessData() const {
173  assert(Kind == Access && "Not an access diagnostic.");
174  return *reinterpret_cast<const AccessedEntity*>(AccessData);
175  }
176 
178  assert(Kind == Availability && "Not an availability diagnostic.");
179  return AvailabilityData.ReferringDecl;
180  }
181 
183  return AvailabilityData.OffendingDecl;
184  }
185 
186  StringRef getAvailabilityMessage() const {
187  assert(Kind == Availability && "Not an availability diagnostic.");
188  return StringRef(AvailabilityData.Message, AvailabilityData.MessageLen);
189  }
190 
192  assert(Kind == Availability && "Not an availability diagnostic.");
193  return llvm::makeArrayRef(AvailabilityData.SelectorLocs,
194  AvailabilityData.NumSelectorLocs);
195  }
196 
198  assert(Kind == Availability && "Not an availability diagnostic.");
199  return AvailabilityData.AR;
200  }
201 
202  /// The diagnostic ID to emit. Used like so:
203  /// Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
204  /// << diag.getForbiddenTypeOperand()
205  /// << diag.getForbiddenTypeArgument();
206  unsigned getForbiddenTypeDiagnostic() const {
207  assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
208  return ForbiddenTypeData.Diagnostic;
209  }
210 
211  unsigned getForbiddenTypeArgument() const {
212  assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
213  return ForbiddenTypeData.Argument;
214  }
215 
217  assert(Kind == ForbiddenType && "not a forbidden-type diagnostic");
218  return QualType::getFromOpaquePtr(ForbiddenTypeData.OperandType);
219  }
220 
222  return AvailabilityData.UnknownObjCClass;
223  }
224 
226  return AvailabilityData.ObjCProperty;
227  }
228 
229  bool getObjCPropertyAccess() const {
230  return AvailabilityData.ObjCPropertyAccess;
231  }
232 
233 private:
234  struct AD {
235  const NamedDecl *ReferringDecl;
236  const NamedDecl *OffendingDecl;
237  const ObjCInterfaceDecl *UnknownObjCClass;
238  const ObjCPropertyDecl *ObjCProperty;
239  const char *Message;
240  size_t MessageLen;
241  SourceLocation *SelectorLocs;
242  size_t NumSelectorLocs;
244  bool ObjCPropertyAccess;
245  };
246 
247  struct FTD {
248  unsigned Diagnostic;
249  unsigned Argument;
250  void *OperandType;
251  };
252 
253  union {
254  struct AD AvailabilityData;
255  struct FTD ForbiddenTypeData;
256 
257  /// Access control.
258  char AccessData[sizeof(AccessedEntity)];
259  };
260 };
261 
262 /// A collection of diagnostics which were delayed.
264  const DelayedDiagnosticPool *Parent;
266 
267 public:
269 
272 
274  : Parent(Other.Parent), Diagnostics(std::move(Other.Diagnostics)) {
275  Other.Diagnostics.clear();
276  }
277 
279  Parent = Other.Parent;
280  Diagnostics = std::move(Other.Diagnostics);
281  Other.Diagnostics.clear();
282  return *this;
283  }
284 
287  i = Diagnostics.begin(), e = Diagnostics.end(); i != e; ++i)
288  i->Destroy();
289  }
290 
291  const DelayedDiagnosticPool *getParent() const { return Parent; }
292 
293  /// Does this pool, or any of its ancestors, contain any diagnostics?
294  bool empty() const {
295  return (Diagnostics.empty() && (!Parent || Parent->empty()));
296  }
297 
298  /// Add a diagnostic to this pool.
299  void add(const DelayedDiagnostic &diag) {
300  Diagnostics.push_back(diag);
301  }
302 
303  /// Steal the diagnostics from the given pool.
305  if (pool.Diagnostics.empty()) return;
306 
307  if (Diagnostics.empty()) {
308  Diagnostics = std::move(pool.Diagnostics);
309  } else {
310  Diagnostics.append(pool.pool_begin(), pool.pool_end());
311  }
312  pool.Diagnostics.clear();
313  }
314 
316 
317  pool_iterator pool_begin() const { return Diagnostics.begin(); }
318  pool_iterator pool_end() const { return Diagnostics.end(); }
319  bool pool_empty() const { return Diagnostics.empty(); }
320 };
321 
322 } // namespace clang
323 
324 /// Add a diagnostic to the current delay pool.
326  assert(shouldDelayDiagnostics() && "trying to delay without pool");
327  CurPool->add(diag);
328 }
329 
330 } // namespace clang
331 
332 #endif // LLVM_CLANG_SEMA_DELAYEDDIAGNOSTIC_H
DelayedDiagnosticPool(const DelayedDiagnosticPool *parent)
DelayedDiagnosticPool(DelayedDiagnosticPool &&Other)
A (possibly-)qualified type.
Definition: Type.h:661
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
AccessedEntity(PartialDiagnostic::DiagStorageAllocator &Allocator, BaseNonce _, CXXRecordDecl *BaseClass, CXXRecordDecl *DerivedClass, AccessSpecifier Access)
const ObjCPropertyDecl * getObjCProperty() const
const NamedDecl * getAvailabilityReferringDecl() const
const AccessedEntity & getAccessData() const
char AccessData[sizeof(AccessedEntity)]
Access control.
Definition: Format.h:2835
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.
Represents an ObjC class declaration.
Definition: DeclObjC.h:1170
AvailabilityResult
Captures the result of checking the availability of a declaration.
Definition: DeclBase.h:75
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)
AccessedEntity(PartialDiagnostic::DiagStorageAllocator &Allocator, MemberNonce _, CXXRecordDecl *NamingClass, DeclAccessPair FoundDecl, QualType BaseObjectType)
DelayedDiagnosticPool & operator=(const DelayedDiagnosticPool &)=delete
An allocator for DiagnosticStorage objects, which uses a small cache to objects, used to reduce mallo...
Definition: Diagnostic.h:1070
ArrayRef< SourceLocation > getAvailabilitySelectorLocs() const
#define false
Definition: stdbool.h:17
Kind
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
bool shouldDelayDiagnostics()
Determines whether diagnostics should be delayed.
Definition: Sema.h:833
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:749
DelayedDiagnosticPool & operator=(DelayedDiagnosticPool &&Other)
static QualType getFromOpaquePtr(const void *Ptr)
Definition: Type.h:708
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.
static DelayedDiagnostic makeAvailability(AvailabilityResult AR, ArrayRef< SourceLocation > Locs, const NamedDecl *ReferringDecl, const NamedDecl *OffendingDecl, const ObjCInterfaceDecl *UnknownObjCClass, const ObjCPropertyDecl *ObjCProperty, StringRef Msg, bool ObjCPropertyAccess)
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
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::SourceLocation class and associated facilities.
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
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:1493
A collection of diagnostics which were delayed.
#define true
Definition: stdbool.h:16
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:223
const PartialDiagnostic & getDiag() const
StringRef getAvailabilityMessage() const
static DelayedDiagnostic makeAccess(SourceLocation Loc, const AccessedEntity &Entity)