Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/AttributeCommonInfo.h
Line
Count
Source (jump to first uncovered line)
1
//======- AttributeCommonInfo.h - Base info about 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 AttributeCommonInfo type, which is the base for a
10
// ParsedAttr and is used by Attr as a way to share info between the two.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_BASIC_ATTRIBUTECOMMONINFO_H
15
#define LLVM_CLANG_BASIC_ATTRIBUTECOMMONINFO_H
16
#include "clang/Basic/SourceLocation.h"
17
18
namespace clang {
19
class IdentifierInfo;
20
class ASTRecordWriter;
21
22
class AttributeCommonInfo {
23
public:
24
  /// The style used to specify an attribute.
25
  enum Syntax {
26
    /// __attribute__((...))
27
    AS_GNU,
28
29
    /// [[...]]
30
    AS_CXX11,
31
32
    /// [[...]]
33
    AS_C2x,
34
35
    /// __declspec(...)
36
    AS_Declspec,
37
38
    /// [uuid("...")] class Foo
39
    AS_Microsoft,
40
41
    /// __ptr16, alignas(...), etc.
42
    AS_Keyword,
43
44
    /// #pragma ...
45
    AS_Pragma,
46
47
    // Note TableGen depends on the order above.  Do not add or change the order
48
    // without adding related code to TableGen/ClangAttrEmitter.cpp.
49
    /// Context-sensitive version of a keyword attribute.
50
    AS_ContextSensitiveKeyword,
51
  };
52
  enum Kind {
53
#define PARSED_ATTR(NAME) AT_##NAME,
54
#include "clang/Sema/AttrParsedAttrList.inc"
55
#undef PARSED_ATTR
56
    NoSemaHandlerAttribute,
57
    IgnoredAttribute,
58
    UnknownAttribute,
59
  };
60
61
private:
62
  const IdentifierInfo *AttrName = nullptr;
63
  const IdentifierInfo *ScopeName = nullptr;
64
  SourceRange AttrRange;
65
  const SourceLocation ScopeLoc;
66
  // Corresponds to the Kind enum.
67
  unsigned AttrKind : 16;
68
  /// Corresponds to the Syntax enum.
69
  unsigned SyntaxUsed : 3;
70
  unsigned SpellingIndex : 4;
71
72
protected:
73
  static constexpr unsigned SpellingNotCalculated = 0xf;
74
75
public:
76
  AttributeCommonInfo(SourceRange AttrRange)
77
      : AttrRange(AttrRange), ScopeLoc(), AttrKind(0), SyntaxUsed(0),
78
3.19M
        SpellingIndex(SpellingNotCalculated) {}
79
80
  AttributeCommonInfo(SourceLocation AttrLoc)
81
      : AttrRange(AttrLoc), ScopeLoc(), AttrKind(0), SyntaxUsed(0),
82
27.0M
        SpellingIndex(SpellingNotCalculated) {}
83
84
  AttributeCommonInfo(const IdentifierInfo *AttrName,
85
                      const IdentifierInfo *ScopeName, SourceRange AttrRange,
86
                      SourceLocation ScopeLoc, Syntax SyntaxUsed)
87
      : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
88
        ScopeLoc(ScopeLoc),
89
        AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
90
108M
        SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
91
92
  AttributeCommonInfo(const IdentifierInfo *AttrName,
93
                      const IdentifierInfo *ScopeName, SourceRange AttrRange,
94
                      SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed)
95
      : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
96
        ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
97
0
        SpellingIndex(SpellingNotCalculated) {}
98
99
  AttributeCommonInfo(const IdentifierInfo *AttrName,
100
                      const IdentifierInfo *ScopeName, SourceRange AttrRange,
101
                      SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed,
102
                      unsigned Spelling)
103
      : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
104
        ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
105
1.39M
        SpellingIndex(Spelling) {}
106
107
  AttributeCommonInfo(const IdentifierInfo *AttrName, SourceRange AttrRange,
108
                      Syntax SyntaxUsed)
109
      : AttrName(AttrName), ScopeName(nullptr), AttrRange(AttrRange),
110
        ScopeLoc(), AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
111
468k
        SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
112
113
  AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed)
114
      : AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
115
        AttrKind(K), SyntaxUsed(SyntaxUsed),
116
928
        SpellingIndex(SpellingNotCalculated) {}
117
118
  AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed,
119
                      unsigned Spelling)
