1 //===--- Attributes.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the AttributeCommonInfo interface.
11 //===----------------------------------------------------------------------===//
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/TargetInfo.h"
20 using namespace clang
;
22 static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax
, StringRef Name
,
23 StringRef ScopeName
, const TargetInfo
&Target
,
24 const LangOptions
&LangOpts
) {
26 #include "clang/Basic/AttrHasAttributeImpl.inc"
31 int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax
,
32 const IdentifierInfo
*Scope
, const IdentifierInfo
*Attr
,
33 const TargetInfo
&Target
, const LangOptions
&LangOpts
) {
34 StringRef Name
= Attr
->getName();
35 // Normalize the attribute name, __foo__ becomes foo.
36 if (Name
.size() >= 4 && Name
.startswith("__") && Name
.endswith("__"))
37 Name
= Name
.substr(2, Name
.size() - 4);
39 // Normalize the scope name, but only for gnu and clang attributes.
40 StringRef ScopeName
= Scope
? Scope
->getName() : "";
41 if (ScopeName
== "__gnu__")
43 else if (ScopeName
== "_Clang")
46 // As a special case, look for the omp::sequence and omp::directive
47 // attributes. We support those, but not through the typical attribute
48 // machinery that goes through TableGen. We support this in all OpenMP modes
49 // so long as double square brackets are enabled.
50 if (LangOpts
.OpenMP
&& ScopeName
== "omp")
51 return (Name
== "directive" || Name
== "sequence") ? 1 : 0;
53 int res
= hasAttributeImpl(Syntax
, Name
, ScopeName
, Target
, LangOpts
);
57 // Check if any plugin provides this attribute.
58 for (auto &Ptr
: getAttributePluginInstances())
59 if (Ptr
->hasSpelling(Syntax
, Name
))
65 const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule
) {
67 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
70 #include "clang/Basic/AttrSubMatchRulesList.inc"
72 llvm_unreachable("Invalid subject match rule");
76 normalizeAttrScopeName(const IdentifierInfo
*Scope
,
77 AttributeCommonInfo::Syntax SyntaxUsed
) {
81 // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
83 StringRef ScopeName
= Scope
->getName();
84 if (SyntaxUsed
== AttributeCommonInfo::AS_CXX11
||
85 SyntaxUsed
== AttributeCommonInfo::AS_C23
) {
86 if (ScopeName
== "__gnu__")
88 else if (ScopeName
== "_Clang")
94 static StringRef
normalizeAttrName(const IdentifierInfo
*Name
,
95 StringRef NormalizedScopeName
,
96 AttributeCommonInfo::Syntax SyntaxUsed
) {
97 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
98 // for GNU attributes, and attributes using the double square bracket syntax.
99 bool ShouldNormalize
=
100 SyntaxUsed
== AttributeCommonInfo::AS_GNU
||
101 ((SyntaxUsed
== AttributeCommonInfo::AS_CXX11
||
102 SyntaxUsed
== AttributeCommonInfo::AS_C23
) &&
103 (NormalizedScopeName
.empty() || NormalizedScopeName
== "gnu" ||
104 NormalizedScopeName
== "clang"));
105 StringRef AttrName
= Name
->getName();
106 if (ShouldNormalize
&& AttrName
.size() >= 4 && AttrName
.startswith("__") &&
107 AttrName
.endswith("__"))
108 AttrName
= AttrName
.slice(2, AttrName
.size() - 2);
113 bool AttributeCommonInfo::isGNUScope() const {
114 return ScopeName
&& (ScopeName
->isStr("gnu") || ScopeName
->isStr("__gnu__"));
117 bool AttributeCommonInfo::isClangScope() const {
118 return ScopeName
&& (ScopeName
->isStr("clang") || ScopeName
->isStr("_Clang"));
121 #include "clang/Sema/AttrParsedAttrKinds.inc"
123 static SmallString
<64> normalizeName(const IdentifierInfo
*Name
,
124 const IdentifierInfo
*Scope
,
125 AttributeCommonInfo::Syntax SyntaxUsed
) {
126 StringRef ScopeName
= normalizeAttrScopeName(Scope
, SyntaxUsed
);
127 StringRef AttrName
= normalizeAttrName(Name
, ScopeName
, SyntaxUsed
);
129 SmallString
<64> FullName
= ScopeName
;
130 if (!ScopeName
.empty()) {
131 assert(SyntaxUsed
== AttributeCommonInfo::AS_CXX11
||
132 SyntaxUsed
== AttributeCommonInfo::AS_C23
);
135 FullName
+= AttrName
;
140 AttributeCommonInfo::Kind
141 AttributeCommonInfo::getParsedKind(const IdentifierInfo
*Name
,
142 const IdentifierInfo
*ScopeName
,
144 return ::getAttrKind(normalizeName(Name
, ScopeName
, SyntaxUsed
), SyntaxUsed
);
147 std::string
AttributeCommonInfo::getNormalizedFullName() const {
148 return static_cast<std::string
>(
149 normalizeName(getAttrName(), getScopeName(), getSyntax()));
152 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
153 // Both variables will be used in tablegen generated
154 // attribute spell list index matching code.
155 auto Syntax
= static_cast<AttributeCommonInfo::Syntax
>(getSyntax());
156 StringRef Scope
= normalizeAttrScopeName(getScopeName(), Syntax
);
157 StringRef Name
= normalizeAttrName(getAttrName(), Scope
, Syntax
);
159 #include "clang/Sema/AttrSpellingListIndex.inc"