clang  13.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 #include "clang/Basic/AttrHasAttributeImpl.inc"
24 
25  return 0;
26 }
27 
29  switch (Rule) {
30 #define ATTR_MATCH_RULE(NAME, SPELLING, IsAbstract) \
31  case attr::NAME: \
32  return SPELLING;
33 #include "clang/Basic/AttrSubMatchRulesList.inc"
34  }
35  llvm_unreachable("Invalid subject match rule");
36 }
37 
38 static StringRef
40  AttributeCommonInfo::Syntax SyntaxUsed) {
41  if (!Scope)
42  return "";
43 
44  // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
45  // to be "clang".
46  StringRef ScopeName = Scope->getName();
47  if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
48  SyntaxUsed == AttributeCommonInfo::AS_C2x) {
49  if (ScopeName == "__gnu__")
50  ScopeName = "gnu";
51  else if (ScopeName == "_Clang")
52  ScopeName = "clang";
53  }
54  return ScopeName;
55 }
56 
57 static StringRef normalizeAttrName(const IdentifierInfo *Name,
58  StringRef NormalizedScopeName,
59  AttributeCommonInfo::Syntax SyntaxUsed) {
60  // Normalize the attribute name, __foo__ becomes foo. This is only allowable
61  // for GNU attributes, and attributes using the double square bracket syntax.
62  bool ShouldNormalize =
63  SyntaxUsed == AttributeCommonInfo::AS_GNU ||
64  ((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
65  SyntaxUsed == AttributeCommonInfo::AS_C2x) &&
66  (NormalizedScopeName.empty() || NormalizedScopeName == "gnu" ||
67  NormalizedScopeName == "clang"));
68  StringRef AttrName = Name->getName();
69  if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
70  AttrName.endswith("__"))
71  AttrName = AttrName.slice(2, AttrName.size() - 2);
72 
73  return AttrName;
74 }
75 
77  return ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"));
78 }
79 
80 #include "clang/Sema/AttrParsedAttrKinds.inc"
81 
83  const IdentifierInfo *Scope,
84  AttributeCommonInfo::Syntax SyntaxUsed) {
85  StringRef ScopeName = normalizeAttrScopeName(Scope, SyntaxUsed);
86  StringRef AttrName = normalizeAttrName(Name, ScopeName, SyntaxUsed);
87 
88  SmallString<64> FullName = ScopeName;
89  if (!ScopeName.empty()) {
90  assert(SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
91  SyntaxUsed == AttributeCommonInfo::AS_C2x);
92  FullName += "::";
93  }
94  FullName += AttrName;
95 
96  return FullName;
97 }
98 
101  const IdentifierInfo *ScopeName,
102  Syntax SyntaxUsed) {
103  return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
104 }
105 
107  return static_cast<std::string>(
109 }
110 
111 unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
112  // Both variables will be used in tablegen generated
113  // attribute spell list index matching code.
114  auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
116  StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
117 
118 #include "clang/Sema/AttrSpellingListIndex.inc"
119 }
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:82
clang::AttributeCommonInfo::Kind
Kind
Definition: AttributeCommonInfo.h:52
normalizeAttrScopeName
static StringRef normalizeAttrScopeName(const IdentifierInfo *Scope, AttributeCommonInfo::Syntax SyntaxUsed)
Definition: Attributes.cpp:39
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:188
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:141
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:28
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:106
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:59
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
Dataflow Directional Tag Classes.
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:57
clang::AttributeCommonInfo::isGNUScope
bool isGNUScope() const
Definition: Attributes.cpp:76