clang  7.0.0
ParsedAttr.cpp
Go to the documentation of this file.
1 //======- ParsedAttr.cpp --------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ParsedAttr class implementation
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Sema/ParsedAttr.h"
15 #include "clang/AST/ASTContext.h"
18 #include "clang/Basic/TargetInfo.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include <cassert>
24 #include <cstddef>
25 #include <utility>
26 
27 using namespace clang;
28 
30  IdentifierInfo *Ident) {
31  IdentifierLoc *Result = new (Ctx) IdentifierLoc;
32  Result->Loc = Loc;
33  Result->Ident = Ident;
34  return Result;
35 }
36 
37 size_t ParsedAttr::allocated_size() const {
38  if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
39  else if (IsTypeTagForDatatype)
41  else if (IsProperty)
43  else if (HasParsedType)
44  return sizeof(ParsedAttr) + sizeof(void *);
45  return (sizeof(ParsedAttr) + NumArgs * sizeof(ArgsUnion));
46 }
47 
49  // Go ahead and configure all the inline capacity. This is just a memset.
50  FreeLists.resize(InlineFreeListsCapacity);
51 }
53 
54 static size_t getFreeListIndexForSize(size_t size) {
55  assert(size >= sizeof(ParsedAttr));
56  assert((size % sizeof(void*)) == 0);
57  return ((size - sizeof(ParsedAttr)) / sizeof(void *));
58 }
59 
60 void *AttributeFactory::allocate(size_t size) {
61  // Check for a previously reclaimed attribute.
62  size_t index = getFreeListIndexForSize(size);
63  if (index < FreeLists.size() && !FreeLists[index].empty()) {
64  ParsedAttr *attr = FreeLists[index].back();
65  FreeLists[index].pop_back();
66  return attr;
67  }
68 
69  // Otherwise, allocate something new.
70  return Alloc.Allocate(size, alignof(AttributeFactory));
71 }
72 
73 void AttributeFactory::deallocate(ParsedAttr *Attr) {
74  size_t size = Attr->allocated_size();
75  size_t freeListIndex = getFreeListIndexForSize(size);
76 
77  // Expand FreeLists to the appropriate size, if required.
78  if (freeListIndex >= FreeLists.size())
79  FreeLists.resize(freeListIndex + 1);
80 
81 #if !NDEBUG
82  // In debug mode, zero out the attribute to help find memory overwriting.
83  memset(Attr, 0, size);
84 #endif
85 
86  // Add 'Attr' to the appropriate free-list.
87  FreeLists[freeListIndex].push_back(Attr);
88 }
89 
90 void AttributeFactory::reclaimPool(AttributePool &cur) {
91  for (ParsedAttr *AL : cur.Attrs)
92  deallocate(AL);
93 }
94 
95 void AttributePool::takePool(AttributePool &pool) {
96  Attrs.insert(Attrs.end(), pool.Attrs.begin(), pool.Attrs.end());
97  pool.Attrs.clear();
98 }
99 
100 #include "clang/Sema/AttrParsedAttrKinds.inc"
101 
102 static StringRef normalizeAttrName(StringRef AttrName, StringRef ScopeName,
103  ParsedAttr::Syntax SyntaxUsed) {
104  // Normalize the attribute name, __foo__ becomes foo. This is only allowable
105  // for GNU attributes.
106  bool IsGNU = SyntaxUsed == ParsedAttr::AS_GNU ||
107  ((SyntaxUsed == ParsedAttr::AS_CXX11 ||
108  SyntaxUsed == ParsedAttr::AS_C2x) &&
109  ScopeName == "gnu");
110  if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
111  AttrName.endswith("__"))
112  AttrName = AttrName.slice(2, AttrName.size() - 2);
113 
114  return AttrName;
115 }
116 
118  const IdentifierInfo *ScopeName,
119  Syntax SyntaxUsed) {
120  StringRef AttrName = Name->getName();
121 
122  SmallString<64> FullName;
123  if (ScopeName)
124  FullName += ScopeName->getName();
125 
126  AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
127 
128  // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
129  // unscoped.
130  if (ScopeName || SyntaxUsed == AS_CXX11 || SyntaxUsed == AS_C2x)
131  FullName += "::";
132  FullName += AttrName;
133 
134  return ::getAttrKind(FullName, SyntaxUsed);
135 }
136 
138  // Both variables will be used in tablegen generated
139  // attribute spell list index matching code.
140  StringRef Scope = ScopeName ? ScopeName->getName() : "";
141  StringRef Name = normalizeAttrName(AttrName->getName(), Scope,
142  (ParsedAttr::Syntax)SyntaxUsed);
143 
144 #include "clang/Sema/AttrSpellingListIndex.inc"
145 
146 }
147 
149  unsigned NumArgs : 4;
150  unsigned OptArgs : 4;
151  unsigned HasCustomParsing : 1;
152  unsigned IsTargetSpecific : 1;
153  unsigned IsType : 1;
154  unsigned IsStmt : 1;
155  unsigned IsKnownToGCC : 1;
157 
158  bool (*DiagAppertainsToDecl)(Sema &S, const ParsedAttr &Attr, const Decl *);
159  bool (*DiagLangOpts)(Sema &S, const ParsedAttr &Attr);
160  bool (*ExistsInTarget)(const TargetInfo &Target);
161  unsigned (*SpellingIndexToSemanticSpelling)(const ParsedAttr &Attr);
162  void (*GetPragmaAttributeMatchRules)(
164  const LangOptions &LangOpts);
165 };
166 
167 namespace {
168 
169 #include "clang/Sema/AttrParsedAttrImpl.inc"
170 
171 } // namespace
172 
173 static const ParsedAttrInfo &getInfo(const ParsedAttr &A) {
174  return AttrInfoMap[A.getKind()];
175 }
176 
177 unsigned ParsedAttr::getMinArgs() const { return getInfo(*this).NumArgs; }
178 
179 unsigned ParsedAttr::getMaxArgs() const {
180  return getMinArgs() + getInfo(*this).OptArgs;
181 }
182 
184  return getInfo(*this).HasCustomParsing;
185 }
186 
187 bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
188  return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
189 }
190 
192  attr::SubjectMatchRule MatchRule) const {
193  return checkAttributeMatchRuleAppliesTo(D, MatchRule);
194 }
195 
197  const LangOptions &LangOpts,
198  SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
199  const {
200  return getInfo(*this).GetPragmaAttributeMatchRules(MatchRules, LangOpts);
201 }
202 
204  return getInfo(*this).DiagLangOpts(S, *this);
205 }
206 
208  return getInfo(*this).IsTargetSpecific;
209 }
210 
211 bool ParsedAttr::isTypeAttr() const { return getInfo(*this).IsType; }
212 
213 bool ParsedAttr::isStmtAttr() const { return getInfo(*this).IsStmt; }
214 
216  return getInfo(*this).ExistsInTarget(Target);
217 }
218 
219 bool ParsedAttr::isKnownToGCC() const { return getInfo(*this).IsKnownToGCC; }
220 
223 }
224 
226  return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
227 }
228 
230  // If the attribute has the maximum number of optional arguments, we will
231  // claim that as being variadic. If we someday get an attribute that
232  // legitimately bumps up against that maximum, we can use another bit to track
233  // whether it's truly variadic or not.
234  return getInfo(*this).OptArgs == 15;
235 }
Defines the clang::ASTContext interface.
bool diagnoseAppertainsTo(class Sema &S, const Decl *D) const
Definition: ParsedAttr.cpp:187
static size_t getFreeListIndexForSize(size_t size)
Definition: ParsedAttr.cpp:54
llvm::PointerUnion< Expr *, IdentifierLoc * > ArgsUnion
A union of the various pointer types that can be passed to an ParsedAttr as an argument.
Definition: ParsedAttr.h:93
The required allocation size of an availability attribute, which we want to ensure is a multiple of s...
Definition: ParsedAttr.h:557
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
IdentifierInfo * Ident
Definition: ParsedAttr.h:85
static IdentifierLoc * create(ASTContext &Ctx, SourceLocation Loc, IdentifierInfo *Ident)
Definition: ParsedAttr.cpp:29
attribute((...))
Definition: ParsedAttr.h:110
unsigned IsStmt
Definition: ParsedAttr.cpp:154
bool(* DiagLangOpts)(Sema &S, const ParsedAttr &Attr)
Definition: ParsedAttr.cpp:159
unsigned OptArgs
Definition: ParsedAttr.cpp:150
bool(* ExistsInTarget)(const TargetInfo &Target)
Definition: ParsedAttr.cpp:160
SourceLocation Loc
Definition: ParsedAttr.h:84
One of these records is kept for each identifier that is lexed.
unsigned getAttributeSpellingListIndex() const
Get an index into the attribute spelling list defined in Attr.td.
Definition: ParsedAttr.cpp:137
SubjectMatchRule
A list of all the recognized kinds of attributes.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:150
void getMatchRules(const LangOptions &LangOpts, SmallVectorImpl< std::pair< attr::SubjectMatchRule, bool >> &MatchRules) const
Definition: ParsedAttr.cpp:196
bool isKnownToGCC() const
Definition: ParsedAttr.cpp:219
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
unsigned NumArgs
Definition: ParsedAttr.cpp:149
bool existsInTarget(const TargetInfo &Target) const
Definition: ParsedAttr.cpp:215
unsigned getMinArgs() const
Definition: ParsedAttr.cpp:177
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
bool hasVariadicArg() const
Definition: ParsedAttr.cpp:229
__DEVICE__ void * memset(void *__a, int __b, size_t __c)
bool(* DiagAppertainsToDecl)(Sema &S, const ParsedAttr &Attr, const Decl *)
Definition: ParsedAttr.cpp:158
bool isTargetSpecificAttr() const
Definition: ParsedAttr.cpp:207
bool hasCustomParsing() const
Definition: ParsedAttr.cpp:183
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:277
Exposes information about the current target.
Definition: TargetInfo.h:54
unsigned HasCustomParsing
Definition: ParsedAttr.cpp:151
Kind getKind() const
Definition: ParsedAttr.h:423
unsigned IsSupportedByPragmaAttribute
Definition: ParsedAttr.cpp:156
#define bool
Definition: stdbool.h:31
unsigned IsKnownToGCC
Definition: ParsedAttr.cpp:155
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
unsigned IsType
Definition: ParsedAttr.cpp:153
Wraps an identifier and optional source location for the identifier.
Definition: ParsedAttr.h:83
The result type of a method or function.
unsigned getSemanticSpelling() const
If the parsed attribute has a semantic equivalent, and it would have a semantic Spelling enumeration ...
Definition: ParsedAttr.cpp:225
Encodes a location in the source.
Syntax
The style used to specify an attribute.
Definition: ParsedAttr.h:108
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:105
bool diagnoseLangOpts(class Sema &S) const
Definition: ParsedAttr.cpp:203
static StringRef normalizeAttrName(StringRef AttrName, StringRef ScopeName, ParsedAttr::Syntax SyntaxUsed)
Definition: ParsedAttr.cpp:102
StringRef getName() const
Return the actual identifier string.
bool isStmtAttr() const
Definition: ParsedAttr.cpp:213
Dataflow Directional Tag Classes.
unsigned IsTargetSpecific
Definition: ParsedAttr.cpp:152
bool isTypeAttr() const
Definition: ParsedAttr.cpp:211
void(* GetPragmaAttributeMatchRules)(llvm::SmallVectorImpl< std::pair< attr::SubjectMatchRule, bool >> &Rules, const LangOptions &LangOpts)
Definition: ParsedAttr.cpp:162
static const ParsedAttrInfo & getInfo(const ParsedAttr &A)
Definition: ParsedAttr.cpp:173
unsigned getMaxArgs() const
Definition: ParsedAttr.cpp:179
A factory, from which one makes pools, from which one creates individual attributes which are dealloc...
Definition: ParsedAttr.h:552
bool appliesToDecl(const Decl *D, attr::SubjectMatchRule MatchRule) const
Definition: ParsedAttr.cpp:191
unsigned(* SpellingIndexToSemanticSpelling)(const ParsedAttr &Attr)
Definition: ParsedAttr.cpp:161
Defines the clang::TargetInfo interface.
bool isSupportedByPragmaAttribute() const
Definition: ParsedAttr.cpp:221
Attr - This represents one attribute.
Definition: Attr.h:43