clang 17.0.0git
Attributes.cpp
Go to the documentation of this file.
6using namespace clang;
7
8static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name,
9 StringRef ScopeName, const TargetInfo &Target,
10 const LangOptions &LangOpts) {
11
12#include "clang/Basic/AttrHasAttributeImpl.inc"
13
14 return 0;
15}
16
19 const TargetInfo &Target, const LangOptions &LangOpts) {
20 StringRef Name = Attr->getName();
21 // Normalize the attribute name, __foo__ becomes foo.
22 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
23 Name = Name.substr(2, Name.size() - 4);
24
25 // Normalize the scope name, but only for gnu and clang attributes.
26 StringRef ScopeName = Scope ? Scope->getName() : "";
27 if (ScopeName == "__gnu__")
28 ScopeName = "gnu";
29 else if (ScopeName == "_Clang")
30 ScopeName = "clang";
31
32 // As a special case, look for the omp::sequence and omp::directive
33 // attributes. We support those, but not through the typical attribute
34 // machinery that goes through TableGen. We support this in all OpenMP modes
35 // so long as double square brackets are enabled.
36 if (LangOpts.OpenMP && LangOpts.DoubleSquareBracketAttributes &&
37 ScopeName == "omp")
38 return (Name == "directive" || Name == "sequence") ? 1 : 0;
39
40 int res = hasAttributeImpl(Syntax, Name, ScopeName, Target, LangOpts);
41 if (res)
42 return res;
43
44 // Check if any plugin provides this attribute.
45 for (auto &Ptr : getAttributePluginInstances())
46 if (Ptr->hasSpelling(Syntax, Name))
47 return 1;
48
49 return 0;
50}
51
53 switch (Rule) {
54#define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
55 case attr::NAME: \
56 return SPELLING;
57#include "clang/Basic/AttrSubMatchRulesList.inc"
58 }
59 llvm_unreachable("Invalid subject match rule");
60}
61
62static StringRef
64 AttributeCommonInfo::Syntax SyntaxUsed) {
65 if (!Scope)
66 return "";
67
68 // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
69 // to be "clang".
70 StringRef ScopeName = Scope->getName();
71 if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
72 SyntaxUsed == AttributeCommonInfo::AS_C2x) {
73 if (ScopeName == "__gnu__")
74 ScopeName = "gnu";
75 else if (ScopeName == "_Clang")
76 ScopeName = "clang";
77 }
78 return ScopeName;
79}
80
81static StringRef normalizeAttrName(const IdentifierInfo *Name,
82 StringRef NormalizedScopeName,
83 AttributeCommonInfo::Syntax SyntaxUsed) {
84 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
85 // for GNU attributes, and attributes using the double square bracket syntax.
86 bool ShouldNormalize =
87 SyntaxUsed == AttributeCommonInfo::AS_GNU ||
88 ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
89 SyntaxUsed == AttributeCommonInfo::AS_C2x) &&
90 (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" ||
91 NormalizedScopeName == "clang"));
92 StringRef AttrName = Name->getName();
93 if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
94 AttrName.endswith("__"))
95 AttrName = AttrName.slice(2, AttrName.size() - 2);
96
97 return AttrName;
98}
99
101 return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"));
102}
103
105 return ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"));
106}
107
108#include "clang/Sema/AttrParsedAttrKinds.inc"
109
111 const IdentifierInfo *Scope,
112 AttributeCommonInfo::Syntax SyntaxUsed) {
113 StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed);
114 StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed);
115
116 SmallString<64> FullName = ScopeName;
117 if (!ScopeName.empty()) {
118 assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
119 SyntaxUsed == AttributeCommonInfo::AS_C2x);
120 FullName += "::";
121 }
122 FullName += AttrName;
123
124 return FullName;
125}
126
129 const IdentifierInfo *ScopeName,
130 Syntax SyntaxUsed) {
131 return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
132}
133
135 return static_cast<std::string>(
137}
138
139unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
140 // Both variables will be used in tablegen generated
141 // attribute spell list index matching code.
142 auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
144 StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
145
146#include "clang/Sema/AttrSpellingListIndex.inc"
147}
static SmallString< 64 > normalizeName(const IdentifierInfo *Name, const IdentifierInfo *Scope, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:110
static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name, StringRef ScopeName, const TargetInfo &Target, const LangOptions &LangOpts)
Definition: Attributes.cpp:8
static StringRef normalizeAttrScopeName(const IdentifierInfo *Scope, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:63
static StringRef normalizeAttrName(const IdentifierInfo *Name, StringRef NormalizedScopeName, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:81
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Attr - This represents one attribute.
Definition: Attr.h:40
std::string getNormalizedFullName() const
Gets the normalized full name, which consists of both scope and name and with surrounding underscores...
Definition: Attributes.cpp:134
Syntax
The style used to specify an attribute.
const IdentifierInfo * getScopeName() const
const IdentifierInfo * getAttrName() const
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Exposes information about the current target.
Definition: TargetInfo.h:206
SubjectMatchRule
A list of all the recognized kinds of attributes.
const char * getSubjectMatchRuleSpelling(SubjectMatchRule Rule)
Definition: Attributes.cpp:52
int hasAttribute(AttributeCommonInfo::Syntax Syntax, const IdentifierInfo *Scope, const IdentifierInfo *Attr, const TargetInfo &Target, const LangOptions &LangOpts)
Return the version number associated with the attribute if we recognize and implement the attribute s...
Definition: Attributes.cpp:17
const std::list< std::unique_ptr< ParsedAttrInfo > > & getAttributePluginInstances()