1 #include "clang/Basic/Attributes.h"
2 #include "clang/Basic/AttrSubjectMatchRules.h"
3 #include "clang/Basic/AttributeCommonInfo.h"
4 #include "clang/Basic/IdentifierTable.h"
5 #include "llvm/ADT/StringSwitch.h"
8 int clang::hasAttribute(AttrSyntax Syntax, const IdentifierInfo *Scope,
9 const IdentifierInfo *Attr, const TargetInfo &Target,
10 const LangOptions &LangOpts) {
11 StringRef Name = Attr->getName();
12 // Normalize the attribute name, __foo__ becomes foo.
13 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
14 Name = Name.substr(2, Name.size() - 4);
16 // Normalize the scope name, but only for gnu and clang attributes.
17 StringRef ScopeName = Scope ? Scope->getName() : "";
18 if (ScopeName == "__gnu__")
20 else if (ScopeName == "_Clang")
23 #include "clang/Basic/AttrHasAttributeImpl.inc"
28 const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule) {
30 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
33 #include "clang/Basic/AttrSubMatchRulesList.inc"
35 llvm_unreachable("Invalid subject match rule");
39 normalizeAttrScopeName(const IdentifierInfo *Scope,
40 AttributeCommonInfo::Syntax SyntaxUsed) {
44 // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
46 StringRef ScopeName = Scope->getName();
47 if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
48 SyntaxUsed == AttributeCommonInfo::AS_C2x) {
49 if (ScopeName == "__gnu__")
51 else if (ScopeName == "_Clang")
57 static StringRef normalizeAttrName(const IdentifierInfo *Name,
58 StringRef NormalizedScopeName,
59 AttributeCommonInfo::Syntax SyntaxUsed) {
60 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
61 // for GNU attributes, and attributes using the double square bracket syntax.
62 bool ShouldNormalize =
63 SyntaxUsed == AttributeCommonInfo::AS_GNU ||
64 ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
65 SyntaxUsed == AttributeCommonInfo::AS_C2x) &&
66 (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" ||
67 NormalizedScopeName == "clang"));
68 StringRef AttrName = Name->getName();
69 if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
70 AttrName.endswith("__"))
71 AttrName = AttrName.slice(2, AttrName.size() - 2);
76 bool AttributeCommonInfo::isGNUScope() const {
77 return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"));
80 #include "clang/Sema/AttrParsedAttrKinds.inc"
82 static SmallString<64> normalizeName(const IdentifierInfo *Name,
83 const IdentifierInfo *Scope,
84 AttributeCommonInfo::Syntax SyntaxUsed) {
85 StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed);
86 StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed);
88 SmallString<64> FullName = ScopeName;
89 if (!ScopeName.empty()) {
90 assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
91 SyntaxUsed == AttributeCommonInfo::AS_C2x);
99 AttributeCommonInfo::Kind
100 AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name,
101 const IdentifierInfo *ScopeName,
103 return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
106 std::string AttributeCommonInfo::getNormalizedFullName() const {
107 return static_cast<std::string>(
108 normalizeName(getAttrName(), getScopeName(), getSyntax()));
111 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
112 // Both variables will be used in tablegen generated
113 // attribute spell list index matching code.
114 auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
115 StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax);
116 StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
118 #include "clang/Sema/AttrSpellingListIndex.inc"