clang  14.0.0git
Attributes.cpp
Go to the documentation of this file.
5 #include "llvm/ADT/StringSwitch.h"
6 using namespace clang;
7 
9  const IdentifierInfo *Attr, const TargetInfo &Target,
10  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 
88 #include "clang/Sema/AttrParsedAttrKinds.inc"
89 
91  const IdentifierInfo *Scope,
92  AttributeCommonInfo::Syntax SyntaxUsed) {
93  StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed);
94  StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed);
95 
96  SmallString<64> FullName = ScopeName;
97  if (!ScopeName.empty()) {
98  assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
99  SyntaxUsed == AttributeCommonInfo::AS_C2x);
100  FullName += "::";
101  }
102  FullName += AttrName;
103 
104  return FullName;
105 }
106 
109  const IdentifierInfo *ScopeName,
110  Syntax SyntaxUsed) {
111  return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
112 }
113 
115  return static_cast<std::string>(
117 }
118 
119 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
120  // Both variables will be used in tablegen generated
121  // attribute spell list index matching code.
122  auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
124  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
125 
126 #include "clang/Sema/AttrSpellingListIndex.inc"
127 }
clang::AttributeCommonInfo::getParsedKind
Kind getParsedKind() const
Definition: AttributeCommonInfo.h:126
normalizeName
static SmallString< 64 > normalizeName(const IdentifierInfo *Name, const IdentifierInfo *Scope, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:90
clang::AttributeCommonInfo::Kind
Kind
Definition: AttributeCommonInfo.h:52
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:189
clang::AttributeCommonInfo::AS_CXX11
@ AS_CXX11
[[...]]
Definition: AttributeCommonInfo.h:30
clang::AttributeCommonInfo::getAttrName
const IdentifierInfo * getAttrName() const
Definition: AttributeCommonInfo.h:128
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::AttrSyntax
AttrSyntax
Definition: Attributes.h:19
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:156
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:127
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::AttributeCommonInfo::getScopeName
const IdentifierInfo * getScopeName() const
Definition: AttributeCommonInfo.h:134
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:114
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:67
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:58
clang
Definition: CalledOnceCheck.h:17
clang::Attr
Attr - This represents one attribute.
Definition: Attr.h:46
clang::hasAttribute
int hasAttribute(AttrSyntax 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
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