Coverage Report

Created: 2020-02-15 09:57

/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/ASTContextAllocate.h"  // For Attrs.inc
17
#include "clang/AST/AttrIterator.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/Expr.h"
20
#include "clang/AST/OpenMPClause.h"
21
#include "clang/AST/Type.h"
22
#include "clang/Basic/AttrKinds.h"
23
#include "clang/Basic/AttributeCommonInfo.h"
24
#include "clang/Basic/LLVM.h"
25
#include "clang/Basic/OpenMPKinds.h"
26
#include "clang/Basic/Sanitizers.h"
27
#include "clang/Basic/SourceLocation.h"
28
#include "llvm/ADT/StringSwitch.h"
29
#include "llvm/Support/ErrorHandling.h"
30
#include "llvm/Support/VersionTuple.h"
31
#include "llvm/Support/raw_ostream.h"
32
#include <algorithm>
33
#include <cassert>
34
35
namespace clang {
36
class ASTContext;
37
class AttributeCommonInfo;
38
class IdentifierInfo;
39
class ObjCInterfaceDecl;
40
class Expr;
41
class QualType;
42
class FunctionDecl;
43
class TypeSourceInfo;
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
5.87M
                     size_t Alignment = 8) noexcept {
72
5.87M
    return ::operator new(Bytes, C, Alignment);
73
5.87M
  }
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
25.3M
        InheritEvenIfAlreadyPresent(false) {}
84
85
public:
86
1.18G
  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
7.29k
  SourceLocation getLocation() const { return getRange().getBegin(); }
94
95
11.6M
  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.41M
  bool isImplicit() const { return Implicit; }
100
11.9M
  void setImplicit(bool I) { Implicit = I; }
101
102
33.6k
  void setPackExpansion(bool PE) { IsPackExpansion = PE; }
103
1.91M
  bool isPackExpansion() const { return IsPackExpansion; }
104
105
  // Clone this attribute.
106
  Attr *clone(ASTContext &C) const;
107
108
1.91M
  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
3.97M
      : 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
1.85k
      : 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
20.0M
      : Attr(Context, CommonInfo, AK, IsLateParsed) {
146
20.0M
    this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
147
20.0M
  }
148
149
public:
150
1.01M
  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
227k
  bool shouldInheritEvenIfAlreadyPresent() const {
155
227k
    return InheritEvenIfAlreadyPresent;
156
227k
  }
157
158
  // Implement isa/cast/dyncast/etc.
159
2.44M
  static bool classof(const Attr *A) {
160
2.44M
    return A->getKind() >= attr::FirstInheritableAttr &&
161
2.44M
           A->getKind() <= attr::LastInheritableAttr;
162
2.44M
  }
163
};
164
165
class InheritableParamAttr : public InheritableAttr {
166
protected:
167
  InheritableParamAttr(ASTContext &Context,
168
                       const AttributeCommonInfo &CommonInfo, attr::Kind AK,
169
                       bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
170
      : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
171
44.7k
                        InheritEvenIfAlreadyPresent) {}
172
173
public:
174
  // Implement isa/cast/dyncast/etc.
175
5.59k
  static bool classof(const Attr *A) {
176
5.59k
    return A->getKind() >= attr::FirstInheritableParamAttr &&
177
5.59k
           A->getKind() <= attr::LastInheritableParamAttr;
178
5.59k
  }
179
};
180
181
/// A parameter attribute which changes the argument-passing ABI rule
182
/// for the parameter.
183
class ParameterABIAttr : public InheritableParamAttr {
184
protected:
185
  ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
186
                   attr::Kind AK, bool IsLateParsed,
187
                   bool InheritEvenIfAlreadyPresent)
188
      : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
189
166
                             InheritEvenIfAlreadyPresent) {}
190
191
public:
192
166
  ParameterABI getABI() const {
193
166
    switch (getKind()) {
194
58
    case attr::SwiftContext:
195
58
      return ParameterABI::SwiftContext;
196
34
    case attr::SwiftErrorResult:
197
34
      return ParameterABI::SwiftErrorResult;
198
74
    case attr::SwiftIndirectResult:
199
74
      return ParameterABI::SwiftIndirectResult;
200
0
    default:
201
0
      llvm_unreachable("bad parameter ABI attribute kind");
202
166
    }
203
166
  }
204
205
1.97M
  static bool classof(const Attr *A) {
206
1.97M
    return A->getKind() >= attr::FirstParameterABIAttr &&
207
1.97M
           
A->getKind() <= attr::LastParameterABIAttr1.97M
;
208
1.97M
   }
