Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/Attr.h
Line
Count
Source (jump to first uncovered line)
1
//===--- Attr.h - Classes for representing attributes ----------*- 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
//  This file defines the Attr interface and subclasses.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_AST_ATTR_H
14
#define LLVM_CLANG_AST_ATTR_H
15
16
#include "clang/AST/ASTFwd.h"
17
#include "clang/AST/AttrIterator.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/Type.h"
20
#include "clang/Basic/AttrKinds.h"
21
#include "clang/Basic/AttributeCommonInfo.h"
22
#include "clang/Basic/LangOptions.h"
23
#include "clang/Basic/LLVM.h"
24
#include "clang/Basic/OpenMPKinds.h"
25
#include "clang/Basic/Sanitizers.h"
26
#include "clang/Basic/SourceLocation.h"
27
#include "llvm/ADT/StringSwitch.h"
28
#include "llvm/Support/ErrorHandling.h"
29
#include "llvm/Support/VersionTuple.h"
30
#include "llvm/Support/raw_ostream.h"
31
#include <algorithm>
32
#include <cassert>
33
34
namespace clang {
35
class ASTContext;
36
class AttributeCommonInfo;
37
class IdentifierInfo;
38
class ObjCInterfaceDecl;
39
class Expr;
40
class QualType;
41
class FunctionDecl;
42
class TypeSourceInfo;
43
class OMPTraitInfo;
44
45
/// Attr - This represents one attribute.
46
class Attr : public AttributeCommonInfo {
47
private:
48
  unsigned AttrKind : 16;
49
50
protected:
51
  /// An index into the spelling list of an
52
  /// attribute defined in Attr.td file.
53
  unsigned Inherited : 1;
54
  unsigned IsPackExpansion : 1;
55
  unsigned Implicit : 1;
56
  // FIXME: These are properties of the attribute kind, not state for this
57
  // instance of the attribute.
58
  unsigned IsLateParsed : 1;
59
  unsigned InheritEvenIfAlreadyPresent : 1;
60
61
0
  void *operator new(size_t bytes) noexcept {
62
0
    llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
63
0
  }
64
0
  void operator delete(void *data) noexcept {
65
0
    llvm_unreachable("Attrs cannot be released with regular 'delete'.");
66
0
  }
67
68
public:
69
  // Forward so that the regular new and delete do not hide global ones.
70
  void *operator new(size_t Bytes, ASTContext &C,
71
34.4M
                     size_t Alignment = 8) noexcept {
72
34.4M
    return ::operator new(Bytes, C, Alignment);
73
34.4M
  }
74
0
  void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
75
0
    return ::operator delete(Ptr, C, Alignment);
76
0
  }
77
78
protected:
79
  Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
80
       attr::Kind AK, bool IsLateParsed)
81
      : AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false),
82
        IsPackExpansion(false), Implicit(false), IsLateParsed(IsLateParsed),
83
143M
        InheritEvenIfAlreadyPresent(false) {}
84
85
public:
86
11.9G
  attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
87
88
0
  unsigned getSpellingListIndex() const {
89
0
    return getAttributeSpellingListIndex();
90
0
  }
91
  const char *getSpelling() const;
92
93
10.3k
  SourceLocation getLocation() const { return getRange().getBegin(); }
94
95
103M
  bool isInherited() const { return Inherited; }
96
97
  /// Returns true if the attribute has been implicitly created instead
98
  /// of explicitly written by the user.
99
1.32M
  bool isImplicit() const { return Implicit; }
100
40.3M
  void setImplicit(bool I) { Implicit = I; }
101
102
1.43M
  void setPackExpansion(bool PE) { IsPackExpansion = PE; }
103
3.23M
  bool isPackExpansion() const { return IsPackExpansion; }
104
105
  // Clone this attribute.
106
  Attr *clone(ASTContext &C) const;
107
108
2.12M
  bool isLateParsed() const { return IsLateParsed; }
109
110
  // Pretty print this attribute.
111
  void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
112
113
  static StringRef getDocumentation(attr::Kind);
114
};
115
116
class TypeAttr : public Attr {
117
protected:
118
  TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
119
           attr::Kind AK, bool IsLateParsed)
120
3.21M
      : Attr(Context, CommonInfo, AK, IsLateParsed) {}
121
122
public:
123
0
  static bool classof(const Attr *A) {
124
0
    return A->getKind() >= attr::FirstTypeAttr &&
125
0
           A->getKind() <= attr::LastTypeAttr;
126
0
  }
127
};
128
129
class StmtAttr : public Attr {
130
protected:
131
  StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
132
           attr::Kind AK, bool IsLateParsed)
133
2.27k
      : Attr(Context, CommonInfo, AK, IsLateParsed) {}
134
135
public:
136
0
  static bool classof(const Attr *A) {
137
0
    return A->getKind() >= attr::FirstStmtAttr &&
138
0
           A->getKind() <= attr::LastStmtAttr;
139
0
  }
