Coverage Report

Created: 2018-11-16 02:38

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/AST/Attr.h
Line
Count
Source (jump to first uncovered line)
1
//===--- Attr.h - Classes for representing attributes ----------*- 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
//  This file defines the Attr interface and subclasses.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_AST_ATTR_H
15
#define LLVM_CLANG_AST_ATTR_H
16
17
#include "clang/AST/AttrIterator.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/Expr.h"
20
#include "clang/AST/Type.h"
21
#include "clang/Basic/AttrKinds.h"
22
#include "clang/Basic/LLVM.h"
23
#include "clang/Basic/OpenMPKinds.h"
24
#include "clang/Basic/Sanitizers.h"
25
#include "clang/Basic/SourceLocation.h"
26
#include "llvm/ADT/StringSwitch.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include "llvm/Support/VersionTuple.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include <algorithm>
31
#include <cassert>
32
33
namespace clang {
34
  class ASTContext;
35
  class IdentifierInfo;
36
  class ObjCInterfaceDecl;
37
  class Expr;
38
  class QualType;
39
  class FunctionDecl;
40
  class TypeSourceInfo;
41
42
/// Attr - This represents one attribute.
43
class Attr {
44
private:
45
  SourceRange Range;
46
  unsigned AttrKind : 16;
47
48
protected:
49
  /// An index into the spelling list of an
50
  /// attribute defined in Attr.td file.
51
  unsigned SpellingListIndex : 4;
52
  unsigned Inherited : 1;
53
  unsigned IsPackExpansion : 1;
54
  unsigned Implicit : 1;
55
  // FIXME: These are properties of the attribute kind, not state for this
56
  // instance of the attribute.
57
  unsigned IsLateParsed : 1;
58
  unsigned InheritEvenIfAlreadyPresent : 1;
59
60
0
  void *operator new(size_t bytes) noexcept {
61
0
    llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
62
0
  }
63
0
  void operator delete(void *data) noexcept {
64
0
    llvm_unreachable("Attrs cannot be released with regular 'delete'.");
65
0
  }
66
67
public:
68
  // Forward so that the regular new and delete do not hide global ones.
69
  void *operator new(size_t Bytes, ASTContext &C,
70
2.83M
                     size_t Alignment = 8) noexcept {
71
2.83M
    return ::operator new(Bytes, C, Alignment);
72
2.83M
  }
73
0
  void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
74
0
    return ::operator delete(Ptr, C, Alignment);
75
0
  }
76
77
protected:
78
  Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
79
       bool IsLateParsed)
80
    : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
81
      Inherited(false), IsPackExpansion(false), Implicit(false),
82
9.73M
      IsLateParsed(IsLateParsed), InheritEvenIfAlreadyPresent(false) {}
83
84
public:
85
86
568M
  attr::Kind getKind() const {
87
568M
    return static_cast<attr::Kind>(AttrKind);
88
568M
  }
89
90
1.84M
  unsigned getSpellingListIndex() const { return SpellingListIndex; }
91
  const char *getSpelling() const;
92
93
1.13M
  SourceLocation getLocation() const { return Range.getBegin(); }
94
464k
  SourceRange getRange() const { return Range; }
95
172
  void setRange(SourceRange R) { Range = R; }
96
97
4.62M
  bool isInherited() const { return Inherited; }
98
99
  /// Returns true if the attribute has been implicitly created instead
100
  /// of explicitly written by the user.
101
273k
  bool isImplicit() const { return Implicit; }
102
2.37M
  void setImplicit(bool I) { Implicit = I; }
103
104
28.4k
  void setPackExpansion(bool PE) { IsPackExpansion = PE; }
105
194
  bool isPackExpansion() const { return IsPackExpansion; }
106
107
  // Clone this attribute.
108
  Attr *clone(ASTContext &C) const;
109
110
2.16M
  bool isLateParsed() const { return IsLateParsed; }
111
112
  // Pretty print this attribute.
113
  void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
114
};
115
116
class TypeAttr : public Attr {
117
protected:
118
  TypeAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
119
           bool IsLateParsed)
120
254k
      : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
121
122
public:
123
  static bool classof(const Attr *A) {
124
    return A->getKind() >= attr::FirstTypeAttr &&
125
           A->getKind() <= attr::LastTypeAttr;
126
  }
127
};
128
129
class StmtAttr : public Attr {
130
protected:
131
  StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
132
                  bool IsLateParsed)
