clang  16.0.0git
Attributes.cpp
Go to the documentation of this file.
5 using namespace clang;
6 
9  const TargetInfo &Target, const LangOptions &LangOpts) {
10  StringRef Name = Attr->getName();
11  // Normalize the attribute name, __foo__ becomes foo.
12  if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
13  Name = Name.substr(2, Name.size() - 4);
14 
15  // Normalize the scope name, but only for gnu and clang attributes.
16  StringRef ScopeName = Scope ? Scope->getName() : "";
17  if (ScopeName == "__gnu__")
18  ScopeName = "gnu";
19  else if (ScopeName == "_Clang")
20  ScopeName = "clang";
21 
22  // As a special case, look for the omp::sequence and omp::directive
23  // attributes. We support those, but not through the typical attribute
24  // machinery that goes through TableGen. We support this in all OpenMP modes
25  // so long as double square brackets are enabled.
26  if (LangOpts.OpenMP && LangOpts.DoubleSquareBracketAttributes &&
27  ScopeName == "omp")
28  return (Name == "directive" || Name == "sequence") ? 1 : 0;
29 
30 #include "clang/Basic/AttrHasAttributeImpl.inc"
31 
32  return 0;
33 }
34 
36  switch (Rule) {
37 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
38  case attr::NAME: \
39  return SPELLING;
40 #include "clang/Basic/AttrSubMatchRulesList.inc"
41  }
42  llvm_unreachable("Invalid subject match rule");
43 }
44 
45 static StringRef
47  AttributeCommonInfo::Syntax SyntaxUsed) {
48  if (!Scope)
49  return "";
50 
51  // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
52  // to be "clang".
53  StringRef ScopeName = Scope->getName();
54  if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
55  SyntaxUsed == AttributeCommonInfo::AS_C2x) {
56  if (ScopeName == "__gnu__")
57  ScopeName = "gnu";
58  else if (ScopeName == "_Clang")
59  ScopeName = "clang";
60  }
61  return ScopeName;
62 }
63 
64 static StringRef normalizeAttrName(const IdentifierInfo *Name,
65  StringRef NormalizedScopeName,
66  AttributeCommonInfo::Syntax SyntaxUsed) {
67  // Normalize the attribute name, __foo__ becomes foo. This is only allowable
68  // for GNU attributes, and attributes using the double square bracket syntax.
69  bool ShouldNormalize =
70  SyntaxUsed == AttributeCommonInfo::AS_GNU ||
71  ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
72  SyntaxUsed == AttributeCommonInfo::AS_C2x) &&
73  (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" ||
74  NormalizedScopeName == "clang"));
75  StringRef AttrName = Name->getName();
76  if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
77  AttrName.endswith("__"))
78  AttrName = AttrName.slice(2, AttrName.size() - 2);
79 
80  return AttrName;
81 }
82 
84  return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"));
85 }
86 
88  return ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"));
89 }
90 
91 #include "clang/Sema/AttrParsedAttrKinds.inc"
92 
94  const IdentifierInfo *Scope,
95  AttributeCommonInfo::Syntax SyntaxUsed) {
96  StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed);
97  StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed);
98 
99  SmallString<64> FullName = ScopeName;
100  if (!ScopeName.empty()) {
101  assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
102  SyntaxUsed == AttributeCommonInfo::AS_C2x);
103  FullName += "::";
104  }
105  FullName += AttrName;
106 
107  return FullName;
108 }
109 
112  const IdentifierInfo *ScopeName,
113  Syntax SyntaxUsed) {
114  return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
115 }
116 
118  return static_cast<std::string>(
120 }
121 
122 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
123  // Both variables will be used in tablegen generated
124  // attribute spell list index matching code.
125  auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
127  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
128 
129 #include "clang/Sema/AttrSpellingListIndex.inc"
130 }
clang::AttributeCommonInfo::getParsedKind
Kind getParsedKind() const
Definition: AttributeCommonInfo.h:129
normalizeName
static SmallString< 64 > normalizeName(const IdentifierInfo *Name, const IdentifierInfo *Scope, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:93
clang::AttributeCommonInfo::Kind
Kind
Definition: AttributeCommonInfo.h:55
normalizeAttrScopeName
static StringRef normalizeAttrScopeName(const IdentifierInfo *Scope, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:46
AttributeCommonInfo.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::TargetInfo
Exposes information about the current target.
Definition: TargetInfo.h:205
clang::AttributeCommonInfo::AS_CXX11
@ AS_CXX11
[[...]]
Definition: AttributeCommonInfo.h:30
clang::AttributeCommonInfo::getAttrName
const IdentifierInfo * getAttrName() const
Definition: AttributeCommonInfo.h:131
clang::AttributeCommonInfo::Syntax
Syntax
The style used to specify an attribute.
Definition: AttributeCommonInfo.h:25
clang::AttributeCommonInfo::AS_GNU
@ AS_GNU
attribute((...))
Definition: AttributeCommonInfo.h:27
clang::attr::SubjectMatchRule
SubjectMatchRule
A list of all the recognized kinds of attributes.
Definition: AttrSubjectMatchRules.h:19
clang::IdentifierInfo::isStr
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
Definition: IdentifierTable.h:177
clang::Scope
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
clang::AttributeCommonInfo::getSyntax
Syntax getSyntax() const
Definition: AttributeCommonInfo.h:130
IdentifierTable.h
clang::attr::getSubjectMatchRuleSpelling
const char * getSubjectMatchRuleSpelling(SubjectMatchRule Rule)
Definition: Attributes.cpp:35
llvm::SmallString
Definition: LLVM.h:37
AttrSubjectMatchRules.h
clang::AttributeCommonInfo::AS_C2x
@ AS_C2x
[[...]]
Definition: AttributeCommonInfo.h:33
clang::hasAttribute
int hasAttribute(AttributeCommonInfo::Syntax Syntax, const IdentifierInfo *Scope, const IdentifierInfo *Attr, const TargetInfo &Target, const LangOptions &LangOpts)
Return the version number associated with the attribute if we recognize and implement the attribute s...
Definition: Attributes.cpp:7
clang::AttributeCommonInfo::getScopeName
const IdentifierInfo * getScopeName() const
Definition: AttributeCommonInfo.h:137
clang::AttributeCommonInfo::getNormalizedFullName
std::string getNormalizedFullName() const
Gets the normalized full name, which consists of both scope and name and with surrounding underscores...
Definition: Attributes.cpp:117
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:85
Attributes.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:81
clang::AttributeCommonInfo::isClangScope
bool isClangScope() const
Definition: Attributes.cpp:87
clang
Definition: CalledOnceCheck.h:17
clang::Attr
Attr - This represents one attribute.
Definition: Attr.h:40
normalizeAttrName
static StringRef normalizeAttrName(const IdentifierInfo *Name, StringRef NormalizedScopeName, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:64
clang::AttributeCommonInfo::isGNUScope
bool isGNUScope() const
Definition: Attributes.cpp:83