1//===--- Attributes.cpp ---------------------------------------------------===//
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 implements the AttributeCommonInfo interface.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Basic/Attributes.h"
14#include "clang/Basic/AttrSubjectMatchRules.h"
15#include "clang/Basic/IdentifierTable.h"
16#include "clang/Basic/LangOptions.h"
17#include "clang/Basic/ParsedAttrInfo.h"
18#include "clang/Basic/SimpleTypoCorrection.h"
19#include "clang/Basic/TargetInfo.h"
20
21#include "llvm/ADT/StringSwitch.h"
22
23using namespace clang;
24
25static StringRef canonicalizeScopeName(StringRef Name) {
26 // Normalize the scope name, but only for gnu and clang attributes.
27 if (Name == "__gnu__")
28 return "gnu";
29
30 if (Name == "_Clang")
31 return "clang";
32
33 return Name;
34}
35
36static StringRef canonicalizeAttrName(StringRef Name) {
37 // Normalize the attribute name, __foo__ becomes foo.
38 if (Name.size() >= 4 && Name.starts_with(Prefix: "__") && Name.ends_with(Suffix: "__"))
39 return Name.substr(Start: 2, N: Name.size() - 4);
40
41 return Name;
42}
43
44static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name,
45 StringRef ScopeName, const TargetInfo &Target,
46 const LangOptions &LangOpts) {
47#include "clang/Basic/AttrHasAttributeImpl.inc"
48 return 0;
49}
50
51int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax, StringRef ScopeName,
52 StringRef Name, const TargetInfo &Target,
53 const LangOptions &LangOpts, bool CheckPlugins) {
54 ScopeName = canonicalizeScopeName(Name: ScopeName);
55 Name = canonicalizeAttrName(Name);
56
57 // As a special case, look for the omp::sequence and omp::directive
58 // attributes. We support those, but not through the typical attribute
59 // machinery that goes through TableGen. We support this in all OpenMP modes
60 // so long as double square brackets are enabled.
61 //
62 // Other OpenMP attributes (e.g. [[omp::assume]]) are handled via the
63 // regular attribute parsing machinery.
64 if (LangOpts.OpenMP && ScopeName == "omp" &&
65 (Name == "directive" || Name == "sequence"))
66 return 1;
67
68 int res = hasAttributeImpl(Syntax, Name, ScopeName, Target, LangOpts);
69 if (res)
70 return res;
71
72 if (CheckPlugins) {
73 // Check if any plugin provides this attribute.
74 for (auto &Ptr : getAttributePluginInstances())
75 if (Ptr->hasSpelling(Syntax, Name))
76 return 1;
77 }
78
79 return 0;
80}
81
82int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax,
83 const IdentifierInfo *Scope, const IdentifierInfo *Attr,
84 const TargetInfo &Target, const LangOptions &LangOpts,
85 bool CheckPlugins) {
86 return hasAttribute(Syntax, ScopeName: Scope ? Scope->getName() : "", Name: Attr->getName(),
87 Target, LangOpts, CheckPlugins);
88}
89
90int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax,
91 const IdentifierInfo *Scope, const IdentifierInfo *Attr,
92 const TargetInfo &Target, const LangOptions &LangOpts) {
93 return hasAttribute(Syntax, Scope, Attr, Target, LangOpts,
94 /*CheckPlugins=*/true);
95}
96
97const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule) {
98 switch (Rule) {
99#define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
100 case attr::NAME: \
101 return SPELLING;
102#include "clang/Basic/AttrSubMatchRulesList.inc"
103 }
104 llvm_unreachable("Invalid subject match rule");
105}
106
107static StringRef
108normalizeAttrScopeName(StringRef ScopeName,
109 AttributeCommonInfo::Syntax SyntaxUsed) {
110 if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
111 SyntaxUsed == AttributeCommonInfo::AS_C23)
112 return canonicalizeScopeName(Name: ScopeName);
113
114 return ScopeName;
115}
116
117static StringRef
118normalizeAttrScopeName(const IdentifierInfo *ScopeName,
119 AttributeCommonInfo::Syntax SyntaxUsed) {
120 if (ScopeName)
121 return normalizeAttrScopeName(ScopeName: ScopeName->getName(), SyntaxUsed);
122 return "";
123}
124
125static StringRef normalizeAttrName(StringRef AttrName,
126 StringRef NormalizedScopeName,
127 AttributeCommonInfo::Syntax SyntaxUsed) {
128 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
129 // for GNU attributes, and attributes using the double square bracket syntax.
130 bool ShouldNormalize =
131 SyntaxUsed == AttributeCommonInfo::AS_GNU ||
132 ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
133 SyntaxUsed == AttributeCommonInfo::AS_C23) &&
134 (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" ||
135 NormalizedScopeName == "clang"));
136
137 if (ShouldNormalize)
138 return canonicalizeAttrName(Name: AttrName);
139
140 return AttrName;
141}
142
143StringRef AttributeCommonInfo::getNormalizedScopeName() const {
144 return normalizeAttrScopeName(ScopeName: getScopeName(), SyntaxUsed: getSyntax());
145}
146
147StringRef
148AttributeCommonInfo::getNormalizedAttrName(StringRef ScopeName) const {
149 return normalizeAttrName(AttrName: getAttrName()->getName(), NormalizedScopeName: ScopeName, SyntaxUsed: getSyntax());
150}
151
152bool AttributeCommonInfo::isGNUScope() const {
153 return AttrScope.isValid() && (AttrScope.getName()->isStr(Str: "gnu") ||
154 AttrScope.getName()->isStr(Str: "__gnu__"));
155}
156
157bool AttributeCommonInfo::isClangScope() const {
158 return AttrScope.isValid() && (AttrScope.getName()->isStr(Str: "clang") ||
159 AttrScope.getName()->isStr(Str: "_Clang"));
160}
161
162#include "clang/Sema/AttrParsedAttrKinds.inc"
163
164static SmallString<64> normalizeName(StringRef AttrName, StringRef ScopeName,
165 AttributeCommonInfo::Syntax SyntaxUsed) {
166 std::string StrAttrName = SyntaxUsed == AttributeCommonInfo::AS_HLSLAnnotation
167 ? AttrName.lower()
168 : AttrName.str();
169 SmallString<64> FullName = ScopeName;
170 if (!ScopeName.empty()) {
171 assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
172 SyntaxUsed == AttributeCommonInfo::AS_C23);
173 FullName += "::";
174 }
175 FullName += StrAttrName;
176 return FullName;
177}
178
179static SmallString<64> normalizeName(const IdentifierInfo *Name,
180 const IdentifierInfo *Scope,
181 AttributeCommonInfo::Syntax SyntaxUsed) {
182 StringRef ScopeName = normalizeAttrScopeName(ScopeName: Scope, SyntaxUsed);
183 StringRef AttrName =
184 normalizeAttrName(AttrName: Name->getName(), NormalizedScopeName: ScopeName, SyntaxUsed);
185 return normalizeName(AttrName, ScopeName, SyntaxUsed);
186}
187
188AttributeCommonInfo::Kind
189AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name,
190 const IdentifierInfo *ScopeName,
191 Syntax SyntaxUsed) {
192 AttributeCommonInfo::Kind Kind =
193 ::getAttrKind(Name: normalizeName(Name, Scope: ScopeName, SyntaxUsed), Syntax: SyntaxUsed);
194 if (SyntaxUsed == AS_HLSLAnnotation &&
195 Kind == AttributeCommonInfo::Kind::UnknownAttribute)
196 return AttributeCommonInfo::Kind::AT_HLSLUnparsedSemantic;
197 return Kind;
198}
199
200AttributeCommonInfo::AttrArgsInfo
201AttributeCommonInfo::getCXX11AttrArgsInfo(const IdentifierInfo *Name) {
202 StringRef AttrName = normalizeAttrName(
203 AttrName: Name->getName(), /*NormalizedScopeName*/ "", SyntaxUsed: Syntax::AS_CXX11);
204#define CXX11_ATTR_ARGS_INFO
205 return llvm::StringSwitch<AttributeCommonInfo::AttrArgsInfo>(AttrName)
206#include "clang/Basic/CXX11AttributeInfo.inc"
207 .Default(Value: AttributeCommonInfo::AttrArgsInfo::None);
208#undef CXX11_ATTR_ARGS_INFO
209}
210
211std::string AttributeCommonInfo::getNormalizedFullName() const {
212 return static_cast<std::string>(
213 normalizeName(Name: getAttrName(), Scope: getScopeName(), SyntaxUsed: getSyntax()));
214}
215
216std::string
217AttributeCommonInfo::getNormalizedFullName(StringRef ScopeName,
218 StringRef AttrName) const {
219 return static_cast<std::string>(
220 normalizeName(AttrName, ScopeName, SyntaxUsed: getSyntax()));
221}
222
223SourceRange AttributeCommonInfo::getNormalizedRange() const {
224 return hasScope() ? SourceRange(AttrScope.getNameLoc(), AttrRange.getEnd())
225 : AttrRange;
226}
227
228static AttributeCommonInfo::Scope
229getScopeFromNormalizedScopeName(StringRef ScopeName) {
230 return llvm::StringSwitch<AttributeCommonInfo::Scope>(ScopeName)
231 .Case(S: "", Value: AttributeCommonInfo::Scope::NONE)
232 .Case(S: "clang", Value: AttributeCommonInfo::Scope::CLANG)
233 .Case(S: "gnu", Value: AttributeCommonInfo::Scope::GNU)
234 .Case(S: "gsl", Value: AttributeCommonInfo::Scope::GSL)
235 .Case(S: "hlsl", Value: AttributeCommonInfo::Scope::HLSL)
236 .Case(S: "vk", Value: AttributeCommonInfo::Scope::VK)
237 .Case(S: "msvc", Value: AttributeCommonInfo::Scope::MSVC)
238 .Case(S: "omp", Value: AttributeCommonInfo::Scope::OMP)
239 .Case(S: "riscv", Value: AttributeCommonInfo::Scope::RISCV);
240}
241
242unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
243 // An unknown attribute (getParsedKind() == UnknownAttribute) has no entry in
244 // the generated spelling tables. The name/scope StringSwitches below assume a
245 // known attribute and intentionally have no default, so computing an index
246 // for an unknown *scoped* attribute such as [[ns::foo]] would fall off the
247 // end (getScopeFromNormalizedScopeName aborts). It has a single no-spelling,
248 // so report index 0 instead.
249 if (getParsedKind() == AttributeCommonInfo::UnknownAttribute)
250 return 0;
251
252 // Both variables will be used in tablegen generated
253 // attribute spell list index matching code.
254 auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
255 StringRef ScopeName = normalizeAttrScopeName(ScopeName: getScopeName(), SyntaxUsed: Syntax);
256 StringRef Name =
257 normalizeAttrName(AttrName: getAttrName()->getName(), NormalizedScopeName: ScopeName, SyntaxUsed: Syntax);
258 AttributeCommonInfo::Scope ComputedScope =
259 getScopeFromNormalizedScopeName(ScopeName);
260
261#include "clang/Sema/AttrSpellingListIndex.inc"
262}
263
264#define ATTR_NAME(NAME) NAME,
265static constexpr const char *AttrSpellingList[] = {
266#include "clang/Basic/AttributeSpellingList.inc"
267};
268
269#define ATTR_SCOPE_NAME(SCOPE_NAME) SCOPE_NAME,
270static constexpr const char *AttrScopeSpellingList[] = {
271#include "clang/Basic/AttributeSpellingList.inc"
272};
273
274std::optional<StringRef>
275AttributeCommonInfo::tryGetCorrectedScopeName(StringRef ScopeName) const {
276 if (ScopeName.size() > 0 &&
277 !llvm::is_contained(Range: AttrScopeSpellingList, Element: ScopeName)) {
278 SimpleTypoCorrection STC(ScopeName);
279 for (const auto &Scope : AttrScopeSpellingList)
280 STC.add(Candidate: Scope);
281
282 if (auto CorrectedScopeName = STC.getCorrection())
283 return CorrectedScopeName;
284 }
285 return std::nullopt;
286}
287
288std::optional<StringRef> AttributeCommonInfo::tryGetCorrectedAttrName(
289 StringRef ScopeName, StringRef AttrName, const TargetInfo &Target,
290 const LangOptions &LangOpts) const {
291 if (!llvm::is_contained(Range: AttrSpellingList, Element: AttrName)) {
292 SimpleTypoCorrection STC(AttrName);
293 for (const auto &Attr : AttrSpellingList)
294 STC.add(Candidate: Attr);
295
296 if (auto CorrectedAttrName = STC.getCorrection()) {
297 if (hasAttribute(Syntax: getSyntax(), ScopeName, Name: *CorrectedAttrName, Target,
298 LangOpts,
299 /*CheckPlugins=*/true))
300 return CorrectedAttrName;
301 }
302 }
303 return std::nullopt;
304}
305