[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / lib / Basic / Attributes.cpp
blob960c9773d192a5a0ad95f7425a6d6038b2ada59e
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"
6 using namespace clang;
8 int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax,
9 const IdentifierInfo *Scope, const IdentifierInfo *Attr,
10 const TargetInfo &Target, 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__")
19 ScopeName = "gnu";
20 else if (ScopeName == "_Clang")
21 ScopeName = "clang";
23 // As a special case, look for the omp::sequence and omp::directive
24 // attributes. We support those, but not through the typical attribute
25 // machinery that goes through TableGen. We support this in all OpenMP modes
26 // so long as double square brackets are enabled.
27 if (LangOpts.OpenMP && LangOpts.DoubleSquareBracketAttributes &&
28 ScopeName == "omp")
29 return (Name == "directive" || Name == "sequence") ? 1 : 0;
31 #include "clang/Basic/AttrHasAttributeImpl.inc"
33 return 0;
36 const char *attr::getSubjectMatchRuleSpelling(attr::SubjectMatchRule Rule) {
37 switch (Rule) {
38 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
39 case attr::NAME: \
40 return SPELLING;
41 #include "clang/Basic/AttrSubMatchRulesList.inc"
43 llvm_unreachable("Invalid subject match rule");
46 static StringRef
47 normalizeAttrScopeName(const IdentifierInfo *Scope,
48 AttributeCommonInfo::Syntax SyntaxUsed) {
49 if (!Scope)
50 return "";
52 // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
53 // to be "clang".
54 StringRef ScopeName = Scope->getName();
55 if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
56 SyntaxUsed == AttributeCommonInfo::AS_C2x) {
57 if (ScopeName == "__gnu__")
58 ScopeName = "gnu";
59 else if (ScopeName == "_Clang")
60 ScopeName = "clang";
62 return ScopeName;
65 static StringRef normalizeAttrName(const IdentifierInfo *Name,
66 StringRef NormalizedScopeName,
67 AttributeCommonInfo::Syntax SyntaxUsed) {
68 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
69 // for GNU attributes, and attributes using the double square bracket syntax.
70 bool ShouldNormalize =
71 SyntaxUsed == AttributeCommonInfo::AS_GNU ||
72 ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
73 SyntaxUsed == AttributeCommonInfo::AS_C2x) &&
74 (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" ||
75 NormalizedScopeName == "clang"));
76 StringRef AttrName = Name->getName();
77 if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
78 AttrName.endswith("__"))
79 AttrName = AttrName.slice(2, AttrName.size() - 2);
81 return AttrName;
84 bool AttributeCommonInfo::isGNUScope() const {
85 return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"));
88 bool AttributeCommonInfo::isClangScope() const {
89 return ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"));
92 #include "clang/Sema/AttrParsedAttrKinds.inc"
94 static SmallString<64> normalizeName(const IdentifierInfo *Name,
95 const IdentifierInfo *Scope,
96 AttributeCommonInfo::Syntax SyntaxUsed) {
97 StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed);
98 StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed);
100 SmallString<64> FullName = ScopeName;
101 if (!ScopeName.empty()) {
102 assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
103 SyntaxUsed == AttributeCommonInfo::AS_C2x);
104 FullName += "::";
106 FullName += AttrName;
108 return FullName;
111 AttributeCommonInfo::Kind
112 AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name,
113 const IdentifierInfo *ScopeName,
114 Syntax SyntaxUsed) {
115 return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
118 std::string AttributeCommonInfo::getNormalizedFullName() const {
119 return static_cast<std::string>(
120 normalizeName(getAttrName(), getScopeName(), getSyntax()));
123 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
124 // Both variables will be used in tablegen generated
125 // attribute spell list index matching code.
126 auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
127 StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax);
128 StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
130 #include "clang/Sema/AttrSpellingListIndex.inc"