| 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/SimpleTypoCorrection.h" |
| 19 | #include "clang/Basic/TargetInfo.h" |
| 20 | |
| 21 | #include "llvm/ADT/StringSwitch.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static StringRef canonicalizeScopeName(StringRef Name) { |
| 26 | // Normalize the scope name, but only for gnu and clang attributes. |
| 27 | if (Name == "__gnu__" ) |
| 28 | return "gnu" ; |
| 29 | |
| 30 | if (Name == "_Clang" ) |
| 31 | return "clang" ; |
| 32 | |
| 33 | return Name; |
| 34 | } |
| 35 | |
| 36 | static StringRef canonicalizeAttrName(StringRef Name) { |
| 37 | // Normalize the attribute name, __foo__ becomes foo. |
| 38 | if (Name.size() >= 4 && Name.starts_with(Prefix: "__" ) && Name.ends_with(Suffix: "__" )) |
| 39 | return Name.substr(Start: 2, N: Name.size() - 4); |
| 40 | |
| 41 | return Name; |
| 42 | } |
| 43 | |
| 44 | static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name, |
| 45 | StringRef ScopeName, const TargetInfo &Target, |
| 46 | const LangOptions &LangOpts) { |
| 47 | #include "clang/Basic/AttrHasAttributeImpl.inc" |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax, StringRef ScopeName, |
| 52 | StringRef Name, const TargetInfo &Target, |
| 53 | const LangOptions &LangOpts, bool CheckPlugins) { |
| 54 | ScopeName = canonicalizeScopeName(Name: ScopeName); |
| 55 | Name = canonicalizeAttrName(Name); |
| 56 | |
| 57 | // As a special case, look for the omp::sequence and omp::directive |
| 58 | // attributes. We support those, but not through the typical attribute |
| 59 | // machinery that goes through TableGen. We support this in all OpenMP modes |
| 60 | // so long as double square brackets are enabled. |
| 61 | // |
| 62 | // Other OpenMP attributes (e.g. [[omp::assume]]) are handled via the |
| 63 | // regular attribute parsing machinery. |
| 64 | if (LangOpts.OpenMP && ScopeName == "omp" && |
| 65 | (Name == "directive" || Name == "sequence" )) |
| 66 | return 1; |
| 67 | |
| 68 | int res = hasAttributeImpl(Syntax, Name, ScopeName, Target, LangOpts); |
| 69 | if (res) |
| 70 | return res; |
| 71 | |
| 72 | if (CheckPlugins) { |
| 73 | // Check if any plugin provides this attribute. |
| 74 | for (auto &Ptr : getAttributePluginInstances()) |
| 75 | if (Ptr->hasSpelling(Syntax, Name)) |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax, |
| 83 | const IdentifierInfo *Scope, const IdentifierInfo *Attr, |
| 84 | const TargetInfo &Target, const LangOptions &LangOpts, |
| 85 | bool CheckPlugins) { |
| 86 | return hasAttribute(Syntax, ScopeName: Scope ? Scope->getName() : "" , Name: Attr->getName(), |
| 87 | Target, LangOpts, CheckPlugins); |
| 88 | } |
| 89 | |
| 90 | int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax, |
| 91 | const IdentifierInfo *Scope, const IdentifierInfo *Attr, |
| 92 | const TargetInfo &Target, const LangOptions &LangOpts) { |
| 93 | return hasAttribute(Syntax, Scope, Attr, Target, LangOpts, |
| 94 | /*CheckPlugins=*/true); |
| 95 | } |
| 96 | |
| 97 | const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule) { |
| 98 | switch (Rule) { |
| 99 | #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \ |
| 100 | case attr::NAME: \ |
| 101 | return SPELLING; |
| 102 | #include "clang/Basic/AttrSubMatchRulesList.inc" |
| 103 | } |
| 104 | llvm_unreachable("Invalid subject match rule" ); |
| 105 | } |
| 106 | |
| 107 | static StringRef |
| 108 | normalizeAttrScopeName(StringRef ScopeName, |
| 109 | AttributeCommonInfo::Syntax SyntaxUsed) { |
| 110 | if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 || |
| 111 | SyntaxUsed == AttributeCommonInfo::AS_C23) |
| 112 | return canonicalizeScopeName(Name: ScopeName); |
| 113 | |
| 114 | return ScopeName; |
| 115 | } |
| 116 | |
| 117 | static StringRef |
| 118 | normalizeAttrScopeName(const IdentifierInfo *ScopeName, |
| 119 | AttributeCommonInfo::Syntax SyntaxUsed) { |
| 120 | if (ScopeName) |
| 121 | return normalizeAttrScopeName(ScopeName: ScopeName->getName(), SyntaxUsed); |
| 122 | |
| 123 | return "" ; |
| 124 | } |
| 125 | |
| 126 | static StringRef normalizeAttrName(StringRef AttrName, |
| 127 | StringRef NormalizedScopeName, |
| 128 | AttributeCommonInfo::Syntax SyntaxUsed) { |
| 129 | // Normalize the attribute name, __foo__ becomes foo. This is only allowable |
| 130 | // for GNU attributes, and attributes using the double square bracket syntax. |
| 131 | bool ShouldNormalize = |
| 132 | SyntaxUsed == AttributeCommonInfo::AS_GNU || |
| 133 | ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 || |
| 134 | SyntaxUsed == AttributeCommonInfo::AS_C23) && |
| 135 | (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" || |
| 136 | NormalizedScopeName == "clang" )); |
| 137 | |
| 138 | if (ShouldNormalize) |
| 139 | return canonicalizeAttrName(Name: AttrName); |
| 140 | |
| 141 | return AttrName; |
| 142 | } |
| 143 | |
| 144 | bool AttributeCommonInfo::isGNUScope() const { |
| 145 | return ScopeName && (ScopeName->isStr(Str: "gnu" ) || ScopeName->isStr(Str: "__gnu__" )); |
| 146 | } |
| 147 | |
| 148 | bool AttributeCommonInfo::isClangScope() const { |
| 149 | return ScopeName && (ScopeName->isStr(Str: "clang" ) || ScopeName->isStr(Str: "_Clang" )); |
| 150 | } |
| 151 | |
| 152 | #include "clang/Sema/AttrParsedAttrKinds.inc" |
| 153 | |
| 154 | static SmallString<64> normalizeName(StringRef AttrName, StringRef ScopeName, |
| 155 | AttributeCommonInfo::Syntax SyntaxUsed) { |
| 156 | std::string StrAttrName = SyntaxUsed == AttributeCommonInfo::AS_HLSLAnnotation |
| 157 | ? AttrName.lower() |
| 158 | : AttrName.str(); |
| 159 | SmallString<64> FullName = ScopeName; |
| 160 | if (!ScopeName.empty()) { |
| 161 | assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 || |
| 162 | SyntaxUsed == AttributeCommonInfo::AS_C23); |
| 163 | FullName += "::" ; |
| 164 | } |
| 165 | FullName += StrAttrName; |
| 166 | return FullName; |
| 167 | } |
| 168 | |
| 169 | static SmallString<64> normalizeName(const IdentifierInfo *Name, |
| 170 | const IdentifierInfo *Scope, |
| 171 | AttributeCommonInfo::Syntax SyntaxUsed) { |
| 172 | StringRef ScopeName = normalizeAttrScopeName(ScopeName: Scope, SyntaxUsed); |
| 173 | StringRef AttrName = |
| 174 | normalizeAttrName(AttrName: Name->getName(), NormalizedScopeName: ScopeName, SyntaxUsed); |
| 175 | return normalizeName(AttrName, ScopeName, SyntaxUsed); |
| 176 | } |
| 177 | |
| 178 | AttributeCommonInfo::Kind |
| 179 | AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name, |
| 180 | const IdentifierInfo *ScopeName, |
| 181 | Syntax SyntaxUsed) { |
| 182 | return ::getAttrKind(normalizeName(Name, Scope: ScopeName, SyntaxUsed), SyntaxUsed); |
| 183 | } |
| 184 | |
| 185 | AttributeCommonInfo::AttrArgsInfo |
| 186 | AttributeCommonInfo::getCXX11AttrArgsInfo(const IdentifierInfo *Name) { |
| 187 | StringRef AttrName = normalizeAttrName( |
| 188 | AttrName: Name->getName(), /*NormalizedScopeName*/ "" , SyntaxUsed: Syntax::AS_CXX11); |
| 189 | #define CXX11_ATTR_ARGS_INFO |
| 190 | return llvm::StringSwitch<AttributeCommonInfo::AttrArgsInfo>(AttrName) |
| 191 | #include "clang/Basic/CXX11AttributeInfo.inc" |
| 192 | .Default(Value: AttributeCommonInfo::AttrArgsInfo::None); |
| 193 | #undef CXX11_ATTR_ARGS_INFO |
| 194 | } |
| 195 | |
| 196 | std::string AttributeCommonInfo::getNormalizedFullName() const { |
| 197 | return static_cast<std::string>( |
| 198 | normalizeName(Name: getAttrName(), Scope: getScopeName(), SyntaxUsed: getSyntax())); |
| 199 | } |
| 200 | |
| 201 | SourceRange AttributeCommonInfo::getNormalizedRange() const { |
| 202 | return hasScope() ? SourceRange(ScopeLoc, AttrRange.getEnd()) : AttrRange; |
| 203 | } |
| 204 | |
| 205 | static AttributeCommonInfo::Scope |
| 206 | getScopeFromNormalizedScopeName(StringRef ScopeName) { |
| 207 | return llvm::StringSwitch<AttributeCommonInfo::Scope>(ScopeName) |
| 208 | .Case(S: "" , Value: AttributeCommonInfo::Scope::NONE) |
| 209 | .Case(S: "clang" , Value: AttributeCommonInfo::Scope::CLANG) |
| 210 | .Case(S: "gnu" , Value: AttributeCommonInfo::Scope::GNU) |
| 211 | .Case(S: "gsl" , Value: AttributeCommonInfo::Scope::GSL) |
| 212 | .Case(S: "hlsl" , Value: AttributeCommonInfo::Scope::HLSL) |
| 213 | .Case(S: "vk" , Value: AttributeCommonInfo::Scope::VK) |
| 214 | .Case(S: "msvc" , Value: AttributeCommonInfo::Scope::MSVC) |
| 215 | .Case(S: "omp" , Value: AttributeCommonInfo::Scope::OMP) |
| 216 | .Case(S: "riscv" , Value: AttributeCommonInfo::Scope::RISCV); |
| 217 | } |
| 218 | |
| 219 | unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const { |
| 220 | // Both variables will be used in tablegen generated |
| 221 | // attribute spell list index matching code. |
| 222 | auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax()); |
| 223 | StringRef ScopeName = normalizeAttrScopeName(ScopeName: getScopeName(), SyntaxUsed: Syntax); |
| 224 | StringRef Name = |
| 225 | normalizeAttrName(AttrName: getAttrName()->getName(), NormalizedScopeName: ScopeName, SyntaxUsed: Syntax); |
| 226 | AttributeCommonInfo::Scope ComputedScope = |
| 227 | getScopeFromNormalizedScopeName(ScopeName); |
| 228 | |
| 229 | #include "clang/Sema/AttrSpellingListIndex.inc" |
| 230 | } |
| 231 | |
| 232 | #define ATTR_NAME(NAME) NAME, |
| 233 | static constexpr const char *AttrSpellingList[] = { |
| 234 | #include "clang/Basic/AttributeSpellingList.inc" |
| 235 | }; |
| 236 | |
| 237 | #define ATTR_SCOPE_NAME(SCOPE_NAME) SCOPE_NAME, |
| 238 | static constexpr const char *AttrScopeSpellingList[] = { |
| 239 | #include "clang/Basic/AttributeSpellingList.inc" |
| 240 | }; |
| 241 | |
| 242 | std::optional<std::string> |
| 243 | AttributeCommonInfo::getCorrectedFullName(const TargetInfo &Target, |
| 244 | const LangOptions &LangOpts) const { |
| 245 | StringRef ScopeName = normalizeAttrScopeName(ScopeName: getScopeName(), SyntaxUsed: getSyntax()); |
| 246 | if (ScopeName.size() > 0 && |
| 247 | llvm::none_of(Range: AttrScopeSpellingList, |
| 248 | P: [&](const char *S) { return S == ScopeName; })) { |
| 249 | SimpleTypoCorrection STC(ScopeName); |
| 250 | for (const auto &Scope : AttrScopeSpellingList) |
| 251 | STC.add(Candidate: Scope); |
| 252 | |
| 253 | if (auto CorrectedScopeName = STC.getCorrection()) |
| 254 | ScopeName = *CorrectedScopeName; |
| 255 | } |
| 256 | |
| 257 | StringRef AttrName = |
| 258 | normalizeAttrName(AttrName: getAttrName()->getName(), NormalizedScopeName: ScopeName, SyntaxUsed: getSyntax()); |
| 259 | if (llvm::none_of(Range: AttrSpellingList, |
| 260 | P: [&](const char *A) { return A == AttrName; })) { |
| 261 | SimpleTypoCorrection STC(AttrName); |
| 262 | for (const auto &Attr : AttrSpellingList) |
| 263 | STC.add(Candidate: Attr); |
| 264 | |
| 265 | if (auto CorrectedAttrName = STC.getCorrection()) |
| 266 | AttrName = *CorrectedAttrName; |
| 267 | } |
| 268 | |
| 269 | if (hasAttribute(Syntax: getSyntax(), ScopeName, Name: AttrName, Target, LangOpts, |
| 270 | /*CheckPlugins=*/true)) |
| 271 | return static_cast<std::string>( |
| 272 | normalizeName(AttrName, ScopeName, getSyntax())); |
| 273 | |
| 274 | return std::nullopt; |
| 275 | } |
| 276 | |