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 #include "llvm/ADT/StringMap.h" 21 #include "llvm/ADT/StringSwitch.h" 22 23 using namespace clang; 24 25 static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name, 26 StringRef ScopeName, const TargetInfo &Target, 27 const LangOptions &LangOpts) { 28 29 #include "clang/Basic/AttrHasAttributeImpl.inc" 30 31 return 0; 32 } 33 34 int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax, 35 const IdentifierInfo *Scope, const IdentifierInfo *Attr, 36 const TargetInfo &Target, const LangOptions &LangOpts, 37 bool CheckPlugins) { 38 StringRef Name = Attr->getName(); 39 // Normalize the attribute name, __foo__ becomes foo. 40 if (Name.size() >= 4 && Name.starts_with("__") && Name.ends_with("__")) 41 Name = Name.substr(2, Name.size() - 4); 42 43 // Normalize the scope name, but only for gnu and clang attributes. 44 StringRef ScopeName = Scope ? Scope->getName() : ""; 45 if (ScopeName == "__gnu__") 46 ScopeName = "gnu"; 47 else if (ScopeName == "_Clang") 48 ScopeName = "clang"; 49 50 // As a special case, look for the omp::sequence and omp::directive 51 // attributes. We support those, but not through the typical attribute 52 // machinery that goes through TableGen. We support this in all OpenMP modes 53 // so long as double square brackets are enabled. 54 // 55 // Other OpenMP attributes (e.g. [[omp::assume]]) are handled via the 56 // regular attribute parsing machinery. 57 if (LangOpts.OpenMP && ScopeName == "omp" && 58 (Name == "directive" || Name == "sequence")) 59 return 1; 60 61 int res = hasAttributeImpl(Syntax, Name, ScopeName, Target, LangOpts); 62 if (res) 63 return res; 64 65 if (CheckPlugins) { 66 // Check if any plugin provides this attribute. 67 for (auto &Ptr : getAttributePluginInstances()) 68 if (Ptr->hasSpelling(Syntax, Name)) 69 return 1; 70 } 71 72 return 0; 73 } 74 75 int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax, 76 const IdentifierInfo *Scope, const IdentifierInfo *Attr, 77 const TargetInfo &Target, const LangOptions &LangOpts) { 78 return hasAttribute(Syntax, Scope, Attr, Target, LangOpts, 79 /*CheckPlugins=*/true); 80 } 81 82 const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule) { 83 switch (Rule) { 84 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \ 85 case attr::NAME: \ 86 return SPELLING; 87 #include "clang/Basic/AttrSubMatchRulesList.inc" 88 } 89 llvm_unreachable("Invalid subject match rule"); 90 } 91 92 static StringRef 93 normalizeAttrScopeName(const IdentifierInfo *Scope, 94 AttributeCommonInfo::Syntax SyntaxUsed) { 95 if (!Scope) 96 return ""; 97 98 // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name 99 // to be "clang". 100 StringRef ScopeName = Scope->getName(); 101 if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 || 102 SyntaxUsed == AttributeCommonInfo::AS_C23) { 103 if (ScopeName == "__gnu__") 104 ScopeName = "gnu"; 105 else if (ScopeName == "_Clang") 106 ScopeName = "clang"; 107 } 108 return ScopeName; 109 } 110 111 static StringRef normalizeAttrName(const IdentifierInfo *Name, 112 StringRef NormalizedScopeName, 113 AttributeCommonInfo::Syntax SyntaxUsed) { 114 // Normalize the attribute name, __foo__ becomes foo. This is only allowable 115 // for GNU attributes, and attributes using the double square bracket syntax. 116 bool ShouldNormalize = 117 SyntaxUsed == AttributeCommonInfo::AS_GNU || 118 ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 || 119 SyntaxUsed == AttributeCommonInfo::AS_C23) && 120 (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" || 121 NormalizedScopeName == "clang")); 122 StringRef AttrName = Name->getName(); 123 if (ShouldNormalize && AttrName.size() >= 4 && AttrName.starts_with("__") && 124 AttrName.ends_with("__")) 125 AttrName = AttrName.slice(2, AttrName.size() - 2); 126 127 return AttrName; 128 } 129 130 bool AttributeCommonInfo::isGNUScope() const { 131 return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__")); 132 } 133 134 bool AttributeCommonInfo::isClangScope() const { 135 return ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang")); 136 } 137 138 #include "clang/Sema/AttrParsedAttrKinds.inc" 139 140 static SmallString<64> normalizeName(const IdentifierInfo *Name, 141 const IdentifierInfo *Scope, 142 AttributeCommonInfo::Syntax SyntaxUsed) { 143 StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed); 144 StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed); 145 146 SmallString<64> FullName = ScopeName; 147 if (!ScopeName.empty()) { 148 assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 || 149 SyntaxUsed == AttributeCommonInfo::AS_C23); 150 FullName += "::"; 151 } 152 FullName += AttrName; 153 154 return FullName; 155 } 156 157 AttributeCommonInfo::Kind 158 AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name, 159 const IdentifierInfo *ScopeName, 160 Syntax SyntaxUsed) { 161 return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed); 162 } 163 164 AttributeCommonInfo::AttrArgsInfo 165 AttributeCommonInfo::getCXX11AttrArgsInfo(const IdentifierInfo *Name) { 166 StringRef AttrName = 167 normalizeAttrName(Name, /*NormalizedScopeName*/ "", Syntax::AS_CXX11); 168 #define CXX11_ATTR_ARGS_INFO 169 return llvm::StringSwitch<AttributeCommonInfo::AttrArgsInfo>(AttrName) 170 #include "clang/Basic/CXX11AttributeInfo.inc" 171 .Default(AttributeCommonInfo::AttrArgsInfo::None); 172 #undef CXX11_ATTR_ARGS_INFO 173 } 174 175 std::string AttributeCommonInfo::getNormalizedFullName() const { 176 return static_cast<std::string>( 177 normalizeName(getAttrName(), getScopeName(), getSyntax())); 178 } 179 180 static AttributeCommonInfo::Scope 181 getScopeFromNormalizedScopeName(StringRef ScopeName) { 182 return llvm::StringSwitch<AttributeCommonInfo::Scope>(ScopeName) 183 .Case("", AttributeCommonInfo::Scope::NONE) 184 .Case("clang", AttributeCommonInfo::Scope::CLANG) 185 .Case("gnu", AttributeCommonInfo::Scope::GNU) 186 .Case("gsl", AttributeCommonInfo::Scope::GSL) 187 .Case("hlsl", AttributeCommonInfo::Scope::HLSL) 188 .Case("msvc", AttributeCommonInfo::Scope::MSVC) 189 .Case("omp", AttributeCommonInfo::Scope::OMP) 190 .Case("riscv", AttributeCommonInfo::Scope::RISCV); 191 } 192 193 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const { 194 // Both variables will be used in tablegen generated 195 // attribute spell list index matching code. 196 auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax()); 197 StringRef ScopeName = normalizeAttrScopeName(getScopeName(), Syntax); 198 StringRef Name = normalizeAttrName(getAttrName(), ScopeName, Syntax); 199 200 AttributeCommonInfo::Scope ComputedScope = 201 getScopeFromNormalizedScopeName(ScopeName); 202 203 #include "clang/Sema/AttrSpellingListIndex.inc" 204 } 205