120
      : AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
121
174k
        AttrKind(K), SyntaxUsed(SyntaxUsed), SpellingIndex(Spelling) {}
122
123
  AttributeCommonInfo(AttributeCommonInfo &&) = default;
124
  AttributeCommonInfo(const AttributeCommonInfo &) = default;
125
126
783M
  Kind getParsedKind() const { return Kind(AttrKind); }
127
1.88M
  Syntax getSyntax() const { return Syntax(SyntaxUsed); }
128
58.6M
  const IdentifierInfo *getAttrName() const { return AttrName; }
129
2.18M
  SourceLocation getLoc() const { return AttrRange.getBegin(); }
130
8.50M
  SourceRange getRange() const { return AttrRange; }
131
480k
  void setRange(SourceRange R) { AttrRange = R; }
132
133
0
  bool hasScope() const { return ScopeName; }
134
1.49M
  const IdentifierInfo *getScopeName() const { return ScopeName; }
135
1.10M
  SourceLocation getScopeLoc() const { return ScopeLoc; }
136
137
  /// Gets the normalized full name, which consists of both scope and name and
138
  /// with surrounding underscores removed as appropriate (e.g.
139
  /// __gnu__::__attr__ will be normalized to gnu::attr).
140
  std::string getNormalizedFullName() const;
141
142
39.3k
  bool isDeclspecAttribute() const { return SyntaxUsed == AS_Declspec; }
143
372
  bool isMicrosoftAttribute() const { return SyntaxUsed == AS_Microsoft; }
144
145
  bool isGNUScope() const;
146
147
322M
  bool isAlignasAttribute() const {
148
    // FIXME: Use a better mechanism to determine this.
149
322M
    return getParsedKind() == AT_Aligned && 
isKeywordAttribute()75.0k
;
150
322M
  }
151
152
322M
  bool isCXX11Attribute() const {
153
322M
    return SyntaxUsed == AS_CXX11 || 
isAlignasAttribute()322M
;
154
322M
  }
155
156
214M
  bool isC2xAttribute() const { return SyntaxUsed == AS_C2x; }
157
158
  /// The attribute is spelled [[]] in either C or C++ mode, including standard
159
  /// attributes spelled with a keyword, like alignas.
160
213M
  bool isStandardAttributeSyntax() const {
161
213M
    return isCXX11Attribute() || 
isC2xAttribute()213M
;
162
213M
  }
163
164
75.0k
  bool isKeywordAttribute() const {
165
75.0k
    return SyntaxUsed == AS_Keyword || 
SyntaxUsed == AS_ContextSensitiveKeyword64.2k
;
166
75.0k
  }
167
168
2.00M
  bool isContextSensitiveKeywordAttribute() const {
169
2.00M
    return SyntaxUsed == AS_ContextSensitiveKeyword;
170
2.00M
  }
171
172
392k
  unsigned getAttributeSpellingListIndex() const {
173
392k
    assert((isAttributeSpellingListCalculated() || AttrName) &&
174
392k
           "Spelling cannot be found");
175
392k
    return isAttributeSpellingListCalculated()
176
392k
               ? 
SpellingIndex3.49k
177
392k
               : 
calculateAttributeSpellingListIndex()388k
;
178
392k
  }
179
30.2M
  void setAttributeSpellingListIndex(unsigned V) { SpellingIndex = V; }
180
181
  static Kind getParsedKind(const IdentifierInfo *Name,
182
                            const IdentifierInfo *Scope, Syntax SyntaxUsed);
183
184
private:
185
  /// Get an index into the attribute spelling list
186
  /// defined in Attr.td. This index is used by an attribute
187
  /// to pretty print itself.
188
  unsigned calculateAttributeSpellingListIndex() const;
189
190
  friend class clang::ASTRecordWriter;
191
  // Used exclusively by ASTDeclWriter to get the raw spelling list state.
192
1.10M
  unsigned getAttributeSpellingListIndexRaw() const { return SpellingIndex; }
193
194
protected:
195
31.6M
  bool isAttributeSpellingListCalculated() const {
196
31.6M
    return SpellingIndex != SpellingNotCalculated;
197
31.6M
  }
198
};
199
} // namespace clang
200
201
#endif // LLVM_CLANG_BASIC_ATTRIBUTECOMMONINFO_H