Coverage Report

Created: 2021-01-23 06:44

/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
15.8M
                     size_t Alignment = 8) noexcept {
72
15.8M
    return ::operator new(Bytes, C, Alignment);
73
15.8M
  }
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
65.7M
        InheritEvenIfAlreadyPresent(false) {}
84
85
public:
86
3.59G
  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
9.15k
  SourceLocation getLocation() const { return getRange().getBegin(); }
94
95
42.2M
  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.44M
  bool isImplicit() const { return Implicit; }
100
23.6M
  void setImplicit(bool I) { Implicit = I; }
101
102
1.53M
  void setPackExpansion(bool PE) { IsPackExpansion = PE; }
103
3.26M
  bool isPackExpansion() const { return IsPackExpansion; }
104
105
  // Clone this attribute.
106
  Attr *clone(ASTContext &C) const;
107
108
2.06M
  bool isLateParsed() const { return IsLateParsed; }
109
110
  // Pretty print this attribute.
111
  void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
112
};
113
114
class TypeAttr : public Attr {
115
protected:
116
  TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
117
           attr::Kind AK, bool IsLateParsed)
118
4.23M
      : Attr(Context, CommonInfo, AK, IsLateParsed) {}
119
120
public:
121
0
  static bool classof(const Attr *A) {
122
0
    return A->getKind() >= attr::FirstTypeAttr &&
123
0
           A->getKind() <= attr::LastTypeAttr;
124
0
  }
125
};
126
127
class StmtAttr : public Attr {
128
protected:
129
  StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
130
           attr::Kind AK, bool IsLateParsed)
131
2.12k
      : Attr(Context, CommonInfo, AK, IsLateParsed) {}
132
133
public:
134
0
  static bool classof(const Attr *A) {
135
0
    return A->getKind() >= attr::FirstStmtAttr &&
136
0
           A->getKind() <= attr::LastStmtAttr;
137
0
  }
138
};
139
140
class InheritableAttr : public Attr {
141
protected:
142
  InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
143
                  attr::Kind AK, bool IsLateParsed,
144
                  bool InheritEvenIfAlreadyPresent)
145
53.2M
      : Attr(Context, CommonInfo, AK, IsLateParsed) {
146
53.2M
    this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
147
53.2M
  }
148
149
public:
150
1.81M
  void setInherited(bool I) { Inherited = I; }
151
152
  /// Should this attribute be inherited from a prior declaration even if it's
153
  /// explicitly provided in the current declaration?
154
260k
  bool shouldInheritEvenIfAlreadyPresent() const {
155
260k
    return InheritEvenIfAlreadyPresent;
156
260k
  }
157
158
  // Implement isa/cast/dyncast/etc.
159
3.55M
  static bool classof(const Attr *A) {
160
3.55M
    return A->getKind() >= attr::FirstInheritableAttr &&
161
3.55M
           A->getKind() <= attr::LastInheritableAttr;
162
3.55M
  }
163
};
164
165
class DeclOrStmtAttr : public InheritableAttr {
166
protected:
167
  DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
168
                 attr::Kind AK, bool IsLateParsed,
169
                 bool InheritEvenIfAlreadyPresent)
170
      : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
171
25
                        InheritEvenIfAlreadyPresent) {}
172
173
public:
174
0
  static bool classof(const Attr *A) {
175
0
    return A->getKind() >= attr::FirstDeclOrStmtAttr &&
176
0
           A->getKind() <= attr::LastDeclOrStmtAttr;
177
0
  }
