clang  15.0.0git
Attributes.cpp
Go to the documentation of this file.
5 #include "llvm/ADT/StringSwitch.h"
6 using namespace clang;
7 
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);
15 
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";
22 
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;
30 
31 #include "clang/Basic/AttrHasAttributeImpl.inc"
32 
33  return 0;
34 }
35 
37  switch (Rule) {
38 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
39  case attr::NAME: \
40  return SPELLING;
41 #include "clang/Basic/AttrSubMatchRulesList.inc"
42  }
43  llvm_unreachable("Invalid subject match rule");
44 }
45 
46 static StringRef
48  AttributeCommonInfo::Syntax SyntaxUsed) {
49  if (!Scope)
50  return "";
51 
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";
61  }
62  return ScopeName;
63 }
64 
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);
80 
81  return AttrName;
82 }
83 
85  return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"));
86 }
87 
89  return ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"));
90 }
91 
92 #include "clang/Sema/AttrParsedAttrKinds.inc"
93 
95  const IdentifierInfo *Scope,
96  AttributeCommonInfo::Syntax SyntaxUsed) {
97  StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed);
98  StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed);
99 
100  SmallString<64> FullName = ScopeName;
101  if (!ScopeName.empty()) {
102  assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
103  SyntaxUsed == AttributeCommonInfo::AS_C2x);
104  FullName += "::";
105  }
106  FullName += AttrName;
107 
108  return FullName;
109 }
110 
113  const IdentifierInfo *ScopeName,
114  Syntax SyntaxUsed) {
115  return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
116 }
117 
119  return static_cast<std::string>(
121 }
122 
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());
128  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
129 
130 #include "clang/Sema/AttrSpellingListIndex.inc"
131 }
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:94
clang::AttributeCommonInfo::Kind
Kind
Definition: AttributeCommonInfo.h:55
normalizeAttrScopeName
static StringRef normalizeAttrScopeName(const IdentifierInfo *Scope, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:47
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:190
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:176
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:36
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:8
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:118
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
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:78
clang::AttributeCommonInfo::isClangScope
bool isClangScope() const
Definition: Attributes.cpp:88
clang
Definition: CalledOnceCheck.h:17
clang::Attr
Attr - This represents one attribute.
Definition: Attr.h:41
normalizeAttrName
static StringRef normalizeAttrName(const IdentifierInfo *Name, StringRef NormalizedScopeName, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:65
clang::AttributeCommonInfo::isGNUScope
bool isGNUScope() const
Definition: Attributes.cpp:84