/src/llvm-project/clang/lib/Sema/ParsedAttr.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //======- ParsedAttr.cpp --------------------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines the ParsedAttr class implementation |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/Sema/ParsedAttr.h" |
14 | | #include "clang/AST/ASTContext.h" |
15 | | #include "clang/Basic/AttrSubjectMatchRules.h" |
16 | | #include "clang/Basic/IdentifierTable.h" |
17 | | #include "clang/Basic/TargetInfo.h" |
18 | | #include "clang/Sema/SemaInternal.h" |
19 | | #include "llvm/ADT/SmallString.h" |
20 | | #include "llvm/ADT/SmallVector.h" |
21 | | #include "llvm/ADT/StringRef.h" |
22 | | #include <cassert> |
23 | | #include <cstddef> |
24 | | #include <utility> |
25 | | |
26 | | using namespace clang; |
27 | | |
28 | | IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc, |
29 | 0 | IdentifierInfo *Ident) { |
30 | 0 | IdentifierLoc *Result = new (Ctx) IdentifierLoc; |
31 | 0 | Result->Loc = Loc; |
32 | 0 | Result->Ident = Ident; |
33 | 0 | return Result; |
34 | 0 | } |
35 | | |
36 | 0 | size_t ParsedAttr::allocated_size() const { |
37 | 0 | if (IsAvailability) return AttributeFactory::AvailabilityAllocSize; |
38 | 0 | else if (IsTypeTagForDatatype) |
39 | 0 | return AttributeFactory::TypeTagForDatatypeAllocSize; |
40 | 0 | else if (IsProperty) |
41 | 0 | return AttributeFactory::PropertyAllocSize; |
42 | 0 | else if (HasParsedType) |
43 | 0 | return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
44 | 0 | detail::TypeTagForDatatypeData, ParsedType, |
45 | 0 | detail::PropertyData>(0, 0, 0, 1, 0); |
46 | 0 | return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
47 | 0 | detail::TypeTagForDatatypeData, ParsedType, |
48 | 0 | detail::PropertyData>(NumArgs, 0, 0, 0, 0); |
49 | 0 | } |
50 | | |
51 | 46 | AttributeFactory::AttributeFactory() { |
52 | | // Go ahead and configure all the inline capacity. This is just a memset. |
53 | 46 | FreeLists.resize(InlineFreeListsCapacity); |
54 | 46 | } |
55 | 46 | AttributeFactory::~AttributeFactory() = default; |
56 | | |
57 | 0 | static size_t getFreeListIndexForSize(size_t size) { |
58 | 0 | assert(size >= sizeof(ParsedAttr)); |
59 | 0 | assert((size % sizeof(void*)) == 0); |
60 | 0 | return ((size - sizeof(ParsedAttr)) / sizeof(void *)); |
61 | 0 | } |
62 | | |
63 | 0 | void *AttributeFactory::allocate(size_t size) { |
64 | | // Check for a previously reclaimed attribute. |
65 | 0 | size_t index = getFreeListIndexForSize(size); |
66 | 0 | if (index < FreeLists.size() && !FreeLists[index].empty()) { |
67 | 0 | ParsedAttr *attr = FreeLists[index].back(); |
68 | 0 | FreeLists[index].pop_back(); |
69 | 0 | return attr; |
70 | 0 | } |
71 | | |
72 | | // Otherwise, allocate something new. |
73 | 0 | return Alloc.Allocate(size, alignof(AttributeFactory)); |
74 | 0 | } |
75 | | |
76 | 0 | void AttributeFactory::deallocate(ParsedAttr *Attr) { |
77 | 0 | size_t size = Attr->allocated_size(); |
78 | 0 | size_t freeListIndex = getFreeListIndexForSize(size); |
79 | | |
80 | | // Expand FreeLists to the appropriate size, if required. |
81 | 0 | if (freeListIndex >= FreeLists.size()) |
82 | 0 | FreeLists.resize(freeListIndex + 1); |
83 | |
|
84 | 0 | #ifndef NDEBUG |
85 | | // In debug mode, zero out the attribute to help find memory overwriting. |
86 | 0 | memset(Attr, 0, size); |
87 | 0 | #endif |
88 | | |
89 | | // Add 'Attr' to the appropriate free-list. |
90 | 0 | FreeLists[freeListIndex].push_back(Attr); |
91 | 0 | } |
92 | | |
93 | 202k | void AttributeFactory::reclaimPool(AttributePool &cur) { |
94 | 202k | for (ParsedAttr *AL : cur.Attrs) |
95 | 0 | deallocate(AL); |
96 | 202k | } |
97 | | |
98 | 48.8k | void AttributePool::takePool(AttributePool &pool) { |
99 | 48.8k | Attrs.insert(Attrs.end(), pool.Attrs.begin(), pool.Attrs.end()); |
100 | 48.8k | pool.Attrs.clear(); |
101 | 48.8k | } |
102 | | |
103 | | namespace { |
104 | | |
105 | | #include "clang/Sema/AttrParsedAttrImpl.inc" |
106 | | |
107 | | } // namespace |
108 | | |
109 | 0 | const ParsedAttrInfo &ParsedAttrInfo::get(const AttributeCommonInfo &A) { |
110 | | // If we have a ParsedAttrInfo for this ParsedAttr then return that. |
111 | 0 | if ((size_t)A.getParsedKind() < std::size(AttrInfoMap)) |
112 | 0 | return *AttrInfoMap[A.getParsedKind()]; |
113 | | |
114 | | // If this is an ignored attribute then return an appropriate ParsedAttrInfo. |
115 | 0 | static const ParsedAttrInfo IgnoredParsedAttrInfo( |
116 | 0 | AttributeCommonInfo::IgnoredAttribute); |
117 | 0 | if (A.getParsedKind() == AttributeCommonInfo::IgnoredAttribute) |
118 | 0 | return IgnoredParsedAttrInfo; |
119 | | |
120 | | // Otherwise this may be an attribute defined by a plugin. |
121 | | |
122 | | // Search for a ParsedAttrInfo whose name and syntax match. |
123 | 0 | std::string FullName = A.getNormalizedFullName(); |
124 | 0 | AttributeCommonInfo::Syntax SyntaxUsed = A.getSyntax(); |
125 | 0 | if (SyntaxUsed == AttributeCommonInfo::AS_ContextSensitiveKeyword) |
126 | 0 | SyntaxUsed = AttributeCommonInfo::AS_Keyword; |
127 | |
|
128 | 0 | for (auto &Ptr : getAttributePluginInstances()) |
129 | 0 | if (Ptr->hasSpelling(SyntaxUsed, FullName)) |
130 | 0 | return *Ptr; |
131 | | |
132 | | // If we failed to find a match then return a default ParsedAttrInfo. |
133 | 0 | static const ParsedAttrInfo DefaultParsedAttrInfo( |
134 | 0 | AttributeCommonInfo::UnknownAttribute); |
135 | 0 | return DefaultParsedAttrInfo; |
136 | 0 | } |
137 | | |
138 | 0 | ArrayRef<const ParsedAttrInfo *> ParsedAttrInfo::getAllBuiltin() { |
139 | 0 | return llvm::ArrayRef(AttrInfoMap); |
140 | 0 | } |
141 | | |
142 | 0 | unsigned ParsedAttr::getMinArgs() const { return getInfo().NumArgs; } |
143 | | |
144 | 0 | unsigned ParsedAttr::getMaxArgs() const { |
145 | 0 | return getMinArgs() + getInfo().OptArgs; |
146 | 0 | } |
147 | | |
148 | 0 | unsigned ParsedAttr::getNumArgMembers() const { |
149 | 0 | return getInfo().NumArgMembers; |
150 | 0 | } |
151 | | |
152 | 0 | bool ParsedAttr::hasCustomParsing() const { |
153 | 0 | return getInfo().HasCustomParsing; |
154 | 0 | } |
155 | | |
156 | 0 | bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const { |
157 | 0 | return getInfo().diagAppertainsToDecl(S, *this, D); |
158 | 0 | } |
159 | | |
160 | 0 | bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Stmt *St) const { |
161 | 0 | return getInfo().diagAppertainsToStmt(S, *this, St); |
162 | 0 | } |
163 | | |
164 | 0 | bool ParsedAttr::diagnoseMutualExclusion(Sema &S, const Decl *D) const { |
165 | 0 | return getInfo().diagMutualExclusion(S, *this, D); |
166 | 0 | } |
167 | | |
168 | | bool ParsedAttr::appliesToDecl(const Decl *D, |
169 | 0 | attr::SubjectMatchRule MatchRule) const { |
170 | 0 | return checkAttributeMatchRuleAppliesTo(D, MatchRule); |
171 | 0 | } |
172 | | |
173 | | void ParsedAttr::getMatchRules( |
174 | | const LangOptions &LangOpts, |
175 | | SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules) |
176 | 0 | const { |
177 | 0 | return getInfo().getPragmaAttributeMatchRules(MatchRules, LangOpts); |
178 | 0 | } |
179 | | |
180 | 0 | bool ParsedAttr::diagnoseLangOpts(Sema &S) const { |
181 | 0 | if (getInfo().acceptsLangOpts(S.getLangOpts())) |
182 | 0 | return true; |
183 | 0 | S.Diag(getLoc(), diag::warn_attribute_ignored) << *this; |
184 | 0 | return false; |
185 | 0 | } |
186 | | |
187 | 0 | bool ParsedAttr::isTargetSpecificAttr() const { |
188 | 0 | return getInfo().IsTargetSpecific; |
189 | 0 | } |
190 | | |
191 | 0 | bool ParsedAttr::isTypeAttr() const { return getInfo().IsType; } |
192 | | |
193 | 0 | bool ParsedAttr::isStmtAttr() const { return getInfo().IsStmt; } |
194 | | |
195 | 0 | bool ParsedAttr::existsInTarget(const TargetInfo &Target) const { |
196 | 0 | Kind K = getParsedKind(); |
197 | | |
198 | | // If the attribute has a target-specific spelling, check that it exists. |
199 | | // Only call this if the attr is not ignored/unknown. For most targets, this |
200 | | // function just returns true. |
201 | 0 | bool HasSpelling = K != IgnoredAttribute && K != UnknownAttribute && |
202 | 0 | K != NoSemaHandlerAttribute; |
203 | 0 | bool TargetSpecificSpellingExists = |
204 | 0 | !HasSpelling || |
205 | 0 | getInfo().spellingExistsInTarget(Target, getAttributeSpellingListIndex()); |
206 | |
|
207 | 0 | return getInfo().existsInTarget(Target) && TargetSpecificSpellingExists; |
208 | 0 | } |
209 | | |
210 | 0 | bool ParsedAttr::isKnownToGCC() const { return getInfo().IsKnownToGCC; } |
211 | | |
212 | 0 | bool ParsedAttr::isSupportedByPragmaAttribute() const { |
213 | 0 | return getInfo().IsSupportedByPragmaAttribute; |
214 | 0 | } |
215 | | |
216 | 0 | bool ParsedAttr::slidesFromDeclToDeclSpecLegacyBehavior() const { |
217 | 0 | if (isRegularKeywordAttribute()) |
218 | | // The appurtenance rules are applied strictly for all regular keyword |
219 | | // atributes. |
220 | 0 | return false; |
221 | | |
222 | 0 | assert(isStandardAttributeSyntax()); |
223 | | |
224 | | // We have historically allowed some type attributes with standard attribute |
225 | | // syntax to slide to the decl-specifier-seq, so we have to keep supporting |
226 | | // it. This property is consciously not defined as a flag in Attr.td because |
227 | | // we don't want new attributes to specify it. |
228 | | // |
229 | | // Note: No new entries should be added to this list. Entries should be |
230 | | // removed from this list after a suitable deprecation period, provided that |
231 | | // there are no compatibility considerations with other compilers. If |
232 | | // possible, we would like this list to go away entirely. |
233 | 0 | switch (getParsedKind()) { |
234 | 0 | case AT_AddressSpace: |
235 | 0 | case AT_OpenCLPrivateAddressSpace: |
236 | 0 | case AT_OpenCLGlobalAddressSpace: |
237 | 0 | case AT_OpenCLGlobalDeviceAddressSpace: |
238 | 0 | case AT_OpenCLGlobalHostAddressSpace: |
239 | 0 | case AT_OpenCLLocalAddressSpace: |
240 | 0 | case AT_OpenCLConstantAddressSpace: |
241 | 0 | case AT_OpenCLGenericAddressSpace: |
242 | 0 | case AT_NeonPolyVectorType: |
243 | 0 | case AT_NeonVectorType: |
244 | 0 | case AT_ArmMveStrictPolymorphism: |
245 | 0 | case AT_BTFTypeTag: |
246 | 0 | case AT_ObjCGC: |
247 | 0 | case AT_MatrixType: |
248 | 0 | return true; |
249 | 0 | default: |
250 | 0 | return false; |
251 | 0 | } |
252 | 0 | } |
253 | | |
254 | 0 | bool ParsedAttr::acceptsExprPack() const { return getInfo().AcceptsExprPack; } |
255 | | |
256 | 0 | unsigned ParsedAttr::getSemanticSpelling() const { |
257 | 0 | return getInfo().spellingIndexToSemanticSpelling(*this); |
258 | 0 | } |
259 | | |
260 | 0 | bool ParsedAttr::hasVariadicArg() const { |
261 | | // If the attribute has the maximum number of optional arguments, we will |
262 | | // claim that as being variadic. If we someday get an attribute that |
263 | | // legitimately bumps up against that maximum, we can use another bit to track |
264 | | // whether it's truly variadic or not. |
265 | 0 | return getInfo().OptArgs == 15; |
266 | 0 | } |
267 | | |
268 | 0 | bool ParsedAttr::isParamExpr(size_t N) const { |
269 | 0 | return getInfo().isParamExpr(N); |
270 | 0 | } |
271 | | |
272 | 0 | void ParsedAttr::handleAttrWithDelayedArgs(Sema &S, Decl *D) const { |
273 | 0 | ::handleAttrWithDelayedArgs(S, D, *this); |
274 | 0 | } |
275 | | |
276 | 0 | static unsigned getNumAttributeArgs(const ParsedAttr &AL) { |
277 | | // FIXME: Include the type in the argument list. |
278 | 0 | return AL.getNumArgs() + AL.hasParsedType(); |
279 | 0 | } |
280 | | |
281 | | template <typename Compare> |
282 | | static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL, |
283 | | unsigned Num, unsigned Diag, |
284 | 0 | Compare Comp) { |
285 | 0 | if (Comp(getNumAttributeArgs(AL), Num)) { |
286 | 0 | S.Diag(AL.getLoc(), Diag) << AL << Num; |
287 | 0 | return false; |
288 | 0 | } |
289 | 0 | return true; |
290 | 0 | } Unexecuted instantiation: ParsedAttr.cpp:bool checkAttributeNumArgsImpl<std::__1::not_equal_to<unsigned int> >(clang::Sema&, clang::ParsedAttr const&, unsigned int, unsigned int, std::__1::not_equal_to<unsigned int>) Unexecuted instantiation: ParsedAttr.cpp:bool checkAttributeNumArgsImpl<std::__1::less<unsigned int> >(clang::Sema&, clang::ParsedAttr const&, unsigned int, unsigned int, std::__1::less<unsigned int>) Unexecuted instantiation: ParsedAttr.cpp:bool checkAttributeNumArgsImpl<std::__1::greater<unsigned int> >(clang::Sema&, clang::ParsedAttr const&, unsigned int, unsigned int, std::__1::greater<unsigned int>) |
291 | | |
292 | 0 | bool ParsedAttr::checkExactlyNumArgs(Sema &S, unsigned Num) const { |
293 | 0 | return checkAttributeNumArgsImpl(S, *this, Num, |
294 | 0 | diag::err_attribute_wrong_number_arguments, |
295 | 0 | std::not_equal_to<unsigned>()); |
296 | 0 | } |
297 | 0 | bool ParsedAttr::checkAtLeastNumArgs(Sema &S, unsigned Num) const { |
298 | 0 | return checkAttributeNumArgsImpl(S, *this, Num, |
299 | 0 | diag::err_attribute_too_few_arguments, |
300 | 0 | std::less<unsigned>()); |
301 | 0 | } |
302 | 0 | bool ParsedAttr::checkAtMostNumArgs(Sema &S, unsigned Num) const { |
303 | 0 | return checkAttributeNumArgsImpl(S, *this, Num, |
304 | 0 | diag::err_attribute_too_many_arguments, |
305 | 0 | std::greater<unsigned>()); |
306 | 0 | } |
307 | | |
308 | | void clang::takeAndConcatenateAttrs(ParsedAttributes &First, |
309 | | ParsedAttributes &Second, |
310 | 1 | ParsedAttributes &Result) { |
311 | | // Note that takeAllFrom() puts the attributes at the beginning of the list, |
312 | | // so to obtain the correct ordering, we add `Second`, then `First`. |
313 | 1 | Result.takeAllFrom(Second); |
314 | 1 | Result.takeAllFrom(First); |
315 | 1 | if (First.Range.getBegin().isValid()) |
316 | 0 | Result.Range.setBegin(First.Range.getBegin()); |
317 | 1 | else |
318 | 1 | Result.Range.setBegin(Second.Range.getBegin()); |
319 | 1 | if (Second.Range.getEnd().isValid()) |
320 | 0 | Result.Range.setEnd(Second.Range.getEnd()); |
321 | 1 | else |
322 | 1 | Result.Range.setEnd(First.Range.getEnd()); |
323 | 1 | } |