140
};
141
142
class InheritableAttr : public Attr {
143
protected:
144
  InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
145
                  attr::Kind AK, bool IsLateParsed,
146
                  bool InheritEvenIfAlreadyPresent)
147
119M
      : Attr(Context, CommonInfo, AK, IsLateParsed) {
148
119M
    this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
149
119M
  }
150
151
public:
152
1.67M
  void setInherited(bool I) { Inherited = I; }
153
154
  /// Should this attribute be inherited from a prior declaration even if it's
155
  /// explicitly provided in the current declaration?
156
223k
  bool shouldInheritEvenIfAlreadyPresent() const {
157
223k
    return InheritEvenIfAlreadyPresent;
158
223k
  }
159
160
  // Implement isa/cast/dyncast/etc.
161
3.15M
  static bool classof(const Attr *A) {
162
3.15M
    return A->getKind() >= attr::FirstInheritableAttr &&
163
3.15M
           A->getKind() <= attr::LastInheritableAttr;
164
3.15M
  }
165
};
166
167
class DeclOrStmtAttr : public InheritableAttr {
168
protected:
169
  DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
170
                 attr::Kind AK, bool IsLateParsed,
171
                 bool InheritEvenIfAlreadyPresent)
172
      : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
173
49
                        InheritEvenIfAlreadyPresent) {}
174
175
public:
176
0
  static bool classof(const Attr *A) {
177
0
    return A->getKind() >= attr::FirstDeclOrStmtAttr &&
178
0
           A->getKind() <= attr::LastDeclOrStmtAttr;
179
0
  }
180
};
181
182
class InheritableParamAttr : public InheritableAttr {
183
protected:
184
  InheritableParamAttr(ASTContext &Context,
185
                       const AttributeCommonInfo &CommonInfo, attr::Kind AK,
186
                       bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
187
      : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
188
77.6k
                        InheritEvenIfAlreadyPresent) {}
189
190
public:
191
  // Implement isa/cast/dyncast/etc.
192
5.86k
  static bool classof(const Attr *A) {
193
5.86k
    return A->getKind() >= attr::FirstInheritableParamAttr &&
194
5.86k
           A->getKind() <= attr::LastInheritableParamAttr;
195
5.86k
  }
196
};
197
198
/// A parameter attribute which changes the argument-passing ABI rule
199
/// for the parameter.
200
class ParameterABIAttr : public InheritableParamAttr {
201
protected:
202
  ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
203
                   attr::Kind AK, bool IsLateParsed,
204
                   bool InheritEvenIfAlreadyPresent)
205
      : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
206
330
                             InheritEvenIfAlreadyPresent) {}
207
208
public:
209
330
  ParameterABI getABI() const {
210
330
    switch (getKind()) {
211
64
    case attr::SwiftContext:
212
64
      return ParameterABI::SwiftContext;
213
149
    case attr::SwiftAsyncContext:
214
149
      return ParameterABI::SwiftAsyncContext;
215
34
    case attr::SwiftErrorResult:
216
34
      return ParameterABI::SwiftErrorResult;
217
83
    case attr::SwiftIndirectResult:
218
83
      return ParameterABI::SwiftIndirectResult;
219
0
    default:
220
0
      llvm_unreachable("bad parameter ABI attribute kind");
221
330
    }
222
330
  }
223
224
2.18M
  static bool classof(const Attr *A) {
225
2.18M
    return A->getKind() >= attr::FirstParameterABIAttr &&
226
2.18M
           
A->getKind() <= attr::LastParameterABIAttr2.18M
;
227
2.18M
   }
