[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / Basic / Attributes.cpp
blobbb495216ca93ca587d776e1775ff12982a5ebbc0
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.
11 //===----------------------------------------------------------------------===//
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"
20 using namespace clang;
22 static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name,
23 StringRef ScopeName, const TargetInfo &Target,
24 const LangOptions &LangOpts) {
26 #include "clang/Basic/AttrHasAttributeImpl.inc"
28 return 0;
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.startswith("__") && Name.endswith("__"))
37 Name = Name.substr(2, Name.size() - 4);
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";
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;
53 int res = hasAttributeImpl(Syntax, Name, ScopeName, Target, LangOpts);
54 if (res)
55 return res;
57 // Check if any plugin provides this attribute.
58 for (auto &Ptr : getAttributePluginInstances())
59 if (Ptr->hasSpelling(Syntax, Name))
60 return 1;
62 return 0;
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"
72 llvm_unreachable("Invalid subject match rule");
75 static StringRef
76 normalizeAttrScopeName(const IdentifierInfo *Scope,
77 AttributeCommonInfo::Syntax SyntaxUsed) {
78 if (!Scope)
79 return "";
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";
91 return ScopeName;
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.startswith("__") &&
107 AttrName.endswith("__"))
108 AttrName = AttrName.slice(2, AttrName.size() - 2);
110 return AttrName;
113 bool AttributeCommonInfo::isGNUScope() const {
114 return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"));
117 bool AttributeCommonInfo::isClangScope() const {
118 return ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"));
121 #include "clang/Sema/AttrParsedAttrKinds.inc"
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);
129 SmallString<64> FullName = ScopeName;
130 if (!ScopeName.empty()) {
131 assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
132 SyntaxUsed == AttributeCommonInfo::AS_C23);
133 FullName += "::";
135 FullName += AttrName;
137 return FullName;
140 AttributeCommonInfo::Kind
141 AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name,
142 const IdentifierInfo *ScopeName,
143 Syntax SyntaxUsed) {
144 return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
147 std::string AttributeCommonInfo::getNormalizedFullName() const {
148 return static_cast<std::string>(
149 normalizeName(getAttrName(), getScopeName(), getSyntax()));
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);
159 #include "clang/Sema/AttrSpellingListIndex.inc"