178
};
179
180
class InheritableParamAttr : public InheritableAttr {
181
protected:
182
  InheritableParamAttr(ASTContext &Context,
183
                       const AttributeCommonInfo &CommonInfo, attr::Kind AK,
184
                       bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
185
      : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
186
98.3k
                        InheritEvenIfAlreadyPresent) {}
187
188
public:
189
  // Implement isa/cast/dyncast/etc.
190
6.92k
  static bool classof(const Attr *A) {
191
6.92k
    return A->getKind() >= attr::FirstInheritableParamAttr &&
192
6.92k
           A->getKind() <= attr::LastInheritableParamAttr;
193
6.92k
  }
194
};
195
196
/// A parameter attribute which changes the argument-passing ABI rule
197
/// for the parameter.
198
class ParameterABIAttr : public InheritableParamAttr {
199
protected:
200
  ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
201
                   attr::Kind AK, bool IsLateParsed,
202
                   bool InheritEvenIfAlreadyPresent)
203
      : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
204
166
                             InheritEvenIfAlreadyPresent) {}
205
206
public:
207
166
  ParameterABI getABI() const {
208
166
    switch (getKind()) {
209
58
    case attr::SwiftContext:
210
58
      return ParameterABI::SwiftContext;
211
34
    case attr::SwiftErrorResult:
212
34
      return ParameterABI::SwiftErrorResult;
213
74
    case attr::SwiftIndirectResult:
214
74
      return ParameterABI::SwiftIndirectResult;
215
0
    default:
216
0
      llvm_unreachable("bad parameter ABI attribute kind");
217
166
    }
218
166
  }
219
220
2.12M
  static bool classof(const Attr *A) {
221
2.12M
    return A->getKind() >= attr::FirstParameterABIAttr &&
222
2.12M
           A->getKind() <= attr::LastParameterABIAttr;
223
2.12M
   }