228
};
229
230
/// A single parameter index whose accessors require each use to make explicit
231
/// the parameter index encoding needed.
232
class ParamIdx {
233
  // Idx is exposed only via accessors that specify specific encodings.
234
  unsigned Idx : 30;
235
  unsigned HasThis : 1;
236
  unsigned IsValid : 1;
237
238
2.43k
  void assertComparable(const ParamIdx &I) const {
239
2.43k
    assert(isValid() && I.isValid() &&
240
2.43k
           "ParamIdx must be valid to be compared");
241
    // It's possible to compare indices from separate functions, but so far
242
    // it's not proven useful.  Moreover, it might be confusing because a
243
    // comparison on the results of getASTIndex might be inconsistent with a
244
    // comparison on the ParamIdx objects themselves.
245
0
    assert(HasThis == I.HasThis &&
246
2.43k
           "ParamIdx must be for the same function to be compared");
247
2.43k
  }
248
249
public:
250
  /// Construct an invalid parameter index (\c isValid returns false and
251
  /// accessors fail an assert).
252
127k
  ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
253
254
  /// \param Idx is the parameter index as it is normally specified in
255
  /// attributes in the source: one-origin including any C++ implicit this
256
  /// parameter.
257
  ///
258
  /// \param D is the declaration containing the parameters.  It is used to
259
  /// determine if there is a C++ implicit this parameter.
260
  ParamIdx(unsigned Idx, const Decl *D)
261
74.3k
      : Idx(Idx), HasThis(false), IsValid(true) {
262
74.3k
    assert(Idx >= 1 && "Idx must be one-origin");
263
74.3k
    if (const auto *FD = dyn_cast<FunctionDecl>(D))
264
73.8k
      HasThis = FD->isCXXInstanceMember();
265
74.3k
  }
266
267
  /// A type into which \c ParamIdx can be serialized.
268
  ///
269
  /// A static assertion that it's of the correct size follows the \c ParamIdx
270
  /// class definition.
271
  typedef uint32_t SerialType;
272
273
  /// Produce a representation that can later be passed to \c deserialize to
274
  /// construct an equivalent \c ParamIdx.
275
3.44k
  SerialType serialize() const {
276
3.44k
    return *reinterpret_cast<const SerialType *>(this);
277
3.44k
  }
278
279
  /// Construct from a result from \c serialize.
280
3.06k
  static ParamIdx deserialize(SerialType S) {
281
    // Using this two-step static_cast via void * instead of reinterpret_cast
282
    // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
283
3.06k
    void *ParamIdxPtr = static_cast<void *>(&S);
284
3.06k
    ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
285
3.06k
    assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
286
0
    return P;
287
3.06k
  }
288
289
  /// Is this parameter index valid?
290
186k
  bool isValid() const { return IsValid; }
291
292
  /// Get the parameter index as it would normally be encoded for attributes at
293
  /// the source level of representation: one-origin including any C++ implicit
294
  /// this parameter.
295
  ///
296
  /// This encoding thus makes sense for diagnostics, pretty printing, and
297
  /// constructing new attributes from a source-like specification.
298
174
  unsigned getSourceIndex() const {
299
174
    assert(isValid() && "ParamIdx must be valid");
300
0
    return Idx;
301
174
  }
302
303
  /// Get the parameter index as it would normally be encoded at the AST level
304
  /// of representation: zero-origin not including any C++ implicit this
305
  /// parameter.
306
  ///
307
  /// This is the encoding primarily used in Sema.  However, in diagnostics,
308
  /// Sema uses \c getSourceIndex instead.
309
174k
  unsigned getASTIndex() const {
310
174k
    assert(isValid() && "ParamIdx must be valid");
311
0
    assert(Idx >= 1 + HasThis &&
312
174k
           "stored index must be base-1 and not specify C++ implicit this");
313
0
    return Idx - 1 - HasThis;
314
174k
  }
315
316
  /// Get the parameter index as it would normally be encoded at the LLVM level
317
  /// of representation: zero-origin including any C++ implicit this parameter.
318
  ///
319
  /// This is the encoding primarily used in CodeGen.
320
2.89k
  unsigned getLLVMIndex() const {
321
2.89k
    assert(isValid() && "ParamIdx must be valid");
322
0
    assert(Idx >= 1 && "stored index must be base-1");
323
0
    return Idx - 1;
324
2.89k
  }
325
326
4
  bool operator==(const ParamIdx &I) const {
327
4
    assertComparable(I);
328
4
    return Idx == I.Idx;
329
4
  }
330
0
  bool operator!=(const ParamIdx &I) const {
331
0
    assertComparable(I);
332
0
    return Idx != I.Idx;
333
0
  }
334
2.43k
  bool operator<(const ParamIdx &I) const {
335
2.43k
    assertComparable(I);
336
2.43k
    return Idx < I.Idx;
337
2.43k
  }
338
0
  bool operator>(const ParamIdx &I) const {
339
0
    assertComparable(I);
340
0
    return Idx > I.Idx;
341
0
  }
342
0
  bool operator<=(const ParamIdx &I) const {
343
0
    assertComparable(I);
344
0
    return Idx <= I.Idx;
345
0
  }
346
0
  bool operator>=(const ParamIdx &I) const {
347
0
    assertComparable(I);
348
0
    return Idx >= I.Idx;
349
0
  }
350
};
351
352
static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
353
              "ParamIdx does not fit its serialization type");
354
355
/// Contains information gathered from parsing the contents of TargetAttr.
356
struct ParsedTargetAttr {
357
  std::vector<std::string> Features;
358
  StringRef Architecture;
359
  StringRef Tune;
360
  StringRef BranchProtection;
361
  bool DuplicateArchitecture = false;
362
  bool DuplicateTune = false;
363
488
  bool operator ==(const ParsedTargetAttr &Other) const {
364
488
    return DuplicateArchitecture == Other.DuplicateArchitecture &&
365
488
           DuplicateTune == Other.DuplicateTune &&
366
488
           Architecture == Other.Architecture &&
367
488
           
Tune == Other.Tune75
&&
368
488
           
BranchProtection == Other.BranchProtection75
&&
369
488
           
Features == Other.Features75
;
370
488
  }
371
};
372
373
#include "clang/AST/Attrs.inc"
374
375
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
376
2.39k
                                             const Attr *At) {
377
2.39k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
378
2.39k
                  DiagnosticsEngine::ak_attr);
379
2.39k
  return DB;
380
2.39k
}
381
}  // end namespace clang
382
383
#endif