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