224
};
225
226
/// A single parameter index whose accessors require each use to make explicit
227
/// the parameter index encoding needed.
228
class ParamIdx {
229
  // Idx is exposed only via accessors that specify specific encodings.
230
  unsigned Idx : 30;
231
  unsigned HasThis : 1;
232
  unsigned IsValid : 1;
233
234
2.43k
  void assertComparable(const ParamIdx &I) const {
235
2.43k
    assert(isValid() && I.isValid() &&
236
2.43k
           "ParamIdx must be valid to be compared");
237
    // It's possible to compare indices from separate functions, but so far
238
    // it's not proven useful.  Moreover, it might be confusing because a
239
    // comparison on the results of getASTIndex might be inconsistent with a
240
    // comparison on the ParamIdx objects themselves.
241
2.43k
    assert(HasThis == I.HasThis &&
242
2.43k
           "ParamIdx must be for the same function to be compared");
243
2.43k
  }
244
245
public:
246
  /// Construct an invalid parameter index (\c isValid returns false and
247
  /// accessors fail an assert).
248
159k
  ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
249
250
  /// \param Idx is the parameter index as it is normally specified in
251
  /// attributes in the source: one-origin including any C++ implicit this
252
  /// parameter.
253
  ///
254
  /// \param D is the declaration containing the parameters.  It is used to
255
  /// determine if there is a C++ implicit this parameter.
256
  ParamIdx(unsigned Idx, const Decl *D)
257
90.5k
      : Idx(Idx), HasThis(false), IsValid(true) {
258
90.5k
    assert(Idx >= 1 && "Idx must be one-origin");
259
90.5k
    if (const auto *FD = dyn_cast<FunctionDecl>(D))
260
89.9k
      HasThis = FD->isCXXInstanceMember();
261
90.5k
  }
262
263
  /// A type into which \c ParamIdx can be serialized.
264
  ///
265
  /// A static assertion that it's of the correct size follows the \c ParamIdx
266
  /// class definition.
267
  typedef uint32_t SerialType;
268
269
  /// Produce a representation that can later be passed to \c deserialize to
270
  /// construct an equivalent \c ParamIdx.
271
3.87k
  SerialType serialize() const {
272
3.87k
    return *reinterpret_cast<const SerialType *>(this);
273
3.87k
  }
274
275
  /// Construct from a result from \c serialize.
276
2.96k
  static ParamIdx deserialize(SerialType S) {
277
    // Using this two-step static_cast via void * instead of reinterpret_cast
278
    // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
279
2.96k
    void *ParamIdxPtr = static_cast<void *>(&S);
280
2.96k
    ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
281
2.96k
    assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
282
2.96k
    return P;
283
2.96k
  }
284
285
  /// Is this parameter index valid?
286
233k
  bool isValid() const { return IsValid; }
287
288
  /// Get the parameter index as it would normally be encoded for attributes at
289
  /// the source level of representation: one-origin including any C++ implicit
290
  /// this parameter.
291
  ///
292
  /// This encoding thus makes sense for diagnostics, pretty printing, and
293
  /// constructing new attributes from a source-like specification.
294
165
  unsigned getSourceIndex() const {
295
165
    assert(isValid() && "ParamIdx must be valid");
296
165
    return Idx;
297
165
  }
298
299
  /// Get the parameter index as it would normally be encoded at the AST level
300
  /// of representation: zero-origin not including any C++ implicit this
301
  /// parameter.
302
  ///
303
  /// This is the encoding primarily used in Sema.  However, in diagnostics,
304
  /// Sema uses \c getSourceIndex instead.
305
221k
  unsigned getASTIndex() const {
306
221k
    assert(isValid() && "ParamIdx must be valid");
307
221k
    assert(Idx >= 1 + HasThis &&
308
221k
           "stored index must be base-1 and not specify C++ implicit this");
309
221k
    return Idx - 1 - HasThis;
310
221k
  }
311
312
  /// Get the parameter index as it would normally be encoded at the LLVM level
313
  /// of representation: zero-origin including any C++ implicit this parameter.
314
  ///
315
  /// This is the encoding primarily used in CodeGen.
316
2.78k
  unsigned getLLVMIndex() const {
317
2.78k
    assert(isValid() && "ParamIdx must be valid");
318
2.78k
    assert(Idx >= 1 && "stored index must be base-1");
319
2.78k
    return Idx - 1;
320
2.78k
  }
321
322
4
  bool operator==(const ParamIdx &I) const {
323
4
    assertComparable(I);
324
4
    return Idx == I.Idx;
325
4
  }
326
0
  bool operator!=(const ParamIdx &I) const {
327
0
    assertComparable(I);
328
0
    return Idx != I.Idx;
329
0
  }
330
2.43k
  bool operator<(const ParamIdx &I) const {
331
2.43k
    assertComparable(I);
332
2.43k
    return Idx < I.Idx;
333
2.43k
  }
334
0
  bool operator>(const ParamIdx &I) const {
335
0
    assertComparable(I);
336
0
    return Idx > I.Idx;
337
0
  }
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
};
347
348
static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
349
              "ParamIdx does not fit its serialization type");
350
351
/// Contains information gathered from parsing the contents of TargetAttr.
352
struct ParsedTargetAttr {
353
  std::vector<std::string> Features;
354
  StringRef Architecture;
355
  StringRef Tune;
356
  StringRef BranchProtection;
357
  bool DuplicateArchitecture = false;
358
  bool DuplicateTune = false;
359
461
  bool operator ==(const ParsedTargetAttr &Other) const {
360
461
    return DuplicateArchitecture == Other.DuplicateArchitecture &&
361
461
           DuplicateTune == Other.DuplicateTune &&
362
461
           Architecture == Other.Architecture &&
363
78
           Tune == Other.Tune &&
364
78
           BranchProtection == Other.BranchProtection &&
365
78
           Features == Other.Features;
366
461
  }
367
};
368
369
#include "clang/AST/Attrs.inc"
370
371
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
372
2.32k
                                             const Attr *At) {
373
2.32k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
374
2.32k
                  DiagnosticsEngine::ak_attr);
375
2.32k
  return DB;
376
2.32k
}
377
}  // end namespace clang
378
379
#endif