133
903
      : Attr(AK, R, SpellingListIndex, 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(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
145
                  bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
146
8.86M
      : Attr(AK, R, SpellingListIndex, IsLateParsed) {
147
8.86M
    this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
148
8.86M
  }
149
150
public:
151
384k
  void setInherited(bool I) { Inherited = I; }
152
153
  /// Should this attribute be inherited from a prior declaration even if it's
154
  /// explicitly provided in the current declaration?
155
332k
  bool shouldInheritEvenIfAlreadyPresent() const {
156
332k
    return InheritEvenIfAlreadyPresent;
157
332k
  }
158
159
  // Implement isa/cast/dyncast/etc.
160
1.02M
  static bool classof(const Attr *A) {
161
1.02M
    return A->getKind() >= attr::FirstInheritableAttr &&
162
1.02M
           A->getKind() <= attr::LastInheritableAttr;
163
1.02M
  }
164
};
165
166
class InheritableParamAttr : public InheritableAttr {
167
protected:
168
  InheritableParamAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex,
169
                       bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
170
      : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
171
9.75k
                        InheritEvenIfAlreadyPresent) {}
172
173
public:
174
  // Implement isa/cast/dyncast/etc.
175
873
  static bool classof(const Attr *A) {
176
873
    return A->getKind() >= attr::FirstInheritableParamAttr &&
177
873
           A->getKind() <= attr::LastInheritableParamAttr;
178
873
  }
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(attr::Kind AK, SourceRange R,
186
                   unsigned SpellingListIndex, bool IsLateParsed,
187
                   bool InheritEvenIfAlreadyPresent)
188
    : InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
189
166
                           InheritEvenIfAlreadyPresent) {}
190
191
public:
192
166
  ParameterABI getABI() const {
193
166
    switch (getKind()) {
194
166
    case attr::SwiftContext:
195
58
      return ParameterABI::SwiftContext;
196
166
    case attr::SwiftErrorResult:
197
34
      return ParameterABI::SwiftErrorResult;
198
166
    case attr::SwiftIndirectResult:
199
74
      return ParameterABI::SwiftIndirectResult;
200
166
    default:
201
0
      llvm_unreachable("bad parameter ABI attribute kind");
202
166
    }
203
166
  }
204
205
2.17M
  static bool classof(const Attr *A) {
206
2.17M
    return A->getKind() >= attr::FirstParameterABIAttr &&
207
2.17M
           
A->getKind() <= attr::LastParameterABIAttr2.17M
;
208
2.17M
   }
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
75
  void assertComparable(const ParamIdx &I) const {
220
75
    assert(isValid() && I.isValid() &&
221
75
           "ParamIdx must be valid to be compared");
222
75
    // It's possible to compare indices from separate functions, but so far
223
75
    // it's not proven useful.  Moreover, it might be confusing because a
224
75
    // comparison on the results of getASTIndex might be inconsistent with a
225
75
    // comparison on the ParamIdx objects themselves.
226
75
    assert(HasThis == I.HasThis &&
227
75
           "ParamIdx must be for the same function to be compared");
228
75
  }
229
230
public:
231
  /// Construct an invalid parameter index (\c isValid returns false and
232
  /// accessors fail an assert).
233
34.7k
  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
30.9k
      : Idx(Idx), HasThis(false), IsValid(true) {
243
30.9k
    assert(Idx >= 1 && "Idx must be one-origin");
244
30.9k
    if (const auto *FD = dyn_cast<FunctionDecl>(D))
245
30.9k
      HasThis = FD->isCXXInstanceMember();
246
30.9k
  }
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
34
  SerialType serialize() const {
257
34
    return *reinterpret_cast<const SerialType *>(this);
258
34
  }
259
260
  /// Construct from a result from \c serialize.
261
34
  static ParamIdx deserialize(SerialType S) {
262
34
    ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
263
34
    assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
264
34
    return P;
265
34
  }
266
267
  /// Is this parameter index valid?
268
3.28k
  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
31.6k
  unsigned getASTIndex() const {
288
31.6k
    assert(isValid() && "ParamIdx must be valid");
289
31.6k
    assert(Idx >= 1 + HasThis &&
290
31.6k
           "stored index must be base-1 and not specify C++ implicit this");
291
31.6k
    return Idx - 1 - HasThis;
292
31.6k
  }
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
3.70k
  unsigned getLLVMIndex() const {
299
3.70k
    assert(isValid() && "ParamIdx must be valid");
300
3.70k
    assert(Idx >= 1 && "stored index must be base-1");
301
3.70k
    return Idx - 1;
302
3.70k
  }
303
304
4
  bool operator==(const ParamIdx &I) const {
305
4
    assertComparable(I);
306
4
    return Idx == I.Idx;
307
4
  }
308
  bool operator!=(const ParamIdx &I) const {
309
    assertComparable(I);
310
    return Idx != I.Idx;
311
  }
312
71
  bool operator<(const ParamIdx &I) const {
313
71
    assertComparable(I);
314
71
    return Idx < I.Idx;
315
71
  }
316
  bool operator>(const ParamIdx &I) const {
317
    assertComparable(I);
318
    return Idx > I.Idx;
319
  }
320
  bool operator<=(const ParamIdx &I) const {
321
    assertComparable(I);
322
    return Idx <= I.Idx;
323
  }
324
  bool operator>=(const ParamIdx &I) const {
325
    assertComparable(I);
326
    return Idx >= I.Idx;
327
  }
328
};
329
330
static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
331
              "ParamIdx does not fit its serialization type");
332
333
#include "clang/AST/Attrs.inc"
334
335
inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
336
1.35k
                                           const Attr *At) {
337
1.35k
  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
338
1.35k
                  DiagnosticsEngine::ak_attr);
339
1.35k
  return DB;
340
1.35k
}
341
342
inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
343
0
                                           const Attr *At) {
344
0
  PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
345
0
                  DiagnosticsEngine::ak_attr);
346
0
  return PD;
347
0
}
348
}  // end namespace clang
349
350
#endif