209
};
210
211
/// A single parameter index whose accessors require each use to make explicit
212
/// the parameter index encoding needed.
213
class ParamIdx {
214
  // Idx is exposed only via accessors that specify specific encodings.
215
  unsigned Idx : 30;
216
  unsigned HasThis : 1;
217
  unsigned IsValid : 1;
218
219
2.43k
  void assertComparable(const ParamIdx &I) const {
220
2.43k
    assert(isValid() && I.isValid() &&
221
2.43k
           "ParamIdx must be valid to be compared");
222
2.43k
    // It's possible to compare indices from separate functions, but so far
223
2.43k
    // it's not proven useful.  Moreover, it might be confusing because a
224
2.43k
    // comparison on the results of getASTIndex might be inconsistent with a
225
2.43k
    // comparison on the ParamIdx objects themselves.
226
2.43k
    assert(HasThis == I.HasThis &&
227
2.43k
           "ParamIdx must be for the same function to be compared");
228
2.43k
  }
229
230
public:
231
  /// Construct an invalid parameter index (\c isValid returns false and
232
  /// accessors fail an assert).
233
67.9k
  ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
234
235
  /// \param Idx is the parameter index as it is normally specified in
236
  /// attributes in the source: one-origin including any C++ implicit this
237
  /// parameter.
238
  ///
239
  /// \param D is the declaration containing the parameters.  It is used to
240
  /// determine if there is a C++ implicit this parameter.
241
  ParamIdx(unsigned Idx, const Decl *D)
242
40.6k
      : Idx(Idx), HasThis(false), IsValid(true) {
243
40.6k
    assert(Idx >= 1 && "Idx must be one-origin");
244
40.6k
    if (const auto *FD = dyn_cast<FunctionDecl>(D))
245
40.3k
      HasThis = FD->isCXXInstanceMember();
246
40.6k
  }
247
248
  /// A type into which \c ParamIdx can be serialized.
249
  ///
250
  /// A static assertion that it's of the correct size follows the \c ParamIdx
251
  /// class definition.
252
  typedef uint32_t SerialType;
253
254
  /// Produce a representation that can later be passed to \c deserialize to
255
  /// construct an equivalent \c ParamIdx.
256
3.05k
  SerialType serialize() const {
257
3.05k
    return *reinterpret_cast<const SerialType *>(this);
258
3.05k
  }
259
260
  /// Construct from a result from \c serialize.
261
351
  static ParamIdx deserialize(SerialType S) {
262
351
    ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
263
351
    assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
264
351
    return P;
265
351
  }
266
267
  /// Is this parameter index valid?
268
100k
  bool isValid() const { return IsValid; }
269
270
  /// Get the parameter index as it would normally be encoded for attributes at
271
  /// the source level of representation: one-origin including any C++ implicit
272
  /// this parameter.
273
  ///
274
  /// This encoding thus makes sense for diagnostics, pretty printing, and
275
  /// constructing new attributes from a source-like specification.
276
97
  unsigned getSourceIndex() const {
277
97
    assert(isValid() && "ParamIdx must be valid");
278
97
    return Idx;
279
97
  }
280
281
  /// Get the parameter index as it would normally be encoded at the AST level
282
  /// of representation: zero-origin not including any C++ implicit this
283
  /// parameter.
284
  ///
285
  /// This is the encoding primarily used in Sema.  However, in diagnostics,
286
  /// Sema uses \c getSourceIndex instead.
287
94.0k
  unsigned getASTIndex() const {
288
94.0k
    assert(isValid() && "ParamIdx must be valid");
289
94.0k
    assert(Idx >= 1 + HasThis &&
290
94.0k
           "stored index must be base-1 and not specify C++ implicit this");
291
94.0k
    return Idx - 1 - HasThis;
292
94.0k
  }
293
294
  /// Get the parameter index as it would normally be encoded at the LLVM level
295
  /// of representation: zero-origin including any C++ implicit this parameter.
296
  ///
297
  /// This is the encoding primarily used in CodeGen.
298
402
  unsigned getLLVMIndex() const {
299
402
    assert(isValid() && "ParamIdx must be valid");
300
402
    assert(Idx >= 1 && "stored index must be base-1");
301
402
    return Idx - 1;
302
402
  }
303
304
4
  bool operator==(const ParamIdx &I) const {
305
4
    assertComparable(I);
306
4
    return Idx == I.Idx;
307
4
  }
308
0
  bool operator!=(const ParamIdx &I) const {
309
0
    assertComparable(I);
310
0
    return Idx != I.Idx;
311
0
  }
312
2.43k
  bool operator<(const ParamIdx &I) const {
313
2.43k
    assertComparable(I);
314
2.43k
    return Idx < I.Idx;
315
2.43k
  }
316
0
  bool operator>(const ParamIdx &I) const {
317
0
    assertComparable(I);
318
0
    return Idx > I.Idx;
319
0
  }
320
0
  bool operator<=(const ParamIdx &I) const {
321
0
    assertComparable(I);
322
0
    return Idx <= I.Idx;
323
0
  }
324
0
  bool operator>=(const ParamIdx &I) const {
325
0
    assertComparable(I);
326
0
    return Idx >= I.Idx;
327
0
  }
328
};
329
330
static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
331
              "ParamIdx does not fit its serialization type");
332
333
/// Contains information gathered from parsing the contents of TargetAttr.
334
struct ParsedTargetAttr {
335
  std::vector<std::string> Features;
336
  StringRef Architecture;
337
  StringRef BranchProtection;
338
  bool DuplicateArchitecture = false;
339
304
  bool operator ==(const ParsedTargetAttr &Other) const {
340
304
    return DuplicateArchitecture == Other.DuplicateArchitecture &&
341
304
           Architecture == Other.Architecture && 
Features == Other.Features67
;
342
304
  }
343
};
344
345
#include "clang/AST/Attrs.inc"
346
347
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
348
1.55k
                                           const Attr *At) {
349
1.55k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
350
1.55k
                  DiagnosticsEngine::ak_attr);
351
1.55k
  return DB;
352
1.55k
}
353
354
inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
355
0
                                           const Attr *At) {
356
0
  PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
357
0
                  DiagnosticsEngine::ak_attr);
358
0
  return PD;
359
0
}
360
}  // end namespace clang
361
362
#endif