clang  15.0.0git
AttributeCommonInfo.h
Go to the documentation of this file.
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
17 
18 namespace clang {
19 class IdentifierInfo;
20 class ASTRecordWriter;
21 
23 public:
24  /// The style used to specify an attribute.
25  enum Syntax {
26  /// __attribute__((...))
28 
29  /// [[...]]
31 
32  /// [[...]]
34 
35  /// __declspec(...)
37 
38  /// [uuid("...")] class Foo
40 
41  /// __ptr16, alignas(...), etc.
43 
44  /// #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.
51 
52  /// <vardecl> : <semantic>
54  };
55  enum Kind {
56 #define PARSED_ATTR(NAME) AT_##NAME,
57 #include "clang/Sema/AttrParsedAttrList.inc"
58 #undef PARSED_ATTR
62  };
63 
64 private:
65  const IdentifierInfo *AttrName = nullptr;
66  const IdentifierInfo *ScopeName = nullptr;
67  SourceRange AttrRange;
68  const SourceLocation ScopeLoc;
69  // Corresponds to the Kind enum.
70  unsigned AttrKind : 16;
71  /// Corresponds to the Syntax enum.
72  unsigned SyntaxUsed : 4;
73  unsigned SpellingIndex : 4;
74 
75 protected:
76  static constexpr unsigned SpellingNotCalculated = 0xf;
77 
78 public:
80  : AttrRange(AttrRange), ScopeLoc(), AttrKind(0), SyntaxUsed(0),
81  SpellingIndex(SpellingNotCalculated) {}
82 
84  : AttrRange(AttrLoc), ScopeLoc(), AttrKind(0), SyntaxUsed(0),
85  SpellingIndex(SpellingNotCalculated) {}
86 
88  const IdentifierInfo *ScopeName, SourceRange AttrRange,
89  SourceLocation ScopeLoc, Syntax SyntaxUsed)
90  : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
91  ScopeLoc(ScopeLoc),
92  AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
93  SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
94 
96  const IdentifierInfo *ScopeName, SourceRange AttrRange,
97  SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed)
98  : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
99  ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
100  SpellingIndex(SpellingNotCalculated) {}
101 
103  const IdentifierInfo *ScopeName, SourceRange AttrRange,
104  SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed,
105  unsigned Spelling)
106  : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
107  ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
108  SpellingIndex(Spelling) {}
109 
110  AttributeCommonInfo(const IdentifierInfo *AttrName, SourceRange AttrRange,
111  Syntax SyntaxUsed)
112  : AttrName(AttrName), ScopeName(nullptr), AttrRange(AttrRange),
113  ScopeLoc(), AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
114  SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
115 
116  AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed)
117  : AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
118  AttrKind(K), SyntaxUsed(SyntaxUsed),
119  SpellingIndex(SpellingNotCalculated) {}
120 
121  AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed,
122  unsigned Spelling)
123  : AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
124  AttrKind(K), SyntaxUsed(SyntaxUsed), SpellingIndex(Spelling) {}
125 
127  AttributeCommonInfo(const AttributeCommonInfo &) = default;
128 
129  Kind getParsedKind() const { return Kind(AttrKind); }
130  Syntax getSyntax() const { return Syntax(SyntaxUsed); }
131  const IdentifierInfo *getAttrName() const { return AttrName; }
132  SourceLocation getLoc() const { return AttrRange.getBegin(); }
133  SourceRange getRange() const { return AttrRange; }
134  void setRange(SourceRange R) { AttrRange = R; }
135 
136  bool hasScope() const { return ScopeName; }
137  const IdentifierInfo *getScopeName() const { return ScopeName; }
138  SourceLocation getScopeLoc() const { return ScopeLoc; }
139 
140  /// Gets the normalized full name, which consists of both scope and name and
141  /// with surrounding underscores removed as appropriate (e.g.
142  /// __gnu__::__attr__ will be normalized to gnu::attr).
144 
145  bool isDeclspecAttribute() const { return SyntaxUsed == AS_Declspec; }
146  bool isMicrosoftAttribute() const { return SyntaxUsed == AS_Microsoft; }
147 
148  bool isGNUScope() const;
149  bool isClangScope() const;
150 
151  bool isAlignasAttribute() const {
152  // FIXME: Use a better mechanism to determine this.
153  // We use this in `isCXX11Attribute` below, so it _should_ only return
154  // true for the `alignas` spelling, but it currently also returns true
155  // for the `_Alignas` spelling, which only exists in C11. Distinguishing
156  // between the two is important because they behave differently:
157  // - `alignas` may only appear in the attribute-specifier-seq before
158  // the decl-specifier-seq and is therefore associated with the
159  // declaration.
160  // - `_Alignas` may appear anywhere within the declaration-specifiers
161  // and is therefore associated with the `DeclSpec`.
162  // It's not clear how best to fix this:
163  // - We have the necessary information in the form of the `SpellingIndex`,
164  // but we would need to compare against AlignedAttr::Keyword_alignas,
165  // and we can't depend on clang/AST/Attr.h here.
166  // - We could test `getAttrName()->getName() == "alignas"`, but this is
167  // inefficient.
168  return getParsedKind() == AT_Aligned && isKeywordAttribute();
169  }
170 
171  bool isCXX11Attribute() const {
172  return SyntaxUsed == AS_CXX11 || isAlignasAttribute();
173  }
174 
175  bool isC2xAttribute() const { return SyntaxUsed == AS_C2x; }
176 
177  /// The attribute is spelled [[]] in either C or C++ mode, including standard
178  /// attributes spelled with a keyword, like alignas.
180  return isCXX11Attribute() || isC2xAttribute();
181  }
182 
183  bool isGNUAttribute() const { return SyntaxUsed == AS_GNU; }
184 
185  bool isKeywordAttribute() const {
186  return SyntaxUsed == AS_Keyword || SyntaxUsed == AS_ContextSensitiveKeyword;
187  }
188 
190  return SyntaxUsed == AS_ContextSensitiveKeyword;
191  }
192 
193  unsigned getAttributeSpellingListIndex() const {
194  assert((isAttributeSpellingListCalculated() || AttrName) &&
195  "Spelling cannot be found");
197  ? SpellingIndex
198  : calculateAttributeSpellingListIndex();
199  }
200  void setAttributeSpellingListIndex(unsigned V) { SpellingIndex = V; }
201 
202  static Kind getParsedKind(const IdentifierInfo *Name,
203  const IdentifierInfo *Scope, Syntax SyntaxUsed);
204 
205 private:
206  /// Get an index into the attribute spelling list
207  /// defined in Attr.td. This index is used by an attribute
208  /// to pretty print itself.
209  unsigned calculateAttributeSpellingListIndex() const;
210 
212  // Used exclusively by ASTDeclWriter to get the raw spelling list state.
213  unsigned getAttributeSpellingListIndexRaw() const { return SpellingIndex; }
214 
215 protected:
217  return SpellingIndex != SpellingNotCalculated;
218  }
219 };
220 } // namespace clang
221 
222 #endif // LLVM_CLANG_BASIC_ATTRIBUTECOMMONINFO_H
clang::AttributeCommonInfo::getParsedKind
Kind getParsedKind() const
Definition: AttributeCommonInfo.h:129
clang::AttributeCommonInfo::AS_Microsoft
@ AS_Microsoft
[uuid("...")] class Foo
Definition: AttributeCommonInfo.h:39
clang::AttributeCommonInfo::isGNUAttribute
bool isGNUAttribute() const
Definition: AttributeCommonInfo.h:183
clang::AttributeCommonInfo::getLoc
SourceLocation getLoc() const
Definition: AttributeCommonInfo.h:132
clang::AttributeCommonInfo::Kind
Kind
Definition: AttributeCommonInfo.h:55
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:210
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::AttributeCommonInfo::setRange
void setRange(SourceRange R)
Definition: AttributeCommonInfo.h:134
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::SourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:219
clang::AttributeCommonInfo::isKeywordAttribute
bool isKeywordAttribute() const
Definition: AttributeCommonInfo.h:185
clang::AttributeCommonInfo::hasScope
bool hasScope() const
Definition: AttributeCommonInfo.h:136
clang::AttributeCommonInfo::AS_CXX11
@ AS_CXX11
[[...]]
Definition: AttributeCommonInfo.h:30
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceLocation AttrLoc)
Definition: AttributeCommonInfo.h:83
clang::AttributeCommonInfo::getAttrName
const IdentifierInfo * getAttrName() const
Definition: AttributeCommonInfo.h:131
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:116
clang::AttributeCommonInfo::Syntax
Syntax
The style used to specify an attribute.
Definition: AttributeCommonInfo.h:25
clang::AttributeCommonInfo::AS_GNU
@ AS_GNU
attribute((...))
Definition: AttributeCommonInfo.h:27
clang::AttributeCommonInfo::isDeclspecAttribute
bool isDeclspecAttribute() const
Definition: AttributeCommonInfo.h:145
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed, unsigned Spelling)
Definition: AttributeCommonInfo.h:121
clang::AttributeCommonInfo::AS_Keyword
@ AS_Keyword
__ptr16, alignas(...), etc.
Definition: AttributeCommonInfo.h:42
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, const IdentifierInfo *ScopeName, SourceRange AttrRange, SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:95
clang::AttributeCommonInfo::isC2xAttribute
bool isC2xAttribute() const
Definition: AttributeCommonInfo.h:175
V
#define V(N, I)
Definition: ASTContext.h:3176
clang::AttributeCommonInfo::UnknownAttribute
@ UnknownAttribute
Definition: AttributeCommonInfo.h:61
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, const IdentifierInfo *ScopeName, SourceRange AttrRange, SourceLocation ScopeLoc, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:87
clang::AttributeCommonInfo::AS_ContextSensitiveKeyword
@ AS_ContextSensitiveKeyword
Context-sensitive version of a keyword attribute.
Definition: AttributeCommonInfo.h:50
clang::AttributeCommonInfo::isStandardAttributeSyntax
bool isStandardAttributeSyntax() const
The attribute is spelled [[]] in either C or C++ mode, including standard attributes spelled with a k...
Definition: AttributeCommonInfo.h:179
clang::Scope
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
clang::AttributeCommonInfo::getSyntax
Syntax getSyntax() const
Definition: AttributeCommonInfo.h:130
clang::AttributeCommonInfo::isContextSensitiveKeywordAttribute
bool isContextSensitiveKeywordAttribute() const
Definition: AttributeCommonInfo.h:189
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, SourceRange AttrRange, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:110
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, const IdentifierInfo *ScopeName, SourceRange AttrRange, SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed, unsigned Spelling)
Definition: AttributeCommonInfo.h:102
clang::AttributeCommonInfo::isAttributeSpellingListCalculated
bool isAttributeSpellingListCalculated() const
Definition: AttributeCommonInfo.h:216
clang::AttributeCommonInfo::AS_HLSLSemantic
@ AS_HLSLSemantic
<vardecl> : <semantic>
Definition: AttributeCommonInfo.h:53
clang::AttributeCommonInfo::getScopeLoc
SourceLocation getScopeLoc() const
Definition: AttributeCommonInfo.h:138
clang::AttributeCommonInfo::AS_C2x
@ AS_C2x
[[...]]
Definition: AttributeCommonInfo.h:33
clang::AttributeCommonInfo::isMicrosoftAttribute
bool isMicrosoftAttribute() const
Definition: AttributeCommonInfo.h:146
SourceLocation.h
clang::AttributeCommonInfo::isCXX11Attribute
bool isCXX11Attribute() const
Definition: AttributeCommonInfo.h:171
clang::AttributeCommonInfo::getRange
SourceRange getRange() const
Definition: AttributeCommonInfo.h:133
clang::AttributeCommonInfo::getScopeName
const IdentifierInfo * getScopeName() const
Definition: AttributeCommonInfo.h:137
clang::AttributeCommonInfo::isAlignasAttribute
bool isAlignasAttribute() const
Definition: AttributeCommonInfo.h:151
clang::AttributeCommonInfo
Definition: AttributeCommonInfo.h:22
clang::ASTRecordWriter
An object for streaming information to a record.
Definition: ASTRecordWriter.h:27
clang::AttributeCommonInfo::getNormalizedFullName
std::string getNormalizedFullName() const
Gets the normalized full name, which consists of both scope and name and with surrounding underscores...
Definition: Attributes.cpp:118
clang::AttributeCommonInfo::NoSemaHandlerAttribute
@ NoSemaHandlerAttribute
Definition: AttributeCommonInfo.h:59
clang::AttributeCommonInfo::AS_Declspec
@ AS_Declspec
__declspec(...)
Definition: AttributeCommonInfo.h:36
clang::AttributeCommonInfo::IgnoredAttribute
@ IgnoredAttribute
Definition: AttributeCommonInfo.h:60
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceRange AttrRange)
Definition: AttributeCommonInfo.h:79
clang::AttributeCommonInfo::isClangScope
bool isClangScope() const
Definition: Attributes.cpp:88
clang
Definition: CalledOnceCheck.h:17
clang::AttributeCommonInfo::setAttributeSpellingListIndex
void setAttributeSpellingListIndex(unsigned V)
Definition: AttributeCommonInfo.h:200
clang::AttributeCommonInfo::SpellingNotCalculated
static constexpr unsigned SpellingNotCalculated
Definition: AttributeCommonInfo.h:76
clang::AttributeCommonInfo::AS_Pragma
@ AS_Pragma
#pragma ...
Definition: AttributeCommonInfo.h:45
clang::AttributeCommonInfo::getAttributeSpellingListIndex
unsigned getAttributeSpellingListIndex() const
Definition: AttributeCommonInfo.h:193
clang::AttributeCommonInfo::isGNUScope
bool isGNUScope() const
Definition: Attributes.cpp:84