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/TargetInfo.h" 19 20 using namespace clang; 21 22 static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name, 23 StringRef ScopeName, const TargetInfo &Target, 24 const LangOptions &LangOpts) { 25 26 #include "clang/Basic/AttrHasAttributeImpl.inc" 27 28 return 0; 29 } 30 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.starts_with("__") && Name.ends_with("__")) 37 Name = Name.substr(2, Name.size() - 4); 38 39 // Normalize the scope name, but only for gnu and clang attributes. 40 StringRef ScopeName = Scope ? Scope->getName() : ""; 41 if (ScopeName == "__gnu__") 42 ScopeName = "gnu"; 43 else if (ScopeName == "_Clang") 44 ScopeName = "clang"; 45 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; 52 53 int res = hasAttributeImpl(Syntax, Name, ScopeName, Target, LangOpts); 54 if (res) 55 return res; 56 57 // Check if any plugin provides this attribute. 58 for (auto &Ptr : getAttributePluginInstances()) 59 if (Ptr->hasSpelling(Syntax, Name)) 60 return 1; 61 62 return 0; 63 } 64 65 const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule) { 66 switch (Rule) { 67 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \ 68 case attr::NAME: \ 69 return SPELLING; 70 #include "clang/Basic/AttrSubMatchRulesList.inc" 71 } 72 llvm_unreachable("Invalid subject match rule"); 73 } 74 75 static StringRef 76 normalizeAttrScopeName(const IdentifierInfo *Scope, 77 AttributeCommonInfo::Syntax SyntaxUsed) { 78 if (!Scope) 79 return ""; 80 81 // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name 82 // to be "clang". 83 StringRef ScopeName = Scope->getName(); 84 if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 || 85 SyntaxUsed == AttributeCommonInfo::AS_C23) { 86 if (ScopeName == "__gnu__") 87 ScopeName = "gnu"; 88 else if (ScopeName == "_Clang") 89 ScopeName = "clang"; 90 } 91 return ScopeName; 92 } 93 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.starts_with("__") && 107 AttrName.ends_with("__")) 108 AttrName = AttrName.slice(2, AttrName.size() - 2); 109 110 return AttrName; 111 } 112 113 bool AttributeCommonInfo::isGNUScope() const { 114 return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__")); 115 } 116 117 bool AttributeCommonInfo::isClangScope() const { 118 return ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang")); 119 } 120 121 #include "clang/Sema/AttrParsedAttrKinds.inc" 122 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); 128 129 SmallString<64> FullName = ScopeName; 130 if (!ScopeName.empty()) { 131 assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 || 132 SyntaxUsed == AttributeCommonInfo::AS_C23); 133 FullName += "::"; 134 } 135 FullName += AttrName; 136 137 return FullName; 138 } 139 140 AttributeCommonInfo::Kind 141 AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name, 142 const IdentifierInfo *ScopeName, 143 Syntax SyntaxUsed) { 144 return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed); 145 } 146 147 std::string AttributeCommonInfo::getNormalizedFullName() const { 148 return static_cast<std::string>( 149 normalizeName(getAttrName(), getScopeName(), getSyntax())); 150 } 151 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); 158 159 #include "clang/Sema/AttrSpellingListIndex.inc" 160 } 161