1 //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements decl-related attribute processing.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTConsumer.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTMutationListener.h"
16 #include "clang/AST/CXXInheritance.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/ExprCXX.h"
22 #include "clang/AST/Mangle.h"
23 #include "clang/AST/RecursiveASTVisitor.h"
24 #include "clang/AST/Type.h"
25 #include "clang/Basic/CharInfo.h"
26 #include "clang/Basic/DarwinSDKInfo.h"
27 #include "clang/Basic/LangOptions.h"
28 #include "clang/Basic/SourceLocation.h"
29 #include "clang/Basic/SourceManager.h"
30 #include "clang/Basic/TargetBuiltins.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Sema/DeclSpec.h"
34 #include "clang/Sema/DelayedDiagnostic.h"
35 #include "clang/Sema/Initialization.h"
36 #include "clang/Sema/Lookup.h"
37 #include "clang/Sema/ParsedAttr.h"
38 #include "clang/Sema/Scope.h"
39 #include "clang/Sema/ScopeInfo.h"
40 #include "clang/Sema/SemaInternal.h"
41 #include "llvm/ADT/Optional.h"
42 #include "llvm/ADT/STLExtras.h"
43 #include "llvm/ADT/StringExtras.h"
44 #include "llvm/IR/Assumptions.h"
45 #include "llvm/MC/MCSectionMachO.h"
46 #include "llvm/Support/Error.h"
47 #include "llvm/Support/MathExtras.h"
48 #include "llvm/Support/raw_ostream.h"
50 using namespace clang
;
53 namespace AttributeLangSupport
{
59 } // end namespace AttributeLangSupport
61 //===----------------------------------------------------------------------===//
63 //===----------------------------------------------------------------------===//
65 /// isFunctionOrMethod - Return true if the given decl has function
66 /// type (function or function-typed variable) or an Objective-C
68 static bool isFunctionOrMethod(const Decl
*D
) {
69 return (D
->getFunctionType() != nullptr) || isa
<ObjCMethodDecl
>(D
);
72 /// Return true if the given decl has function type (function or
73 /// function-typed variable) or an Objective-C method or a block.
74 static bool isFunctionOrMethodOrBlock(const Decl
*D
) {
75 return isFunctionOrMethod(D
) || isa
<BlockDecl
>(D
);
78 /// Return true if the given decl has a declarator that should have
79 /// been processed by Sema::GetTypeForDeclarator.
80 static bool hasDeclarator(const Decl
*D
) {
81 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
82 return isa
<DeclaratorDecl
>(D
) || isa
<BlockDecl
>(D
) || isa
<TypedefNameDecl
>(D
) ||
83 isa
<ObjCPropertyDecl
>(D
);
86 /// hasFunctionProto - Return true if the given decl has a argument
87 /// information. This decl should have already passed
88 /// isFunctionOrMethod or isFunctionOrMethodOrBlock.
89 static bool hasFunctionProto(const Decl
*D
) {
90 if (const FunctionType
*FnTy
= D
->getFunctionType())
91 return isa
<FunctionProtoType
>(FnTy
);
92 return isa
<ObjCMethodDecl
>(D
) || isa
<BlockDecl
>(D
);
95 /// getFunctionOrMethodNumParams - Return number of function or method
96 /// parameters. It is an error to call this on a K&R function (use
97 /// hasFunctionProto first).
98 static unsigned getFunctionOrMethodNumParams(const Decl
*D
) {
99 if (const FunctionType
*FnTy
= D
->getFunctionType())
100 return cast
<FunctionProtoType
>(FnTy
)->getNumParams();
101 if (const auto *BD
= dyn_cast
<BlockDecl
>(D
))
102 return BD
->getNumParams();
103 return cast
<ObjCMethodDecl
>(D
)->param_size();
106 static const ParmVarDecl
*getFunctionOrMethodParam(const Decl
*D
,
108 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
))
109 return FD
->getParamDecl(Idx
);
110 if (const auto *MD
= dyn_cast
<ObjCMethodDecl
>(D
))
111 return MD
->getParamDecl(Idx
);
112 if (const auto *BD
= dyn_cast
<BlockDecl
>(D
))
113 return BD
->getParamDecl(Idx
);
117 static QualType
getFunctionOrMethodParamType(const Decl
*D
, unsigned Idx
) {
118 if (const FunctionType
*FnTy
= D
->getFunctionType())
119 return cast
<FunctionProtoType
>(FnTy
)->getParamType(Idx
);
120 if (const auto *BD
= dyn_cast
<BlockDecl
>(D
))
121 return BD
->getParamDecl(Idx
)->getType();
123 return cast
<ObjCMethodDecl
>(D
)->parameters()[Idx
]->getType();
126 static SourceRange
getFunctionOrMethodParamRange(const Decl
*D
, unsigned Idx
) {
127 if (auto *PVD
= getFunctionOrMethodParam(D
, Idx
))
128 return PVD
->getSourceRange();
129 return SourceRange();
132 static QualType
getFunctionOrMethodResultType(const Decl
*D
) {
133 if (const FunctionType
*FnTy
= D
->getFunctionType())
134 return FnTy
->getReturnType();
135 return cast
<ObjCMethodDecl
>(D
)->getReturnType();
138 static SourceRange
getFunctionOrMethodResultSourceRange(const Decl
*D
) {
139 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
))
140 return FD
->getReturnTypeSourceRange();
141 if (const auto *MD
= dyn_cast
<ObjCMethodDecl
>(D
))
142 return MD
->getReturnTypeSourceRange();
143 return SourceRange();
146 static bool isFunctionOrMethodVariadic(const Decl
*D
) {
147 if (const FunctionType
*FnTy
= D
->getFunctionType())
148 return cast
<FunctionProtoType
>(FnTy
)->isVariadic();
149 if (const auto *BD
= dyn_cast
<BlockDecl
>(D
))
150 return BD
->isVariadic();
151 return cast
<ObjCMethodDecl
>(D
)->isVariadic();
154 static bool isInstanceMethod(const Decl
*D
) {
155 if (const auto *MethodDecl
= dyn_cast
<CXXMethodDecl
>(D
))
156 return MethodDecl
->isInstance();
160 static inline bool isNSStringType(QualType T
, ASTContext
&Ctx
,
161 bool AllowNSAttributedString
= false) {
162 const auto *PT
= T
->getAs
<ObjCObjectPointerType
>();
166 ObjCInterfaceDecl
*Cls
= PT
->getObjectType()->getInterface();
170 IdentifierInfo
* ClsName
= Cls
->getIdentifier();
172 if (AllowNSAttributedString
&&
173 ClsName
== &Ctx
.Idents
.get("NSAttributedString"))
175 // FIXME: Should we walk the chain of classes?
176 return ClsName
== &Ctx
.Idents
.get("NSString") ||
177 ClsName
== &Ctx
.Idents
.get("NSMutableString");
180 static inline bool isCFStringType(QualType T
, ASTContext
&Ctx
) {
181 const auto *PT
= T
->getAs
<PointerType
>();
185 const auto *RT
= PT
->getPointeeType()->getAs
<RecordType
>();
189 const RecordDecl
*RD
= RT
->getDecl();
190 if (RD
->getTagKind() != TTK_Struct
)
193 return RD
->getIdentifier() == &Ctx
.Idents
.get("__CFString");
196 static unsigned getNumAttributeArgs(const ParsedAttr
&AL
) {
197 // FIXME: Include the type in the argument list.
198 return AL
.getNumArgs() + AL
.hasParsedType();
201 /// A helper function to provide Attribute Location for the Attr types
202 /// AND the ParsedAttr.
203 template <typename AttrInfo
>
204 static std::enable_if_t
<std::is_base_of
<Attr
, AttrInfo
>::value
, SourceLocation
>
205 getAttrLoc(const AttrInfo
&AL
) {
206 return AL
.getLocation();
208 static SourceLocation
getAttrLoc(const ParsedAttr
&AL
) { return AL
.getLoc(); }
210 /// If Expr is a valid integer constant, get the value of the integer
211 /// expression and return success or failure. May output an error.
213 /// Negative argument is implicitly converted to unsigned, unless
214 /// \p StrictlyUnsigned is true.
215 template <typename AttrInfo
>
216 static bool checkUInt32Argument(Sema
&S
, const AttrInfo
&AI
, const Expr
*Expr
,
217 uint32_t &Val
, unsigned Idx
= UINT_MAX
,
218 bool StrictlyUnsigned
= false) {
219 Optional
<llvm::APSInt
> I
= llvm::APSInt(32);
220 if (Expr
->isTypeDependent() ||
221 !(I
= Expr
->getIntegerConstantExpr(S
.Context
))) {
223 S
.Diag(getAttrLoc(AI
), diag::err_attribute_argument_n_type
)
224 << &AI
<< Idx
<< AANT_ArgumentIntegerConstant
225 << Expr
->getSourceRange();
227 S
.Diag(getAttrLoc(AI
), diag::err_attribute_argument_type
)
228 << &AI
<< AANT_ArgumentIntegerConstant
<< Expr
->getSourceRange();
232 if (!I
->isIntN(32)) {
233 S
.Diag(Expr
->getExprLoc(), diag::err_ice_too_large
)
234 << toString(*I
, 10, false) << 32 << /* Unsigned */ 1;
238 if (StrictlyUnsigned
&& I
->isSigned() && I
->isNegative()) {
239 S
.Diag(getAttrLoc(AI
), diag::err_attribute_requires_positive_integer
)
240 << &AI
<< /*non-negative*/ 1;
244 Val
= (uint32_t)I
->getZExtValue();
248 /// Wrapper around checkUInt32Argument, with an extra check to be sure
249 /// that the result will fit into a regular (signed) int. All args have the same
250 /// purpose as they do in checkUInt32Argument.
251 template <typename AttrInfo
>
252 static bool checkPositiveIntArgument(Sema
&S
, const AttrInfo
&AI
, const Expr
*Expr
,
253 int &Val
, unsigned Idx
= UINT_MAX
) {
255 if (!checkUInt32Argument(S
, AI
, Expr
, UVal
, Idx
))
258 if (UVal
> (uint32_t)std::numeric_limits
<int>::max()) {
259 llvm::APSInt
I(32); // for toString
261 S
.Diag(Expr
->getExprLoc(), diag::err_ice_too_large
)
262 << toString(I
, 10, false) << 32 << /* Unsigned */ 0;
270 /// Diagnose mutually exclusive attributes when present on a given
271 /// declaration. Returns true if diagnosed.
272 template <typename AttrTy
>
273 static bool checkAttrMutualExclusion(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
274 if (const auto *A
= D
->getAttr
<AttrTy
>()) {
275 S
.Diag(AL
.getLoc(), diag::err_attributes_are_not_compatible
) << AL
<< A
;
276 S
.Diag(A
->getLocation(), diag::note_conflicting_attribute
);
282 template <typename AttrTy
>
283 static bool checkAttrMutualExclusion(Sema
&S
, Decl
*D
, const Attr
&AL
) {
284 if (const auto *A
= D
->getAttr
<AttrTy
>()) {
285 S
.Diag(AL
.getLocation(), diag::err_attributes_are_not_compatible
) << &AL
287 S
.Diag(A
->getLocation(), diag::note_conflicting_attribute
);
293 /// Check if IdxExpr is a valid parameter index for a function or
294 /// instance method D. May output an error.
296 /// \returns true if IdxExpr is a valid index.
297 template <typename AttrInfo
>
298 static bool checkFunctionOrMethodParameterIndex(
299 Sema
&S
, const Decl
*D
, const AttrInfo
&AI
, unsigned AttrArgNum
,
300 const Expr
*IdxExpr
, ParamIdx
&Idx
, bool CanIndexImplicitThis
= false) {
301 assert(isFunctionOrMethodOrBlock(D
));
303 // In C++ the implicit 'this' function parameter also counts.
304 // Parameters are counted from one.
305 bool HP
= hasFunctionProto(D
);
306 bool HasImplicitThisParam
= isInstanceMethod(D
);
307 bool IV
= HP
&& isFunctionOrMethodVariadic(D
);
309 (HP
? getFunctionOrMethodNumParams(D
) : 0) + HasImplicitThisParam
;
311 Optional
<llvm::APSInt
> IdxInt
;
312 if (IdxExpr
->isTypeDependent() ||
313 !(IdxInt
= IdxExpr
->getIntegerConstantExpr(S
.Context
))) {
314 S
.Diag(getAttrLoc(AI
), diag::err_attribute_argument_n_type
)
315 << &AI
<< AttrArgNum
<< AANT_ArgumentIntegerConstant
316 << IdxExpr
->getSourceRange();
320 unsigned IdxSource
= IdxInt
->getLimitedValue(UINT_MAX
);
321 if (IdxSource
< 1 || (!IV
&& IdxSource
> NumParams
)) {
322 S
.Diag(getAttrLoc(AI
), diag::err_attribute_argument_out_of_bounds
)
323 << &AI
<< AttrArgNum
<< IdxExpr
->getSourceRange();
326 if (HasImplicitThisParam
&& !CanIndexImplicitThis
) {
327 if (IdxSource
== 1) {
328 S
.Diag(getAttrLoc(AI
), diag::err_attribute_invalid_implicit_this_argument
)
329 << &AI
<< IdxExpr
->getSourceRange();
334 Idx
= ParamIdx(IdxSource
, D
);
338 /// Check if the argument \p E is a ASCII string literal. If not emit an error
339 /// and return false, otherwise set \p Str to the value of the string literal
341 bool Sema::checkStringLiteralArgumentAttr(const AttributeCommonInfo
&CI
,
342 const Expr
*E
, StringRef
&Str
,
343 SourceLocation
*ArgLocation
) {
344 const auto *Literal
= dyn_cast
<StringLiteral
>(E
->IgnoreParenCasts());
346 *ArgLocation
= E
->getBeginLoc();
348 if (!Literal
|| !Literal
->isOrdinary()) {
349 Diag(E
->getBeginLoc(), diag::err_attribute_argument_type
)
350 << CI
<< AANT_ArgumentString
;
354 Str
= Literal
->getString();
358 /// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
359 /// If not emit an error and return false. If the argument is an identifier it
360 /// will emit an error with a fixit hint and treat it as if it was a string
362 bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr
&AL
, unsigned ArgNum
,
364 SourceLocation
*ArgLocation
) {
365 // Look for identifiers. If we have one emit a hint to fix it to a literal.
366 if (AL
.isArgIdent(ArgNum
)) {
367 IdentifierLoc
*Loc
= AL
.getArgAsIdent(ArgNum
);
368 Diag(Loc
->Loc
, diag::err_attribute_argument_type
)
369 << AL
<< AANT_ArgumentString
370 << FixItHint::CreateInsertion(Loc
->Loc
, "\"")
371 << FixItHint::CreateInsertion(getLocForEndOfToken(Loc
->Loc
), "\"");
372 Str
= Loc
->Ident
->getName();
374 *ArgLocation
= Loc
->Loc
;
378 // Now check for an actual string literal.
379 Expr
*ArgExpr
= AL
.getArgAsExpr(ArgNum
);
380 return checkStringLiteralArgumentAttr(AL
, ArgExpr
, Str
, ArgLocation
);
383 /// Applies the given attribute to the Decl without performing any
384 /// additional semantic checking.
385 template <typename AttrType
>
386 static void handleSimpleAttribute(Sema
&S
, Decl
*D
,
387 const AttributeCommonInfo
&CI
) {
388 D
->addAttr(::new (S
.Context
) AttrType(S
.Context
, CI
));
391 template <typename
... DiagnosticArgs
>
392 static const Sema::SemaDiagnosticBuilder
&
393 appendDiagnostics(const Sema::SemaDiagnosticBuilder
&Bldr
) {
397 template <typename T
, typename
... DiagnosticArgs
>
398 static const Sema::SemaDiagnosticBuilder
&
399 appendDiagnostics(const Sema::SemaDiagnosticBuilder
&Bldr
, T
&&ExtraArg
,
400 DiagnosticArgs
&&... ExtraArgs
) {
401 return appendDiagnostics(Bldr
<< std::forward
<T
>(ExtraArg
),
402 std::forward
<DiagnosticArgs
>(ExtraArgs
)...);
405 /// Add an attribute @c AttrType to declaration @c D, provided that
406 /// @c PassesCheck is true.
407 /// Otherwise, emit diagnostic @c DiagID, passing in all parameters
408 /// specified in @c ExtraArgs.
409 template <typename AttrType
, typename
... DiagnosticArgs
>
410 static void handleSimpleAttributeOrDiagnose(Sema
&S
, Decl
*D
,
411 const AttributeCommonInfo
&CI
,
412 bool PassesCheck
, unsigned DiagID
,
413 DiagnosticArgs
&&... ExtraArgs
) {
415 Sema::SemaDiagnosticBuilder DB
= S
.Diag(D
->getBeginLoc(), DiagID
);
416 appendDiagnostics(DB
, std::forward
<DiagnosticArgs
>(ExtraArgs
)...);
419 handleSimpleAttribute
<AttrType
>(S
, D
, CI
);
422 /// Check if the passed-in expression is of type int or bool.
423 static bool isIntOrBool(Expr
*Exp
) {
424 QualType QT
= Exp
->getType();
425 return QT
->isBooleanType() || QT
->isIntegerType();
429 // Check to see if the type is a smart pointer of some kind. We assume
430 // it's a smart pointer if it defines both operator-> and operator*.
431 static bool threadSafetyCheckIsSmartPointer(Sema
&S
, const RecordType
* RT
) {
432 auto IsOverloadedOperatorPresent
= [&S
](const RecordDecl
*Record
,
433 OverloadedOperatorKind Op
) {
434 DeclContextLookupResult Result
=
435 Record
->lookup(S
.Context
.DeclarationNames
.getCXXOperatorName(Op
));
436 return !Result
.empty();
439 const RecordDecl
*Record
= RT
->getDecl();
440 bool foundStarOperator
= IsOverloadedOperatorPresent(Record
, OO_Star
);
441 bool foundArrowOperator
= IsOverloadedOperatorPresent(Record
, OO_Arrow
);
442 if (foundStarOperator
&& foundArrowOperator
)
445 const CXXRecordDecl
*CXXRecord
= dyn_cast
<CXXRecordDecl
>(Record
);
449 for (auto BaseSpecifier
: CXXRecord
->bases()) {
450 if (!foundStarOperator
)
451 foundStarOperator
= IsOverloadedOperatorPresent(
452 BaseSpecifier
.getType()->getAsRecordDecl(), OO_Star
);
453 if (!foundArrowOperator
)
454 foundArrowOperator
= IsOverloadedOperatorPresent(
455 BaseSpecifier
.getType()->getAsRecordDecl(), OO_Arrow
);
458 if (foundStarOperator
&& foundArrowOperator
)
464 /// Check if passed in Decl is a pointer type.
465 /// Note that this function may produce an error message.
466 /// \return true if the Decl is a pointer type; false otherwise
467 static bool threadSafetyCheckIsPointer(Sema
&S
, const Decl
*D
,
468 const ParsedAttr
&AL
) {
469 const auto *VD
= cast
<ValueDecl
>(D
);
470 QualType QT
= VD
->getType();
471 if (QT
->isAnyPointerType())
474 if (const auto *RT
= QT
->getAs
<RecordType
>()) {
475 // If it's an incomplete type, it could be a smart pointer; skip it.
476 // (We don't want to force template instantiation if we can avoid it,
477 // since that would alter the order in which templates are instantiated.)
478 if (RT
->isIncompleteType())
481 if (threadSafetyCheckIsSmartPointer(S
, RT
))
485 S
.Diag(AL
.getLoc(), diag::warn_thread_attribute_decl_not_pointer
) << AL
<< QT
;
489 /// Checks that the passed in QualType either is of RecordType or points
490 /// to RecordType. Returns the relevant RecordType, null if it does not exit.
491 static const RecordType
*getRecordType(QualType QT
) {
492 if (const auto *RT
= QT
->getAs
<RecordType
>())
495 // Now check if we point to record type.
496 if (const auto *PT
= QT
->getAs
<PointerType
>())
497 return PT
->getPointeeType()->getAs
<RecordType
>();
502 template <typename AttrType
>
503 static bool checkRecordDeclForAttr(const RecordDecl
*RD
) {
504 // Check if the record itself has the attribute.
505 if (RD
->hasAttr
<AttrType
>())
508 // Else check if any base classes have the attribute.
509 if (const auto *CRD
= dyn_cast
<CXXRecordDecl
>(RD
)) {
510 if (!CRD
->forallBases([](const CXXRecordDecl
*Base
) {
511 return !Base
->hasAttr
<AttrType
>();
518 static bool checkRecordTypeForCapability(Sema
&S
, QualType Ty
) {
519 const RecordType
*RT
= getRecordType(Ty
);
524 // Don't check for the capability if the class hasn't been defined yet.
525 if (RT
->isIncompleteType())
528 // Allow smart pointers to be used as capability objects.
529 // FIXME -- Check the type that the smart pointer points to.
530 if (threadSafetyCheckIsSmartPointer(S
, RT
))
533 return checkRecordDeclForAttr
<CapabilityAttr
>(RT
->getDecl());
536 static bool checkTypedefTypeForCapability(QualType Ty
) {
537 const auto *TD
= Ty
->getAs
<TypedefType
>();
541 TypedefNameDecl
*TN
= TD
->getDecl();
545 return TN
->hasAttr
<CapabilityAttr
>();
548 static bool typeHasCapability(Sema
&S
, QualType Ty
) {
549 if (checkTypedefTypeForCapability(Ty
))
552 if (checkRecordTypeForCapability(S
, Ty
))
558 static bool isCapabilityExpr(Sema
&S
, const Expr
*Ex
) {
559 // Capability expressions are simple expressions involving the boolean logic
560 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
561 // a DeclRefExpr is found, its type should be checked to determine whether it
562 // is a capability or not.
564 if (const auto *E
= dyn_cast
<CastExpr
>(Ex
))
565 return isCapabilityExpr(S
, E
->getSubExpr());
566 else if (const auto *E
= dyn_cast
<ParenExpr
>(Ex
))
567 return isCapabilityExpr(S
, E
->getSubExpr());
568 else if (const auto *E
= dyn_cast
<UnaryOperator
>(Ex
)) {
569 if (E
->getOpcode() == UO_LNot
|| E
->getOpcode() == UO_AddrOf
||
570 E
->getOpcode() == UO_Deref
)
571 return isCapabilityExpr(S
, E
->getSubExpr());
573 } else if (const auto *E
= dyn_cast
<BinaryOperator
>(Ex
)) {
574 if (E
->getOpcode() == BO_LAnd
|| E
->getOpcode() == BO_LOr
)
575 return isCapabilityExpr(S
, E
->getLHS()) &&
576 isCapabilityExpr(S
, E
->getRHS());
580 return typeHasCapability(S
, Ex
->getType());
583 /// Checks that all attribute arguments, starting from Sidx, resolve to
584 /// a capability object.
585 /// \param Sidx The attribute argument index to start checking with.
586 /// \param ParamIdxOk Whether an argument can be indexing into a function
588 static void checkAttrArgsAreCapabilityObjs(Sema
&S
, Decl
*D
,
589 const ParsedAttr
&AL
,
590 SmallVectorImpl
<Expr
*> &Args
,
592 bool ParamIdxOk
= false) {
593 if (Sidx
== AL
.getNumArgs()) {
594 // If we don't have any capability arguments, the attribute implicitly
595 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
596 // a non-static method, and that the class is a (scoped) capability.
597 const auto *MD
= dyn_cast
<const CXXMethodDecl
>(D
);
598 if (MD
&& !MD
->isStatic()) {
599 const CXXRecordDecl
*RD
= MD
->getParent();
600 // FIXME -- need to check this again on template instantiation
601 if (!checkRecordDeclForAttr
<CapabilityAttr
>(RD
) &&
602 !checkRecordDeclForAttr
<ScopedLockableAttr
>(RD
))
604 diag::warn_thread_attribute_not_on_capability_member
)
605 << AL
<< MD
->getParent();
607 S
.Diag(AL
.getLoc(), diag::warn_thread_attribute_not_on_non_static_member
)
612 for (unsigned Idx
= Sidx
; Idx
< AL
.getNumArgs(); ++Idx
) {
613 Expr
*ArgExp
= AL
.getArgAsExpr(Idx
);
615 if (ArgExp
->isTypeDependent()) {
616 // FIXME -- need to check this again on template instantiation
617 Args
.push_back(ArgExp
);
621 if (const auto *StrLit
= dyn_cast
<StringLiteral
>(ArgExp
)) {
622 if (StrLit
->getLength() == 0 ||
623 (StrLit
->isOrdinary() && StrLit
->getString() == StringRef("*"))) {
624 // Pass empty strings to the analyzer without warnings.
625 // Treat "*" as the universal lock.
626 Args
.push_back(ArgExp
);
630 // We allow constant strings to be used as a placeholder for expressions
631 // that are not valid C++ syntax, but warn that they are ignored.
632 S
.Diag(AL
.getLoc(), diag::warn_thread_attribute_ignored
) << AL
;
633 Args
.push_back(ArgExp
);
637 QualType ArgTy
= ArgExp
->getType();
639 // A pointer to member expression of the form &MyClass::mu is treated
640 // specially -- we need to look at the type of the member.
641 if (const auto *UOp
= dyn_cast
<UnaryOperator
>(ArgExp
))
642 if (UOp
->getOpcode() == UO_AddrOf
)
643 if (const auto *DRE
= dyn_cast
<DeclRefExpr
>(UOp
->getSubExpr()))
644 if (DRE
->getDecl()->isCXXInstanceMember())
645 ArgTy
= DRE
->getDecl()->getType();
647 // First see if we can just cast to record type, or pointer to record type.
648 const RecordType
*RT
= getRecordType(ArgTy
);
650 // Now check if we index into a record type function param.
651 if(!RT
&& ParamIdxOk
) {
652 const auto *FD
= dyn_cast
<FunctionDecl
>(D
);
653 const auto *IL
= dyn_cast
<IntegerLiteral
>(ArgExp
);
655 unsigned int NumParams
= FD
->getNumParams();
656 llvm::APInt ArgValue
= IL
->getValue();
657 uint64_t ParamIdxFromOne
= ArgValue
.getZExtValue();
658 uint64_t ParamIdxFromZero
= ParamIdxFromOne
- 1;
659 if (!ArgValue
.isStrictlyPositive() || ParamIdxFromOne
> NumParams
) {
661 diag::err_attribute_argument_out_of_bounds_extra_info
)
662 << AL
<< Idx
+ 1 << NumParams
;
665 ArgTy
= FD
->getParamDecl(ParamIdxFromZero
)->getType();
669 // If the type does not have a capability, see if the components of the
670 // expression have capabilities. This allows for writing C code where the
671 // capability may be on the type, and the expression is a capability
672 // boolean logic expression. Eg) requires_capability(A || B && !C)
673 if (!typeHasCapability(S
, ArgTy
) && !isCapabilityExpr(S
, ArgExp
))
674 S
.Diag(AL
.getLoc(), diag::warn_thread_attribute_argument_not_lockable
)
677 Args
.push_back(ArgExp
);
681 //===----------------------------------------------------------------------===//
682 // Attribute Implementations
683 //===----------------------------------------------------------------------===//
685 static void handlePtGuardedVarAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
686 if (!threadSafetyCheckIsPointer(S
, D
, AL
))
689 D
->addAttr(::new (S
.Context
) PtGuardedVarAttr(S
.Context
, AL
));
692 static bool checkGuardedByAttrCommon(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
,
694 SmallVector
<Expr
*, 1> Args
;
695 // check that all arguments are lockable objects
696 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
);
697 unsigned Size
= Args
.size();
706 static void handleGuardedByAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
708 if (!checkGuardedByAttrCommon(S
, D
, AL
, Arg
))
711 D
->addAttr(::new (S
.Context
) GuardedByAttr(S
.Context
, AL
, Arg
));
714 static void handlePtGuardedByAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
716 if (!checkGuardedByAttrCommon(S
, D
, AL
, Arg
))
719 if (!threadSafetyCheckIsPointer(S
, D
, AL
))
722 D
->addAttr(::new (S
.Context
) PtGuardedByAttr(S
.Context
, AL
, Arg
));
725 static bool checkAcquireOrderAttrCommon(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
,
726 SmallVectorImpl
<Expr
*> &Args
) {
727 if (!AL
.checkAtLeastNumArgs(S
, 1))
730 // Check that this attribute only applies to lockable types.
731 QualType QT
= cast
<ValueDecl
>(D
)->getType();
732 if (!QT
->isDependentType() && !typeHasCapability(S
, QT
)) {
733 S
.Diag(AL
.getLoc(), diag::warn_thread_attribute_decl_not_lockable
) << AL
;
737 // Check that all arguments are lockable objects.
738 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
);
745 static void handleAcquiredAfterAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
746 SmallVector
<Expr
*, 1> Args
;
747 if (!checkAcquireOrderAttrCommon(S
, D
, AL
, Args
))
750 Expr
**StartArg
= &Args
[0];
751 D
->addAttr(::new (S
.Context
)
752 AcquiredAfterAttr(S
.Context
, AL
, StartArg
, Args
.size()));
755 static void handleAcquiredBeforeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
756 SmallVector
<Expr
*, 1> Args
;
757 if (!checkAcquireOrderAttrCommon(S
, D
, AL
, Args
))
760 Expr
**StartArg
= &Args
[0];
761 D
->addAttr(::new (S
.Context
)
762 AcquiredBeforeAttr(S
.Context
, AL
, StartArg
, Args
.size()));
765 static bool checkLockFunAttrCommon(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
,
766 SmallVectorImpl
<Expr
*> &Args
) {
767 // zero or more arguments ok
768 // check that all arguments are lockable objects
769 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
, 0, /*ParamIdxOk=*/true);
774 static void handleAssertSharedLockAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
775 SmallVector
<Expr
*, 1> Args
;
776 if (!checkLockFunAttrCommon(S
, D
, AL
, Args
))
779 unsigned Size
= Args
.size();
780 Expr
**StartArg
= Size
== 0 ? nullptr : &Args
[0];
781 D
->addAttr(::new (S
.Context
)
782 AssertSharedLockAttr(S
.Context
, AL
, StartArg
, Size
));
785 static void handleAssertExclusiveLockAttr(Sema
&S
, Decl
*D
,
786 const ParsedAttr
&AL
) {
787 SmallVector
<Expr
*, 1> Args
;
788 if (!checkLockFunAttrCommon(S
, D
, AL
, Args
))
791 unsigned Size
= Args
.size();
792 Expr
**StartArg
= Size
== 0 ? nullptr : &Args
[0];
793 D
->addAttr(::new (S
.Context
)
794 AssertExclusiveLockAttr(S
.Context
, AL
, StartArg
, Size
));
797 /// Checks to be sure that the given parameter number is in bounds, and
798 /// is an integral type. Will emit appropriate diagnostics if this returns
801 /// AttrArgNo is used to actually retrieve the argument, so it's base-0.
802 template <typename AttrInfo
>
803 static bool checkParamIsIntegerType(Sema
&S
, const Decl
*D
, const AttrInfo
&AI
,
804 unsigned AttrArgNo
) {
805 assert(AI
.isArgExpr(AttrArgNo
) && "Expected expression argument");
806 Expr
*AttrArg
= AI
.getArgAsExpr(AttrArgNo
);
808 if (!checkFunctionOrMethodParameterIndex(S
, D
, AI
, AttrArgNo
+ 1, AttrArg
,
812 QualType ParamTy
= getFunctionOrMethodParamType(D
, Idx
.getASTIndex());
813 if (!ParamTy
->isIntegerType() && !ParamTy
->isCharType()) {
814 SourceLocation SrcLoc
= AttrArg
->getBeginLoc();
815 S
.Diag(SrcLoc
, diag::err_attribute_integers_only
)
816 << AI
<< getFunctionOrMethodParamRange(D
, Idx
.getASTIndex());
822 static void handleAllocSizeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
823 if (!AL
.checkAtLeastNumArgs(S
, 1) || !AL
.checkAtMostNumArgs(S
, 2))
826 assert(isFunctionOrMethod(D
) && hasFunctionProto(D
));
828 QualType RetTy
= getFunctionOrMethodResultType(D
);
829 if (!RetTy
->isPointerType()) {
830 S
.Diag(AL
.getLoc(), diag::warn_attribute_return_pointers_only
) << AL
;
834 const Expr
*SizeExpr
= AL
.getArgAsExpr(0);
836 // Parameter indices are 1-indexed, hence Index=1
837 if (!checkPositiveIntArgument(S
, AL
, SizeExpr
, SizeArgNoVal
, /*Idx=*/1))
839 if (!checkParamIsIntegerType(S
, D
, AL
, /*AttrArgNo=*/0))
841 ParamIdx
SizeArgNo(SizeArgNoVal
, D
);
843 ParamIdx NumberArgNo
;
844 if (AL
.getNumArgs() == 2) {
845 const Expr
*NumberExpr
= AL
.getArgAsExpr(1);
847 // Parameter indices are 1-based, hence Index=2
848 if (!checkPositiveIntArgument(S
, AL
, NumberExpr
, Val
, /*Idx=*/2))
850 if (!checkParamIsIntegerType(S
, D
, AL
, /*AttrArgNo=*/1))
852 NumberArgNo
= ParamIdx(Val
, D
);
855 D
->addAttr(::new (S
.Context
)
856 AllocSizeAttr(S
.Context
, AL
, SizeArgNo
, NumberArgNo
));
859 static bool checkTryLockFunAttrCommon(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
,
860 SmallVectorImpl
<Expr
*> &Args
) {
861 if (!AL
.checkAtLeastNumArgs(S
, 1))
864 if (!isIntOrBool(AL
.getArgAsExpr(0))) {
865 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
866 << AL
<< 1 << AANT_ArgumentIntOrBool
;
870 // check that all arguments are lockable objects
871 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
, 1);
876 static void handleSharedTrylockFunctionAttr(Sema
&S
, Decl
*D
,
877 const ParsedAttr
&AL
) {
878 SmallVector
<Expr
*, 2> Args
;
879 if (!checkTryLockFunAttrCommon(S
, D
, AL
, Args
))
882 D
->addAttr(::new (S
.Context
) SharedTrylockFunctionAttr(
883 S
.Context
, AL
, AL
.getArgAsExpr(0), Args
.data(), Args
.size()));
886 static void handleExclusiveTrylockFunctionAttr(Sema
&S
, Decl
*D
,
887 const ParsedAttr
&AL
) {
888 SmallVector
<Expr
*, 2> Args
;
889 if (!checkTryLockFunAttrCommon(S
, D
, AL
, Args
))
892 D
->addAttr(::new (S
.Context
) ExclusiveTrylockFunctionAttr(
893 S
.Context
, AL
, AL
.getArgAsExpr(0), Args
.data(), Args
.size()));
896 static void handleLockReturnedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
897 // check that the argument is lockable object
898 SmallVector
<Expr
*, 1> Args
;
899 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
);
900 unsigned Size
= Args
.size();
904 D
->addAttr(::new (S
.Context
) LockReturnedAttr(S
.Context
, AL
, Args
[0]));
907 static void handleLocksExcludedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
908 if (!AL
.checkAtLeastNumArgs(S
, 1))
911 // check that all arguments are lockable objects
912 SmallVector
<Expr
*, 1> Args
;
913 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
);
914 unsigned Size
= Args
.size();
917 Expr
**StartArg
= &Args
[0];
919 D
->addAttr(::new (S
.Context
)
920 LocksExcludedAttr(S
.Context
, AL
, StartArg
, Size
));
923 static bool checkFunctionConditionAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
,
924 Expr
*&Cond
, StringRef
&Msg
) {
925 Cond
= AL
.getArgAsExpr(0);
926 if (!Cond
->isTypeDependent()) {
927 ExprResult Converted
= S
.PerformContextuallyConvertToBool(Cond
);
928 if (Converted
.isInvalid())
930 Cond
= Converted
.get();
933 if (!S
.checkStringLiteralArgumentAttr(AL
, 1, Msg
))
937 Msg
= "<no message provided>";
939 SmallVector
<PartialDiagnosticAt
, 8> Diags
;
940 if (isa
<FunctionDecl
>(D
) && !Cond
->isValueDependent() &&
941 !Expr::isPotentialConstantExprUnevaluated(Cond
, cast
<FunctionDecl
>(D
),
943 S
.Diag(AL
.getLoc(), diag::err_attr_cond_never_constant_expr
) << AL
;
944 for (const PartialDiagnosticAt
&PDiag
: Diags
)
945 S
.Diag(PDiag
.first
, PDiag
.second
);
951 static void handleEnableIfAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
952 S
.Diag(AL
.getLoc(), diag::ext_clang_enable_if
);
956 if (checkFunctionConditionAttr(S
, D
, AL
, Cond
, Msg
))
957 D
->addAttr(::new (S
.Context
) EnableIfAttr(S
.Context
, AL
, Cond
, Msg
));
960 static void handleErrorAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
961 StringRef NewUserDiagnostic
;
962 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, NewUserDiagnostic
))
964 if (ErrorAttr
*EA
= S
.mergeErrorAttr(D
, AL
, NewUserDiagnostic
))
969 /// Determines if a given Expr references any of the given function's
970 /// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
971 class ArgumentDependenceChecker
972 : public RecursiveASTVisitor
<ArgumentDependenceChecker
> {
974 const CXXRecordDecl
*ClassType
;
976 llvm::SmallPtrSet
<const ParmVarDecl
*, 16> Parms
;
980 ArgumentDependenceChecker(const FunctionDecl
*FD
) {
982 if (const auto *MD
= dyn_cast
<CXXMethodDecl
>(FD
))
983 ClassType
= MD
->getParent();
987 Parms
.insert(FD
->param_begin(), FD
->param_end());
990 bool referencesArgs(Expr
*E
) {
996 bool VisitCXXThisExpr(CXXThisExpr
*E
) {
997 assert(E
->getType()->getPointeeCXXRecordDecl() == ClassType
&&
998 "`this` doesn't refer to the enclosing class?");
1003 bool VisitDeclRefExpr(DeclRefExpr
*DRE
) {
1004 if (const auto *PVD
= dyn_cast
<ParmVarDecl
>(DRE
->getDecl()))
1005 if (Parms
.count(PVD
)) {
1014 static void handleDiagnoseAsBuiltinAttr(Sema
&S
, Decl
*D
,
1015 const ParsedAttr
&AL
) {
1016 const auto *DeclFD
= cast
<FunctionDecl
>(D
);
1018 if (const auto *MethodDecl
= dyn_cast
<CXXMethodDecl
>(DeclFD
))
1019 if (!MethodDecl
->isStatic()) {
1020 S
.Diag(AL
.getLoc(), diag::err_attribute_no_member_function
) << AL
;
1024 auto DiagnoseType
= [&](unsigned Index
, AttributeArgumentNType T
) {
1025 SourceLocation Loc
= [&]() {
1026 auto Union
= AL
.getArg(Index
- 1);
1027 if (Union
.is
<Expr
*>())
1028 return Union
.get
<Expr
*>()->getBeginLoc();
1029 return Union
.get
<IdentifierLoc
*>()->Loc
;
1032 S
.Diag(Loc
, diag::err_attribute_argument_n_type
) << AL
<< Index
<< T
;
1035 FunctionDecl
*AttrFD
= [&]() -> FunctionDecl
* {
1036 if (!AL
.isArgExpr(0))
1038 auto *F
= dyn_cast_or_null
<DeclRefExpr
>(AL
.getArgAsExpr(0));
1041 return dyn_cast_or_null
<FunctionDecl
>(F
->getFoundDecl());
1044 if (!AttrFD
|| !AttrFD
->getBuiltinID(true)) {
1045 DiagnoseType(1, AANT_ArgumentBuiltinFunction
);
1049 if (AttrFD
->getNumParams() != AL
.getNumArgs() - 1) {
1050 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_number_arguments_for
)
1051 << AL
<< AttrFD
<< AttrFD
->getNumParams();
1055 SmallVector
<unsigned, 8> Indices
;
1057 for (unsigned I
= 1; I
< AL
.getNumArgs(); ++I
) {
1058 if (!AL
.isArgExpr(I
)) {
1059 DiagnoseType(I
+ 1, AANT_ArgumentIntegerConstant
);
1063 const Expr
*IndexExpr
= AL
.getArgAsExpr(I
);
1066 if (!checkUInt32Argument(S
, AL
, IndexExpr
, Index
, I
+ 1, false))
1069 if (Index
> DeclFD
->getNumParams()) {
1070 S
.Diag(AL
.getLoc(), diag::err_attribute_bounds_for_function
)
1071 << AL
<< Index
<< DeclFD
<< DeclFD
->getNumParams();
1075 QualType T1
= AttrFD
->getParamDecl(I
- 1)->getType();
1076 QualType T2
= DeclFD
->getParamDecl(Index
- 1)->getType();
1078 if (T1
.getCanonicalType().getUnqualifiedType() !=
1079 T2
.getCanonicalType().getUnqualifiedType()) {
1080 S
.Diag(IndexExpr
->getBeginLoc(), diag::err_attribute_parameter_types
)
1081 << AL
<< Index
<< DeclFD
<< T2
<< I
<< AttrFD
<< T1
;
1085 Indices
.push_back(Index
- 1);
1088 D
->addAttr(::new (S
.Context
) DiagnoseAsBuiltinAttr(
1089 S
.Context
, AL
, AttrFD
, Indices
.data(), Indices
.size()));
1092 static void handleDiagnoseIfAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1093 S
.Diag(AL
.getLoc(), diag::ext_clang_diagnose_if
);
1097 if (!checkFunctionConditionAttr(S
, D
, AL
, Cond
, Msg
))
1100 StringRef DiagTypeStr
;
1101 if (!S
.checkStringLiteralArgumentAttr(AL
, 2, DiagTypeStr
))
1104 DiagnoseIfAttr::DiagnosticType DiagType
;
1105 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr
, DiagType
)) {
1106 S
.Diag(AL
.getArgAsExpr(2)->getBeginLoc(),
1107 diag::err_diagnose_if_invalid_diagnostic_type
);
1111 bool ArgDependent
= false;
1112 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
))
1113 ArgDependent
= ArgumentDependenceChecker(FD
).referencesArgs(Cond
);
1114 D
->addAttr(::new (S
.Context
) DiagnoseIfAttr(
1115 S
.Context
, AL
, Cond
, Msg
, DiagType
, ArgDependent
, cast
<NamedDecl
>(D
)));
1118 static void handleNoBuiltinAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1119 static constexpr const StringRef kWildcard
= "*";
1121 llvm::SmallVector
<StringRef
, 16> Names
;
1122 bool HasWildcard
= false;
1124 const auto AddBuiltinName
= [&Names
, &HasWildcard
](StringRef Name
) {
1125 if (Name
== kWildcard
)
1127 Names
.push_back(Name
);
1130 // Add previously defined attributes.
1131 if (const auto *NBA
= D
->getAttr
<NoBuiltinAttr
>())
1132 for (StringRef BuiltinName
: NBA
->builtinNames())
1133 AddBuiltinName(BuiltinName
);
1135 // Add current attributes.
1136 if (AL
.getNumArgs() == 0)
1137 AddBuiltinName(kWildcard
);
1139 for (unsigned I
= 0, E
= AL
.getNumArgs(); I
!= E
; ++I
) {
1140 StringRef BuiltinName
;
1141 SourceLocation LiteralLoc
;
1142 if (!S
.checkStringLiteralArgumentAttr(AL
, I
, BuiltinName
, &LiteralLoc
))
1145 if (Builtin::Context::isBuiltinFunc(BuiltinName
))
1146 AddBuiltinName(BuiltinName
);
1148 S
.Diag(LiteralLoc
, diag::warn_attribute_no_builtin_invalid_builtin_name
)
1149 << BuiltinName
<< AL
;
1152 // Repeating the same attribute is fine.
1154 Names
.erase(std::unique(Names
.begin(), Names
.end()), Names
.end());
1156 // Empty no_builtin must be on its own.
1157 if (HasWildcard
&& Names
.size() > 1)
1158 S
.Diag(D
->getLocation(),
1159 diag::err_attribute_no_builtin_wildcard_or_builtin_name
)
1162 if (D
->hasAttr
<NoBuiltinAttr
>())
1163 D
->dropAttr
<NoBuiltinAttr
>();
1164 D
->addAttr(::new (S
.Context
)
1165 NoBuiltinAttr(S
.Context
, AL
, Names
.data(), Names
.size()));
1168 static void handlePassObjectSizeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1169 if (D
->hasAttr
<PassObjectSizeAttr
>()) {
1170 S
.Diag(D
->getBeginLoc(), diag::err_attribute_only_once_per_parameter
) << AL
;
1174 Expr
*E
= AL
.getArgAsExpr(0);
1176 if (!checkUInt32Argument(S
, AL
, E
, Type
, /*Idx=*/1))
1179 // pass_object_size's argument is passed in as the second argument of
1180 // __builtin_object_size. So, it has the same constraints as that second
1181 // argument; namely, it must be in the range [0, 3].
1183 S
.Diag(E
->getBeginLoc(), diag::err_attribute_argument_out_of_range
)
1184 << AL
<< 0 << 3 << E
->getSourceRange();
1188 // pass_object_size is only supported on constant pointer parameters; as a
1189 // kindness to users, we allow the parameter to be non-const for declarations.
1190 // At this point, we have no clue if `D` belongs to a function declaration or
1191 // definition, so we defer the constness check until later.
1192 if (!cast
<ParmVarDecl
>(D
)->getType()->isPointerType()) {
1193 S
.Diag(D
->getBeginLoc(), diag::err_attribute_pointers_only
) << AL
<< 1;
1197 D
->addAttr(::new (S
.Context
) PassObjectSizeAttr(S
.Context
, AL
, (int)Type
));
1200 static void handleConsumableAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1201 ConsumableAttr::ConsumedState DefaultState
;
1203 if (AL
.isArgIdent(0)) {
1204 IdentifierLoc
*IL
= AL
.getArgAsIdent(0);
1205 if (!ConsumableAttr::ConvertStrToConsumedState(IL
->Ident
->getName(),
1207 S
.Diag(IL
->Loc
, diag::warn_attribute_type_not_supported
) << AL
1212 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
1213 << AL
<< AANT_ArgumentIdentifier
;
1217 D
->addAttr(::new (S
.Context
) ConsumableAttr(S
.Context
, AL
, DefaultState
));
1220 static bool checkForConsumableClass(Sema
&S
, const CXXMethodDecl
*MD
,
1221 const ParsedAttr
&AL
) {
1222 QualType ThisType
= MD
->getThisType()->getPointeeType();
1224 if (const CXXRecordDecl
*RD
= ThisType
->getAsCXXRecordDecl()) {
1225 if (!RD
->hasAttr
<ConsumableAttr
>()) {
1226 S
.Diag(AL
.getLoc(), diag::warn_attr_on_unconsumable_class
) << RD
;
1235 static void handleCallableWhenAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1236 if (!AL
.checkAtLeastNumArgs(S
, 1))
1239 if (!checkForConsumableClass(S
, cast
<CXXMethodDecl
>(D
), AL
))
1242 SmallVector
<CallableWhenAttr::ConsumedState
, 3> States
;
1243 for (unsigned ArgIndex
= 0; ArgIndex
< AL
.getNumArgs(); ++ArgIndex
) {
1244 CallableWhenAttr::ConsumedState CallableState
;
1246 StringRef StateString
;
1248 if (AL
.isArgIdent(ArgIndex
)) {
1249 IdentifierLoc
*Ident
= AL
.getArgAsIdent(ArgIndex
);
1250 StateString
= Ident
->Ident
->getName();
1253 if (!S
.checkStringLiteralArgumentAttr(AL
, ArgIndex
, StateString
, &Loc
))
1257 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString
,
1259 S
.Diag(Loc
, diag::warn_attribute_type_not_supported
) << AL
<< StateString
;
1263 States
.push_back(CallableState
);
1266 D
->addAttr(::new (S
.Context
)
1267 CallableWhenAttr(S
.Context
, AL
, States
.data(), States
.size()));
1270 static void handleParamTypestateAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1271 ParamTypestateAttr::ConsumedState ParamState
;
1273 if (AL
.isArgIdent(0)) {
1274 IdentifierLoc
*Ident
= AL
.getArgAsIdent(0);
1275 StringRef StateString
= Ident
->Ident
->getName();
1277 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString
,
1279 S
.Diag(Ident
->Loc
, diag::warn_attribute_type_not_supported
)
1280 << AL
<< StateString
;
1284 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
1285 << AL
<< AANT_ArgumentIdentifier
;
1289 // FIXME: This check is currently being done in the analysis. It can be
1290 // enabled here only after the parser propagates attributes at
1291 // template specialization definition, not declaration.
1292 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1293 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1295 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1296 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1297 // ReturnType.getAsString();
1301 D
->addAttr(::new (S
.Context
) ParamTypestateAttr(S
.Context
, AL
, ParamState
));
1304 static void handleReturnTypestateAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1305 ReturnTypestateAttr::ConsumedState ReturnState
;
1307 if (AL
.isArgIdent(0)) {
1308 IdentifierLoc
*IL
= AL
.getArgAsIdent(0);
1309 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL
->Ident
->getName(),
1311 S
.Diag(IL
->Loc
, diag::warn_attribute_type_not_supported
) << AL
1316 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
1317 << AL
<< AANT_ArgumentIdentifier
;
1321 // FIXME: This check is currently being done in the analysis. It can be
1322 // enabled here only after the parser propagates attributes at
1323 // template specialization definition, not declaration.
1324 //QualType ReturnType;
1326 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1327 // ReturnType = Param->getType();
1329 //} else if (const CXXConstructorDecl *Constructor =
1330 // dyn_cast<CXXConstructorDecl>(D)) {
1331 // ReturnType = Constructor->getThisType()->getPointeeType();
1335 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1338 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1340 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1341 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1342 // ReturnType.getAsString();
1346 D
->addAttr(::new (S
.Context
) ReturnTypestateAttr(S
.Context
, AL
, ReturnState
));
1349 static void handleSetTypestateAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1350 if (!checkForConsumableClass(S
, cast
<CXXMethodDecl
>(D
), AL
))
1353 SetTypestateAttr::ConsumedState NewState
;
1354 if (AL
.isArgIdent(0)) {
1355 IdentifierLoc
*Ident
= AL
.getArgAsIdent(0);
1356 StringRef Param
= Ident
->Ident
->getName();
1357 if (!SetTypestateAttr::ConvertStrToConsumedState(Param
, NewState
)) {
1358 S
.Diag(Ident
->Loc
, diag::warn_attribute_type_not_supported
) << AL
1363 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
1364 << AL
<< AANT_ArgumentIdentifier
;
1368 D
->addAttr(::new (S
.Context
) SetTypestateAttr(S
.Context
, AL
, NewState
));
1371 static void handleTestTypestateAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1372 if (!checkForConsumableClass(S
, cast
<CXXMethodDecl
>(D
), AL
))
1375 TestTypestateAttr::ConsumedState TestState
;
1376 if (AL
.isArgIdent(0)) {
1377 IdentifierLoc
*Ident
= AL
.getArgAsIdent(0);
1378 StringRef Param
= Ident
->Ident
->getName();
1379 if (!TestTypestateAttr::ConvertStrToConsumedState(Param
, TestState
)) {
1380 S
.Diag(Ident
->Loc
, diag::warn_attribute_type_not_supported
) << AL
1385 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
1386 << AL
<< AANT_ArgumentIdentifier
;
1390 D
->addAttr(::new (S
.Context
) TestTypestateAttr(S
.Context
, AL
, TestState
));
1393 static void handleExtVectorTypeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1394 // Remember this typedef decl, we will need it later for diagnostics.
1395 S
.ExtVectorDecls
.push_back(cast
<TypedefNameDecl
>(D
));
1398 static void handlePackedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1399 if (auto *TD
= dyn_cast
<TagDecl
>(D
))
1400 TD
->addAttr(::new (S
.Context
) PackedAttr(S
.Context
, AL
));
1401 else if (auto *FD
= dyn_cast
<FieldDecl
>(D
)) {
1402 bool BitfieldByteAligned
= (!FD
->getType()->isDependentType() &&
1403 !FD
->getType()->isIncompleteType() &&
1405 S
.Context
.getTypeAlign(FD
->getType()) <= 8);
1407 if (S
.getASTContext().getTargetInfo().getTriple().isPS()) {
1408 if (BitfieldByteAligned
)
1409 // The PS4/PS5 targets need to maintain ABI backwards compatibility.
1410 S
.Diag(AL
.getLoc(), diag::warn_attribute_ignored_for_field_of_type
)
1411 << AL
<< FD
->getType();
1413 FD
->addAttr(::new (S
.Context
) PackedAttr(S
.Context
, AL
));
1415 // Report warning about changed offset in the newer compiler versions.
1416 if (BitfieldByteAligned
)
1417 S
.Diag(AL
.getLoc(), diag::warn_attribute_packed_for_bitfield
);
1419 FD
->addAttr(::new (S
.Context
) PackedAttr(S
.Context
, AL
));
1423 S
.Diag(AL
.getLoc(), diag::warn_attribute_ignored
) << AL
;
1426 static void handlePreferredName(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1427 auto *RD
= cast
<CXXRecordDecl
>(D
);
1428 ClassTemplateDecl
*CTD
= RD
->getDescribedClassTemplate();
1429 assert(CTD
&& "attribute does not appertain to this declaration");
1431 ParsedType PT
= AL
.getTypeArg();
1432 TypeSourceInfo
*TSI
= nullptr;
1433 QualType T
= S
.GetTypeFromParser(PT
, &TSI
);
1435 TSI
= S
.Context
.getTrivialTypeSourceInfo(T
, AL
.getLoc());
1437 if (!T
.hasQualifiers() && T
->isTypedefNameType()) {
1438 // Find the template name, if this type names a template specialization.
1439 const TemplateDecl
*Template
= nullptr;
1440 if (const auto *CTSD
= dyn_cast_or_null
<ClassTemplateSpecializationDecl
>(
1441 T
->getAsCXXRecordDecl())) {
1442 Template
= CTSD
->getSpecializedTemplate();
1443 } else if (const auto *TST
= T
->getAs
<TemplateSpecializationType
>()) {
1444 while (TST
&& TST
->isTypeAlias())
1445 TST
= TST
->getAliasedType()->getAs
<TemplateSpecializationType
>();
1447 Template
= TST
->getTemplateName().getAsTemplateDecl();
1450 if (Template
&& declaresSameEntity(Template
, CTD
)) {
1451 D
->addAttr(::new (S
.Context
) PreferredNameAttr(S
.Context
, AL
, TSI
));
1456 S
.Diag(AL
.getLoc(), diag::err_attribute_preferred_name_arg_invalid
)
1458 if (const auto *TT
= T
->getAs
<TypedefType
>())
1459 S
.Diag(TT
->getDecl()->getLocation(), diag::note_entity_declared_at
)
1463 static bool checkIBOutletCommon(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1464 // The IBOutlet/IBOutletCollection attributes only apply to instance
1465 // variables or properties of Objective-C classes. The outlet must also
1466 // have an object reference type.
1467 if (const auto *VD
= dyn_cast
<ObjCIvarDecl
>(D
)) {
1468 if (!VD
->getType()->getAs
<ObjCObjectPointerType
>()) {
1469 S
.Diag(AL
.getLoc(), diag::warn_iboutlet_object_type
)
1470 << AL
<< VD
->getType() << 0;
1474 else if (const auto *PD
= dyn_cast
<ObjCPropertyDecl
>(D
)) {
1475 if (!PD
->getType()->getAs
<ObjCObjectPointerType
>()) {
1476 S
.Diag(AL
.getLoc(), diag::warn_iboutlet_object_type
)
1477 << AL
<< PD
->getType() << 1;
1482 S
.Diag(AL
.getLoc(), diag::warn_attribute_iboutlet
) << AL
;
1489 static void handleIBOutlet(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1490 if (!checkIBOutletCommon(S
, D
, AL
))
1493 D
->addAttr(::new (S
.Context
) IBOutletAttr(S
.Context
, AL
));
1496 static void handleIBOutletCollection(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1498 // The iboutletcollection attribute can have zero or one arguments.
1499 if (AL
.getNumArgs() > 1) {
1500 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_number_arguments
) << AL
<< 1;
1504 if (!checkIBOutletCommon(S
, D
, AL
))
1509 if (AL
.hasParsedType())
1510 PT
= AL
.getTypeArg();
1512 PT
= S
.getTypeName(S
.Context
.Idents
.get("NSObject"), AL
.getLoc(),
1513 S
.getScopeForContext(D
->getDeclContext()->getParent()));
1515 S
.Diag(AL
.getLoc(), diag::err_iboutletcollection_type
) << "NSObject";
1520 TypeSourceInfo
*QTLoc
= nullptr;
1521 QualType QT
= S
.GetTypeFromParser(PT
, &QTLoc
);
1523 QTLoc
= S
.Context
.getTrivialTypeSourceInfo(QT
, AL
.getLoc());
1525 // Diagnose use of non-object type in iboutletcollection attribute.
1526 // FIXME. Gnu attribute extension ignores use of builtin types in
1527 // attributes. So, __attribute__((iboutletcollection(char))) will be
1528 // treated as __attribute__((iboutletcollection())).
1529 if (!QT
->isObjCIdType() && !QT
->isObjCObjectType()) {
1531 QT
->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1532 : diag::err_iboutletcollection_type
) << QT
;
1536 D
->addAttr(::new (S
.Context
) IBOutletCollectionAttr(S
.Context
, AL
, QTLoc
));
1539 bool Sema::isValidPointerAttrType(QualType T
, bool RefOkay
) {
1541 if (T
->isReferenceType())
1544 T
= T
.getNonReferenceType();
1547 // The nonnull attribute, and other similar attributes, can be applied to a
1548 // transparent union that contains a pointer type.
1549 if (const RecordType
*UT
= T
->getAsUnionType()) {
1550 if (UT
&& UT
->getDecl()->hasAttr
<TransparentUnionAttr
>()) {
1551 RecordDecl
*UD
= UT
->getDecl();
1552 for (const auto *I
: UD
->fields()) {
1553 QualType QT
= I
->getType();
1554 if (QT
->isAnyPointerType() || QT
->isBlockPointerType())
1560 return T
->isAnyPointerType() || T
->isBlockPointerType();
1563 static bool attrNonNullArgCheck(Sema
&S
, QualType T
, const ParsedAttr
&AL
,
1564 SourceRange AttrParmRange
,
1565 SourceRange TypeRange
,
1566 bool isReturnValue
= false) {
1567 if (!S
.isValidPointerAttrType(T
)) {
1569 S
.Diag(AL
.getLoc(), diag::warn_attribute_return_pointers_only
)
1570 << AL
<< AttrParmRange
<< TypeRange
;
1572 S
.Diag(AL
.getLoc(), diag::warn_attribute_pointers_only
)
1573 << AL
<< AttrParmRange
<< TypeRange
<< 0;
1579 static void handleNonNullAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1580 SmallVector
<ParamIdx
, 8> NonNullArgs
;
1581 for (unsigned I
= 0; I
< AL
.getNumArgs(); ++I
) {
1582 Expr
*Ex
= AL
.getArgAsExpr(I
);
1584 if (!checkFunctionOrMethodParameterIndex(S
, D
, AL
, I
+ 1, Ex
, Idx
))
1587 // Is the function argument a pointer type?
1588 if (Idx
.getASTIndex() < getFunctionOrMethodNumParams(D
) &&
1589 !attrNonNullArgCheck(
1590 S
, getFunctionOrMethodParamType(D
, Idx
.getASTIndex()), AL
,
1591 Ex
->getSourceRange(),
1592 getFunctionOrMethodParamRange(D
, Idx
.getASTIndex())))
1595 NonNullArgs
.push_back(Idx
);
1598 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1599 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1600 // check if the attribute came from a macro expansion or a template
1602 if (NonNullArgs
.empty() && AL
.getLoc().isFileID() &&
1603 !S
.inTemplateInstantiation()) {
1604 bool AnyPointers
= isFunctionOrMethodVariadic(D
);
1605 for (unsigned I
= 0, E
= getFunctionOrMethodNumParams(D
);
1606 I
!= E
&& !AnyPointers
; ++I
) {
1607 QualType T
= getFunctionOrMethodParamType(D
, I
);
1608 if (T
->isDependentType() || S
.isValidPointerAttrType(T
))
1613 S
.Diag(AL
.getLoc(), diag::warn_attribute_nonnull_no_pointers
);
1616 ParamIdx
*Start
= NonNullArgs
.data();
1617 unsigned Size
= NonNullArgs
.size();
1618 llvm::array_pod_sort(Start
, Start
+ Size
);
1619 D
->addAttr(::new (S
.Context
) NonNullAttr(S
.Context
, AL
, Start
, Size
));
1622 static void handleNonNullAttrParameter(Sema
&S
, ParmVarDecl
*D
,
1623 const ParsedAttr
&AL
) {
1624 if (AL
.getNumArgs() > 0) {
1625 if (D
->getFunctionType()) {
1626 handleNonNullAttr(S
, D
, AL
);
1628 S
.Diag(AL
.getLoc(), diag::warn_attribute_nonnull_parm_no_args
)
1629 << D
->getSourceRange();
1634 // Is the argument a pointer type?
1635 if (!attrNonNullArgCheck(S
, D
->getType(), AL
, SourceRange(),
1636 D
->getSourceRange()))
1639 D
->addAttr(::new (S
.Context
) NonNullAttr(S
.Context
, AL
, nullptr, 0));
1642 static void handleReturnsNonNullAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1643 QualType ResultType
= getFunctionOrMethodResultType(D
);
1644 SourceRange SR
= getFunctionOrMethodResultSourceRange(D
);
1645 if (!attrNonNullArgCheck(S
, ResultType
, AL
, SourceRange(), SR
,
1646 /* isReturnValue */ true))
1649 D
->addAttr(::new (S
.Context
) ReturnsNonNullAttr(S
.Context
, AL
));
1652 static void handleNoEscapeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1653 if (D
->isInvalidDecl())
1656 // noescape only applies to pointer types.
1657 QualType T
= cast
<ParmVarDecl
>(D
)->getType();
1658 if (!S
.isValidPointerAttrType(T
, /* RefOkay */ true)) {
1659 S
.Diag(AL
.getLoc(), diag::warn_attribute_pointers_only
)
1660 << AL
<< AL
.getRange() << 0;
1664 D
->addAttr(::new (S
.Context
) NoEscapeAttr(S
.Context
, AL
));
1667 static void handleAssumeAlignedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1668 Expr
*E
= AL
.getArgAsExpr(0),
1669 *OE
= AL
.getNumArgs() > 1 ? AL
.getArgAsExpr(1) : nullptr;
1670 S
.AddAssumeAlignedAttr(D
, AL
, E
, OE
);
1673 static void handleAllocAlignAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1674 S
.AddAllocAlignAttr(D
, AL
, AL
.getArgAsExpr(0));
1677 void Sema::AddAssumeAlignedAttr(Decl
*D
, const AttributeCommonInfo
&CI
, Expr
*E
,
1679 QualType ResultType
= getFunctionOrMethodResultType(D
);
1680 SourceRange SR
= getFunctionOrMethodResultSourceRange(D
);
1682 AssumeAlignedAttr
TmpAttr(Context
, CI
, E
, OE
);
1683 SourceLocation AttrLoc
= TmpAttr
.getLocation();
1685 if (!isValidPointerAttrType(ResultType
, /* RefOkay */ true)) {
1686 Diag(AttrLoc
, diag::warn_attribute_return_pointers_refs_only
)
1687 << &TmpAttr
<< TmpAttr
.getRange() << SR
;
1691 if (!E
->isValueDependent()) {
1692 Optional
<llvm::APSInt
> I
= llvm::APSInt(64);
1693 if (!(I
= E
->getIntegerConstantExpr(Context
))) {
1695 Diag(AttrLoc
, diag::err_attribute_argument_n_type
)
1696 << &TmpAttr
<< 1 << AANT_ArgumentIntegerConstant
1697 << E
->getSourceRange();
1699 Diag(AttrLoc
, diag::err_attribute_argument_type
)
1700 << &TmpAttr
<< AANT_ArgumentIntegerConstant
1701 << E
->getSourceRange();
1705 if (!I
->isPowerOf2()) {
1706 Diag(AttrLoc
, diag::err_alignment_not_power_of_two
)
1707 << E
->getSourceRange();
1711 if (*I
> Sema::MaximumAlignment
)
1712 Diag(CI
.getLoc(), diag::warn_assume_aligned_too_great
)
1713 << CI
.getRange() << Sema::MaximumAlignment
;
1716 if (OE
&& !OE
->isValueDependent() && !OE
->isIntegerConstantExpr(Context
)) {
1717 Diag(AttrLoc
, diag::err_attribute_argument_n_type
)
1718 << &TmpAttr
<< 2 << AANT_ArgumentIntegerConstant
1719 << OE
->getSourceRange();
1723 D
->addAttr(::new (Context
) AssumeAlignedAttr(Context
, CI
, E
, OE
));
1726 void Sema::AddAllocAlignAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
1728 QualType ResultType
= getFunctionOrMethodResultType(D
);
1730 AllocAlignAttr
TmpAttr(Context
, CI
, ParamIdx());
1731 SourceLocation AttrLoc
= CI
.getLoc();
1733 if (!ResultType
->isDependentType() &&
1734 !isValidPointerAttrType(ResultType
, /* RefOkay */ true)) {
1735 Diag(AttrLoc
, diag::warn_attribute_return_pointers_refs_only
)
1736 << &TmpAttr
<< CI
.getRange() << getFunctionOrMethodResultSourceRange(D
);
1741 const auto *FuncDecl
= cast
<FunctionDecl
>(D
);
1742 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl
, TmpAttr
,
1743 /*AttrArgNum=*/1, ParamExpr
, Idx
))
1746 QualType Ty
= getFunctionOrMethodParamType(D
, Idx
.getASTIndex());
1747 if (!Ty
->isDependentType() && !Ty
->isIntegralType(Context
) &&
1748 !Ty
->isAlignValT()) {
1749 Diag(ParamExpr
->getBeginLoc(), diag::err_attribute_integers_only
)
1751 << FuncDecl
->getParamDecl(Idx
.getASTIndex())->getSourceRange();
1755 D
->addAttr(::new (Context
) AllocAlignAttr(Context
, CI
, Idx
));
1758 /// Check if \p AssumptionStr is a known assumption and warn if not.
1759 static void checkAssumptionAttr(Sema
&S
, SourceLocation Loc
,
1760 StringRef AssumptionStr
) {
1761 if (llvm::KnownAssumptionStrings
.count(AssumptionStr
))
1764 unsigned BestEditDistance
= 3;
1765 StringRef Suggestion
;
1766 for (const auto &KnownAssumptionIt
: llvm::KnownAssumptionStrings
) {
1767 unsigned EditDistance
=
1768 AssumptionStr
.edit_distance(KnownAssumptionIt
.getKey());
1769 if (EditDistance
< BestEditDistance
) {
1770 Suggestion
= KnownAssumptionIt
.getKey();
1771 BestEditDistance
= EditDistance
;
1775 if (!Suggestion
.empty())
1776 S
.Diag(Loc
, diag::warn_assume_attribute_string_unknown_suggested
)
1777 << AssumptionStr
<< Suggestion
;
1779 S
.Diag(Loc
, diag::warn_assume_attribute_string_unknown
) << AssumptionStr
;
1782 static void handleAssumumptionAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1783 // Handle the case where the attribute has a text message.
1785 SourceLocation AttrStrLoc
;
1786 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &AttrStrLoc
))
1789 checkAssumptionAttr(S
, AttrStrLoc
, Str
);
1791 D
->addAttr(::new (S
.Context
) AssumptionAttr(S
.Context
, AL
, Str
));
1794 /// Normalize the attribute, __foo__ becomes foo.
1795 /// Returns true if normalization was applied.
1796 static bool normalizeName(StringRef
&AttrName
) {
1797 if (AttrName
.size() > 4 && AttrName
.startswith("__") &&
1798 AttrName
.endswith("__")) {
1799 AttrName
= AttrName
.drop_front(2).drop_back(2);
1805 static void handleOwnershipAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1806 // This attribute must be applied to a function declaration. The first
1807 // argument to the attribute must be an identifier, the name of the resource,
1808 // for example: malloc. The following arguments must be argument indexes, the
1809 // arguments must be of integer type for Returns, otherwise of pointer type.
1810 // The difference between Holds and Takes is that a pointer may still be used
1811 // after being held. free() should be __attribute((ownership_takes)), whereas
1812 // a list append function may well be __attribute((ownership_holds)).
1814 if (!AL
.isArgIdent(0)) {
1815 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
1816 << AL
<< 1 << AANT_ArgumentIdentifier
;
1820 // Figure out our Kind.
1821 OwnershipAttr::OwnershipKind K
=
1822 OwnershipAttr(S
.Context
, AL
, nullptr, nullptr, 0).getOwnKind();
1826 case OwnershipAttr::Takes
:
1827 case OwnershipAttr::Holds
:
1828 if (AL
.getNumArgs() < 2) {
1829 S
.Diag(AL
.getLoc(), diag::err_attribute_too_few_arguments
) << AL
<< 2;
1833 case OwnershipAttr::Returns
:
1834 if (AL
.getNumArgs() > 2) {
1835 S
.Diag(AL
.getLoc(), diag::err_attribute_too_many_arguments
) << AL
<< 1;
1841 IdentifierInfo
*Module
= AL
.getArgAsIdent(0)->Ident
;
1843 StringRef ModuleName
= Module
->getName();
1844 if (normalizeName(ModuleName
)) {
1845 Module
= &S
.PP
.getIdentifierTable().get(ModuleName
);
1848 SmallVector
<ParamIdx
, 8> OwnershipArgs
;
1849 for (unsigned i
= 1; i
< AL
.getNumArgs(); ++i
) {
1850 Expr
*Ex
= AL
.getArgAsExpr(i
);
1852 if (!checkFunctionOrMethodParameterIndex(S
, D
, AL
, i
, Ex
, Idx
))
1855 // Is the function argument a pointer type?
1856 QualType T
= getFunctionOrMethodParamType(D
, Idx
.getASTIndex());
1857 int Err
= -1; // No error
1859 case OwnershipAttr::Takes
:
1860 case OwnershipAttr::Holds
:
1861 if (!T
->isAnyPointerType() && !T
->isBlockPointerType())
1864 case OwnershipAttr::Returns
:
1865 if (!T
->isIntegerType())
1870 S
.Diag(AL
.getLoc(), diag::err_ownership_type
) << AL
<< Err
1871 << Ex
->getSourceRange();
1875 // Check we don't have a conflict with another ownership attribute.
1876 for (const auto *I
: D
->specific_attrs
<OwnershipAttr
>()) {
1877 // Cannot have two ownership attributes of different kinds for the same
1879 if (I
->getOwnKind() != K
&& llvm::is_contained(I
->args(), Idx
)) {
1880 S
.Diag(AL
.getLoc(), diag::err_attributes_are_not_compatible
) << AL
<< I
;
1882 } else if (K
== OwnershipAttr::Returns
&&
1883 I
->getOwnKind() == OwnershipAttr::Returns
) {
1884 // A returns attribute conflicts with any other returns attribute using
1885 // a different index.
1886 if (!llvm::is_contained(I
->args(), Idx
)) {
1887 S
.Diag(I
->getLocation(), diag::err_ownership_returns_index_mismatch
)
1888 << I
->args_begin()->getSourceIndex();
1890 S
.Diag(AL
.getLoc(), diag::note_ownership_returns_index_mismatch
)
1891 << Idx
.getSourceIndex() << Ex
->getSourceRange();
1896 OwnershipArgs
.push_back(Idx
);
1899 ParamIdx
*Start
= OwnershipArgs
.data();
1900 unsigned Size
= OwnershipArgs
.size();
1901 llvm::array_pod_sort(Start
, Start
+ Size
);
1902 D
->addAttr(::new (S
.Context
)
1903 OwnershipAttr(S
.Context
, AL
, Module
, Start
, Size
));
1906 static void handleWeakRefAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1907 // Check the attribute arguments.
1908 if (AL
.getNumArgs() > 1) {
1909 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_number_arguments
) << AL
<< 1;
1915 // static int a __attribute__((weakref ("v2")));
1916 // static int b() __attribute__((weakref ("f3")));
1918 // and ignores the attributes of
1920 // static int a __attribute__((weakref ("v2")));
1923 const DeclContext
*Ctx
= D
->getDeclContext()->getRedeclContext();
1924 if (!Ctx
->isFileContext()) {
1925 S
.Diag(AL
.getLoc(), diag::err_attribute_weakref_not_global_context
)
1926 << cast
<NamedDecl
>(D
);
1930 // The GCC manual says
1932 // At present, a declaration to which `weakref' is attached can only
1937 // Without a TARGET,
1938 // given as an argument to `weakref' or to `alias', `weakref' is
1939 // equivalent to `weak'.
1941 // gcc 4.4.1 will accept
1942 // int a7 __attribute__((weakref));
1944 // int a7 __attribute__((weak));
1945 // This looks like a bug in gcc. We reject that for now. We should revisit
1946 // it if this behaviour is actually used.
1949 // static ((alias ("y"), weakref)).
1950 // Should we? How to check that weakref is before or after alias?
1952 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1953 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1954 // StringRef parameter it was given anyway.
1956 if (AL
.getNumArgs() && S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
1957 // GCC will accept anything as the argument of weakref. Should we
1958 // check for an existing decl?
1959 D
->addAttr(::new (S
.Context
) AliasAttr(S
.Context
, AL
, Str
));
1961 D
->addAttr(::new (S
.Context
) WeakRefAttr(S
.Context
, AL
));
1964 static void handleIFuncAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1966 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
1969 // Aliases should be on declarations, not definitions.
1970 const auto *FD
= cast
<FunctionDecl
>(D
);
1971 if (FD
->isThisDeclarationADefinition()) {
1972 S
.Diag(AL
.getLoc(), diag::err_alias_is_definition
) << FD
<< 1;
1976 D
->addAttr(::new (S
.Context
) IFuncAttr(S
.Context
, AL
, Str
));
1979 static void handleAliasAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
1981 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
1984 if (S
.Context
.getTargetInfo().getTriple().isOSDarwin()) {
1985 S
.Diag(AL
.getLoc(), diag::err_alias_not_supported_on_darwin
);
1988 if (S
.Context
.getTargetInfo().getTriple().isNVPTX()) {
1989 S
.Diag(AL
.getLoc(), diag::err_alias_not_supported_on_nvptx
);
1992 // Aliases should be on declarations, not definitions.
1993 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
1994 if (FD
->isThisDeclarationADefinition()) {
1995 S
.Diag(AL
.getLoc(), diag::err_alias_is_definition
) << FD
<< 0;
1999 const auto *VD
= cast
<VarDecl
>(D
);
2000 if (VD
->isThisDeclarationADefinition() && VD
->isExternallyVisible()) {
2001 S
.Diag(AL
.getLoc(), diag::err_alias_is_definition
) << VD
<< 0;
2006 // Mark target used to prevent unneeded-internal-declaration warnings.
2007 if (!S
.LangOpts
.CPlusPlus
) {
2008 // FIXME: demangle Str for C++, as the attribute refers to the mangled
2009 // linkage name, not the pre-mangled identifier.
2010 const DeclarationNameInfo
target(&S
.Context
.Idents
.get(Str
), AL
.getLoc());
2011 LookupResult
LR(S
, target
, Sema::LookupOrdinaryName
);
2012 if (S
.LookupQualifiedName(LR
, S
.getCurLexicalContext()))
2013 for (NamedDecl
*ND
: LR
)
2014 ND
->markUsed(S
.Context
);
2017 D
->addAttr(::new (S
.Context
) AliasAttr(S
.Context
, AL
, Str
));
2020 static void handleTLSModelAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2022 SourceLocation LiteralLoc
;
2023 // Check that it is a string.
2024 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Model
, &LiteralLoc
))
2027 // Check that the value.
2028 if (Model
!= "global-dynamic" && Model
!= "local-dynamic"
2029 && Model
!= "initial-exec" && Model
!= "local-exec") {
2030 S
.Diag(LiteralLoc
, diag::err_attr_tlsmodel_arg
);
2034 if (S
.Context
.getTargetInfo().getTriple().isOSAIX() &&
2035 Model
!= "global-dynamic") {
2036 S
.Diag(LiteralLoc
, diag::err_aix_attr_unsupported_tls_model
) << Model
;
2040 D
->addAttr(::new (S
.Context
) TLSModelAttr(S
.Context
, AL
, Model
));
2043 static void handleRestrictAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2044 QualType ResultType
= getFunctionOrMethodResultType(D
);
2045 if (ResultType
->isAnyPointerType() || ResultType
->isBlockPointerType()) {
2046 D
->addAttr(::new (S
.Context
) RestrictAttr(S
.Context
, AL
));
2050 S
.Diag(AL
.getLoc(), diag::warn_attribute_return_pointers_only
)
2051 << AL
<< getFunctionOrMethodResultSourceRange(D
);
2054 static void handleCPUSpecificAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2055 // Ensure we don't combine these with themselves, since that causes some
2056 // confusing behavior.
2057 if (AL
.getParsedKind() == ParsedAttr::AT_CPUDispatch
) {
2058 if (checkAttrMutualExclusion
<CPUSpecificAttr
>(S
, D
, AL
))
2061 if (const auto *Other
= D
->getAttr
<CPUDispatchAttr
>()) {
2062 S
.Diag(AL
.getLoc(), diag::err_disallowed_duplicate_attribute
) << AL
;
2063 S
.Diag(Other
->getLocation(), diag::note_conflicting_attribute
);
2066 } else if (AL
.getParsedKind() == ParsedAttr::AT_CPUSpecific
) {
2067 if (checkAttrMutualExclusion
<CPUDispatchAttr
>(S
, D
, AL
))
2070 if (const auto *Other
= D
->getAttr
<CPUSpecificAttr
>()) {
2071 S
.Diag(AL
.getLoc(), diag::err_disallowed_duplicate_attribute
) << AL
;
2072 S
.Diag(Other
->getLocation(), diag::note_conflicting_attribute
);
2077 FunctionDecl
*FD
= cast
<FunctionDecl
>(D
);
2079 if (const auto *MD
= dyn_cast
<CXXMethodDecl
>(D
)) {
2080 if (MD
->getParent()->isLambda()) {
2081 S
.Diag(AL
.getLoc(), diag::err_attribute_dll_lambda
) << AL
;
2086 if (!AL
.checkAtLeastNumArgs(S
, 1))
2089 SmallVector
<IdentifierInfo
*, 8> CPUs
;
2090 for (unsigned ArgNo
= 0; ArgNo
< getNumAttributeArgs(AL
); ++ArgNo
) {
2091 if (!AL
.isArgIdent(ArgNo
)) {
2092 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
2093 << AL
<< AANT_ArgumentIdentifier
;
2097 IdentifierLoc
*CPUArg
= AL
.getArgAsIdent(ArgNo
);
2098 StringRef CPUName
= CPUArg
->Ident
->getName().trim();
2100 if (!S
.Context
.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName
)) {
2101 S
.Diag(CPUArg
->Loc
, diag::err_invalid_cpu_specific_dispatch_value
)
2102 << CPUName
<< (AL
.getKind() == ParsedAttr::AT_CPUDispatch
);
2106 const TargetInfo
&Target
= S
.Context
.getTargetInfo();
2107 if (llvm::any_of(CPUs
, [CPUName
, &Target
](const IdentifierInfo
*Cur
) {
2108 return Target
.CPUSpecificManglingCharacter(CPUName
) ==
2109 Target
.CPUSpecificManglingCharacter(Cur
->getName());
2111 S
.Diag(AL
.getLoc(), diag::warn_multiversion_duplicate_entries
);
2114 CPUs
.push_back(CPUArg
->Ident
);
2117 FD
->setIsMultiVersion(true);
2118 if (AL
.getKind() == ParsedAttr::AT_CPUSpecific
)
2119 D
->addAttr(::new (S
.Context
)
2120 CPUSpecificAttr(S
.Context
, AL
, CPUs
.data(), CPUs
.size()));
2122 D
->addAttr(::new (S
.Context
)
2123 CPUDispatchAttr(S
.Context
, AL
, CPUs
.data(), CPUs
.size()));
2126 static void handleCommonAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2127 if (S
.LangOpts
.CPlusPlus
) {
2128 S
.Diag(AL
.getLoc(), diag::err_attribute_not_supported_in_lang
)
2129 << AL
<< AttributeLangSupport::Cpp
;
2133 D
->addAttr(::new (S
.Context
) CommonAttr(S
.Context
, AL
));
2136 static void handleCmseNSEntryAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2137 if (S
.LangOpts
.CPlusPlus
&& !D
->getDeclContext()->isExternCContext()) {
2138 S
.Diag(AL
.getLoc(), diag::err_attribute_not_clinkage
) << AL
;
2142 const auto *FD
= cast
<FunctionDecl
>(D
);
2143 if (!FD
->isExternallyVisible()) {
2144 S
.Diag(AL
.getLoc(), diag::warn_attribute_cmse_entry_static
);
2148 D
->addAttr(::new (S
.Context
) CmseNSEntryAttr(S
.Context
, AL
));
2151 static void handleNakedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2152 if (AL
.isDeclspecAttribute()) {
2153 const auto &Triple
= S
.getASTContext().getTargetInfo().getTriple();
2154 const auto &Arch
= Triple
.getArch();
2155 if (Arch
!= llvm::Triple::x86
&&
2156 (Arch
!= llvm::Triple::arm
&& Arch
!= llvm::Triple::thumb
)) {
2157 S
.Diag(AL
.getLoc(), diag::err_attribute_not_supported_on_arch
)
2158 << AL
<< Triple
.getArchName();
2162 // This form is not allowed to be written on a member function (static or
2163 // nonstatic) when in Microsoft compatibility mode.
2164 if (S
.getLangOpts().MSVCCompat
&& isa
<CXXMethodDecl
>(D
)) {
2165 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_decl_type_str
)
2166 << AL
<< "non-member functions";
2171 D
->addAttr(::new (S
.Context
) NakedAttr(S
.Context
, AL
));
2174 static void handleNoReturnAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&Attrs
) {
2175 if (hasDeclarator(D
)) return;
2177 if (!isa
<ObjCMethodDecl
>(D
)) {
2178 S
.Diag(Attrs
.getLoc(), diag::warn_attribute_wrong_decl_type
)
2179 << Attrs
<< ExpectedFunctionOrMethod
;
2183 D
->addAttr(::new (S
.Context
) NoReturnAttr(S
.Context
, Attrs
));
2186 static void handleStandardNoReturnAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&A
) {
2187 // The [[_Noreturn]] spelling is deprecated in C2x, so if that was used,
2188 // issue an appropriate diagnostic. However, don't issue a diagnostic if the
2189 // attribute name comes from a macro expansion. We don't want to punish users
2190 // who write [[noreturn]] after including <stdnoreturn.h> (where 'noreturn'
2191 // is defined as a macro which expands to '_Noreturn').
2192 if (!S
.getLangOpts().CPlusPlus
&&
2193 A
.getSemanticSpelling() == CXX11NoReturnAttr::C2x_Noreturn
&&
2194 !(A
.getLoc().isMacroID() &&
2195 S
.getSourceManager().isInSystemMacro(A
.getLoc())))
2196 S
.Diag(A
.getLoc(), diag::warn_deprecated_noreturn_spelling
) << A
.getRange();
2198 D
->addAttr(::new (S
.Context
) CXX11NoReturnAttr(S
.Context
, A
));
2201 static void handleNoCfCheckAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&Attrs
) {
2202 if (!S
.getLangOpts().CFProtectionBranch
)
2203 S
.Diag(Attrs
.getLoc(), diag::warn_nocf_check_attribute_ignored
);
2205 handleSimpleAttribute
<AnyX86NoCfCheckAttr
>(S
, D
, Attrs
);
2208 bool Sema::CheckAttrNoArgs(const ParsedAttr
&Attrs
) {
2209 if (!Attrs
.checkExactlyNumArgs(*this, 0)) {
2217 bool Sema::CheckAttrTarget(const ParsedAttr
&AL
) {
2218 // Check whether the attribute is valid on the current target.
2219 if (!AL
.existsInTarget(Context
.getTargetInfo())) {
2220 Diag(AL
.getLoc(), diag::warn_unknown_attribute_ignored
)
2221 << AL
<< AL
.getRange();
2229 static void handleAnalyzerNoReturnAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2231 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2232 // because 'analyzer_noreturn' does not impact the type.
2233 if (!isFunctionOrMethodOrBlock(D
)) {
2234 ValueDecl
*VD
= dyn_cast
<ValueDecl
>(D
);
2235 if (!VD
|| (!VD
->getType()->isBlockPointerType() &&
2236 !VD
->getType()->isFunctionPointerType())) {
2237 S
.Diag(AL
.getLoc(), AL
.isStandardAttributeSyntax()
2238 ? diag::err_attribute_wrong_decl_type
2239 : diag::warn_attribute_wrong_decl_type
)
2240 << AL
<< ExpectedFunctionMethodOrBlock
;
2245 D
->addAttr(::new (S
.Context
) AnalyzerNoReturnAttr(S
.Context
, AL
));
2248 // PS3 PPU-specific.
2249 static void handleVecReturnAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2251 Returning a Vector Class in Registers
2253 According to the PPU ABI specifications, a class with a single member of
2254 vector type is returned in memory when used as the return value of a
2256 This results in inefficient code when implementing vector classes. To return
2257 the value in a single vector register, add the vecreturn attribute to the
2258 class definition. This attribute is also applicable to struct types.
2264 __vector float xyzw;
2265 } __attribute__((vecreturn));
2267 Vector Add(Vector lhs, Vector rhs)
2270 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2271 return result; // This will be returned in a register
2274 if (VecReturnAttr
*A
= D
->getAttr
<VecReturnAttr
>()) {
2275 S
.Diag(AL
.getLoc(), diag::err_repeat_attribute
) << A
;
2279 const auto *R
= cast
<RecordDecl
>(D
);
2282 if (!isa
<CXXRecordDecl
>(R
)) {
2283 S
.Diag(AL
.getLoc(), diag::err_attribute_vecreturn_only_vector_member
);
2287 if (!cast
<CXXRecordDecl
>(R
)->isPOD()) {
2288 S
.Diag(AL
.getLoc(), diag::err_attribute_vecreturn_only_pod_record
);
2292 for (const auto *I
: R
->fields()) {
2293 if ((count
== 1) || !I
->getType()->isVectorType()) {
2294 S
.Diag(AL
.getLoc(), diag::err_attribute_vecreturn_only_vector_member
);
2300 D
->addAttr(::new (S
.Context
) VecReturnAttr(S
.Context
, AL
));
2303 static void handleDependencyAttr(Sema
&S
, Scope
*Scope
, Decl
*D
,
2304 const ParsedAttr
&AL
) {
2305 if (isa
<ParmVarDecl
>(D
)) {
2306 // [[carries_dependency]] can only be applied to a parameter if it is a
2307 // parameter of a function declaration or lambda.
2308 if (!(Scope
->getFlags() & clang::Scope::FunctionDeclarationScope
)) {
2310 diag::err_carries_dependency_param_not_function_decl
);
2315 D
->addAttr(::new (S
.Context
) CarriesDependencyAttr(S
.Context
, AL
));
2318 static void handleUnusedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2319 bool IsCXX17Attr
= AL
.isCXX11Attribute() && !AL
.getScopeName();
2321 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
2322 // about using it as an extension.
2323 if (!S
.getLangOpts().CPlusPlus17
&& IsCXX17Attr
)
2324 S
.Diag(AL
.getLoc(), diag::ext_cxx17_attr
) << AL
;
2326 D
->addAttr(::new (S
.Context
) UnusedAttr(S
.Context
, AL
));
2329 static void handleConstructorAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2330 uint32_t priority
= ConstructorAttr::DefaultPriority
;
2331 if (S
.getLangOpts().HLSL
&& AL
.getNumArgs()) {
2332 S
.Diag(AL
.getLoc(), diag::err_hlsl_init_priority_unsupported
);
2335 if (AL
.getNumArgs() &&
2336 !checkUInt32Argument(S
, AL
, AL
.getArgAsExpr(0), priority
))
2339 D
->addAttr(::new (S
.Context
) ConstructorAttr(S
.Context
, AL
, priority
));
2342 static void handleDestructorAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2343 uint32_t priority
= DestructorAttr::DefaultPriority
;
2344 if (AL
.getNumArgs() &&
2345 !checkUInt32Argument(S
, AL
, AL
.getArgAsExpr(0), priority
))
2348 D
->addAttr(::new (S
.Context
) DestructorAttr(S
.Context
, AL
, priority
));
2351 template <typename AttrTy
>
2352 static void handleAttrWithMessage(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2353 // Handle the case where the attribute has a text message.
2355 if (AL
.getNumArgs() == 1 && !S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
2358 D
->addAttr(::new (S
.Context
) AttrTy(S
.Context
, AL
, Str
));
2361 static void handleObjCSuppresProtocolAttr(Sema
&S
, Decl
*D
,
2362 const ParsedAttr
&AL
) {
2363 if (!cast
<ObjCProtocolDecl
>(D
)->isThisDeclarationADefinition()) {
2364 S
.Diag(AL
.getLoc(), diag::err_objc_attr_protocol_requires_definition
)
2365 << AL
<< AL
.getRange();
2369 D
->addAttr(::new (S
.Context
) ObjCExplicitProtocolImplAttr(S
.Context
, AL
));
2372 static bool checkAvailabilityAttr(Sema
&S
, SourceRange Range
,
2373 IdentifierInfo
*Platform
,
2374 VersionTuple Introduced
,
2375 VersionTuple Deprecated
,
2376 VersionTuple Obsoleted
) {
2377 StringRef PlatformName
2378 = AvailabilityAttr::getPrettyPlatformName(Platform
->getName());
2379 if (PlatformName
.empty())
2380 PlatformName
= Platform
->getName();
2382 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2383 // of these steps are needed).
2384 if (!Introduced
.empty() && !Deprecated
.empty() &&
2385 !(Introduced
<= Deprecated
)) {
2386 S
.Diag(Range
.getBegin(), diag::warn_availability_version_ordering
)
2387 << 1 << PlatformName
<< Deprecated
.getAsString()
2388 << 0 << Introduced
.getAsString();
2392 if (!Introduced
.empty() && !Obsoleted
.empty() &&
2393 !(Introduced
<= Obsoleted
)) {
2394 S
.Diag(Range
.getBegin(), diag::warn_availability_version_ordering
)
2395 << 2 << PlatformName
<< Obsoleted
.getAsString()
2396 << 0 << Introduced
.getAsString();
2400 if (!Deprecated
.empty() && !Obsoleted
.empty() &&
2401 !(Deprecated
<= Obsoleted
)) {
2402 S
.Diag(Range
.getBegin(), diag::warn_availability_version_ordering
)
2403 << 2 << PlatformName
<< Obsoleted
.getAsString()
2404 << 1 << Deprecated
.getAsString();
2411 /// Check whether the two versions match.
2413 /// If either version tuple is empty, then they are assumed to match. If
2414 /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2415 static bool versionsMatch(const VersionTuple
&X
, const VersionTuple
&Y
,
2416 bool BeforeIsOkay
) {
2417 if (X
.empty() || Y
.empty())
2423 if (BeforeIsOkay
&& X
< Y
)
2429 AvailabilityAttr
*Sema::mergeAvailabilityAttr(
2430 NamedDecl
*D
, const AttributeCommonInfo
&CI
, IdentifierInfo
*Platform
,
2431 bool Implicit
, VersionTuple Introduced
, VersionTuple Deprecated
,
2432 VersionTuple Obsoleted
, bool IsUnavailable
, StringRef Message
,
2433 bool IsStrict
, StringRef Replacement
, AvailabilityMergeKind AMK
,
2435 VersionTuple MergedIntroduced
= Introduced
;
2436 VersionTuple MergedDeprecated
= Deprecated
;
2437 VersionTuple MergedObsoleted
= Obsoleted
;
2438 bool FoundAny
= false;
2439 bool OverrideOrImpl
= false;
2442 case AMK_Redeclaration
:
2443 OverrideOrImpl
= false;
2447 case AMK_ProtocolImplementation
:
2448 case AMK_OptionalProtocolImplementation
:
2449 OverrideOrImpl
= true;
2453 if (D
->hasAttrs()) {
2454 AttrVec
&Attrs
= D
->getAttrs();
2455 for (unsigned i
= 0, e
= Attrs
.size(); i
!= e
;) {
2456 const auto *OldAA
= dyn_cast
<AvailabilityAttr
>(Attrs
[i
]);
2462 IdentifierInfo
*OldPlatform
= OldAA
->getPlatform();
2463 if (OldPlatform
!= Platform
) {
2468 // If there is an existing availability attribute for this platform that
2469 // has a lower priority use the existing one and discard the new
2471 if (OldAA
->getPriority() < Priority
)
2474 // If there is an existing attribute for this platform that has a higher
2475 // priority than the new attribute then erase the old one and continue
2476 // processing the attributes.
2477 if (OldAA
->getPriority() > Priority
) {
2478 Attrs
.erase(Attrs
.begin() + i
);
2484 VersionTuple OldIntroduced
= OldAA
->getIntroduced();
2485 VersionTuple OldDeprecated
= OldAA
->getDeprecated();
2486 VersionTuple OldObsoleted
= OldAA
->getObsoleted();
2487 bool OldIsUnavailable
= OldAA
->getUnavailable();
2489 if (!versionsMatch(OldIntroduced
, Introduced
, OverrideOrImpl
) ||
2490 !versionsMatch(Deprecated
, OldDeprecated
, OverrideOrImpl
) ||
2491 !versionsMatch(Obsoleted
, OldObsoleted
, OverrideOrImpl
) ||
2492 !(OldIsUnavailable
== IsUnavailable
||
2493 (OverrideOrImpl
&& !OldIsUnavailable
&& IsUnavailable
))) {
2494 if (OverrideOrImpl
) {
2496 VersionTuple FirstVersion
;
2497 VersionTuple SecondVersion
;
2498 if (!versionsMatch(OldIntroduced
, Introduced
, OverrideOrImpl
)) {
2500 FirstVersion
= OldIntroduced
;
2501 SecondVersion
= Introduced
;
2502 } else if (!versionsMatch(Deprecated
, OldDeprecated
, OverrideOrImpl
)) {
2504 FirstVersion
= Deprecated
;
2505 SecondVersion
= OldDeprecated
;
2506 } else if (!versionsMatch(Obsoleted
, OldObsoleted
, OverrideOrImpl
)) {
2508 FirstVersion
= Obsoleted
;
2509 SecondVersion
= OldObsoleted
;
2513 Diag(OldAA
->getLocation(),
2514 diag::warn_mismatched_availability_override_unavail
)
2515 << AvailabilityAttr::getPrettyPlatformName(Platform
->getName())
2516 << (AMK
== AMK_Override
);
2517 } else if (Which
!= 1 && AMK
== AMK_OptionalProtocolImplementation
) {
2518 // Allow different 'introduced' / 'obsoleted' availability versions
2519 // on a method that implements an optional protocol requirement. It
2520 // makes less sense to allow this for 'deprecated' as the user can't
2521 // see if the method is 'deprecated' as 'respondsToSelector' will
2522 // still return true when the method is deprecated.
2526 Diag(OldAA
->getLocation(),
2527 diag::warn_mismatched_availability_override
)
2529 << AvailabilityAttr::getPrettyPlatformName(Platform
->getName())
2530 << FirstVersion
.getAsString() << SecondVersion
.getAsString()
2531 << (AMK
== AMK_Override
);
2533 if (AMK
== AMK_Override
)
2534 Diag(CI
.getLoc(), diag::note_overridden_method
);
2536 Diag(CI
.getLoc(), diag::note_protocol_method
);
2538 Diag(OldAA
->getLocation(), diag::warn_mismatched_availability
);
2539 Diag(CI
.getLoc(), diag::note_previous_attribute
);
2542 Attrs
.erase(Attrs
.begin() + i
);
2547 VersionTuple MergedIntroduced2
= MergedIntroduced
;
2548 VersionTuple MergedDeprecated2
= MergedDeprecated
;
2549 VersionTuple MergedObsoleted2
= MergedObsoleted
;
2551 if (MergedIntroduced2
.empty())
2552 MergedIntroduced2
= OldIntroduced
;
2553 if (MergedDeprecated2
.empty())
2554 MergedDeprecated2
= OldDeprecated
;
2555 if (MergedObsoleted2
.empty())
2556 MergedObsoleted2
= OldObsoleted
;
2558 if (checkAvailabilityAttr(*this, OldAA
->getRange(), Platform
,
2559 MergedIntroduced2
, MergedDeprecated2
,
2560 MergedObsoleted2
)) {
2561 Attrs
.erase(Attrs
.begin() + i
);
2566 MergedIntroduced
= MergedIntroduced2
;
2567 MergedDeprecated
= MergedDeprecated2
;
2568 MergedObsoleted
= MergedObsoleted2
;
2574 MergedIntroduced
== Introduced
&&
2575 MergedDeprecated
== Deprecated
&&
2576 MergedObsoleted
== Obsoleted
)
2579 // Only create a new attribute if !OverrideOrImpl, but we want to do
2581 if (!checkAvailabilityAttr(*this, CI
.getRange(), Platform
, MergedIntroduced
,
2582 MergedDeprecated
, MergedObsoleted
) &&
2584 auto *Avail
= ::new (Context
) AvailabilityAttr(
2585 Context
, CI
, Platform
, Introduced
, Deprecated
, Obsoleted
, IsUnavailable
,
2586 Message
, IsStrict
, Replacement
, Priority
);
2587 Avail
->setImplicit(Implicit
);
2593 static void handleAvailabilityAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2594 if (isa
<UsingDecl
, UnresolvedUsingTypenameDecl
, UnresolvedUsingValueDecl
>(
2596 S
.Diag(AL
.getRange().getBegin(), diag::warn_deprecated_ignored_on_using
)
2601 if (!AL
.checkExactlyNumArgs(S
, 1))
2603 IdentifierLoc
*Platform
= AL
.getArgAsIdent(0);
2605 IdentifierInfo
*II
= Platform
->Ident
;
2606 if (AvailabilityAttr::getPrettyPlatformName(II
->getName()).empty())
2607 S
.Diag(Platform
->Loc
, diag::warn_availability_unknown_platform
)
2610 auto *ND
= dyn_cast
<NamedDecl
>(D
);
2611 if (!ND
) // We warned about this already, so just return.
2614 AvailabilityChange Introduced
= AL
.getAvailabilityIntroduced();
2615 AvailabilityChange Deprecated
= AL
.getAvailabilityDeprecated();
2616 AvailabilityChange Obsoleted
= AL
.getAvailabilityObsoleted();
2617 bool IsUnavailable
= AL
.getUnavailableLoc().isValid();
2618 bool IsStrict
= AL
.getStrictLoc().isValid();
2620 if (const auto *SE
= dyn_cast_or_null
<StringLiteral
>(AL
.getMessageExpr()))
2621 Str
= SE
->getString();
2622 StringRef Replacement
;
2623 if (const auto *SE
= dyn_cast_or_null
<StringLiteral
>(AL
.getReplacementExpr()))
2624 Replacement
= SE
->getString();
2626 if (II
->isStr("swift")) {
2627 if (Introduced
.isValid() || Obsoleted
.isValid() ||
2628 (!IsUnavailable
&& !Deprecated
.isValid())) {
2630 diag::warn_availability_swift_unavailable_deprecated_only
);
2635 if (II
->isStr("fuchsia")) {
2636 Optional
<unsigned> Min
, Sub
;
2637 if ((Min
= Introduced
.Version
.getMinor()) ||
2638 (Sub
= Introduced
.Version
.getSubminor())) {
2639 S
.Diag(AL
.getLoc(), diag::warn_availability_fuchsia_unavailable_minor
);
2644 int PriorityModifier
= AL
.isPragmaClangAttribute()
2645 ? Sema::AP_PragmaClangAttribute
2646 : Sema::AP_Explicit
;
2647 AvailabilityAttr
*NewAttr
= S
.mergeAvailabilityAttr(
2648 ND
, AL
, II
, false /*Implicit*/, Introduced
.Version
, Deprecated
.Version
,
2649 Obsoleted
.Version
, IsUnavailable
, Str
, IsStrict
, Replacement
,
2650 Sema::AMK_None
, PriorityModifier
);
2652 D
->addAttr(NewAttr
);
2654 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2655 // matches before the start of the watchOS platform.
2656 if (S
.Context
.getTargetInfo().getTriple().isWatchOS()) {
2657 IdentifierInfo
*NewII
= nullptr;
2658 if (II
->getName() == "ios")
2659 NewII
= &S
.Context
.Idents
.get("watchos");
2660 else if (II
->getName() == "ios_app_extension")
2661 NewII
= &S
.Context
.Idents
.get("watchos_app_extension");
2664 const auto *SDKInfo
= S
.getDarwinSDKInfoForAvailabilityChecking();
2665 const auto *IOSToWatchOSMapping
=
2666 SDKInfo
? SDKInfo
->getVersionMapping(
2667 DarwinSDKInfo::OSEnvPair::iOStoWatchOSPair())
2670 auto adjustWatchOSVersion
=
2671 [IOSToWatchOSMapping
](VersionTuple Version
) -> VersionTuple
{
2672 if (Version
.empty())
2674 auto MinimumWatchOSVersion
= VersionTuple(2, 0);
2676 if (IOSToWatchOSMapping
) {
2677 if (auto MappedVersion
= IOSToWatchOSMapping
->map(
2678 Version
, MinimumWatchOSVersion
, None
)) {
2679 return MappedVersion
.value();
2683 auto Major
= Version
.getMajor();
2684 auto NewMajor
= Major
>= 9 ? Major
- 7 : 0;
2685 if (NewMajor
>= 2) {
2686 if (Version
.getMinor()) {
2687 if (Version
.getSubminor())
2688 return VersionTuple(NewMajor
, Version
.getMinor().value(),
2689 Version
.getSubminor().value());
2691 return VersionTuple(NewMajor
, Version
.getMinor().value());
2693 return VersionTuple(NewMajor
);
2696 return MinimumWatchOSVersion
;
2699 auto NewIntroduced
= adjustWatchOSVersion(Introduced
.Version
);
2700 auto NewDeprecated
= adjustWatchOSVersion(Deprecated
.Version
);
2701 auto NewObsoleted
= adjustWatchOSVersion(Obsoleted
.Version
);
2703 AvailabilityAttr
*NewAttr
= S
.mergeAvailabilityAttr(
2704 ND
, AL
, NewII
, true /*Implicit*/, NewIntroduced
, NewDeprecated
,
2705 NewObsoleted
, IsUnavailable
, Str
, IsStrict
, Replacement
,
2707 PriorityModifier
+ Sema::AP_InferredFromOtherPlatform
);
2709 D
->addAttr(NewAttr
);
2711 } else if (S
.Context
.getTargetInfo().getTriple().isTvOS()) {
2712 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2713 // matches before the start of the tvOS platform.
2714 IdentifierInfo
*NewII
= nullptr;
2715 if (II
->getName() == "ios")
2716 NewII
= &S
.Context
.Idents
.get("tvos");
2717 else if (II
->getName() == "ios_app_extension")
2718 NewII
= &S
.Context
.Idents
.get("tvos_app_extension");
2721 const auto *SDKInfo
= S
.getDarwinSDKInfoForAvailabilityChecking();
2722 const auto *IOSToTvOSMapping
=
2723 SDKInfo
? SDKInfo
->getVersionMapping(
2724 DarwinSDKInfo::OSEnvPair::iOStoTvOSPair())
2727 auto AdjustTvOSVersion
=
2728 [IOSToTvOSMapping
](VersionTuple Version
) -> VersionTuple
{
2729 if (Version
.empty())
2732 if (IOSToTvOSMapping
) {
2733 if (auto MappedVersion
=
2734 IOSToTvOSMapping
->map(Version
, VersionTuple(0, 0), None
)) {
2735 return *MappedVersion
;
2741 auto NewIntroduced
= AdjustTvOSVersion(Introduced
.Version
);
2742 auto NewDeprecated
= AdjustTvOSVersion(Deprecated
.Version
);
2743 auto NewObsoleted
= AdjustTvOSVersion(Obsoleted
.Version
);
2745 AvailabilityAttr
*NewAttr
= S
.mergeAvailabilityAttr(
2746 ND
, AL
, NewII
, true /*Implicit*/, NewIntroduced
, NewDeprecated
,
2747 NewObsoleted
, IsUnavailable
, Str
, IsStrict
, Replacement
,
2749 PriorityModifier
+ Sema::AP_InferredFromOtherPlatform
);
2751 D
->addAttr(NewAttr
);
2753 } else if (S
.Context
.getTargetInfo().getTriple().getOS() ==
2754 llvm::Triple::IOS
&&
2755 S
.Context
.getTargetInfo().getTriple().isMacCatalystEnvironment()) {
2756 auto GetSDKInfo
= [&]() {
2757 return S
.getDarwinSDKInfoForAvailabilityChecking(AL
.getRange().getBegin(),
2761 // Transcribe "ios" to "maccatalyst" (and add a new attribute).
2762 IdentifierInfo
*NewII
= nullptr;
2763 if (II
->getName() == "ios")
2764 NewII
= &S
.Context
.Idents
.get("maccatalyst");
2765 else if (II
->getName() == "ios_app_extension")
2766 NewII
= &S
.Context
.Idents
.get("maccatalyst_app_extension");
2768 auto MinMacCatalystVersion
= [](const VersionTuple
&V
) {
2771 if (V
.getMajor() < 13 ||
2772 (V
.getMajor() == 13 && V
.getMinor() && *V
.getMinor() < 1))
2773 return VersionTuple(13, 1); // The min Mac Catalyst version is 13.1.
2776 AvailabilityAttr
*NewAttr
= S
.mergeAvailabilityAttr(
2777 ND
, AL
.getRange(), NewII
, true /*Implicit*/,
2778 MinMacCatalystVersion(Introduced
.Version
),
2779 MinMacCatalystVersion(Deprecated
.Version
),
2780 MinMacCatalystVersion(Obsoleted
.Version
), IsUnavailable
, Str
,
2781 IsStrict
, Replacement
, Sema::AMK_None
,
2782 PriorityModifier
+ Sema::AP_InferredFromOtherPlatform
);
2784 D
->addAttr(NewAttr
);
2785 } else if (II
->getName() == "macos" && GetSDKInfo() &&
2786 (!Introduced
.Version
.empty() || !Deprecated
.Version
.empty() ||
2787 !Obsoleted
.Version
.empty())) {
2788 if (const auto *MacOStoMacCatalystMapping
=
2789 GetSDKInfo()->getVersionMapping(
2790 DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair())) {
2791 // Infer Mac Catalyst availability from the macOS availability attribute
2792 // if it has versioned availability. Don't infer 'unavailable'. This
2793 // inferred availability has lower priority than the other availability
2794 // attributes that are inferred from 'ios'.
2795 NewII
= &S
.Context
.Idents
.get("maccatalyst");
2796 auto RemapMacOSVersion
=
2797 [&](const VersionTuple
&V
) -> Optional
<VersionTuple
> {
2800 // API_TO_BE_DEPRECATED is 100000.
2801 if (V
.getMajor() == 100000)
2802 return VersionTuple(100000);
2803 // The minimum iosmac version is 13.1
2804 return MacOStoMacCatalystMapping
->map(V
, VersionTuple(13, 1), None
);
2806 Optional
<VersionTuple
> NewIntroduced
=
2807 RemapMacOSVersion(Introduced
.Version
),
2809 RemapMacOSVersion(Deprecated
.Version
),
2811 RemapMacOSVersion(Obsoleted
.Version
);
2812 if (NewIntroduced
|| NewDeprecated
|| NewObsoleted
) {
2813 auto VersionOrEmptyVersion
=
2814 [](const Optional
<VersionTuple
> &V
) -> VersionTuple
{
2815 return V
? *V
: VersionTuple();
2817 AvailabilityAttr
*NewAttr
= S
.mergeAvailabilityAttr(
2818 ND
, AL
.getRange(), NewII
, true /*Implicit*/,
2819 VersionOrEmptyVersion(NewIntroduced
),
2820 VersionOrEmptyVersion(NewDeprecated
),
2821 VersionOrEmptyVersion(NewObsoleted
), /*IsUnavailable=*/false, Str
,
2822 IsStrict
, Replacement
, Sema::AMK_None
,
2823 PriorityModifier
+ Sema::AP_InferredFromOtherPlatform
+
2824 Sema::AP_InferredFromOtherPlatform
);
2826 D
->addAttr(NewAttr
);
2833 static void handleExternalSourceSymbolAttr(Sema
&S
, Decl
*D
,
2834 const ParsedAttr
&AL
) {
2835 if (!AL
.checkAtLeastNumArgs(S
, 1) || !AL
.checkAtMostNumArgs(S
, 3))
2839 if (const auto *SE
= dyn_cast_or_null
<StringLiteral
>(AL
.getArgAsExpr(0)))
2840 Language
= SE
->getString();
2841 StringRef DefinedIn
;
2842 if (const auto *SE
= dyn_cast_or_null
<StringLiteral
>(AL
.getArgAsExpr(1)))
2843 DefinedIn
= SE
->getString();
2844 bool IsGeneratedDeclaration
= AL
.getArgAsIdent(2) != nullptr;
2846 D
->addAttr(::new (S
.Context
) ExternalSourceSymbolAttr(
2847 S
.Context
, AL
, Language
, DefinedIn
, IsGeneratedDeclaration
));
2851 static T
*mergeVisibilityAttr(Sema
&S
, Decl
*D
, const AttributeCommonInfo
&CI
,
2852 typename
T::VisibilityType value
) {
2853 T
*existingAttr
= D
->getAttr
<T
>();
2855 typename
T::VisibilityType existingValue
= existingAttr
->getVisibility();
2856 if (existingValue
== value
)
2858 S
.Diag(existingAttr
->getLocation(), diag::err_mismatched_visibility
);
2859 S
.Diag(CI
.getLoc(), diag::note_previous_attribute
);
2862 return ::new (S
.Context
) T(S
.Context
, CI
, value
);
2865 VisibilityAttr
*Sema::mergeVisibilityAttr(Decl
*D
,
2866 const AttributeCommonInfo
&CI
,
2867 VisibilityAttr::VisibilityType Vis
) {
2868 return ::mergeVisibilityAttr
<VisibilityAttr
>(*this, D
, CI
, Vis
);
2871 TypeVisibilityAttr
*
2872 Sema::mergeTypeVisibilityAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
2873 TypeVisibilityAttr::VisibilityType Vis
) {
2874 return ::mergeVisibilityAttr
<TypeVisibilityAttr
>(*this, D
, CI
, Vis
);
2877 static void handleVisibilityAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
,
2878 bool isTypeVisibility
) {
2879 // Visibility attributes don't mean anything on a typedef.
2880 if (isa
<TypedefNameDecl
>(D
)) {
2881 S
.Diag(AL
.getRange().getBegin(), diag::warn_attribute_ignored
) << AL
;
2885 // 'type_visibility' can only go on a type or namespace.
2886 if (isTypeVisibility
&&
2887 !(isa
<TagDecl
>(D
) ||
2888 isa
<ObjCInterfaceDecl
>(D
) ||
2889 isa
<NamespaceDecl
>(D
))) {
2890 S
.Diag(AL
.getRange().getBegin(), diag::err_attribute_wrong_decl_type
)
2891 << AL
<< ExpectedTypeOrNamespace
;
2895 // Check that the argument is a string literal.
2897 SourceLocation LiteralLoc
;
2898 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, TypeStr
, &LiteralLoc
))
2901 VisibilityAttr::VisibilityType type
;
2902 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr
, type
)) {
2903 S
.Diag(LiteralLoc
, diag::warn_attribute_type_not_supported
) << AL
2908 // Complain about attempts to use protected visibility on targets
2909 // (like Darwin) that don't support it.
2910 if (type
== VisibilityAttr::Protected
&&
2911 !S
.Context
.getTargetInfo().hasProtectedVisibility()) {
2912 S
.Diag(AL
.getLoc(), diag::warn_attribute_protected_visibility
);
2913 type
= VisibilityAttr::Default
;
2917 if (isTypeVisibility
) {
2918 newAttr
= S
.mergeTypeVisibilityAttr(
2919 D
, AL
, (TypeVisibilityAttr::VisibilityType
)type
);
2921 newAttr
= S
.mergeVisibilityAttr(D
, AL
, type
);
2924 D
->addAttr(newAttr
);
2927 static void handleObjCDirectAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2928 // objc_direct cannot be set on methods declared in the context of a protocol
2929 if (isa
<ObjCProtocolDecl
>(D
->getDeclContext())) {
2930 S
.Diag(AL
.getLoc(), diag::err_objc_direct_on_protocol
) << false;
2934 if (S
.getLangOpts().ObjCRuntime
.allowsDirectDispatch()) {
2935 handleSimpleAttribute
<ObjCDirectAttr
>(S
, D
, AL
);
2937 S
.Diag(AL
.getLoc(), diag::warn_objc_direct_ignored
) << AL
;
2941 static void handleObjCDirectMembersAttr(Sema
&S
, Decl
*D
,
2942 const ParsedAttr
&AL
) {
2943 if (S
.getLangOpts().ObjCRuntime
.allowsDirectDispatch()) {
2944 handleSimpleAttribute
<ObjCDirectMembersAttr
>(S
, D
, AL
);
2946 S
.Diag(AL
.getLoc(), diag::warn_objc_direct_ignored
) << AL
;
2950 static void handleObjCMethodFamilyAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2951 const auto *M
= cast
<ObjCMethodDecl
>(D
);
2952 if (!AL
.isArgIdent(0)) {
2953 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
2954 << AL
<< 1 << AANT_ArgumentIdentifier
;
2958 IdentifierLoc
*IL
= AL
.getArgAsIdent(0);
2959 ObjCMethodFamilyAttr::FamilyKind F
;
2960 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL
->Ident
->getName(), F
)) {
2961 S
.Diag(IL
->Loc
, diag::warn_attribute_type_not_supported
) << AL
<< IL
->Ident
;
2965 if (F
== ObjCMethodFamilyAttr::OMF_init
&&
2966 !M
->getReturnType()->isObjCObjectPointerType()) {
2967 S
.Diag(M
->getLocation(), diag::err_init_method_bad_return_type
)
2968 << M
->getReturnType();
2969 // Ignore the attribute.
2973 D
->addAttr(new (S
.Context
) ObjCMethodFamilyAttr(S
.Context
, AL
, F
));
2976 static void handleObjCNSObject(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
2977 if (const auto *TD
= dyn_cast
<TypedefNameDecl
>(D
)) {
2978 QualType T
= TD
->getUnderlyingType();
2979 if (!T
->isCARCBridgableType()) {
2980 S
.Diag(TD
->getLocation(), diag::err_nsobject_attribute
);
2984 else if (const auto *PD
= dyn_cast
<ObjCPropertyDecl
>(D
)) {
2985 QualType T
= PD
->getType();
2986 if (!T
->isCARCBridgableType()) {
2987 S
.Diag(PD
->getLocation(), diag::err_nsobject_attribute
);
2992 // It is okay to include this attribute on properties, e.g.:
2994 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2996 // In this case it follows tradition and suppresses an error in the above
2998 S
.Diag(D
->getLocation(), diag::warn_nsobject_attribute
);
3000 D
->addAttr(::new (S
.Context
) ObjCNSObjectAttr(S
.Context
, AL
));
3003 static void handleObjCIndependentClass(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3004 if (const auto *TD
= dyn_cast
<TypedefNameDecl
>(D
)) {
3005 QualType T
= TD
->getUnderlyingType();
3006 if (!T
->isObjCObjectPointerType()) {
3007 S
.Diag(TD
->getLocation(), diag::warn_ptr_independentclass_attribute
);
3011 S
.Diag(D
->getLocation(), diag::warn_independentclass_attribute
);
3014 D
->addAttr(::new (S
.Context
) ObjCIndependentClassAttr(S
.Context
, AL
));
3017 static void handleBlocksAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3018 if (!AL
.isArgIdent(0)) {
3019 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
3020 << AL
<< 1 << AANT_ArgumentIdentifier
;
3024 IdentifierInfo
*II
= AL
.getArgAsIdent(0)->Ident
;
3025 BlocksAttr::BlockType type
;
3026 if (!BlocksAttr::ConvertStrToBlockType(II
->getName(), type
)) {
3027 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
) << AL
<< II
;
3031 D
->addAttr(::new (S
.Context
) BlocksAttr(S
.Context
, AL
, type
));
3034 static void handleSentinelAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3035 unsigned sentinel
= (unsigned)SentinelAttr::DefaultSentinel
;
3036 if (AL
.getNumArgs() > 0) {
3037 Expr
*E
= AL
.getArgAsExpr(0);
3038 Optional
<llvm::APSInt
> Idx
= llvm::APSInt(32);
3039 if (E
->isTypeDependent() || !(Idx
= E
->getIntegerConstantExpr(S
.Context
))) {
3040 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
3041 << AL
<< 1 << AANT_ArgumentIntegerConstant
<< E
->getSourceRange();
3045 if (Idx
->isSigned() && Idx
->isNegative()) {
3046 S
.Diag(AL
.getLoc(), diag::err_attribute_sentinel_less_than_zero
)
3047 << E
->getSourceRange();
3051 sentinel
= Idx
->getZExtValue();
3054 unsigned nullPos
= (unsigned)SentinelAttr::DefaultNullPos
;
3055 if (AL
.getNumArgs() > 1) {
3056 Expr
*E
= AL
.getArgAsExpr(1);
3057 Optional
<llvm::APSInt
> Idx
= llvm::APSInt(32);
3058 if (E
->isTypeDependent() || !(Idx
= E
->getIntegerConstantExpr(S
.Context
))) {
3059 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
3060 << AL
<< 2 << AANT_ArgumentIntegerConstant
<< E
->getSourceRange();
3063 nullPos
= Idx
->getZExtValue();
3065 if ((Idx
->isSigned() && Idx
->isNegative()) || nullPos
> 1) {
3066 // FIXME: This error message could be improved, it would be nice
3067 // to say what the bounds actually are.
3068 S
.Diag(AL
.getLoc(), diag::err_attribute_sentinel_not_zero_or_one
)
3069 << E
->getSourceRange();
3074 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
3075 const FunctionType
*FT
= FD
->getType()->castAs
<FunctionType
>();
3076 if (isa
<FunctionNoProtoType
>(FT
)) {
3077 S
.Diag(AL
.getLoc(), diag::warn_attribute_sentinel_named_arguments
);
3081 if (!cast
<FunctionProtoType
>(FT
)->isVariadic()) {
3082 S
.Diag(AL
.getLoc(), diag::warn_attribute_sentinel_not_variadic
) << 0;
3085 } else if (const auto *MD
= dyn_cast
<ObjCMethodDecl
>(D
)) {
3086 if (!MD
->isVariadic()) {
3087 S
.Diag(AL
.getLoc(), diag::warn_attribute_sentinel_not_variadic
) << 0;
3090 } else if (const auto *BD
= dyn_cast
<BlockDecl
>(D
)) {
3091 if (!BD
->isVariadic()) {
3092 S
.Diag(AL
.getLoc(), diag::warn_attribute_sentinel_not_variadic
) << 1;
3095 } else if (const auto *V
= dyn_cast
<VarDecl
>(D
)) {
3096 QualType Ty
= V
->getType();
3097 if (Ty
->isBlockPointerType() || Ty
->isFunctionPointerType()) {
3098 const FunctionType
*FT
= Ty
->isFunctionPointerType()
3099 ? D
->getFunctionType()
3100 : Ty
->castAs
<BlockPointerType
>()
3102 ->castAs
<FunctionType
>();
3103 if (!cast
<FunctionProtoType
>(FT
)->isVariadic()) {
3104 int m
= Ty
->isFunctionPointerType() ? 0 : 1;
3105 S
.Diag(AL
.getLoc(), diag::warn_attribute_sentinel_not_variadic
) << m
;
3109 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
)
3110 << AL
<< ExpectedFunctionMethodOrBlock
;
3114 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
)
3115 << AL
<< ExpectedFunctionMethodOrBlock
;
3118 D
->addAttr(::new (S
.Context
) SentinelAttr(S
.Context
, AL
, sentinel
, nullPos
));
3121 static void handleWarnUnusedResult(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3122 if (D
->getFunctionType() &&
3123 D
->getFunctionType()->getReturnType()->isVoidType() &&
3124 !isa
<CXXConstructorDecl
>(D
)) {
3125 S
.Diag(AL
.getLoc(), diag::warn_attribute_void_function_method
) << AL
<< 0;
3128 if (const auto *MD
= dyn_cast
<ObjCMethodDecl
>(D
))
3129 if (MD
->getReturnType()->isVoidType()) {
3130 S
.Diag(AL
.getLoc(), diag::warn_attribute_void_function_method
) << AL
<< 1;
3135 if (AL
.isStandardAttributeSyntax() && !AL
.getScopeName()) {
3136 // The standard attribute cannot be applied to variable declarations such
3137 // as a function pointer.
3138 if (isa
<VarDecl
>(D
))
3139 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type_str
)
3140 << AL
<< "functions, classes, or enumerations";
3142 // If this is spelled as the standard C++17 attribute, but not in C++17,
3143 // warn about using it as an extension. If there are attribute arguments,
3144 // then claim it's a C++2a extension instead.
3145 // FIXME: If WG14 does not seem likely to adopt the same feature, add an
3146 // extension warning for C2x mode.
3147 const LangOptions
&LO
= S
.getLangOpts();
3148 if (AL
.getNumArgs() == 1) {
3149 if (LO
.CPlusPlus
&& !LO
.CPlusPlus20
)
3150 S
.Diag(AL
.getLoc(), diag::ext_cxx20_attr
) << AL
;
3152 // Since this this is spelled [[nodiscard]], get the optional string
3153 // literal. If in C++ mode, but not in C++2a mode, diagnose as an
3155 // FIXME: C2x should support this feature as well, even as an extension.
3156 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, nullptr))
3158 } else if (LO
.CPlusPlus
&& !LO
.CPlusPlus17
)
3159 S
.Diag(AL
.getLoc(), diag::ext_cxx17_attr
) << AL
;
3162 if ((!AL
.isGNUAttribute() &&
3163 !(AL
.isStandardAttributeSyntax() && AL
.isClangScope())) &&
3164 isa
<TypedefNameDecl
>(D
)) {
3165 S
.Diag(AL
.getLoc(), diag::warn_unused_result_typedef_unsupported_spelling
)
3170 D
->addAttr(::new (S
.Context
) WarnUnusedResultAttr(S
.Context
, AL
, Str
));
3173 static void handleWeakImportAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3174 // weak_import only applies to variable & function declarations.
3176 if (!D
->canBeWeakImported(isDef
)) {
3178 S
.Diag(AL
.getLoc(), diag::warn_attribute_invalid_on_definition
)
3180 else if (isa
<ObjCPropertyDecl
>(D
) || isa
<ObjCMethodDecl
>(D
) ||
3181 (S
.Context
.getTargetInfo().getTriple().isOSDarwin() &&
3182 (isa
<ObjCInterfaceDecl
>(D
) || isa
<EnumDecl
>(D
)))) {
3183 // Nothing to warn about here.
3185 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
)
3186 << AL
<< ExpectedVariableOrFunction
;
3191 D
->addAttr(::new (S
.Context
) WeakImportAttr(S
.Context
, AL
));
3194 // Handles reqd_work_group_size and work_group_size_hint.
3195 template <typename WorkGroupAttr
>
3196 static void handleWorkGroupSize(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3198 for (unsigned i
= 0; i
< 3; ++i
) {
3199 const Expr
*E
= AL
.getArgAsExpr(i
);
3200 if (!checkUInt32Argument(S
, AL
, E
, WGSize
[i
], i
,
3201 /*StrictlyUnsigned=*/true))
3203 if (WGSize
[i
] == 0) {
3204 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_is_zero
)
3205 << AL
<< E
->getSourceRange();
3210 WorkGroupAttr
*Existing
= D
->getAttr
<WorkGroupAttr
>();
3211 if (Existing
&& !(Existing
->getXDim() == WGSize
[0] &&
3212 Existing
->getYDim() == WGSize
[1] &&
3213 Existing
->getZDim() == WGSize
[2]))
3214 S
.Diag(AL
.getLoc(), diag::warn_duplicate_attribute
) << AL
;
3216 D
->addAttr(::new (S
.Context
)
3217 WorkGroupAttr(S
.Context
, AL
, WGSize
[0], WGSize
[1], WGSize
[2]));
3220 // Handles intel_reqd_sub_group_size.
3221 static void handleSubGroupSize(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3223 const Expr
*E
= AL
.getArgAsExpr(0);
3224 if (!checkUInt32Argument(S
, AL
, E
, SGSize
))
3227 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_is_zero
)
3228 << AL
<< E
->getSourceRange();
3232 OpenCLIntelReqdSubGroupSizeAttr
*Existing
=
3233 D
->getAttr
<OpenCLIntelReqdSubGroupSizeAttr
>();
3234 if (Existing
&& Existing
->getSubGroupSize() != SGSize
)
3235 S
.Diag(AL
.getLoc(), diag::warn_duplicate_attribute
) << AL
;
3237 D
->addAttr(::new (S
.Context
)
3238 OpenCLIntelReqdSubGroupSizeAttr(S
.Context
, AL
, SGSize
));
3241 static void handleVecTypeHint(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3242 if (!AL
.hasParsedType()) {
3243 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_number_arguments
) << AL
<< 1;
3247 TypeSourceInfo
*ParmTSI
= nullptr;
3248 QualType ParmType
= S
.GetTypeFromParser(AL
.getTypeArg(), &ParmTSI
);
3249 assert(ParmTSI
&& "no type source info for attribute argument");
3251 if (!ParmType
->isExtVectorType() && !ParmType
->isFloatingType() &&
3252 (ParmType
->isBooleanType() ||
3253 !ParmType
->isIntegralType(S
.getASTContext()))) {
3254 S
.Diag(AL
.getLoc(), diag::err_attribute_invalid_argument
) << 2 << AL
;
3258 if (VecTypeHintAttr
*A
= D
->getAttr
<VecTypeHintAttr
>()) {
3259 if (!S
.Context
.hasSameType(A
->getTypeHint(), ParmType
)) {
3260 S
.Diag(AL
.getLoc(), diag::warn_duplicate_attribute
) << AL
;
3265 D
->addAttr(::new (S
.Context
) VecTypeHintAttr(S
.Context
, AL
, ParmTSI
));
3268 SectionAttr
*Sema::mergeSectionAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
3270 // Explicit or partial specializations do not inherit
3271 // the section attribute from the primary template.
3272 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
3273 if (CI
.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate
&&
3274 FD
->isFunctionTemplateSpecialization())
3277 if (SectionAttr
*ExistingAttr
= D
->getAttr
<SectionAttr
>()) {
3278 if (ExistingAttr
->getName() == Name
)
3280 Diag(ExistingAttr
->getLocation(), diag::warn_mismatched_section
)
3282 Diag(CI
.getLoc(), diag::note_previous_attribute
);
3285 return ::new (Context
) SectionAttr(Context
, CI
, Name
);
3288 /// Used to implement to perform semantic checking on
3289 /// attribute((section("foo"))) specifiers.
3291 /// In this case, "foo" is passed in to be checked. If the section
3292 /// specifier is invalid, return an Error that indicates the problem.
3294 /// This is a simple quality of implementation feature to catch errors
3295 /// and give good diagnostics in cases when the assembler or code generator
3296 /// would otherwise reject the section specifier.
3297 llvm::Error
Sema::isValidSectionSpecifier(StringRef SecName
) {
3298 if (!Context
.getTargetInfo().getTriple().isOSDarwin())
3299 return llvm::Error::success();
3301 // Let MCSectionMachO validate this.
3302 StringRef Segment
, Section
;
3303 unsigned TAA
, StubSize
;
3305 return llvm::MCSectionMachO::ParseSectionSpecifier(SecName
, Segment
, Section
,
3306 TAA
, HasTAA
, StubSize
);
3309 bool Sema::checkSectionName(SourceLocation LiteralLoc
, StringRef SecName
) {
3310 if (llvm::Error E
= isValidSectionSpecifier(SecName
)) {
3311 Diag(LiteralLoc
, diag::err_attribute_section_invalid_for_target
)
3312 << toString(std::move(E
)) << 1 /*'section'*/;
3318 static void handleSectionAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3319 // Make sure that there is a string literal as the sections's single
3322 SourceLocation LiteralLoc
;
3323 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &LiteralLoc
))
3326 if (!S
.checkSectionName(LiteralLoc
, Str
))
3329 SectionAttr
*NewAttr
= S
.mergeSectionAttr(D
, AL
, Str
);
3331 D
->addAttr(NewAttr
);
3332 if (isa
<FunctionDecl
, FunctionTemplateDecl
, ObjCMethodDecl
,
3333 ObjCPropertyDecl
>(D
))
3334 S
.UnifySection(NewAttr
->getName(),
3335 ASTContext::PSF_Execute
| ASTContext::PSF_Read
,
3336 cast
<NamedDecl
>(D
));
3340 // This is used for `__declspec(code_seg("segname"))` on a decl.
3341 // `#pragma code_seg("segname")` uses checkSectionName() instead.
3342 static bool checkCodeSegName(Sema
&S
, SourceLocation LiteralLoc
,
3343 StringRef CodeSegName
) {
3344 if (llvm::Error E
= S
.isValidSectionSpecifier(CodeSegName
)) {
3345 S
.Diag(LiteralLoc
, diag::err_attribute_section_invalid_for_target
)
3346 << toString(std::move(E
)) << 0 /*'code-seg'*/;
3353 CodeSegAttr
*Sema::mergeCodeSegAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
3355 // Explicit or partial specializations do not inherit
3356 // the code_seg attribute from the primary template.
3357 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
3358 if (FD
->isFunctionTemplateSpecialization())
3361 if (const auto *ExistingAttr
= D
->getAttr
<CodeSegAttr
>()) {
3362 if (ExistingAttr
->getName() == Name
)
3364 Diag(ExistingAttr
->getLocation(), diag::warn_mismatched_section
)
3366 Diag(CI
.getLoc(), diag::note_previous_attribute
);
3369 return ::new (Context
) CodeSegAttr(Context
, CI
, Name
);
3372 static void handleCodeSegAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3374 SourceLocation LiteralLoc
;
3375 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &LiteralLoc
))
3377 if (!checkCodeSegName(S
, LiteralLoc
, Str
))
3379 if (const auto *ExistingAttr
= D
->getAttr
<CodeSegAttr
>()) {
3380 if (!ExistingAttr
->isImplicit()) {
3382 ExistingAttr
->getName() == Str
3383 ? diag::warn_duplicate_codeseg_attribute
3384 : diag::err_conflicting_codeseg_attribute
);
3387 D
->dropAttr
<CodeSegAttr
>();
3389 if (CodeSegAttr
*CSA
= S
.mergeCodeSegAttr(D
, AL
, Str
))
3393 // Check for things we'd like to warn about. Multiversioning issues are
3394 // handled later in the process, once we know how many exist.
3395 bool Sema::checkTargetAttr(SourceLocation LiteralLoc
, StringRef AttrStr
) {
3396 enum FirstParam
{ Unsupported
, Duplicate
, Unknown
};
3397 enum SecondParam
{ None
, Architecture
, Tune
};
3398 enum ThirdParam
{ Target
, TargetClones
};
3399 if (AttrStr
.contains("fpmath="))
3400 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3401 << Unsupported
<< None
<< "fpmath=" << Target
;
3403 // Diagnose use of tune if target doesn't support it.
3404 if (!Context
.getTargetInfo().supportsTargetAttributeTune() &&
3405 AttrStr
.contains("tune="))
3406 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3407 << Unsupported
<< None
<< "tune=" << Target
;
3409 ParsedTargetAttr ParsedAttrs
= TargetAttr::parse(AttrStr
);
3411 if (!ParsedAttrs
.Architecture
.empty() &&
3412 !Context
.getTargetInfo().isValidCPUName(ParsedAttrs
.Architecture
))
3413 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3414 << Unknown
<< Architecture
<< ParsedAttrs
.Architecture
<< Target
;
3416 if (!ParsedAttrs
.Tune
.empty() &&
3417 !Context
.getTargetInfo().isValidCPUName(ParsedAttrs
.Tune
))
3418 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3419 << Unknown
<< Tune
<< ParsedAttrs
.Tune
<< Target
;
3421 if (ParsedAttrs
.DuplicateArchitecture
)
3422 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3423 << Duplicate
<< None
<< "arch=" << Target
;
3424 if (ParsedAttrs
.DuplicateTune
)
3425 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3426 << Duplicate
<< None
<< "tune=" << Target
;
3428 for (const auto &Feature
: ParsedAttrs
.Features
) {
3429 auto CurFeature
= StringRef(Feature
).drop_front(); // remove + or -.
3430 if (!Context
.getTargetInfo().isValidFeatureName(CurFeature
))
3431 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3432 << Unsupported
<< None
<< CurFeature
<< Target
;
3435 TargetInfo::BranchProtectionInfo BPI
;
3437 if (ParsedAttrs
.BranchProtection
.empty())
3439 if (!Context
.getTargetInfo().validateBranchProtection(
3440 ParsedAttrs
.BranchProtection
, ParsedAttrs
.Architecture
, BPI
,
3442 if (DiagMsg
.empty())
3443 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3444 << Unsupported
<< None
<< "branch-protection" << Target
;
3445 return Diag(LiteralLoc
, diag::err_invalid_branch_protection_spec
)
3448 if (!DiagMsg
.empty())
3449 Diag(LiteralLoc
, diag::warn_unsupported_branch_protection_spec
) << DiagMsg
;
3454 static void handleTargetAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3456 SourceLocation LiteralLoc
;
3457 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &LiteralLoc
) ||
3458 S
.checkTargetAttr(LiteralLoc
, Str
))
3461 TargetAttr
*NewAttr
= ::new (S
.Context
) TargetAttr(S
.Context
, AL
, Str
);
3462 D
->addAttr(NewAttr
);
3465 bool Sema::checkTargetClonesAttrString(SourceLocation LiteralLoc
, StringRef Str
,
3466 const StringLiteral
*Literal
,
3467 bool &HasDefault
, bool &HasCommas
,
3468 SmallVectorImpl
<StringRef
> &Strings
) {
3469 enum FirstParam
{ Unsupported
, Duplicate
, Unknown
};
3470 enum SecondParam
{ None
, Architecture
, Tune
};
3471 enum ThirdParam
{ Target
, TargetClones
};
3472 HasCommas
= HasCommas
|| Str
.contains(',');
3473 // Warn on empty at the beginning of a string.
3474 if (Str
.size() == 0)
3475 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3476 << Unsupported
<< None
<< "" << TargetClones
;
3478 std::pair
<StringRef
, StringRef
> Parts
= {{}, Str
};
3479 while (!Parts
.second
.empty()) {
3480 Parts
= Parts
.second
.split(',');
3481 StringRef Cur
= Parts
.first
.trim();
3482 SourceLocation CurLoc
= Literal
->getLocationOfByte(
3483 Cur
.data() - Literal
->getString().data(), getSourceManager(),
3484 getLangOpts(), Context
.getTargetInfo());
3486 bool DefaultIsDupe
= false;
3488 return Diag(CurLoc
, diag::warn_unsupported_target_attribute
)
3489 << Unsupported
<< None
<< "" << TargetClones
;
3491 if (Cur
.startswith("arch=")) {
3492 if (!Context
.getTargetInfo().isValidCPUName(
3493 Cur
.drop_front(sizeof("arch=") - 1)))
3494 return Diag(CurLoc
, diag::warn_unsupported_target_attribute
)
3495 << Unsupported
<< Architecture
3496 << Cur
.drop_front(sizeof("arch=") - 1) << TargetClones
;
3497 } else if (Cur
== "default") {
3498 DefaultIsDupe
= HasDefault
;
3500 } else if (!Context
.getTargetInfo().isValidFeatureName(Cur
))
3501 return Diag(CurLoc
, diag::warn_unsupported_target_attribute
)
3502 << Unsupported
<< None
<< Cur
<< TargetClones
;
3504 if (llvm::is_contained(Strings
, Cur
) || DefaultIsDupe
)
3505 Diag(CurLoc
, diag::warn_target_clone_duplicate_options
);
3506 // Note: Add even if there are duplicates, since it changes name mangling.
3507 Strings
.push_back(Cur
);
3510 if (Str
.rtrim().endswith(","))
3511 return Diag(LiteralLoc
, diag::warn_unsupported_target_attribute
)
3512 << Unsupported
<< None
<< "" << TargetClones
;
3516 static void handleTargetClonesAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3517 // Ensure we don't combine these with themselves, since that causes some
3518 // confusing behavior.
3519 if (const auto *Other
= D
->getAttr
<TargetClonesAttr
>()) {
3520 S
.Diag(AL
.getLoc(), diag::err_disallowed_duplicate_attribute
) << AL
;
3521 S
.Diag(Other
->getLocation(), diag::note_conflicting_attribute
);
3524 if (checkAttrMutualExclusion
<TargetClonesAttr
>(S
, D
, AL
))
3527 SmallVector
<StringRef
, 2> Strings
;
3528 bool HasCommas
= false, HasDefault
= false;
3530 for (unsigned I
= 0, E
= AL
.getNumArgs(); I
!= E
; ++I
) {
3532 SourceLocation LiteralLoc
;
3533 if (!S
.checkStringLiteralArgumentAttr(AL
, I
, CurStr
, &LiteralLoc
) ||
3534 S
.checkTargetClonesAttrString(
3536 cast
<StringLiteral
>(AL
.getArgAsExpr(I
)->IgnoreParenCasts()),
3537 HasDefault
, HasCommas
, Strings
))
3541 if (HasCommas
&& AL
.getNumArgs() > 1)
3542 S
.Diag(AL
.getLoc(), diag::warn_target_clone_mixed_values
);
3545 S
.Diag(AL
.getLoc(), diag::err_target_clone_must_have_default
);
3549 // FIXME: We could probably figure out how to get this to work for lambdas
3551 if (const auto *MD
= dyn_cast
<CXXMethodDecl
>(D
)) {
3552 if (MD
->getParent()->isLambda()) {
3553 S
.Diag(D
->getLocation(), diag::err_multiversion_doesnt_support
)
3554 << static_cast<unsigned>(MultiVersionKind::TargetClones
)
3560 cast
<FunctionDecl
>(D
)->setIsMultiVersion();
3561 TargetClonesAttr
*NewAttr
= ::new (S
.Context
)
3562 TargetClonesAttr(S
.Context
, AL
, Strings
.data(), Strings
.size());
3563 D
->addAttr(NewAttr
);
3566 static void handleMinVectorWidthAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3567 Expr
*E
= AL
.getArgAsExpr(0);
3569 if (!checkUInt32Argument(S
, AL
, E
, VecWidth
)) {
3574 MinVectorWidthAttr
*Existing
= D
->getAttr
<MinVectorWidthAttr
>();
3575 if (Existing
&& Existing
->getVectorWidth() != VecWidth
) {
3576 S
.Diag(AL
.getLoc(), diag::warn_duplicate_attribute
) << AL
;
3580 D
->addAttr(::new (S
.Context
) MinVectorWidthAttr(S
.Context
, AL
, VecWidth
));
3583 static void handleCleanupAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3584 Expr
*E
= AL
.getArgAsExpr(0);
3585 SourceLocation Loc
= E
->getExprLoc();
3586 FunctionDecl
*FD
= nullptr;
3587 DeclarationNameInfo NI
;
3589 // gcc only allows for simple identifiers. Since we support more than gcc, we
3590 // will warn the user.
3591 if (auto *DRE
= dyn_cast
<DeclRefExpr
>(E
)) {
3592 if (DRE
->hasQualifier())
3593 S
.Diag(Loc
, diag::warn_cleanup_ext
);
3594 FD
= dyn_cast
<FunctionDecl
>(DRE
->getDecl());
3595 NI
= DRE
->getNameInfo();
3597 S
.Diag(Loc
, diag::err_attribute_cleanup_arg_not_function
) << 1
3601 } else if (auto *ULE
= dyn_cast
<UnresolvedLookupExpr
>(E
)) {
3602 if (ULE
->hasExplicitTemplateArgs())
3603 S
.Diag(Loc
, diag::warn_cleanup_ext
);
3604 FD
= S
.ResolveSingleFunctionTemplateSpecialization(ULE
, true);
3605 NI
= ULE
->getNameInfo();
3607 S
.Diag(Loc
, diag::err_attribute_cleanup_arg_not_function
) << 2
3609 if (ULE
->getType() == S
.Context
.OverloadTy
)
3610 S
.NoteAllOverloadCandidates(ULE
);
3614 S
.Diag(Loc
, diag::err_attribute_cleanup_arg_not_function
) << 0;
3618 if (FD
->getNumParams() != 1) {
3619 S
.Diag(Loc
, diag::err_attribute_cleanup_func_must_take_one_arg
)
3624 // We're currently more strict than GCC about what function types we accept.
3625 // If this ever proves to be a problem it should be easy to fix.
3626 QualType Ty
= S
.Context
.getPointerType(cast
<VarDecl
>(D
)->getType());
3627 QualType ParamTy
= FD
->getParamDecl(0)->getType();
3628 if (S
.CheckAssignmentConstraints(FD
->getParamDecl(0)->getLocation(),
3629 ParamTy
, Ty
) != Sema::Compatible
) {
3630 S
.Diag(Loc
, diag::err_attribute_cleanup_func_arg_incompatible_type
)
3631 << NI
.getName() << ParamTy
<< Ty
;
3635 D
->addAttr(::new (S
.Context
) CleanupAttr(S
.Context
, AL
, FD
));
3638 static void handleEnumExtensibilityAttr(Sema
&S
, Decl
*D
,
3639 const ParsedAttr
&AL
) {
3640 if (!AL
.isArgIdent(0)) {
3641 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
3642 << AL
<< 0 << AANT_ArgumentIdentifier
;
3646 EnumExtensibilityAttr::Kind ExtensibilityKind
;
3647 IdentifierInfo
*II
= AL
.getArgAsIdent(0)->Ident
;
3648 if (!EnumExtensibilityAttr::ConvertStrToKind(II
->getName(),
3649 ExtensibilityKind
)) {
3650 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
) << AL
<< II
;
3654 D
->addAttr(::new (S
.Context
)
3655 EnumExtensibilityAttr(S
.Context
, AL
, ExtensibilityKind
));
3658 /// Handle __attribute__((format_arg((idx)))) attribute based on
3659 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
3660 static void handleFormatArgAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3661 Expr
*IdxExpr
= AL
.getArgAsExpr(0);
3663 if (!checkFunctionOrMethodParameterIndex(S
, D
, AL
, 1, IdxExpr
, Idx
))
3666 // Make sure the format string is really a string.
3667 QualType Ty
= getFunctionOrMethodParamType(D
, Idx
.getASTIndex());
3669 bool NotNSStringTy
= !isNSStringType(Ty
, S
.Context
);
3670 if (NotNSStringTy
&&
3671 !isCFStringType(Ty
, S
.Context
) &&
3672 (!Ty
->isPointerType() ||
3673 !Ty
->castAs
<PointerType
>()->getPointeeType()->isCharType())) {
3674 S
.Diag(AL
.getLoc(), diag::err_format_attribute_not
)
3675 << IdxExpr
->getSourceRange() << getFunctionOrMethodParamRange(D
, 0);
3678 Ty
= getFunctionOrMethodResultType(D
);
3679 // replace instancetype with the class type
3680 auto Instancetype
= S
.Context
.getObjCInstanceTypeDecl()->getTypeForDecl();
3681 if (Ty
->getAs
<TypedefType
>() == Instancetype
)
3682 if (auto *OMD
= dyn_cast
<ObjCMethodDecl
>(D
))
3683 if (auto *Interface
= OMD
->getClassInterface())
3684 Ty
= S
.Context
.getObjCObjectPointerType(
3685 QualType(Interface
->getTypeForDecl(), 0));
3686 if (!isNSStringType(Ty
, S
.Context
, /*AllowNSAttributedString=*/true) &&
3687 !isCFStringType(Ty
, S
.Context
) &&
3688 (!Ty
->isPointerType() ||
3689 !Ty
->castAs
<PointerType
>()->getPointeeType()->isCharType())) {
3690 S
.Diag(AL
.getLoc(), diag::err_format_attribute_result_not
)
3691 << (NotNSStringTy
? "string type" : "NSString")
3692 << IdxExpr
->getSourceRange() << getFunctionOrMethodParamRange(D
, 0);
3696 D
->addAttr(::new (S
.Context
) FormatArgAttr(S
.Context
, AL
, Idx
));
3699 enum FormatAttrKind
{
3708 /// getFormatAttrKind - Map from format attribute names to supported format
3710 static FormatAttrKind
getFormatAttrKind(StringRef Format
) {
3711 return llvm::StringSwitch
<FormatAttrKind
>(Format
)
3712 // Check for formats that get handled specially.
3713 .Case("NSString", NSStringFormat
)
3714 .Case("CFString", CFStringFormat
)
3715 .Case("strftime", StrftimeFormat
)
3717 // Otherwise, check for supported formats.
3718 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat
)
3719 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat
)
3720 .Case("kprintf", SupportedFormat
) // OpenBSD.
3721 .Case("freebsd_kprintf", SupportedFormat
) // FreeBSD.
3722 .Case("os_trace", SupportedFormat
)
3723 .Case("os_log", SupportedFormat
)
3725 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat
)
3726 .Default(InvalidFormat
);
3729 /// Handle __attribute__((init_priority(priority))) attributes based on
3730 /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
3731 static void handleInitPriorityAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3732 if (!S
.getLangOpts().CPlusPlus
) {
3733 S
.Diag(AL
.getLoc(), diag::warn_attribute_ignored
) << AL
;
3737 if (S
.getLangOpts().HLSL
) {
3738 S
.Diag(AL
.getLoc(), diag::err_hlsl_init_priority_unsupported
);
3742 if (S
.getCurFunctionOrMethodDecl()) {
3743 S
.Diag(AL
.getLoc(), diag::err_init_priority_object_attr
);
3747 QualType T
= cast
<VarDecl
>(D
)->getType();
3748 if (S
.Context
.getAsArrayType(T
))
3749 T
= S
.Context
.getBaseElementType(T
);
3750 if (!T
->getAs
<RecordType
>()) {
3751 S
.Diag(AL
.getLoc(), diag::err_init_priority_object_attr
);
3756 Expr
*E
= AL
.getArgAsExpr(0);
3757 uint32_t prioritynum
;
3758 if (!checkUInt32Argument(S
, AL
, E
, prioritynum
)) {
3763 // Only perform the priority check if the attribute is outside of a system
3764 // header. Values <= 100 are reserved for the implementation, and libc++
3765 // benefits from being able to specify values in that range.
3766 if ((prioritynum
< 101 || prioritynum
> 65535) &&
3767 !S
.getSourceManager().isInSystemHeader(AL
.getLoc())) {
3768 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_range
)
3769 << E
->getSourceRange() << AL
<< 101 << 65535;
3773 D
->addAttr(::new (S
.Context
) InitPriorityAttr(S
.Context
, AL
, prioritynum
));
3776 ErrorAttr
*Sema::mergeErrorAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
3777 StringRef NewUserDiagnostic
) {
3778 if (const auto *EA
= D
->getAttr
<ErrorAttr
>()) {
3779 std::string NewAttr
= CI
.getNormalizedFullName();
3780 assert((NewAttr
== "error" || NewAttr
== "warning") &&
3781 "unexpected normalized full name");
3782 bool Match
= (EA
->isError() && NewAttr
== "error") ||
3783 (EA
->isWarning() && NewAttr
== "warning");
3785 Diag(EA
->getLocation(), diag::err_attributes_are_not_compatible
)
3787 Diag(CI
.getLoc(), diag::note_conflicting_attribute
);
3790 if (EA
->getUserDiagnostic() != NewUserDiagnostic
) {
3791 Diag(CI
.getLoc(), diag::warn_duplicate_attribute
) << EA
;
3792 Diag(EA
->getLoc(), diag::note_previous_attribute
);
3794 D
->dropAttr
<ErrorAttr
>();
3796 return ::new (Context
) ErrorAttr(Context
, CI
, NewUserDiagnostic
);
3799 FormatAttr
*Sema::mergeFormatAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
3800 IdentifierInfo
*Format
, int FormatIdx
,
3802 // Check whether we already have an equivalent format attribute.
3803 for (auto *F
: D
->specific_attrs
<FormatAttr
>()) {
3804 if (F
->getType() == Format
&&
3805 F
->getFormatIdx() == FormatIdx
&&
3806 F
->getFirstArg() == FirstArg
) {
3807 // If we don't have a valid location for this attribute, adopt the
3809 if (F
->getLocation().isInvalid())
3810 F
->setRange(CI
.getRange());
3815 return ::new (Context
) FormatAttr(Context
, CI
, Format
, FormatIdx
, FirstArg
);
3818 /// Handle __attribute__((format(type,idx,firstarg))) attributes based on
3819 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
3820 static void handleFormatAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3821 if (!AL
.isArgIdent(0)) {
3822 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
3823 << AL
<< 1 << AANT_ArgumentIdentifier
;
3827 // In C++ the implicit 'this' function parameter also counts, and they are
3828 // counted from one.
3829 bool HasImplicitThisParam
= isInstanceMethod(D
);
3830 unsigned NumArgs
= getFunctionOrMethodNumParams(D
) + HasImplicitThisParam
;
3832 IdentifierInfo
*II
= AL
.getArgAsIdent(0)->Ident
;
3833 StringRef Format
= II
->getName();
3835 if (normalizeName(Format
)) {
3836 // If we've modified the string name, we need a new identifier for it.
3837 II
= &S
.Context
.Idents
.get(Format
);
3840 // Check for supported formats.
3841 FormatAttrKind Kind
= getFormatAttrKind(Format
);
3843 if (Kind
== IgnoredFormat
)
3846 if (Kind
== InvalidFormat
) {
3847 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
)
3848 << AL
<< II
->getName();
3852 // checks for the 2nd argument
3853 Expr
*IdxExpr
= AL
.getArgAsExpr(1);
3855 if (!checkUInt32Argument(S
, AL
, IdxExpr
, Idx
, 2))
3858 if (Idx
< 1 || Idx
> NumArgs
) {
3859 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_bounds
)
3860 << AL
<< 2 << IdxExpr
->getSourceRange();
3864 // FIXME: Do we need to bounds check?
3865 unsigned ArgIdx
= Idx
- 1;
3867 if (HasImplicitThisParam
) {
3870 diag::err_format_attribute_implicit_this_format_string
)
3871 << IdxExpr
->getSourceRange();
3877 // make sure the format string is really a string
3878 QualType Ty
= getFunctionOrMethodParamType(D
, ArgIdx
);
3880 if (!isNSStringType(Ty
, S
.Context
, true) &&
3881 !isCFStringType(Ty
, S
.Context
) &&
3882 (!Ty
->isPointerType() ||
3883 !Ty
->castAs
<PointerType
>()->getPointeeType()->isCharType())) {
3884 S
.Diag(AL
.getLoc(), diag::err_format_attribute_not
)
3885 << IdxExpr
->getSourceRange() << getFunctionOrMethodParamRange(D
, ArgIdx
);
3889 // check the 3rd argument
3890 Expr
*FirstArgExpr
= AL
.getArgAsExpr(2);
3892 if (!checkUInt32Argument(S
, AL
, FirstArgExpr
, FirstArg
, 3))
3895 // check if the function is variadic if the 3rd argument non-zero
3896 if (FirstArg
!= 0) {
3897 if (isFunctionOrMethodVariadic(D
))
3898 ++NumArgs
; // +1 for ...
3900 S
.Diag(D
->getLocation(), diag::warn_gcc_requires_variadic_function
) << AL
;
3903 // strftime requires FirstArg to be 0 because it doesn't read from any
3904 // variable the input is just the current time + the format string.
3905 if (Kind
== StrftimeFormat
) {
3906 if (FirstArg
!= 0) {
3907 S
.Diag(AL
.getLoc(), diag::err_format_strftime_third_parameter
)
3908 << FirstArgExpr
->getSourceRange();
3911 // if 0 it disables parameter checking (to use with e.g. va_list)
3912 } else if (FirstArg
!= 0 && FirstArg
!= NumArgs
) {
3913 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_bounds
)
3914 << AL
<< 3 << FirstArgExpr
->getSourceRange();
3918 FormatAttr
*NewAttr
= S
.mergeFormatAttr(D
, AL
, II
, Idx
, FirstArg
);
3920 D
->addAttr(NewAttr
);
3923 /// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3924 static void handleCallbackAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
3925 // The index that identifies the callback callee is mandatory.
3926 if (AL
.getNumArgs() == 0) {
3927 S
.Diag(AL
.getLoc(), diag::err_callback_attribute_no_callee
)
3932 bool HasImplicitThisParam
= isInstanceMethod(D
);
3933 int32_t NumArgs
= getFunctionOrMethodNumParams(D
);
3935 FunctionDecl
*FD
= D
->getAsFunction();
3936 assert(FD
&& "Expected a function declaration!");
3938 llvm::StringMap
<int> NameIdxMapping
;
3939 NameIdxMapping
["__"] = -1;
3941 NameIdxMapping
["this"] = 0;
3944 for (const ParmVarDecl
*PVD
: FD
->parameters())
3945 NameIdxMapping
[PVD
->getName()] = Idx
++;
3947 auto UnknownName
= NameIdxMapping
.end();
3949 SmallVector
<int, 8> EncodingIndices
;
3950 for (unsigned I
= 0, E
= AL
.getNumArgs(); I
< E
; ++I
) {
3954 if (AL
.isArgIdent(I
)) {
3955 IdentifierLoc
*IdLoc
= AL
.getArgAsIdent(I
);
3956 auto It
= NameIdxMapping
.find(IdLoc
->Ident
->getName());
3957 if (It
== UnknownName
) {
3958 S
.Diag(AL
.getLoc(), diag::err_callback_attribute_argument_unknown
)
3959 << IdLoc
->Ident
<< IdLoc
->Loc
;
3963 SR
= SourceRange(IdLoc
->Loc
);
3964 ArgIdx
= It
->second
;
3965 } else if (AL
.isArgExpr(I
)) {
3966 Expr
*IdxExpr
= AL
.getArgAsExpr(I
);
3968 // If the expression is not parseable as an int32_t we have a problem.
3969 if (!checkUInt32Argument(S
, AL
, IdxExpr
, (uint32_t &)ArgIdx
, I
+ 1,
3971 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_bounds
)
3972 << AL
<< (I
+ 1) << IdxExpr
->getSourceRange();
3976 // Check oob, excluding the special values, 0 and -1.
3977 if (ArgIdx
< -1 || ArgIdx
> NumArgs
) {
3978 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_bounds
)
3979 << AL
<< (I
+ 1) << IdxExpr
->getSourceRange();
3983 SR
= IdxExpr
->getSourceRange();
3985 llvm_unreachable("Unexpected ParsedAttr argument type!");
3988 if (ArgIdx
== 0 && !HasImplicitThisParam
) {
3989 S
.Diag(AL
.getLoc(), diag::err_callback_implicit_this_not_available
)
3994 // Adjust for the case we do not have an implicit "this" parameter. In this
3995 // case we decrease all positive values by 1 to get LLVM argument indices.
3996 if (!HasImplicitThisParam
&& ArgIdx
> 0)
3999 EncodingIndices
.push_back(ArgIdx
);
4002 int CalleeIdx
= EncodingIndices
.front();
4003 // Check if the callee index is proper, thus not "this" and not "unknown".
4004 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
4005 // is false and positive if "HasImplicitThisParam" is true.
4006 if (CalleeIdx
< (int)HasImplicitThisParam
) {
4007 S
.Diag(AL
.getLoc(), diag::err_callback_attribute_invalid_callee
)
4012 // Get the callee type, note the index adjustment as the AST doesn't contain
4013 // the this type (which the callee cannot reference anyway!).
4014 const Type
*CalleeType
=
4015 getFunctionOrMethodParamType(D
, CalleeIdx
- HasImplicitThisParam
)
4017 if (!CalleeType
|| !CalleeType
->isFunctionPointerType()) {
4018 S
.Diag(AL
.getLoc(), diag::err_callback_callee_no_function_type
)
4023 const Type
*CalleeFnType
=
4024 CalleeType
->getPointeeType()->getUnqualifiedDesugaredType();
4026 // TODO: Check the type of the callee arguments.
4028 const auto *CalleeFnProtoType
= dyn_cast
<FunctionProtoType
>(CalleeFnType
);
4029 if (!CalleeFnProtoType
) {
4030 S
.Diag(AL
.getLoc(), diag::err_callback_callee_no_function_type
)
4035 if (CalleeFnProtoType
->getNumParams() > EncodingIndices
.size() - 1) {
4036 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_number_arguments
)
4037 << AL
<< (unsigned)(EncodingIndices
.size() - 1);
4041 if (CalleeFnProtoType
->getNumParams() < EncodingIndices
.size() - 1) {
4042 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_number_arguments
)
4043 << AL
<< (unsigned)(EncodingIndices
.size() - 1);
4047 if (CalleeFnProtoType
->isVariadic()) {
4048 S
.Diag(AL
.getLoc(), diag::err_callback_callee_is_variadic
) << AL
.getRange();
4052 // Do not allow multiple callback attributes.
4053 if (D
->hasAttr
<CallbackAttr
>()) {
4054 S
.Diag(AL
.getLoc(), diag::err_callback_attribute_multiple
) << AL
.getRange();
4058 D
->addAttr(::new (S
.Context
) CallbackAttr(
4059 S
.Context
, AL
, EncodingIndices
.data(), EncodingIndices
.size()));
4062 static bool isFunctionLike(const Type
&T
) {
4063 // Check for explicit function types.
4064 // 'called_once' is only supported in Objective-C and it has
4065 // function pointers and block pointers.
4066 return T
.isFunctionPointerType() || T
.isBlockPointerType();
4069 /// Handle 'called_once' attribute.
4070 static void handleCalledOnceAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4071 // 'called_once' only applies to parameters representing functions.
4072 QualType T
= cast
<ParmVarDecl
>(D
)->getType();
4074 if (!isFunctionLike(*T
)) {
4075 S
.Diag(AL
.getLoc(), diag::err_called_once_attribute_wrong_type
);
4079 D
->addAttr(::new (S
.Context
) CalledOnceAttr(S
.Context
, AL
));
4082 static void handleTransparentUnionAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4083 // Try to find the underlying union declaration.
4084 RecordDecl
*RD
= nullptr;
4085 const auto *TD
= dyn_cast
<TypedefNameDecl
>(D
);
4086 if (TD
&& TD
->getUnderlyingType()->isUnionType())
4087 RD
= TD
->getUnderlyingType()->getAsUnionType()->getDecl();
4089 RD
= dyn_cast
<RecordDecl
>(D
);
4091 if (!RD
|| !RD
->isUnion()) {
4092 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
) << AL
4097 if (!RD
->isCompleteDefinition()) {
4098 if (!RD
->isBeingDefined())
4100 diag::warn_transparent_union_attribute_not_definition
);
4104 RecordDecl::field_iterator Field
= RD
->field_begin(),
4105 FieldEnd
= RD
->field_end();
4106 if (Field
== FieldEnd
) {
4107 S
.Diag(AL
.getLoc(), diag::warn_transparent_union_attribute_zero_fields
);
4111 FieldDecl
*FirstField
= *Field
;
4112 QualType FirstType
= FirstField
->getType();
4113 if (FirstType
->hasFloatingRepresentation() || FirstType
->isVectorType()) {
4114 S
.Diag(FirstField
->getLocation(),
4115 diag::warn_transparent_union_attribute_floating
)
4116 << FirstType
->isVectorType() << FirstType
;
4120 if (FirstType
->isIncompleteType())
4122 uint64_t FirstSize
= S
.Context
.getTypeSize(FirstType
);
4123 uint64_t FirstAlign
= S
.Context
.getTypeAlign(FirstType
);
4124 for (; Field
!= FieldEnd
; ++Field
) {
4125 QualType FieldType
= Field
->getType();
4126 if (FieldType
->isIncompleteType())
4128 // FIXME: this isn't fully correct; we also need to test whether the
4129 // members of the union would all have the same calling convention as the
4130 // first member of the union. Checking just the size and alignment isn't
4131 // sufficient (consider structs passed on the stack instead of in registers
4133 if (S
.Context
.getTypeSize(FieldType
) != FirstSize
||
4134 S
.Context
.getTypeAlign(FieldType
) > FirstAlign
) {
4135 // Warn if we drop the attribute.
4136 bool isSize
= S
.Context
.getTypeSize(FieldType
) != FirstSize
;
4137 unsigned FieldBits
= isSize
? S
.Context
.getTypeSize(FieldType
)
4138 : S
.Context
.getTypeAlign(FieldType
);
4139 S
.Diag(Field
->getLocation(),
4140 diag::warn_transparent_union_attribute_field_size_align
)
4141 << isSize
<< *Field
<< FieldBits
;
4142 unsigned FirstBits
= isSize
? FirstSize
: FirstAlign
;
4143 S
.Diag(FirstField
->getLocation(),
4144 diag::note_transparent_union_first_field_size_align
)
4145 << isSize
<< FirstBits
;
4150 RD
->addAttr(::new (S
.Context
) TransparentUnionAttr(S
.Context
, AL
));
4153 void Sema::AddAnnotationAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
4154 StringRef Str
, MutableArrayRef
<Expr
*> Args
) {
4155 auto *Attr
= AnnotateAttr::Create(Context
, Str
, Args
.data(), Args
.size(), CI
);
4156 if (ConstantFoldAttrArgs(
4157 CI
, MutableArrayRef
<Expr
*>(Attr
->args_begin(), Attr
->args_end()))) {
4162 static void handleAnnotateAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4163 // Make sure that there is a string literal as the annotation's first
4166 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
4169 llvm::SmallVector
<Expr
*, 4> Args
;
4170 Args
.reserve(AL
.getNumArgs() - 1);
4171 for (unsigned Idx
= 1; Idx
< AL
.getNumArgs(); Idx
++) {
4172 assert(!AL
.isArgIdent(Idx
));
4173 Args
.push_back(AL
.getArgAsExpr(Idx
));
4176 S
.AddAnnotationAttr(D
, AL
, Str
, Args
);
4179 static void handleAlignValueAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4180 S
.AddAlignValueAttr(D
, AL
, AL
.getArgAsExpr(0));
4183 void Sema::AddAlignValueAttr(Decl
*D
, const AttributeCommonInfo
&CI
, Expr
*E
) {
4184 AlignValueAttr
TmpAttr(Context
, CI
, E
);
4185 SourceLocation AttrLoc
= CI
.getLoc();
4188 if (const auto *TD
= dyn_cast
<TypedefNameDecl
>(D
))
4189 T
= TD
->getUnderlyingType();
4190 else if (const auto *VD
= dyn_cast
<ValueDecl
>(D
))
4193 llvm_unreachable("Unknown decl type for align_value");
4195 if (!T
->isDependentType() && !T
->isAnyPointerType() &&
4196 !T
->isReferenceType() && !T
->isMemberPointerType()) {
4197 Diag(AttrLoc
, diag::warn_attribute_pointer_or_reference_only
)
4198 << &TmpAttr
<< T
<< D
->getSourceRange();
4202 if (!E
->isValueDependent()) {
4203 llvm::APSInt Alignment
;
4204 ExprResult ICE
= VerifyIntegerConstantExpression(
4205 E
, &Alignment
, diag::err_align_value_attribute_argument_not_int
);
4206 if (ICE
.isInvalid())
4209 if (!Alignment
.isPowerOf2()) {
4210 Diag(AttrLoc
, diag::err_alignment_not_power_of_two
)
4211 << E
->getSourceRange();
4215 D
->addAttr(::new (Context
) AlignValueAttr(Context
, CI
, ICE
.get()));
4219 // Save dependent expressions in the AST to be instantiated.
4220 D
->addAttr(::new (Context
) AlignValueAttr(Context
, CI
, E
));
4223 static void handleAlignedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4224 // check the attribute arguments.
4225 if (AL
.getNumArgs() > 1) {
4226 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_number_arguments
) << AL
<< 1;
4230 if (AL
.getNumArgs() == 0) {
4231 D
->addAttr(::new (S
.Context
) AlignedAttr(S
.Context
, AL
, true, nullptr));
4235 Expr
*E
= AL
.getArgAsExpr(0);
4236 if (AL
.isPackExpansion() && !E
->containsUnexpandedParameterPack()) {
4237 S
.Diag(AL
.getEllipsisLoc(),
4238 diag::err_pack_expansion_without_parameter_packs
);
4242 if (!AL
.isPackExpansion() && S
.DiagnoseUnexpandedParameterPack(E
))
4245 S
.AddAlignedAttr(D
, AL
, E
, AL
.isPackExpansion());
4248 void Sema::AddAlignedAttr(Decl
*D
, const AttributeCommonInfo
&CI
, Expr
*E
,
4249 bool IsPackExpansion
) {
4250 AlignedAttr
TmpAttr(Context
, CI
, true, E
);
4251 SourceLocation AttrLoc
= CI
.getLoc();
4253 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
4254 if (TmpAttr
.isAlignas()) {
4255 // C++11 [dcl.align]p1:
4256 // An alignment-specifier may be applied to a variable or to a class
4257 // data member, but it shall not be applied to a bit-field, a function
4258 // parameter, the formal parameter of a catch clause, or a variable
4259 // declared with the register storage class specifier. An
4260 // alignment-specifier may also be applied to the declaration of a class
4261 // or enumeration type.
4263 // CWG agreed to remove permission for alignas to be applied to
4266 // An alignment attribute shall not be specified in a declaration of
4267 // a typedef, or a bit-field, or a function, or a parameter, or an
4268 // object declared with the register storage-class specifier.
4270 if (isa
<ParmVarDecl
>(D
)) {
4272 } else if (const auto *VD
= dyn_cast
<VarDecl
>(D
)) {
4273 if (VD
->getStorageClass() == SC_Register
)
4275 if (VD
->isExceptionVariable())
4277 } else if (const auto *FD
= dyn_cast
<FieldDecl
>(D
)) {
4278 if (FD
->isBitField())
4280 } else if (const auto *ED
= dyn_cast
<EnumDecl
>(D
)) {
4281 if (ED
->getLangOpts().CPlusPlus
)
4283 } else if (!isa
<TagDecl
>(D
)) {
4284 Diag(AttrLoc
, diag::err_attribute_wrong_decl_type
) << &TmpAttr
4285 << (TmpAttr
.isC11() ? ExpectedVariableOrField
4286 : ExpectedVariableFieldOrTag
);
4289 if (DiagKind
!= -1) {
4290 Diag(AttrLoc
, diag::err_alignas_attribute_wrong_decl_type
)
4291 << &TmpAttr
<< DiagKind
;
4296 if (E
->isValueDependent()) {
4297 // We can't support a dependent alignment on a non-dependent type,
4298 // because we have no way to model that a type is "alignment-dependent"
4299 // but not dependent in any other way.
4300 if (const auto *TND
= dyn_cast
<TypedefNameDecl
>(D
)) {
4301 if (!TND
->getUnderlyingType()->isDependentType()) {
4302 Diag(AttrLoc
, diag::err_alignment_dependent_typedef_name
)
4303 << E
->getSourceRange();
4308 // Save dependent expressions in the AST to be instantiated.
4309 AlignedAttr
*AA
= ::new (Context
) AlignedAttr(Context
, CI
, true, E
);
4310 AA
->setPackExpansion(IsPackExpansion
);
4315 // FIXME: Cache the number on the AL object?
4316 llvm::APSInt Alignment
;
4317 ExprResult ICE
= VerifyIntegerConstantExpression(
4318 E
, &Alignment
, diag::err_aligned_attribute_argument_not_int
);
4319 if (ICE
.isInvalid())
4322 uint64_t AlignVal
= Alignment
.getZExtValue();
4323 // C++11 [dcl.align]p2:
4324 // -- if the constant expression evaluates to zero, the alignment
4325 // specifier shall have no effect
4327 // An alignment specification of zero has no effect.
4328 if (!(TmpAttr
.isAlignas() && !Alignment
)) {
4329 if (!llvm::isPowerOf2_64(AlignVal
)) {
4330 Diag(AttrLoc
, diag::err_alignment_not_power_of_two
)
4331 << E
->getSourceRange();
4336 uint64_t MaximumAlignment
= Sema::MaximumAlignment
;
4337 if (Context
.getTargetInfo().getTriple().isOSBinFormatCOFF())
4338 MaximumAlignment
= std::min(MaximumAlignment
, uint64_t(8192));
4339 if (AlignVal
> MaximumAlignment
) {
4340 Diag(AttrLoc
, diag::err_attribute_aligned_too_great
)
4341 << MaximumAlignment
<< E
->getSourceRange();
4345 const auto *VD
= dyn_cast
<VarDecl
>(D
);
4346 if (VD
&& Context
.getTargetInfo().isTLSSupported()) {
4347 unsigned MaxTLSAlign
=
4348 Context
.toCharUnitsFromBits(Context
.getTargetInfo().getMaxTLSAlign())
4350 if (MaxTLSAlign
&& AlignVal
> MaxTLSAlign
&&
4351 VD
->getTLSKind() != VarDecl::TLS_None
) {
4352 Diag(VD
->getLocation(), diag::err_tls_var_aligned_over_maximum
)
4353 << (unsigned)AlignVal
<< VD
<< MaxTLSAlign
;
4358 // On AIX, an aligned attribute can not decrease the alignment when applied
4359 // to a variable declaration with vector type.
4360 if (VD
&& Context
.getTargetInfo().getTriple().isOSAIX()) {
4361 const Type
*Ty
= VD
->getType().getTypePtr();
4362 if (Ty
->isVectorType() && AlignVal
< 16) {
4363 Diag(VD
->getLocation(), diag::warn_aligned_attr_underaligned
)
4364 << VD
->getType() << 16;
4369 AlignedAttr
*AA
= ::new (Context
) AlignedAttr(Context
, CI
, true, ICE
.get());
4370 AA
->setPackExpansion(IsPackExpansion
);
4374 void Sema::AddAlignedAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
4375 TypeSourceInfo
*TS
, bool IsPackExpansion
) {
4376 // FIXME: Cache the number on the AL object if non-dependent?
4377 // FIXME: Perform checking of type validity
4378 AlignedAttr
*AA
= ::new (Context
) AlignedAttr(Context
, CI
, false, TS
);
4379 AA
->setPackExpansion(IsPackExpansion
);
4383 void Sema::CheckAlignasUnderalignment(Decl
*D
) {
4384 assert(D
->hasAttrs() && "no attributes on decl");
4386 QualType UnderlyingTy
, DiagTy
;
4387 if (const auto *VD
= dyn_cast
<ValueDecl
>(D
)) {
4388 UnderlyingTy
= DiagTy
= VD
->getType();
4390 UnderlyingTy
= DiagTy
= Context
.getTagDeclType(cast
<TagDecl
>(D
));
4391 if (const auto *ED
= dyn_cast
<EnumDecl
>(D
))
4392 UnderlyingTy
= ED
->getIntegerType();
4394 if (DiagTy
->isDependentType() || DiagTy
->isIncompleteType())
4397 // C++11 [dcl.align]p5, C11 6.7.5/4:
4398 // The combined effect of all alignment attributes in a declaration shall
4399 // not specify an alignment that is less strict than the alignment that
4400 // would otherwise be required for the entity being declared.
4401 AlignedAttr
*AlignasAttr
= nullptr;
4402 AlignedAttr
*LastAlignedAttr
= nullptr;
4404 for (auto *I
: D
->specific_attrs
<AlignedAttr
>()) {
4405 if (I
->isAlignmentDependent())
4409 Align
= std::max(Align
, I
->getAlignment(Context
));
4410 LastAlignedAttr
= I
;
4413 if (Align
&& DiagTy
->isSizelessType()) {
4414 Diag(LastAlignedAttr
->getLocation(), diag::err_attribute_sizeless_type
)
4415 << LastAlignedAttr
<< DiagTy
;
4416 } else if (AlignasAttr
&& Align
) {
4417 CharUnits RequestedAlign
= Context
.toCharUnitsFromBits(Align
);
4418 CharUnits NaturalAlign
= Context
.getTypeAlignInChars(UnderlyingTy
);
4419 if (NaturalAlign
> RequestedAlign
)
4420 Diag(AlignasAttr
->getLocation(), diag::err_alignas_underaligned
)
4421 << DiagTy
<< (unsigned)NaturalAlign
.getQuantity();
4425 bool Sema::checkMSInheritanceAttrOnDefinition(
4426 CXXRecordDecl
*RD
, SourceRange Range
, bool BestCase
,
4427 MSInheritanceModel ExplicitModel
) {
4428 assert(RD
->hasDefinition() && "RD has no definition!");
4430 // We may not have seen base specifiers or any virtual methods yet. We will
4431 // have to wait until the record is defined to catch any mismatches.
4432 if (!RD
->getDefinition()->isCompleteDefinition())
4435 // The unspecified model never matches what a definition could need.
4436 if (ExplicitModel
== MSInheritanceModel::Unspecified
)
4440 if (RD
->calculateInheritanceModel() == ExplicitModel
)
4443 if (RD
->calculateInheritanceModel() <= ExplicitModel
)
4447 Diag(Range
.getBegin(), diag::err_mismatched_ms_inheritance
)
4448 << 0 /*definition*/;
4449 Diag(RD
->getDefinition()->getLocation(), diag::note_defined_here
) << RD
;
4453 /// parseModeAttrArg - Parses attribute mode string and returns parsed type
4455 static void parseModeAttrArg(Sema
&S
, StringRef Str
, unsigned &DestWidth
,
4456 bool &IntegerMode
, bool &ComplexMode
,
4457 FloatModeKind
&ExplicitType
) {
4459 ComplexMode
= false;
4460 ExplicitType
= FloatModeKind::NoFloat
;
4461 switch (Str
.size()) {
4479 case 'K': // KFmode - IEEE quad precision (__float128)
4480 ExplicitType
= FloatModeKind::Float128
;
4481 DestWidth
= Str
[1] == 'I' ? 0 : 128;
4484 ExplicitType
= FloatModeKind::LongDouble
;
4488 ExplicitType
= FloatModeKind::Ibm128
;
4489 DestWidth
= Str
[1] == 'I' ? 0 : 128;
4492 if (Str
[1] == 'F') {
4493 IntegerMode
= false;
4494 } else if (Str
[1] == 'C') {
4495 IntegerMode
= false;
4497 } else if (Str
[1] != 'I') {
4502 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
4503 // pointer on PIC16 and other embedded platforms.
4505 DestWidth
= S
.Context
.getTargetInfo().getRegisterWidth();
4506 else if (Str
== "byte")
4507 DestWidth
= S
.Context
.getTargetInfo().getCharWidth();
4510 if (Str
== "pointer")
4511 DestWidth
= S
.Context
.getTargetInfo().getPointerWidth(0);
4514 if (Str
== "unwind_word")
4515 DestWidth
= S
.Context
.getTargetInfo().getUnwindWordWidth();
4520 /// handleModeAttr - This attribute modifies the width of a decl with primitive
4523 /// Despite what would be logical, the mode attribute is a decl attribute, not a
4524 /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
4525 /// HImode, not an intermediate pointer.
4526 static void handleModeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4527 // This attribute isn't documented, but glibc uses it. It changes
4528 // the width of an int or unsigned int to the specified size.
4529 if (!AL
.isArgIdent(0)) {
4530 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
4531 << AL
<< AANT_ArgumentIdentifier
;
4535 IdentifierInfo
*Name
= AL
.getArgAsIdent(0)->Ident
;
4537 S
.AddModeAttr(D
, AL
, Name
);
4540 void Sema::AddModeAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
4541 IdentifierInfo
*Name
, bool InInstantiation
) {
4542 StringRef Str
= Name
->getName();
4544 SourceLocation AttrLoc
= CI
.getLoc();
4546 unsigned DestWidth
= 0;
4547 bool IntegerMode
= true;
4548 bool ComplexMode
= false;
4549 FloatModeKind ExplicitType
= FloatModeKind::NoFloat
;
4550 llvm::APInt
VectorSize(64, 0);
4551 if (Str
.size() >= 4 && Str
[0] == 'V') {
4552 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
4553 size_t StrSize
= Str
.size();
4554 size_t VectorStringLength
= 0;
4555 while ((VectorStringLength
+ 1) < StrSize
&&
4556 isdigit(Str
[VectorStringLength
+ 1]))
4557 ++VectorStringLength
;
4558 if (VectorStringLength
&&
4559 !Str
.substr(1, VectorStringLength
).getAsInteger(10, VectorSize
) &&
4560 VectorSize
.isPowerOf2()) {
4561 parseModeAttrArg(*this, Str
.substr(VectorStringLength
+ 1), DestWidth
,
4562 IntegerMode
, ComplexMode
, ExplicitType
);
4563 // Avoid duplicate warning from template instantiation.
4564 if (!InInstantiation
)
4565 Diag(AttrLoc
, diag::warn_vector_mode_deprecated
);
4572 parseModeAttrArg(*this, Str
, DestWidth
, IntegerMode
, ComplexMode
,
4575 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4576 // and friends, at least with glibc.
4577 // FIXME: Make sure floating-point mappings are accurate
4578 // FIXME: Support XF and TF types
4580 Diag(AttrLoc
, diag::err_machine_mode
) << 0 /*Unknown*/ << Name
;
4585 if (const auto *TD
= dyn_cast
<TypedefNameDecl
>(D
))
4586 OldTy
= TD
->getUnderlyingType();
4587 else if (const auto *ED
= dyn_cast
<EnumDecl
>(D
)) {
4588 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4589 // Try to get type from enum declaration, default to int.
4590 OldTy
= ED
->getIntegerType();
4592 OldTy
= Context
.IntTy
;
4594 OldTy
= cast
<ValueDecl
>(D
)->getType();
4596 if (OldTy
->isDependentType()) {
4597 D
->addAttr(::new (Context
) ModeAttr(Context
, CI
, Name
));
4601 // Base type can also be a vector type (see PR17453).
4602 // Distinguish between base type and base element type.
4603 QualType OldElemTy
= OldTy
;
4604 if (const auto *VT
= OldTy
->getAs
<VectorType
>())
4605 OldElemTy
= VT
->getElementType();
4607 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4608 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4609 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4610 if ((isa
<EnumDecl
>(D
) || OldElemTy
->getAs
<EnumType
>()) &&
4611 VectorSize
.getBoolValue()) {
4612 Diag(AttrLoc
, diag::err_enum_mode_vector_type
) << Name
<< CI
.getRange();
4615 bool IntegralOrAnyEnumType
= (OldElemTy
->isIntegralOrEnumerationType() &&
4616 !OldElemTy
->isBitIntType()) ||
4617 OldElemTy
->getAs
<EnumType
>();
4619 if (!OldElemTy
->getAs
<BuiltinType
>() && !OldElemTy
->isComplexType() &&
4620 !IntegralOrAnyEnumType
)
4621 Diag(AttrLoc
, diag::err_mode_not_primitive
);
4622 else if (IntegerMode
) {
4623 if (!IntegralOrAnyEnumType
)
4624 Diag(AttrLoc
, diag::err_mode_wrong_type
);
4625 } else if (ComplexMode
) {
4626 if (!OldElemTy
->isComplexType())
4627 Diag(AttrLoc
, diag::err_mode_wrong_type
);
4629 if (!OldElemTy
->isFloatingType())
4630 Diag(AttrLoc
, diag::err_mode_wrong_type
);
4636 NewElemTy
= Context
.getIntTypeForBitwidth(DestWidth
,
4637 OldElemTy
->isSignedIntegerType());
4639 NewElemTy
= Context
.getRealTypeForBitwidth(DestWidth
, ExplicitType
);
4641 if (NewElemTy
.isNull()) {
4642 Diag(AttrLoc
, diag::err_machine_mode
) << 1 /*Unsupported*/ << Name
;
4647 NewElemTy
= Context
.getComplexType(NewElemTy
);
4650 QualType NewTy
= NewElemTy
;
4651 if (VectorSize
.getBoolValue()) {
4652 NewTy
= Context
.getVectorType(NewTy
, VectorSize
.getZExtValue(),
4653 VectorType::GenericVector
);
4654 } else if (const auto *OldVT
= OldTy
->getAs
<VectorType
>()) {
4655 // Complex machine mode does not support base vector types.
4657 Diag(AttrLoc
, diag::err_complex_mode_vector_type
);
4660 unsigned NumElements
= Context
.getTypeSize(OldElemTy
) *
4661 OldVT
->getNumElements() /
4662 Context
.getTypeSize(NewElemTy
);
4664 Context
.getVectorType(NewElemTy
, NumElements
, OldVT
->getVectorKind());
4667 if (NewTy
.isNull()) {
4668 Diag(AttrLoc
, diag::err_mode_wrong_type
);
4672 // Install the new type.
4673 if (auto *TD
= dyn_cast
<TypedefNameDecl
>(D
))
4674 TD
->setModedTypeSourceInfo(TD
->getTypeSourceInfo(), NewTy
);
4675 else if (auto *ED
= dyn_cast
<EnumDecl
>(D
))
4676 ED
->setIntegerType(NewTy
);
4678 cast
<ValueDecl
>(D
)->setType(NewTy
);
4680 D
->addAttr(::new (Context
) ModeAttr(Context
, CI
, Name
));
4683 static void handleNoDebugAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4684 D
->addAttr(::new (S
.Context
) NoDebugAttr(S
.Context
, AL
));
4687 AlwaysInlineAttr
*Sema::mergeAlwaysInlineAttr(Decl
*D
,
4688 const AttributeCommonInfo
&CI
,
4689 const IdentifierInfo
*Ident
) {
4690 if (OptimizeNoneAttr
*Optnone
= D
->getAttr
<OptimizeNoneAttr
>()) {
4691 Diag(CI
.getLoc(), diag::warn_attribute_ignored
) << Ident
;
4692 Diag(Optnone
->getLocation(), diag::note_conflicting_attribute
);
4696 if (D
->hasAttr
<AlwaysInlineAttr
>())
4699 return ::new (Context
) AlwaysInlineAttr(Context
, CI
);
4702 InternalLinkageAttr
*Sema::mergeInternalLinkageAttr(Decl
*D
,
4703 const ParsedAttr
&AL
) {
4704 if (const auto *VD
= dyn_cast
<VarDecl
>(D
)) {
4705 // Attribute applies to Var but not any subclass of it (like ParmVar,
4706 // ImplicitParm or VarTemplateSpecialization).
4707 if (VD
->getKind() != Decl::Var
) {
4708 Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
)
4709 << AL
<< (getLangOpts().CPlusPlus
? ExpectedFunctionVariableOrClass
4710 : ExpectedVariableOrFunction
);
4713 // Attribute does not apply to non-static local variables.
4714 if (VD
->hasLocalStorage()) {
4715 Diag(VD
->getLocation(), diag::warn_internal_linkage_local_storage
);
4720 return ::new (Context
) InternalLinkageAttr(Context
, AL
);
4722 InternalLinkageAttr
*
4723 Sema::mergeInternalLinkageAttr(Decl
*D
, const InternalLinkageAttr
&AL
) {
4724 if (const auto *VD
= dyn_cast
<VarDecl
>(D
)) {
4725 // Attribute applies to Var but not any subclass of it (like ParmVar,
4726 // ImplicitParm or VarTemplateSpecialization).
4727 if (VD
->getKind() != Decl::Var
) {
4728 Diag(AL
.getLocation(), diag::warn_attribute_wrong_decl_type
)
4729 << &AL
<< (getLangOpts().CPlusPlus
? ExpectedFunctionVariableOrClass
4730 : ExpectedVariableOrFunction
);
4733 // Attribute does not apply to non-static local variables.
4734 if (VD
->hasLocalStorage()) {
4735 Diag(VD
->getLocation(), diag::warn_internal_linkage_local_storage
);
4740 return ::new (Context
) InternalLinkageAttr(Context
, AL
);
4743 MinSizeAttr
*Sema::mergeMinSizeAttr(Decl
*D
, const AttributeCommonInfo
&CI
) {
4744 if (OptimizeNoneAttr
*Optnone
= D
->getAttr
<OptimizeNoneAttr
>()) {
4745 Diag(CI
.getLoc(), diag::warn_attribute_ignored
) << "'minsize'";
4746 Diag(Optnone
->getLocation(), diag::note_conflicting_attribute
);
4750 if (D
->hasAttr
<MinSizeAttr
>())
4753 return ::new (Context
) MinSizeAttr(Context
, CI
);
4756 SwiftNameAttr
*Sema::mergeSwiftNameAttr(Decl
*D
, const SwiftNameAttr
&SNA
,
4758 if (const auto *PrevSNA
= D
->getAttr
<SwiftNameAttr
>()) {
4759 if (PrevSNA
->getName() != Name
&& !PrevSNA
->isImplicit()) {
4760 Diag(PrevSNA
->getLocation(), diag::err_attributes_are_not_compatible
)
4762 Diag(SNA
.getLoc(), diag::note_conflicting_attribute
);
4765 D
->dropAttr
<SwiftNameAttr
>();
4767 return ::new (Context
) SwiftNameAttr(Context
, SNA
, Name
);
4770 OptimizeNoneAttr
*Sema::mergeOptimizeNoneAttr(Decl
*D
,
4771 const AttributeCommonInfo
&CI
) {
4772 if (AlwaysInlineAttr
*Inline
= D
->getAttr
<AlwaysInlineAttr
>()) {
4773 Diag(Inline
->getLocation(), diag::warn_attribute_ignored
) << Inline
;
4774 Diag(CI
.getLoc(), diag::note_conflicting_attribute
);
4775 D
->dropAttr
<AlwaysInlineAttr
>();
4777 if (MinSizeAttr
*MinSize
= D
->getAttr
<MinSizeAttr
>()) {
4778 Diag(MinSize
->getLocation(), diag::warn_attribute_ignored
) << MinSize
;
4779 Diag(CI
.getLoc(), diag::note_conflicting_attribute
);
4780 D
->dropAttr
<MinSizeAttr
>();
4783 if (D
->hasAttr
<OptimizeNoneAttr
>())
4786 return ::new (Context
) OptimizeNoneAttr(Context
, CI
);
4789 static void handleAlwaysInlineAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4790 if (AlwaysInlineAttr
*Inline
=
4791 S
.mergeAlwaysInlineAttr(D
, AL
, AL
.getAttrName()))
4795 static void handleMinSizeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4796 if (MinSizeAttr
*MinSize
= S
.mergeMinSizeAttr(D
, AL
))
4797 D
->addAttr(MinSize
);
4800 static void handleOptimizeNoneAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4801 if (OptimizeNoneAttr
*Optnone
= S
.mergeOptimizeNoneAttr(D
, AL
))
4802 D
->addAttr(Optnone
);
4805 static void handleConstantAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4806 const auto *VD
= cast
<VarDecl
>(D
);
4807 if (VD
->hasLocalStorage()) {
4808 S
.Diag(AL
.getLoc(), diag::err_cuda_nonstatic_constdev
);
4811 // constexpr variable may already get an implicit constant attr, which should
4812 // be replaced by the explicit constant attr.
4813 if (auto *A
= D
->getAttr
<CUDAConstantAttr
>()) {
4814 if (!A
->isImplicit())
4816 D
->dropAttr
<CUDAConstantAttr
>();
4818 D
->addAttr(::new (S
.Context
) CUDAConstantAttr(S
.Context
, AL
));
4821 static void handleSharedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4822 const auto *VD
= cast
<VarDecl
>(D
);
4823 // extern __shared__ is only allowed on arrays with no length (e.g.
4825 if (!S
.getLangOpts().GPURelocatableDeviceCode
&& VD
->hasExternalStorage() &&
4826 !isa
<IncompleteArrayType
>(VD
->getType())) {
4827 S
.Diag(AL
.getLoc(), diag::err_cuda_extern_shared
) << VD
;
4830 if (S
.getLangOpts().CUDA
&& VD
->hasLocalStorage() &&
4831 S
.CUDADiagIfHostCode(AL
.getLoc(), diag::err_cuda_host_shared
)
4832 << S
.CurrentCUDATarget())
4834 D
->addAttr(::new (S
.Context
) CUDASharedAttr(S
.Context
, AL
));
4837 static void handleGlobalAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4838 const auto *FD
= cast
<FunctionDecl
>(D
);
4839 if (!FD
->getReturnType()->isVoidType() &&
4840 !FD
->getReturnType()->getAs
<AutoType
>() &&
4841 !FD
->getReturnType()->isInstantiationDependentType()) {
4842 SourceRange RTRange
= FD
->getReturnTypeSourceRange();
4843 S
.Diag(FD
->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return
)
4845 << (RTRange
.isValid() ? FixItHint::CreateReplacement(RTRange
, "void")
4849 if (const auto *Method
= dyn_cast
<CXXMethodDecl
>(FD
)) {
4850 if (Method
->isInstance()) {
4851 S
.Diag(Method
->getBeginLoc(), diag::err_kern_is_nonstatic_method
)
4855 S
.Diag(Method
->getBeginLoc(), diag::warn_kern_is_method
) << Method
;
4857 // Only warn for "inline" when compiling for host, to cut down on noise.
4858 if (FD
->isInlineSpecified() && !S
.getLangOpts().CUDAIsDevice
)
4859 S
.Diag(FD
->getBeginLoc(), diag::warn_kern_is_inline
) << FD
;
4861 D
->addAttr(::new (S
.Context
) CUDAGlobalAttr(S
.Context
, AL
));
4862 // In host compilation the kernel is emitted as a stub function, which is
4863 // a helper function for launching the kernel. The instructions in the helper
4864 // function has nothing to do with the source code of the kernel. Do not emit
4865 // debug info for the stub function to avoid confusing the debugger.
4866 if (S
.LangOpts
.HIP
&& !S
.LangOpts
.CUDAIsDevice
)
4867 D
->addAttr(NoDebugAttr::CreateImplicit(S
.Context
));
4870 static void handleDeviceAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4871 if (const auto *VD
= dyn_cast
<VarDecl
>(D
)) {
4872 if (VD
->hasLocalStorage()) {
4873 S
.Diag(AL
.getLoc(), diag::err_cuda_nonstatic_constdev
);
4878 if (auto *A
= D
->getAttr
<CUDADeviceAttr
>()) {
4879 if (!A
->isImplicit())
4881 D
->dropAttr
<CUDADeviceAttr
>();
4883 D
->addAttr(::new (S
.Context
) CUDADeviceAttr(S
.Context
, AL
));
4886 static void handleManagedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4887 if (const auto *VD
= dyn_cast
<VarDecl
>(D
)) {
4888 if (VD
->hasLocalStorage()) {
4889 S
.Diag(AL
.getLoc(), diag::err_cuda_nonstatic_constdev
);
4893 if (!D
->hasAttr
<HIPManagedAttr
>())
4894 D
->addAttr(::new (S
.Context
) HIPManagedAttr(S
.Context
, AL
));
4895 if (!D
->hasAttr
<CUDADeviceAttr
>())
4896 D
->addAttr(CUDADeviceAttr::CreateImplicit(S
.Context
));
4899 static void handleGNUInlineAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4900 const auto *Fn
= cast
<FunctionDecl
>(D
);
4901 if (!Fn
->isInlineSpecified()) {
4902 S
.Diag(AL
.getLoc(), diag::warn_gnu_inline_attribute_requires_inline
);
4906 if (S
.LangOpts
.CPlusPlus
&& Fn
->getStorageClass() != SC_Extern
)
4907 S
.Diag(AL
.getLoc(), diag::warn_gnu_inline_cplusplus_without_extern
);
4909 D
->addAttr(::new (S
.Context
) GNUInlineAttr(S
.Context
, AL
));
4912 static void handleCallConvAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
4913 if (hasDeclarator(D
)) return;
4915 // Diagnostic is emitted elsewhere: here we store the (valid) AL
4916 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
4918 if (S
.CheckCallingConvAttr(AL
, CC
, /*FD*/nullptr))
4921 if (!isa
<ObjCMethodDecl
>(D
)) {
4922 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
)
4923 << AL
<< ExpectedFunctionOrMethod
;
4927 switch (AL
.getKind()) {
4928 case ParsedAttr::AT_FastCall
:
4929 D
->addAttr(::new (S
.Context
) FastCallAttr(S
.Context
, AL
));
4931 case ParsedAttr::AT_StdCall
:
4932 D
->addAttr(::new (S
.Context
) StdCallAttr(S
.Context
, AL
));
4934 case ParsedAttr::AT_ThisCall
:
4935 D
->addAttr(::new (S
.Context
) ThisCallAttr(S
.Context
, AL
));
4937 case ParsedAttr::AT_CDecl
:
4938 D
->addAttr(::new (S
.Context
) CDeclAttr(S
.Context
, AL
));
4940 case ParsedAttr::AT_Pascal
:
4941 D
->addAttr(::new (S
.Context
) PascalAttr(S
.Context
, AL
));
4943 case ParsedAttr::AT_SwiftCall
:
4944 D
->addAttr(::new (S
.Context
) SwiftCallAttr(S
.Context
, AL
));
4946 case ParsedAttr::AT_SwiftAsyncCall
:
4947 D
->addAttr(::new (S
.Context
) SwiftAsyncCallAttr(S
.Context
, AL
));
4949 case ParsedAttr::AT_VectorCall
:
4950 D
->addAttr(::new (S
.Context
) VectorCallAttr(S
.Context
, AL
));
4952 case ParsedAttr::AT_MSABI
:
4953 D
->addAttr(::new (S
.Context
) MSABIAttr(S
.Context
, AL
));
4955 case ParsedAttr::AT_SysVABI
:
4956 D
->addAttr(::new (S
.Context
) SysVABIAttr(S
.Context
, AL
));
4958 case ParsedAttr::AT_RegCall
:
4959 D
->addAttr(::new (S
.Context
) RegCallAttr(S
.Context
, AL
));
4961 case ParsedAttr::AT_Pcs
: {
4962 PcsAttr::PCSType PCS
;
4965 PCS
= PcsAttr::AAPCS
;
4968 PCS
= PcsAttr::AAPCS_VFP
;
4971 llvm_unreachable("unexpected calling convention in pcs attribute");
4974 D
->addAttr(::new (S
.Context
) PcsAttr(S
.Context
, AL
, PCS
));
4977 case ParsedAttr::AT_AArch64VectorPcs
:
4978 D
->addAttr(::new (S
.Context
) AArch64VectorPcsAttr(S
.Context
, AL
));
4980 case ParsedAttr::AT_AArch64SVEPcs
:
4981 D
->addAttr(::new (S
.Context
) AArch64SVEPcsAttr(S
.Context
, AL
));
4983 case ParsedAttr::AT_AMDGPUKernelCall
:
4984 D
->addAttr(::new (S
.Context
) AMDGPUKernelCallAttr(S
.Context
, AL
));
4986 case ParsedAttr::AT_IntelOclBicc
:
4987 D
->addAttr(::new (S
.Context
) IntelOclBiccAttr(S
.Context
, AL
));
4989 case ParsedAttr::AT_PreserveMost
:
4990 D
->addAttr(::new (S
.Context
) PreserveMostAttr(S
.Context
, AL
));
4992 case ParsedAttr::AT_PreserveAll
:
4993 D
->addAttr(::new (S
.Context
) PreserveAllAttr(S
.Context
, AL
));
4996 llvm_unreachable("unexpected attribute kind");
5000 static void handleSuppressAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5001 if (!AL
.checkAtLeastNumArgs(S
, 1))
5004 std::vector
<StringRef
> DiagnosticIdentifiers
;
5005 for (unsigned I
= 0, E
= AL
.getNumArgs(); I
!= E
; ++I
) {
5008 if (!S
.checkStringLiteralArgumentAttr(AL
, I
, RuleName
, nullptr))
5011 // FIXME: Warn if the rule name is unknown. This is tricky because only
5012 // clang-tidy knows about available rules.
5013 DiagnosticIdentifiers
.push_back(RuleName
);
5015 D
->addAttr(::new (S
.Context
)
5016 SuppressAttr(S
.Context
, AL
, DiagnosticIdentifiers
.data(),
5017 DiagnosticIdentifiers
.size()));
5020 static void handleLifetimeCategoryAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5021 TypeSourceInfo
*DerefTypeLoc
= nullptr;
5023 if (AL
.hasParsedType()) {
5024 ParmType
= S
.GetTypeFromParser(AL
.getTypeArg(), &DerefTypeLoc
);
5026 unsigned SelectIdx
= ~0U;
5027 if (ParmType
->isReferenceType())
5029 else if (ParmType
->isArrayType())
5032 if (SelectIdx
!= ~0U) {
5033 S
.Diag(AL
.getLoc(), diag::err_attribute_invalid_argument
)
5039 // To check if earlier decl attributes do not conflict the newly parsed ones
5040 // we always add (and check) the attribute to the canonical decl. We need
5041 // to repeat the check for attribute mutual exclusion because we're attaching
5042 // all of the attributes to the canonical declaration rather than the current
5044 D
= D
->getCanonicalDecl();
5045 if (AL
.getKind() == ParsedAttr::AT_Owner
) {
5046 if (checkAttrMutualExclusion
<PointerAttr
>(S
, D
, AL
))
5048 if (const auto *OAttr
= D
->getAttr
<OwnerAttr
>()) {
5049 const Type
*ExistingDerefType
= OAttr
->getDerefTypeLoc()
5050 ? OAttr
->getDerefType().getTypePtr()
5052 if (ExistingDerefType
!= ParmType
.getTypePtrOrNull()) {
5053 S
.Diag(AL
.getLoc(), diag::err_attributes_are_not_compatible
)
5055 S
.Diag(OAttr
->getLocation(), diag::note_conflicting_attribute
);
5059 for (Decl
*Redecl
: D
->redecls()) {
5060 Redecl
->addAttr(::new (S
.Context
) OwnerAttr(S
.Context
, AL
, DerefTypeLoc
));
5063 if (checkAttrMutualExclusion
<OwnerAttr
>(S
, D
, AL
))
5065 if (const auto *PAttr
= D
->getAttr
<PointerAttr
>()) {
5066 const Type
*ExistingDerefType
= PAttr
->getDerefTypeLoc()
5067 ? PAttr
->getDerefType().getTypePtr()
5069 if (ExistingDerefType
!= ParmType
.getTypePtrOrNull()) {
5070 S
.Diag(AL
.getLoc(), diag::err_attributes_are_not_compatible
)
5072 S
.Diag(PAttr
->getLocation(), diag::note_conflicting_attribute
);
5076 for (Decl
*Redecl
: D
->redecls()) {
5077 Redecl
->addAttr(::new (S
.Context
)
5078 PointerAttr(S
.Context
, AL
, DerefTypeLoc
));
5083 static void handleRandomizeLayoutAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5084 if (checkAttrMutualExclusion
<NoRandomizeLayoutAttr
>(S
, D
, AL
))
5086 if (!D
->hasAttr
<RandomizeLayoutAttr
>())
5087 D
->addAttr(::new (S
.Context
) RandomizeLayoutAttr(S
.Context
, AL
));
5090 static void handleNoRandomizeLayoutAttr(Sema
&S
, Decl
*D
,
5091 const ParsedAttr
&AL
) {
5092 if (checkAttrMutualExclusion
<RandomizeLayoutAttr
>(S
, D
, AL
))
5094 if (!D
->hasAttr
<NoRandomizeLayoutAttr
>())
5095 D
->addAttr(::new (S
.Context
) NoRandomizeLayoutAttr(S
.Context
, AL
));
5098 bool Sema::CheckCallingConvAttr(const ParsedAttr
&Attrs
, CallingConv
&CC
,
5099 const FunctionDecl
*FD
) {
5100 if (Attrs
.isInvalid())
5103 if (Attrs
.hasProcessingCache()) {
5104 CC
= (CallingConv
) Attrs
.getProcessingCache();
5108 unsigned ReqArgs
= Attrs
.getKind() == ParsedAttr::AT_Pcs
? 1 : 0;
5109 if (!Attrs
.checkExactlyNumArgs(*this, ReqArgs
)) {
5114 // TODO: diagnose uses of these conventions on the wrong target.
5115 switch (Attrs
.getKind()) {
5116 case ParsedAttr::AT_CDecl
:
5119 case ParsedAttr::AT_FastCall
:
5120 CC
= CC_X86FastCall
;
5122 case ParsedAttr::AT_StdCall
:
5125 case ParsedAttr::AT_ThisCall
:
5126 CC
= CC_X86ThisCall
;
5128 case ParsedAttr::AT_Pascal
:
5131 case ParsedAttr::AT_SwiftCall
:
5134 case ParsedAttr::AT_SwiftAsyncCall
:
5137 case ParsedAttr::AT_VectorCall
:
5138 CC
= CC_X86VectorCall
;
5140 case ParsedAttr::AT_AArch64VectorPcs
:
5141 CC
= CC_AArch64VectorCall
;
5143 case ParsedAttr::AT_AArch64SVEPcs
:
5144 CC
= CC_AArch64SVEPCS
;
5146 case ParsedAttr::AT_AMDGPUKernelCall
:
5147 CC
= CC_AMDGPUKernelCall
;
5149 case ParsedAttr::AT_RegCall
:
5152 case ParsedAttr::AT_MSABI
:
5153 CC
= Context
.getTargetInfo().getTriple().isOSWindows() ? CC_C
:
5156 case ParsedAttr::AT_SysVABI
:
5157 CC
= Context
.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV
:
5160 case ParsedAttr::AT_Pcs
: {
5162 if (!checkStringLiteralArgumentAttr(Attrs
, 0, StrRef
)) {
5166 if (StrRef
== "aapcs") {
5169 } else if (StrRef
== "aapcs-vfp") {
5175 Diag(Attrs
.getLoc(), diag::err_invalid_pcs
);
5178 case ParsedAttr::AT_IntelOclBicc
:
5179 CC
= CC_IntelOclBicc
;
5181 case ParsedAttr::AT_PreserveMost
:
5182 CC
= CC_PreserveMost
;
5184 case ParsedAttr::AT_PreserveAll
:
5185 CC
= CC_PreserveAll
;
5187 default: llvm_unreachable("unexpected attribute kind");
5190 TargetInfo::CallingConvCheckResult A
= TargetInfo::CCCR_OK
;
5191 const TargetInfo
&TI
= Context
.getTargetInfo();
5192 // CUDA functions may have host and/or device attributes which indicate
5193 // their targeted execution environment, therefore the calling convention
5194 // of functions in CUDA should be checked against the target deduced based
5195 // on their host/device attributes.
5196 if (LangOpts
.CUDA
) {
5197 auto *Aux
= Context
.getAuxTargetInfo();
5198 auto CudaTarget
= IdentifyCUDATarget(FD
);
5199 bool CheckHost
= false, CheckDevice
= false;
5200 switch (CudaTarget
) {
5201 case CFT_HostDevice
:
5212 case CFT_InvalidTarget
:
5213 llvm_unreachable("unexpected cuda target");
5215 auto *HostTI
= LangOpts
.CUDAIsDevice
? Aux
: &TI
;
5216 auto *DeviceTI
= LangOpts
.CUDAIsDevice
? &TI
: Aux
;
5217 if (CheckHost
&& HostTI
)
5218 A
= HostTI
->checkCallingConvention(CC
);
5219 if (A
== TargetInfo::CCCR_OK
&& CheckDevice
&& DeviceTI
)
5220 A
= DeviceTI
->checkCallingConvention(CC
);
5222 A
= TI
.checkCallingConvention(CC
);
5226 case TargetInfo::CCCR_OK
:
5229 case TargetInfo::CCCR_Ignore
:
5230 // Treat an ignored convention as if it was an explicit C calling convention
5231 // attribute. For example, __stdcall on Win x64 functions as __cdecl, so
5232 // that command line flags that change the default convention to
5233 // __vectorcall don't affect declarations marked __stdcall.
5237 case TargetInfo::CCCR_Error
:
5238 Diag(Attrs
.getLoc(), diag::error_cconv_unsupported
)
5239 << Attrs
<< (int)CallingConventionIgnoredReason::ForThisTarget
;
5242 case TargetInfo::CCCR_Warning
: {
5243 Diag(Attrs
.getLoc(), diag::warn_cconv_unsupported
)
5244 << Attrs
<< (int)CallingConventionIgnoredReason::ForThisTarget
;
5246 // This convention is not valid for the target. Use the default function or
5247 // method calling convention.
5248 bool IsCXXMethod
= false, IsVariadic
= false;
5250 IsCXXMethod
= FD
->isCXXInstanceMember();
5251 IsVariadic
= FD
->isVariadic();
5253 CC
= Context
.getDefaultCallingConvention(IsVariadic
, IsCXXMethod
);
5258 Attrs
.setProcessingCache((unsigned) CC
);
5262 /// Pointer-like types in the default address space.
5263 static bool isValidSwiftContextType(QualType Ty
) {
5264 if (!Ty
->hasPointerRepresentation())
5265 return Ty
->isDependentType();
5266 return Ty
->getPointeeType().getAddressSpace() == LangAS::Default
;
5269 /// Pointers and references in the default address space.
5270 static bool isValidSwiftIndirectResultType(QualType Ty
) {
5271 if (const auto *PtrType
= Ty
->getAs
<PointerType
>()) {
5272 Ty
= PtrType
->getPointeeType();
5273 } else if (const auto *RefType
= Ty
->getAs
<ReferenceType
>()) {
5274 Ty
= RefType
->getPointeeType();
5276 return Ty
->isDependentType();
5278 return Ty
.getAddressSpace() == LangAS::Default
;
5281 /// Pointers and references to pointers in the default address space.
5282 static bool isValidSwiftErrorResultType(QualType Ty
) {
5283 if (const auto *PtrType
= Ty
->getAs
<PointerType
>()) {
5284 Ty
= PtrType
->getPointeeType();
5285 } else if (const auto *RefType
= Ty
->getAs
<ReferenceType
>()) {
5286 Ty
= RefType
->getPointeeType();
5288 return Ty
->isDependentType();
5290 if (!Ty
.getQualifiers().empty())
5292 return isValidSwiftContextType(Ty
);
5295 void Sema::AddParameterABIAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
5298 QualType type
= cast
<ParmVarDecl
>(D
)->getType();
5300 if (auto existingAttr
= D
->getAttr
<ParameterABIAttr
>()) {
5301 if (existingAttr
->getABI() != abi
) {
5302 Diag(CI
.getLoc(), diag::err_attributes_are_not_compatible
)
5303 << getParameterABISpelling(abi
) << existingAttr
;
5304 Diag(existingAttr
->getLocation(), diag::note_conflicting_attribute
);
5310 case ParameterABI::Ordinary
:
5311 llvm_unreachable("explicit attribute for ordinary parameter ABI?");
5313 case ParameterABI::SwiftContext
:
5314 if (!isValidSwiftContextType(type
)) {
5315 Diag(CI
.getLoc(), diag::err_swift_abi_parameter_wrong_type
)
5316 << getParameterABISpelling(abi
) << /*pointer to pointer */ 0 << type
;
5318 D
->addAttr(::new (Context
) SwiftContextAttr(Context
, CI
));
5321 case ParameterABI::SwiftAsyncContext
:
5322 if (!isValidSwiftContextType(type
)) {
5323 Diag(CI
.getLoc(), diag::err_swift_abi_parameter_wrong_type
)
5324 << getParameterABISpelling(abi
) << /*pointer to pointer */ 0 << type
;
5326 D
->addAttr(::new (Context
) SwiftAsyncContextAttr(Context
, CI
));
5329 case ParameterABI::SwiftErrorResult
:
5330 if (!isValidSwiftErrorResultType(type
)) {
5331 Diag(CI
.getLoc(), diag::err_swift_abi_parameter_wrong_type
)
5332 << getParameterABISpelling(abi
) << /*pointer to pointer */ 1 << type
;
5334 D
->addAttr(::new (Context
) SwiftErrorResultAttr(Context
, CI
));
5337 case ParameterABI::SwiftIndirectResult
:
5338 if (!isValidSwiftIndirectResultType(type
)) {
5339 Diag(CI
.getLoc(), diag::err_swift_abi_parameter_wrong_type
)
5340 << getParameterABISpelling(abi
) << /*pointer*/ 0 << type
;
5342 D
->addAttr(::new (Context
) SwiftIndirectResultAttr(Context
, CI
));
5345 llvm_unreachable("bad parameter ABI attribute");
5348 /// Checks a regparm attribute, returning true if it is ill-formed and
5349 /// otherwise setting numParams to the appropriate value.
5350 bool Sema::CheckRegparmAttr(const ParsedAttr
&AL
, unsigned &numParams
) {
5354 if (!AL
.checkExactlyNumArgs(*this, 1)) {
5360 Expr
*NumParamsExpr
= AL
.getArgAsExpr(0);
5361 if (!checkUInt32Argument(*this, AL
, NumParamsExpr
, NP
)) {
5366 if (Context
.getTargetInfo().getRegParmMax() == 0) {
5367 Diag(AL
.getLoc(), diag::err_attribute_regparm_wrong_platform
)
5368 << NumParamsExpr
->getSourceRange();
5374 if (numParams
> Context
.getTargetInfo().getRegParmMax()) {
5375 Diag(AL
.getLoc(), diag::err_attribute_regparm_invalid_number
)
5376 << Context
.getTargetInfo().getRegParmMax() << NumParamsExpr
->getSourceRange();
5384 // Checks whether an argument of launch_bounds attribute is
5385 // acceptable, performs implicit conversion to Rvalue, and returns
5386 // non-nullptr Expr result on success. Otherwise, it returns nullptr
5387 // and may output an error.
5388 static Expr
*makeLaunchBoundsArgExpr(Sema
&S
, Expr
*E
,
5389 const CUDALaunchBoundsAttr
&AL
,
5390 const unsigned Idx
) {
5391 if (S
.DiagnoseUnexpandedParameterPack(E
))
5394 // Accept template arguments for now as they depend on something else.
5395 // We'll get to check them when they eventually get instantiated.
5396 if (E
->isValueDependent())
5399 Optional
<llvm::APSInt
> I
= llvm::APSInt(64);
5400 if (!(I
= E
->getIntegerConstantExpr(S
.Context
))) {
5401 S
.Diag(E
->getExprLoc(), diag::err_attribute_argument_n_type
)
5402 << &AL
<< Idx
<< AANT_ArgumentIntegerConstant
<< E
->getSourceRange();
5405 // Make sure we can fit it in 32 bits.
5406 if (!I
->isIntN(32)) {
5407 S
.Diag(E
->getExprLoc(), diag::err_ice_too_large
)
5408 << toString(*I
, 10, false) << 32 << /* Unsigned */ 1;
5412 S
.Diag(E
->getExprLoc(), diag::warn_attribute_argument_n_negative
)
5413 << &AL
<< Idx
<< E
->getSourceRange();
5415 // We may need to perform implicit conversion of the argument.
5416 InitializedEntity Entity
= InitializedEntity::InitializeParameter(
5417 S
.Context
, S
.Context
.getConstType(S
.Context
.IntTy
), /*consume*/ false);
5418 ExprResult ValArg
= S
.PerformCopyInitialization(Entity
, SourceLocation(), E
);
5419 assert(!ValArg
.isInvalid() &&
5420 "Unexpected PerformCopyInitialization() failure.");
5422 return ValArg
.getAs
<Expr
>();
5425 void Sema::AddLaunchBoundsAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
5426 Expr
*MaxThreads
, Expr
*MinBlocks
) {
5427 CUDALaunchBoundsAttr
TmpAttr(Context
, CI
, MaxThreads
, MinBlocks
);
5428 MaxThreads
= makeLaunchBoundsArgExpr(*this, MaxThreads
, TmpAttr
, 0);
5429 if (MaxThreads
== nullptr)
5433 MinBlocks
= makeLaunchBoundsArgExpr(*this, MinBlocks
, TmpAttr
, 1);
5434 if (MinBlocks
== nullptr)
5438 D
->addAttr(::new (Context
)
5439 CUDALaunchBoundsAttr(Context
, CI
, MaxThreads
, MinBlocks
));
5442 static void handleLaunchBoundsAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5443 if (!AL
.checkAtLeastNumArgs(S
, 1) || !AL
.checkAtMostNumArgs(S
, 2))
5446 S
.AddLaunchBoundsAttr(D
, AL
, AL
.getArgAsExpr(0),
5447 AL
.getNumArgs() > 1 ? AL
.getArgAsExpr(1) : nullptr);
5450 static void handleArgumentWithTypeTagAttr(Sema
&S
, Decl
*D
,
5451 const ParsedAttr
&AL
) {
5452 if (!AL
.isArgIdent(0)) {
5453 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
5454 << AL
<< /* arg num = */ 1 << AANT_ArgumentIdentifier
;
5458 ParamIdx ArgumentIdx
;
5459 if (!checkFunctionOrMethodParameterIndex(S
, D
, AL
, 2, AL
.getArgAsExpr(1),
5463 ParamIdx TypeTagIdx
;
5464 if (!checkFunctionOrMethodParameterIndex(S
, D
, AL
, 3, AL
.getArgAsExpr(2),
5468 bool IsPointer
= AL
.getAttrName()->getName() == "pointer_with_type_tag";
5470 // Ensure that buffer has a pointer type.
5471 unsigned ArgumentIdxAST
= ArgumentIdx
.getASTIndex();
5472 if (ArgumentIdxAST
>= getFunctionOrMethodNumParams(D
) ||
5473 !getFunctionOrMethodParamType(D
, ArgumentIdxAST
)->isPointerType())
5474 S
.Diag(AL
.getLoc(), diag::err_attribute_pointers_only
) << AL
<< 0;
5477 D
->addAttr(::new (S
.Context
) ArgumentWithTypeTagAttr(
5478 S
.Context
, AL
, AL
.getArgAsIdent(0)->Ident
, ArgumentIdx
, TypeTagIdx
,
5482 static void handleTypeTagForDatatypeAttr(Sema
&S
, Decl
*D
,
5483 const ParsedAttr
&AL
) {
5484 if (!AL
.isArgIdent(0)) {
5485 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
5486 << AL
<< 1 << AANT_ArgumentIdentifier
;
5490 if (!AL
.checkExactlyNumArgs(S
, 1))
5493 if (!isa
<VarDecl
>(D
)) {
5494 S
.Diag(AL
.getLoc(), diag::err_attribute_wrong_decl_type
)
5495 << AL
<< ExpectedVariable
;
5499 IdentifierInfo
*PointerKind
= AL
.getArgAsIdent(0)->Ident
;
5500 TypeSourceInfo
*MatchingCTypeLoc
= nullptr;
5501 S
.GetTypeFromParser(AL
.getMatchingCType(), &MatchingCTypeLoc
);
5502 assert(MatchingCTypeLoc
&& "no type source info for attribute argument");
5504 D
->addAttr(::new (S
.Context
) TypeTagForDatatypeAttr(
5505 S
.Context
, AL
, PointerKind
, MatchingCTypeLoc
, AL
.getLayoutCompatible(),
5506 AL
.getMustBeNull()));
5509 static void handleXRayLogArgsAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5512 if (!checkFunctionOrMethodParameterIndex(S
, D
, AL
, 1, AL
.getArgAsExpr(0),
5514 true /* CanIndexImplicitThis */))
5517 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
5518 D
->addAttr(::new (S
.Context
)
5519 XRayLogArgsAttr(S
.Context
, AL
, ArgCount
.getSourceIndex()));
5522 static void handlePatchableFunctionEntryAttr(Sema
&S
, Decl
*D
,
5523 const ParsedAttr
&AL
) {
5524 uint32_t Count
= 0, Offset
= 0;
5525 if (!checkUInt32Argument(S
, AL
, AL
.getArgAsExpr(0), Count
, 0, true))
5527 if (AL
.getNumArgs() == 2) {
5528 Expr
*Arg
= AL
.getArgAsExpr(1);
5529 if (!checkUInt32Argument(S
, AL
, Arg
, Offset
, 1, true))
5531 if (Count
< Offset
) {
5532 S
.Diag(getAttrLoc(AL
), diag::err_attribute_argument_out_of_range
)
5533 << &AL
<< 0 << Count
<< Arg
->getBeginLoc();
5537 D
->addAttr(::new (S
.Context
)
5538 PatchableFunctionEntryAttr(S
.Context
, AL
, Count
, Offset
));
5542 struct IntrinToName
{
5547 } // unnamed namespace
5549 static bool ArmBuiltinAliasValid(unsigned BuiltinID
, StringRef AliasName
,
5550 ArrayRef
<IntrinToName
> Map
,
5551 const char *IntrinNames
) {
5552 if (AliasName
.startswith("__arm_"))
5553 AliasName
= AliasName
.substr(6);
5554 const IntrinToName
*It
=
5555 llvm::lower_bound(Map
, BuiltinID
, [](const IntrinToName
&L
, unsigned Id
) {
5558 if (It
== Map
.end() || It
->Id
!= BuiltinID
)
5560 StringRef
FullName(&IntrinNames
[It
->FullName
]);
5561 if (AliasName
== FullName
)
5563 if (It
->ShortName
== -1)
5565 StringRef
ShortName(&IntrinNames
[It
->ShortName
]);
5566 return AliasName
== ShortName
;
5569 static bool ArmMveAliasValid(unsigned BuiltinID
, StringRef AliasName
) {
5570 #include "clang/Basic/arm_mve_builtin_aliases.inc"
5571 // The included file defines:
5572 // - ArrayRef<IntrinToName> Map
5573 // - const char IntrinNames[]
5574 return ArmBuiltinAliasValid(BuiltinID
, AliasName
, Map
, IntrinNames
);
5577 static bool ArmCdeAliasValid(unsigned BuiltinID
, StringRef AliasName
) {
5578 #include "clang/Basic/arm_cde_builtin_aliases.inc"
5579 return ArmBuiltinAliasValid(BuiltinID
, AliasName
, Map
, IntrinNames
);
5582 static bool ArmSveAliasValid(ASTContext
&Context
, unsigned BuiltinID
,
5583 StringRef AliasName
) {
5584 if (Context
.BuiltinInfo
.isAuxBuiltinID(BuiltinID
))
5585 BuiltinID
= Context
.BuiltinInfo
.getAuxBuiltinID(BuiltinID
);
5586 return BuiltinID
>= AArch64::FirstSVEBuiltin
&&
5587 BuiltinID
<= AArch64::LastSVEBuiltin
;
5590 static void handleArmBuiltinAliasAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5591 if (!AL
.isArgIdent(0)) {
5592 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
5593 << AL
<< 1 << AANT_ArgumentIdentifier
;
5597 IdentifierInfo
*Ident
= AL
.getArgAsIdent(0)->Ident
;
5598 unsigned BuiltinID
= Ident
->getBuiltinID();
5599 StringRef AliasName
= cast
<FunctionDecl
>(D
)->getIdentifier()->getName();
5601 bool IsAArch64
= S
.Context
.getTargetInfo().getTriple().isAArch64();
5602 if ((IsAArch64
&& !ArmSveAliasValid(S
.Context
, BuiltinID
, AliasName
)) ||
5603 (!IsAArch64
&& !ArmMveAliasValid(BuiltinID
, AliasName
) &&
5604 !ArmCdeAliasValid(BuiltinID
, AliasName
))) {
5605 S
.Diag(AL
.getLoc(), diag::err_attribute_arm_builtin_alias
);
5609 D
->addAttr(::new (S
.Context
) ArmBuiltinAliasAttr(S
.Context
, AL
, Ident
));
5612 static bool RISCVAliasValid(unsigned BuiltinID
, StringRef AliasName
) {
5613 return BuiltinID
>= RISCV::FirstRVVBuiltin
&&
5614 BuiltinID
<= RISCV::LastRVVBuiltin
;
5617 static void handleBuiltinAliasAttr(Sema
&S
, Decl
*D
,
5618 const ParsedAttr
&AL
) {
5619 if (!AL
.isArgIdent(0)) {
5620 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
5621 << AL
<< 1 << AANT_ArgumentIdentifier
;
5625 IdentifierInfo
*Ident
= AL
.getArgAsIdent(0)->Ident
;
5626 unsigned BuiltinID
= Ident
->getBuiltinID();
5627 StringRef AliasName
= cast
<FunctionDecl
>(D
)->getIdentifier()->getName();
5629 bool IsAArch64
= S
.Context
.getTargetInfo().getTriple().isAArch64();
5630 bool IsARM
= S
.Context
.getTargetInfo().getTriple().isARM();
5631 bool IsRISCV
= S
.Context
.getTargetInfo().getTriple().isRISCV();
5632 bool IsHLSL
= S
.Context
.getLangOpts().HLSL
;
5633 if ((IsAArch64
&& !ArmSveAliasValid(S
.Context
, BuiltinID
, AliasName
)) ||
5634 (IsARM
&& !ArmMveAliasValid(BuiltinID
, AliasName
) &&
5635 !ArmCdeAliasValid(BuiltinID
, AliasName
)) ||
5636 (IsRISCV
&& !RISCVAliasValid(BuiltinID
, AliasName
)) ||
5637 (!IsAArch64
&& !IsARM
&& !IsRISCV
&& !IsHLSL
)) {
5638 S
.Diag(AL
.getLoc(), diag::err_attribute_builtin_alias
) << AL
;
5642 D
->addAttr(::new (S
.Context
) BuiltinAliasAttr(S
.Context
, AL
, Ident
));
5645 //===----------------------------------------------------------------------===//
5646 // Checker-specific attribute handlers.
5647 //===----------------------------------------------------------------------===//
5648 static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType QT
) {
5649 return QT
->isDependentType() || QT
->isObjCRetainableType();
5652 static bool isValidSubjectOfNSAttribute(QualType QT
) {
5653 return QT
->isDependentType() || QT
->isObjCObjectPointerType() ||
5654 QT
->isObjCNSObjectType();
5657 static bool isValidSubjectOfCFAttribute(QualType QT
) {
5658 return QT
->isDependentType() || QT
->isPointerType() ||
5659 isValidSubjectOfNSAttribute(QT
);
5662 static bool isValidSubjectOfOSAttribute(QualType QT
) {
5663 if (QT
->isDependentType())
5665 QualType PT
= QT
->getPointeeType();
5666 return !PT
.isNull() && PT
->getAsCXXRecordDecl() != nullptr;
5669 void Sema::AddXConsumedAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
5670 RetainOwnershipKind K
,
5671 bool IsTemplateInstantiation
) {
5672 ValueDecl
*VD
= cast
<ValueDecl
>(D
);
5674 case RetainOwnershipKind::OS
:
5675 handleSimpleAttributeOrDiagnose
<OSConsumedAttr
>(
5676 *this, VD
, CI
, isValidSubjectOfOSAttribute(VD
->getType()),
5677 diag::warn_ns_attribute_wrong_parameter_type
,
5678 /*ExtraArgs=*/CI
.getRange(), "os_consumed", /*pointers*/ 1);
5680 case RetainOwnershipKind::NS
:
5681 handleSimpleAttributeOrDiagnose
<NSConsumedAttr
>(
5682 *this, VD
, CI
, isValidSubjectOfNSAttribute(VD
->getType()),
5684 // These attributes are normally just advisory, but in ARC, ns_consumed
5685 // is significant. Allow non-dependent code to contain inappropriate
5686 // attributes even in ARC, but require template instantiations to be
5687 // set up correctly.
5688 ((IsTemplateInstantiation
&& getLangOpts().ObjCAutoRefCount
)
5689 ? diag::err_ns_attribute_wrong_parameter_type
5690 : diag::warn_ns_attribute_wrong_parameter_type
),
5691 /*ExtraArgs=*/CI
.getRange(), "ns_consumed", /*objc pointers*/ 0);
5693 case RetainOwnershipKind::CF
:
5694 handleSimpleAttributeOrDiagnose
<CFConsumedAttr
>(
5695 *this, VD
, CI
, isValidSubjectOfCFAttribute(VD
->getType()),
5696 diag::warn_ns_attribute_wrong_parameter_type
,
5697 /*ExtraArgs=*/CI
.getRange(), "cf_consumed", /*pointers*/ 1);
5702 static Sema::RetainOwnershipKind
5703 parsedAttrToRetainOwnershipKind(const ParsedAttr
&AL
) {
5704 switch (AL
.getKind()) {
5705 case ParsedAttr::AT_CFConsumed
:
5706 case ParsedAttr::AT_CFReturnsRetained
:
5707 case ParsedAttr::AT_CFReturnsNotRetained
:
5708 return Sema::RetainOwnershipKind::CF
;
5709 case ParsedAttr::AT_OSConsumesThis
:
5710 case ParsedAttr::AT_OSConsumed
:
5711 case ParsedAttr::AT_OSReturnsRetained
:
5712 case ParsedAttr::AT_OSReturnsNotRetained
:
5713 case ParsedAttr::AT_OSReturnsRetainedOnZero
:
5714 case ParsedAttr::AT_OSReturnsRetainedOnNonZero
:
5715 return Sema::RetainOwnershipKind::OS
;
5716 case ParsedAttr::AT_NSConsumesSelf
:
5717 case ParsedAttr::AT_NSConsumed
:
5718 case ParsedAttr::AT_NSReturnsRetained
:
5719 case ParsedAttr::AT_NSReturnsNotRetained
:
5720 case ParsedAttr::AT_NSReturnsAutoreleased
:
5721 return Sema::RetainOwnershipKind::NS
;
5723 llvm_unreachable("Wrong argument supplied");
5727 bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc
, QualType QT
) {
5728 if (isValidSubjectOfNSReturnsRetainedAttribute(QT
))
5731 Diag(Loc
, diag::warn_ns_attribute_wrong_return_type
)
5732 << "'ns_returns_retained'" << 0 << 0;
5736 /// \return whether the parameter is a pointer to OSObject pointer.
5737 static bool isValidOSObjectOutParameter(const Decl
*D
) {
5738 const auto *PVD
= dyn_cast
<ParmVarDecl
>(D
);
5741 QualType QT
= PVD
->getType();
5742 QualType PT
= QT
->getPointeeType();
5743 return !PT
.isNull() && isValidSubjectOfOSAttribute(PT
);
5746 static void handleXReturnsXRetainedAttr(Sema
&S
, Decl
*D
,
5747 const ParsedAttr
&AL
) {
5748 QualType ReturnType
;
5749 Sema::RetainOwnershipKind K
= parsedAttrToRetainOwnershipKind(AL
);
5751 if (const auto *MD
= dyn_cast
<ObjCMethodDecl
>(D
)) {
5752 ReturnType
= MD
->getReturnType();
5753 } else if (S
.getLangOpts().ObjCAutoRefCount
&& hasDeclarator(D
) &&
5754 (AL
.getKind() == ParsedAttr::AT_NSReturnsRetained
)) {
5755 return; // ignore: was handled as a type attribute
5756 } else if (const auto *PD
= dyn_cast
<ObjCPropertyDecl
>(D
)) {
5757 ReturnType
= PD
->getType();
5758 } else if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
5759 ReturnType
= FD
->getReturnType();
5760 } else if (const auto *Param
= dyn_cast
<ParmVarDecl
>(D
)) {
5761 // Attributes on parameters are used for out-parameters,
5762 // passed as pointers-to-pointers.
5763 unsigned DiagID
= K
== Sema::RetainOwnershipKind::CF
5764 ? /*pointer-to-CF-pointer*/2
5765 : /*pointer-to-OSObject-pointer*/3;
5766 ReturnType
= Param
->getType()->getPointeeType();
5767 if (ReturnType
.isNull()) {
5768 S
.Diag(D
->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type
)
5769 << AL
<< DiagID
<< AL
.getRange();
5772 } else if (AL
.isUsedAsTypeAttr()) {
5775 AttributeDeclKind ExpectedDeclKind
;
5776 switch (AL
.getKind()) {
5777 default: llvm_unreachable("invalid ownership attribute");
5778 case ParsedAttr::AT_NSReturnsRetained
:
5779 case ParsedAttr::AT_NSReturnsAutoreleased
:
5780 case ParsedAttr::AT_NSReturnsNotRetained
:
5781 ExpectedDeclKind
= ExpectedFunctionOrMethod
;
5784 case ParsedAttr::AT_OSReturnsRetained
:
5785 case ParsedAttr::AT_OSReturnsNotRetained
:
5786 case ParsedAttr::AT_CFReturnsRetained
:
5787 case ParsedAttr::AT_CFReturnsNotRetained
:
5788 ExpectedDeclKind
= ExpectedFunctionMethodOrParameter
;
5791 S
.Diag(D
->getBeginLoc(), diag::warn_attribute_wrong_decl_type
)
5792 << AL
.getRange() << AL
<< ExpectedDeclKind
;
5798 unsigned ParmDiagID
= 2; // Pointer-to-CF-pointer
5799 switch (AL
.getKind()) {
5800 default: llvm_unreachable("invalid ownership attribute");
5801 case ParsedAttr::AT_NSReturnsRetained
:
5802 TypeOK
= isValidSubjectOfNSReturnsRetainedAttribute(ReturnType
);
5806 case ParsedAttr::AT_NSReturnsAutoreleased
:
5807 case ParsedAttr::AT_NSReturnsNotRetained
:
5808 TypeOK
= isValidSubjectOfNSAttribute(ReturnType
);
5812 case ParsedAttr::AT_CFReturnsRetained
:
5813 case ParsedAttr::AT_CFReturnsNotRetained
:
5814 TypeOK
= isValidSubjectOfCFAttribute(ReturnType
);
5818 case ParsedAttr::AT_OSReturnsRetained
:
5819 case ParsedAttr::AT_OSReturnsNotRetained
:
5820 TypeOK
= isValidSubjectOfOSAttribute(ReturnType
);
5822 ParmDiagID
= 3; // Pointer-to-OSObject-pointer
5827 if (AL
.isUsedAsTypeAttr())
5830 if (isa
<ParmVarDecl
>(D
)) {
5831 S
.Diag(D
->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type
)
5832 << AL
<< ParmDiagID
<< AL
.getRange();
5834 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5839 } SubjectKind
= Function
;
5840 if (isa
<ObjCMethodDecl
>(D
))
5841 SubjectKind
= Method
;
5842 else if (isa
<ObjCPropertyDecl
>(D
))
5843 SubjectKind
= Property
;
5844 S
.Diag(D
->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type
)
5845 << AL
<< SubjectKind
<< Cf
<< AL
.getRange();
5850 switch (AL
.getKind()) {
5852 llvm_unreachable("invalid ownership attribute");
5853 case ParsedAttr::AT_NSReturnsAutoreleased
:
5854 handleSimpleAttribute
<NSReturnsAutoreleasedAttr
>(S
, D
, AL
);
5856 case ParsedAttr::AT_CFReturnsNotRetained
:
5857 handleSimpleAttribute
<CFReturnsNotRetainedAttr
>(S
, D
, AL
);
5859 case ParsedAttr::AT_NSReturnsNotRetained
:
5860 handleSimpleAttribute
<NSReturnsNotRetainedAttr
>(S
, D
, AL
);
5862 case ParsedAttr::AT_CFReturnsRetained
:
5863 handleSimpleAttribute
<CFReturnsRetainedAttr
>(S
, D
, AL
);
5865 case ParsedAttr::AT_NSReturnsRetained
:
5866 handleSimpleAttribute
<NSReturnsRetainedAttr
>(S
, D
, AL
);
5868 case ParsedAttr::AT_OSReturnsRetained
:
5869 handleSimpleAttribute
<OSReturnsRetainedAttr
>(S
, D
, AL
);
5871 case ParsedAttr::AT_OSReturnsNotRetained
:
5872 handleSimpleAttribute
<OSReturnsNotRetainedAttr
>(S
, D
, AL
);
5877 static void handleObjCReturnsInnerPointerAttr(Sema
&S
, Decl
*D
,
5878 const ParsedAttr
&Attrs
) {
5879 const int EP_ObjCMethod
= 1;
5880 const int EP_ObjCProperty
= 2;
5882 SourceLocation loc
= Attrs
.getLoc();
5883 QualType resultType
;
5884 if (isa
<ObjCMethodDecl
>(D
))
5885 resultType
= cast
<ObjCMethodDecl
>(D
)->getReturnType();
5887 resultType
= cast
<ObjCPropertyDecl
>(D
)->getType();
5889 if (!resultType
->isReferenceType() &&
5890 (!resultType
->isPointerType() || resultType
->isObjCRetainableType())) {
5891 S
.Diag(D
->getBeginLoc(), diag::warn_ns_attribute_wrong_return_type
)
5892 << SourceRange(loc
) << Attrs
5893 << (isa
<ObjCMethodDecl
>(D
) ? EP_ObjCMethod
: EP_ObjCProperty
)
5894 << /*non-retainable pointer*/ 2;
5896 // Drop the attribute.
5900 D
->addAttr(::new (S
.Context
) ObjCReturnsInnerPointerAttr(S
.Context
, Attrs
));
5903 static void handleObjCRequiresSuperAttr(Sema
&S
, Decl
*D
,
5904 const ParsedAttr
&Attrs
) {
5905 const auto *Method
= cast
<ObjCMethodDecl
>(D
);
5907 const DeclContext
*DC
= Method
->getDeclContext();
5908 if (const auto *PDecl
= dyn_cast_or_null
<ObjCProtocolDecl
>(DC
)) {
5909 S
.Diag(D
->getBeginLoc(), diag::warn_objc_requires_super_protocol
) << Attrs
5911 S
.Diag(PDecl
->getLocation(), diag::note_protocol_decl
);
5914 if (Method
->getMethodFamily() == OMF_dealloc
) {
5915 S
.Diag(D
->getBeginLoc(), diag::warn_objc_requires_super_protocol
) << Attrs
5920 D
->addAttr(::new (S
.Context
) ObjCRequiresSuperAttr(S
.Context
, Attrs
));
5923 static void handleNSErrorDomain(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5924 auto *E
= AL
.getArgAsExpr(0);
5925 auto Loc
= E
? E
->getBeginLoc() : AL
.getLoc();
5927 auto *DRE
= dyn_cast
<DeclRefExpr
>(AL
.getArgAsExpr(0));
5929 S
.Diag(Loc
, diag::err_nserrordomain_invalid_decl
) << 0;
5933 auto *VD
= dyn_cast
<VarDecl
>(DRE
->getDecl());
5935 S
.Diag(Loc
, diag::err_nserrordomain_invalid_decl
) << 1 << DRE
->getDecl();
5939 if (!isNSStringType(VD
->getType(), S
.Context
) &&
5940 !isCFStringType(VD
->getType(), S
.Context
)) {
5941 S
.Diag(Loc
, diag::err_nserrordomain_wrong_type
) << VD
;
5945 D
->addAttr(::new (S
.Context
) NSErrorDomainAttr(S
.Context
, AL
, VD
));
5948 static void handleObjCBridgeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
5949 IdentifierLoc
*Parm
= AL
.isArgIdent(0) ? AL
.getArgAsIdent(0) : nullptr;
5952 S
.Diag(D
->getBeginLoc(), diag::err_objc_attr_not_id
) << AL
<< 0;
5956 // Typedefs only allow objc_bridge(id) and have some additional checking.
5957 if (const auto *TD
= dyn_cast
<TypedefNameDecl
>(D
)) {
5958 if (!Parm
->Ident
->isStr("id")) {
5959 S
.Diag(AL
.getLoc(), diag::err_objc_attr_typedef_not_id
) << AL
;
5963 // Only allow 'cv void *'.
5964 QualType T
= TD
->getUnderlyingType();
5965 if (!T
->isVoidPointerType()) {
5966 S
.Diag(AL
.getLoc(), diag::err_objc_attr_typedef_not_void_pointer
);
5971 D
->addAttr(::new (S
.Context
) ObjCBridgeAttr(S
.Context
, AL
, Parm
->Ident
));
5974 static void handleObjCBridgeMutableAttr(Sema
&S
, Decl
*D
,
5975 const ParsedAttr
&AL
) {
5976 IdentifierLoc
*Parm
= AL
.isArgIdent(0) ? AL
.getArgAsIdent(0) : nullptr;
5979 S
.Diag(D
->getBeginLoc(), diag::err_objc_attr_not_id
) << AL
<< 0;
5983 D
->addAttr(::new (S
.Context
)
5984 ObjCBridgeMutableAttr(S
.Context
, AL
, Parm
->Ident
));
5987 static void handleObjCBridgeRelatedAttr(Sema
&S
, Decl
*D
,
5988 const ParsedAttr
&AL
) {
5989 IdentifierInfo
*RelatedClass
=
5990 AL
.isArgIdent(0) ? AL
.getArgAsIdent(0)->Ident
: nullptr;
5991 if (!RelatedClass
) {
5992 S
.Diag(D
->getBeginLoc(), diag::err_objc_attr_not_id
) << AL
<< 0;
5995 IdentifierInfo
*ClassMethod
=
5996 AL
.getArgAsIdent(1) ? AL
.getArgAsIdent(1)->Ident
: nullptr;
5997 IdentifierInfo
*InstanceMethod
=
5998 AL
.getArgAsIdent(2) ? AL
.getArgAsIdent(2)->Ident
: nullptr;
5999 D
->addAttr(::new (S
.Context
) ObjCBridgeRelatedAttr(
6000 S
.Context
, AL
, RelatedClass
, ClassMethod
, InstanceMethod
));
6003 static void handleObjCDesignatedInitializer(Sema
&S
, Decl
*D
,
6004 const ParsedAttr
&AL
) {
6005 DeclContext
*Ctx
= D
->getDeclContext();
6007 // This attribute can only be applied to methods in interfaces or class
6009 if (!isa
<ObjCInterfaceDecl
>(Ctx
) &&
6010 !(isa
<ObjCCategoryDecl
>(Ctx
) &&
6011 cast
<ObjCCategoryDecl
>(Ctx
)->IsClassExtension())) {
6012 S
.Diag(D
->getLocation(), diag::err_designated_init_attr_non_init
);
6016 ObjCInterfaceDecl
*IFace
;
6017 if (auto *CatDecl
= dyn_cast
<ObjCCategoryDecl
>(Ctx
))
6018 IFace
= CatDecl
->getClassInterface();
6020 IFace
= cast
<ObjCInterfaceDecl
>(Ctx
);
6025 IFace
->setHasDesignatedInitializers();
6026 D
->addAttr(::new (S
.Context
) ObjCDesignatedInitializerAttr(S
.Context
, AL
));
6029 static void handleObjCRuntimeName(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6030 StringRef MetaDataName
;
6031 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, MetaDataName
))
6033 D
->addAttr(::new (S
.Context
)
6034 ObjCRuntimeNameAttr(S
.Context
, AL
, MetaDataName
));
6037 // When a user wants to use objc_boxable with a union or struct
6038 // but they don't have access to the declaration (legacy/third-party code)
6039 // then they can 'enable' this feature with a typedef:
6040 // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct;
6041 static void handleObjCBoxable(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6042 bool notify
= false;
6044 auto *RD
= dyn_cast
<RecordDecl
>(D
);
6045 if (RD
&& RD
->getDefinition()) {
6046 RD
= RD
->getDefinition();
6051 ObjCBoxableAttr
*BoxableAttr
=
6052 ::new (S
.Context
) ObjCBoxableAttr(S
.Context
, AL
);
6053 RD
->addAttr(BoxableAttr
);
6055 // we need to notify ASTReader/ASTWriter about
6056 // modification of existing declaration
6057 if (ASTMutationListener
*L
= S
.getASTMutationListener())
6058 L
->AddedAttributeToRecord(BoxableAttr
, RD
);
6063 static void handleObjCOwnershipAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6064 if (hasDeclarator(D
)) return;
6066 S
.Diag(D
->getBeginLoc(), diag::err_attribute_wrong_decl_type
)
6067 << AL
.getRange() << AL
<< ExpectedVariable
;
6070 static void handleObjCPreciseLifetimeAttr(Sema
&S
, Decl
*D
,
6071 const ParsedAttr
&AL
) {
6072 const auto *VD
= cast
<ValueDecl
>(D
);
6073 QualType QT
= VD
->getType();
6075 if (!QT
->isDependentType() &&
6076 !QT
->isObjCLifetimeType()) {
6077 S
.Diag(AL
.getLoc(), diag::err_objc_precise_lifetime_bad_type
)
6082 Qualifiers::ObjCLifetime Lifetime
= QT
.getObjCLifetime();
6084 // If we have no lifetime yet, check the lifetime we're presumably
6086 if (Lifetime
== Qualifiers::OCL_None
&& !QT
->isDependentType())
6087 Lifetime
= QT
->getObjCARCImplicitLifetime();
6090 case Qualifiers::OCL_None
:
6091 assert(QT
->isDependentType() &&
6092 "didn't infer lifetime for non-dependent type?");
6095 case Qualifiers::OCL_Weak
: // meaningful
6096 case Qualifiers::OCL_Strong
: // meaningful
6099 case Qualifiers::OCL_ExplicitNone
:
6100 case Qualifiers::OCL_Autoreleasing
:
6101 S
.Diag(AL
.getLoc(), diag::warn_objc_precise_lifetime_meaningless
)
6102 << (Lifetime
== Qualifiers::OCL_Autoreleasing
);
6106 D
->addAttr(::new (S
.Context
) ObjCPreciseLifetimeAttr(S
.Context
, AL
));
6109 static void handleSwiftAttrAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6110 // Make sure that there is a string literal as the annotation's single
6113 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
6116 D
->addAttr(::new (S
.Context
) SwiftAttrAttr(S
.Context
, AL
, Str
));
6119 static void handleSwiftBridge(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6120 // Make sure that there is a string literal as the annotation's single
6123 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, BT
))
6126 // Warn about duplicate attributes if they have different arguments, but drop
6127 // any duplicate attributes regardless.
6128 if (const auto *Other
= D
->getAttr
<SwiftBridgeAttr
>()) {
6129 if (Other
->getSwiftType() != BT
)
6130 S
.Diag(AL
.getLoc(), diag::warn_duplicate_attribute
) << AL
;
6134 D
->addAttr(::new (S
.Context
) SwiftBridgeAttr(S
.Context
, AL
, BT
));
6137 static bool isErrorParameter(Sema
&S
, QualType QT
) {
6138 const auto *PT
= QT
->getAs
<PointerType
>();
6142 QualType Pointee
= PT
->getPointeeType();
6144 // Check for NSError**.
6145 if (const auto *OPT
= Pointee
->getAs
<ObjCObjectPointerType
>())
6146 if (const auto *ID
= OPT
->getInterfaceDecl())
6147 if (ID
->getIdentifier() == S
.getNSErrorIdent())
6150 // Check for CFError**.
6151 if (const auto *PT
= Pointee
->getAs
<PointerType
>())
6152 if (const auto *RT
= PT
->getPointeeType()->getAs
<RecordType
>())
6153 if (S
.isCFError(RT
->getDecl()))
6159 static void handleSwiftError(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6160 auto hasErrorParameter
= [](Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) -> bool {
6161 for (unsigned I
= 0, E
= getFunctionOrMethodNumParams(D
); I
!= E
; ++I
) {
6162 if (isErrorParameter(S
, getFunctionOrMethodParamType(D
, I
)))
6166 S
.Diag(AL
.getLoc(), diag::err_attr_swift_error_no_error_parameter
)
6167 << AL
<< isa
<ObjCMethodDecl
>(D
);
6171 auto hasPointerResult
= [](Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) -> bool {
6172 // - C, ObjC, and block pointers are definitely okay.
6173 // - References are definitely not okay.
6174 // - nullptr_t is weird, but acceptable.
6175 QualType RT
= getFunctionOrMethodResultType(D
);
6176 if (RT
->hasPointerRepresentation() && !RT
->isReferenceType())
6179 S
.Diag(AL
.getLoc(), diag::err_attr_swift_error_return_type
)
6180 << AL
<< AL
.getArgAsIdent(0)->Ident
->getName() << isa
<ObjCMethodDecl
>(D
)
6185 auto hasIntegerResult
= [](Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) -> bool {
6186 QualType RT
= getFunctionOrMethodResultType(D
);
6187 if (RT
->isIntegralType(S
.Context
))
6190 S
.Diag(AL
.getLoc(), diag::err_attr_swift_error_return_type
)
6191 << AL
<< AL
.getArgAsIdent(0)->Ident
->getName() << isa
<ObjCMethodDecl
>(D
)
6196 if (D
->isInvalidDecl())
6199 IdentifierLoc
*Loc
= AL
.getArgAsIdent(0);
6200 SwiftErrorAttr::ConventionKind Convention
;
6201 if (!SwiftErrorAttr::ConvertStrToConventionKind(Loc
->Ident
->getName(),
6203 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
)
6204 << AL
<< Loc
->Ident
;
6208 switch (Convention
) {
6209 case SwiftErrorAttr::None
:
6210 // No additional validation required.
6213 case SwiftErrorAttr::NonNullError
:
6214 if (!hasErrorParameter(S
, D
, AL
))
6218 case SwiftErrorAttr::NullResult
:
6219 if (!hasErrorParameter(S
, D
, AL
) || !hasPointerResult(S
, D
, AL
))
6223 case SwiftErrorAttr::NonZeroResult
:
6224 case SwiftErrorAttr::ZeroResult
:
6225 if (!hasErrorParameter(S
, D
, AL
) || !hasIntegerResult(S
, D
, AL
))
6230 D
->addAttr(::new (S
.Context
) SwiftErrorAttr(S
.Context
, AL
, Convention
));
6233 static void checkSwiftAsyncErrorBlock(Sema
&S
, Decl
*D
,
6234 const SwiftAsyncErrorAttr
*ErrorAttr
,
6235 const SwiftAsyncAttr
*AsyncAttr
) {
6236 if (AsyncAttr
->getKind() == SwiftAsyncAttr::None
) {
6237 if (ErrorAttr
->getConvention() != SwiftAsyncErrorAttr::None
) {
6238 S
.Diag(AsyncAttr
->getLocation(),
6239 diag::err_swift_async_error_without_swift_async
)
6240 << AsyncAttr
<< isa
<ObjCMethodDecl
>(D
);
6245 const ParmVarDecl
*HandlerParam
= getFunctionOrMethodParam(
6246 D
, AsyncAttr
->getCompletionHandlerIndex().getASTIndex());
6247 // handleSwiftAsyncAttr already verified the type is correct, so no need to
6248 // double-check it here.
6249 const auto *FuncTy
= HandlerParam
->getType()
6250 ->castAs
<BlockPointerType
>()
6252 ->getAs
<FunctionProtoType
>();
6253 ArrayRef
<QualType
> BlockParams
;
6255 BlockParams
= FuncTy
->getParamTypes();
6257 switch (ErrorAttr
->getConvention()) {
6258 case SwiftAsyncErrorAttr::ZeroArgument
:
6259 case SwiftAsyncErrorAttr::NonZeroArgument
: {
6260 uint32_t ParamIdx
= ErrorAttr
->getHandlerParamIdx();
6261 if (ParamIdx
== 0 || ParamIdx
> BlockParams
.size()) {
6262 S
.Diag(ErrorAttr
->getLocation(),
6263 diag::err_attribute_argument_out_of_bounds
) << ErrorAttr
<< 2;
6266 QualType ErrorParam
= BlockParams
[ParamIdx
- 1];
6267 if (!ErrorParam
->isIntegralType(S
.Context
)) {
6269 ErrorAttr
->getConvention() == SwiftAsyncErrorAttr::ZeroArgument
6271 : "nonzero_argument";
6272 S
.Diag(ErrorAttr
->getLocation(), diag::err_swift_async_error_non_integral
)
6273 << ErrorAttr
<< ConvStr
<< ParamIdx
<< ErrorParam
;
6278 case SwiftAsyncErrorAttr::NonNullError
: {
6279 bool AnyErrorParams
= false;
6280 for (QualType Param
: BlockParams
) {
6281 // Check for NSError *.
6282 if (const auto *ObjCPtrTy
= Param
->getAs
<ObjCObjectPointerType
>()) {
6283 if (const auto *ID
= ObjCPtrTy
->getInterfaceDecl()) {
6284 if (ID
->getIdentifier() == S
.getNSErrorIdent()) {
6285 AnyErrorParams
= true;
6290 // Check for CFError *.
6291 if (const auto *PtrTy
= Param
->getAs
<PointerType
>()) {
6292 if (const auto *RT
= PtrTy
->getPointeeType()->getAs
<RecordType
>()) {
6293 if (S
.isCFError(RT
->getDecl())) {
6294 AnyErrorParams
= true;
6301 if (!AnyErrorParams
) {
6302 S
.Diag(ErrorAttr
->getLocation(),
6303 diag::err_swift_async_error_no_error_parameter
)
6304 << ErrorAttr
<< isa
<ObjCMethodDecl
>(D
);
6309 case SwiftAsyncErrorAttr::None
:
6314 static void handleSwiftAsyncError(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6315 IdentifierLoc
*IDLoc
= AL
.getArgAsIdent(0);
6316 SwiftAsyncErrorAttr::ConventionKind ConvKind
;
6317 if (!SwiftAsyncErrorAttr::ConvertStrToConventionKind(IDLoc
->Ident
->getName(),
6319 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
)
6320 << AL
<< IDLoc
->Ident
;
6324 uint32_t ParamIdx
= 0;
6326 case SwiftAsyncErrorAttr::ZeroArgument
:
6327 case SwiftAsyncErrorAttr::NonZeroArgument
: {
6328 if (!AL
.checkExactlyNumArgs(S
, 2))
6331 Expr
*IdxExpr
= AL
.getArgAsExpr(1);
6332 if (!checkUInt32Argument(S
, AL
, IdxExpr
, ParamIdx
))
6336 case SwiftAsyncErrorAttr::NonNullError
:
6337 case SwiftAsyncErrorAttr::None
: {
6338 if (!AL
.checkExactlyNumArgs(S
, 1))
6345 ::new (S
.Context
) SwiftAsyncErrorAttr(S
.Context
, AL
, ConvKind
, ParamIdx
);
6346 D
->addAttr(ErrorAttr
);
6348 if (auto *AsyncAttr
= D
->getAttr
<SwiftAsyncAttr
>())
6349 checkSwiftAsyncErrorBlock(S
, D
, ErrorAttr
, AsyncAttr
);
6352 // For a function, this will validate a compound Swift name, e.g.
6353 // <code>init(foo:bar:baz:)</code> or <code>controllerForName(_:)</code>, and
6354 // the function will output the number of parameter names, and whether this is a
6355 // single-arg initializer.
6357 // For a type, enum constant, property, or variable declaration, this will
6358 // validate either a simple identifier, or a qualified
6359 // <code>context.identifier</code> name.
6361 validateSwiftFunctionName(Sema
&S
, const ParsedAttr
&AL
, SourceLocation Loc
,
6362 StringRef Name
, unsigned &SwiftParamCount
,
6363 bool &IsSingleParamInit
) {
6364 SwiftParamCount
= 0;
6365 IsSingleParamInit
= false;
6367 // Check whether this will be mapped to a getter or setter of a property.
6368 bool IsGetter
= false, IsSetter
= false;
6369 if (Name
.startswith("getter:")) {
6371 Name
= Name
.substr(7);
6372 } else if (Name
.startswith("setter:")) {
6374 Name
= Name
.substr(7);
6377 if (Name
.back() != ')') {
6378 S
.Diag(Loc
, diag::warn_attr_swift_name_function
) << AL
;
6382 bool IsMember
= false;
6383 StringRef ContextName
, BaseName
, Parameters
;
6385 std::tie(BaseName
, Parameters
) = Name
.split('(');
6387 // Split at the first '.', if it exists, which separates the context name
6388 // from the base name.
6389 std::tie(ContextName
, BaseName
) = BaseName
.split('.');
6390 if (BaseName
.empty()) {
6391 BaseName
= ContextName
;
6392 ContextName
= StringRef();
6393 } else if (ContextName
.empty() || !isValidAsciiIdentifier(ContextName
)) {
6394 S
.Diag(Loc
, diag::warn_attr_swift_name_invalid_identifier
)
6395 << AL
<< /*context*/ 1;
6401 if (!isValidAsciiIdentifier(BaseName
) || BaseName
== "_") {
6402 S
.Diag(Loc
, diag::warn_attr_swift_name_invalid_identifier
)
6403 << AL
<< /*basename*/ 0;
6407 bool IsSubscript
= BaseName
== "subscript";
6408 // A subscript accessor must be a getter or setter.
6409 if (IsSubscript
&& !IsGetter
&& !IsSetter
) {
6410 S
.Diag(Loc
, diag::warn_attr_swift_name_subscript_invalid_parameter
)
6411 << AL
<< /* getter or setter */ 0;
6415 if (Parameters
.empty()) {
6416 S
.Diag(Loc
, diag::warn_attr_swift_name_missing_parameters
) << AL
;
6420 assert(Parameters
.back() == ')' && "expected ')'");
6421 Parameters
= Parameters
.drop_back(); // ')'
6423 if (Parameters
.empty()) {
6424 // Setters and subscripts must have at least one parameter.
6426 S
.Diag(Loc
, diag::warn_attr_swift_name_subscript_invalid_parameter
)
6427 << AL
<< /* have at least one parameter */1;
6432 S
.Diag(Loc
, diag::warn_attr_swift_name_setter_parameters
) << AL
;
6439 if (Parameters
.back() != ':') {
6440 S
.Diag(Loc
, diag::warn_attr_swift_name_function
) << AL
;
6444 StringRef CurrentParam
;
6445 llvm::Optional
<unsigned> SelfLocation
;
6446 unsigned NewValueCount
= 0;
6447 llvm::Optional
<unsigned> NewValueLocation
;
6449 std::tie(CurrentParam
, Parameters
) = Parameters
.split(':');
6451 if (!isValidAsciiIdentifier(CurrentParam
)) {
6452 S
.Diag(Loc
, diag::warn_attr_swift_name_invalid_identifier
)
6453 << AL
<< /*parameter*/2;
6457 if (IsMember
&& CurrentParam
== "self") {
6458 // "self" indicates the "self" argument for a member.
6460 // More than one "self"?
6462 S
.Diag(Loc
, diag::warn_attr_swift_name_multiple_selfs
) << AL
;
6466 // The "self" location is the current parameter.
6467 SelfLocation
= SwiftParamCount
;
6468 } else if (CurrentParam
== "newValue") {
6469 // "newValue" indicates the "newValue" argument for a setter.
6471 // There should only be one 'newValue', but it's only significant for
6472 // subscript accessors, so don't error right away.
6475 NewValueLocation
= SwiftParamCount
;
6479 } while (!Parameters
.empty());
6481 // Only instance subscripts are currently supported.
6482 if (IsSubscript
&& !SelfLocation
) {
6483 S
.Diag(Loc
, diag::warn_attr_swift_name_subscript_invalid_parameter
)
6484 << AL
<< /*have a 'self:' parameter*/2;
6489 SwiftParamCount
== 1 && BaseName
== "init" && CurrentParam
!= "_";
6491 // Check the number of parameters for a getter/setter.
6492 if (IsGetter
|| IsSetter
) {
6493 // Setters have one parameter for the new value.
6494 unsigned NumExpectedParams
= IsGetter
? 0 : 1;
6495 unsigned ParamDiag
=
6496 IsGetter
? diag::warn_attr_swift_name_getter_parameters
6497 : diag::warn_attr_swift_name_setter_parameters
;
6499 // Instance methods have one parameter for "self".
6501 ++NumExpectedParams
;
6503 // Subscripts may have additional parameters beyond the expected params for
6506 if (SwiftParamCount
< NumExpectedParams
) {
6507 S
.Diag(Loc
, ParamDiag
) << AL
;
6511 // A subscript setter must explicitly label its newValue parameter to
6512 // distinguish it from index parameters.
6514 if (!NewValueLocation
) {
6515 S
.Diag(Loc
, diag::warn_attr_swift_name_subscript_setter_no_newValue
)
6519 if (NewValueCount
> 1) {
6520 S
.Diag(Loc
, diag::warn_attr_swift_name_subscript_setter_multiple_newValues
)
6525 // Subscript getters should have no 'newValue:' parameter.
6526 if (NewValueLocation
) {
6527 S
.Diag(Loc
, diag::warn_attr_swift_name_subscript_getter_newValue
)
6533 // Property accessors must have exactly the number of expected params.
6534 if (SwiftParamCount
!= NumExpectedParams
) {
6535 S
.Diag(Loc
, ParamDiag
) << AL
;
6544 bool Sema::DiagnoseSwiftName(Decl
*D
, StringRef Name
, SourceLocation Loc
,
6545 const ParsedAttr
&AL
, bool IsAsync
) {
6546 if (isa
<ObjCMethodDecl
>(D
) || isa
<FunctionDecl
>(D
)) {
6547 ArrayRef
<ParmVarDecl
*> Params
;
6548 unsigned ParamCount
;
6550 if (const auto *Method
= dyn_cast
<ObjCMethodDecl
>(D
)) {
6551 ParamCount
= Method
->getSelector().getNumArgs();
6552 Params
= Method
->parameters().slice(0, ParamCount
);
6554 const auto *F
= cast
<FunctionDecl
>(D
);
6556 ParamCount
= F
->getNumParams();
6557 Params
= F
->parameters();
6559 if (!F
->hasWrittenPrototype()) {
6560 Diag(Loc
, diag::warn_attribute_wrong_decl_type
) << AL
6561 << ExpectedFunctionWithProtoType
;
6566 // The async name drops the last callback parameter.
6568 if (ParamCount
== 0) {
6569 Diag(Loc
, diag::warn_attr_swift_name_decl_missing_params
)
6570 << AL
<< isa
<ObjCMethodDecl
>(D
);
6576 unsigned SwiftParamCount
;
6577 bool IsSingleParamInit
;
6578 if (!validateSwiftFunctionName(*this, AL
, Loc
, Name
,
6579 SwiftParamCount
, IsSingleParamInit
))
6582 bool ParamCountValid
;
6583 if (SwiftParamCount
== ParamCount
) {
6584 ParamCountValid
= true;
6585 } else if (SwiftParamCount
> ParamCount
) {
6586 ParamCountValid
= IsSingleParamInit
&& ParamCount
== 0;
6588 // We have fewer Swift parameters than Objective-C parameters, but that
6589 // might be because we've transformed some of them. Check for potential
6590 // "out" parameters and err on the side of not warning.
6591 unsigned MaybeOutParamCount
=
6592 llvm::count_if(Params
, [](const ParmVarDecl
*Param
) -> bool {
6593 QualType ParamTy
= Param
->getType();
6594 if (ParamTy
->isReferenceType() || ParamTy
->isPointerType())
6595 return !ParamTy
->getPointeeType().isConstQualified();
6599 ParamCountValid
= SwiftParamCount
+ MaybeOutParamCount
>= ParamCount
;
6602 if (!ParamCountValid
) {
6603 Diag(Loc
, diag::warn_attr_swift_name_num_params
)
6604 << (SwiftParamCount
> ParamCount
) << AL
<< ParamCount
6608 } else if ((isa
<EnumConstantDecl
>(D
) || isa
<ObjCProtocolDecl
>(D
) ||
6609 isa
<ObjCInterfaceDecl
>(D
) || isa
<ObjCPropertyDecl
>(D
) ||
6610 isa
<VarDecl
>(D
) || isa
<TypedefNameDecl
>(D
) || isa
<TagDecl
>(D
) ||
6611 isa
<IndirectFieldDecl
>(D
) || isa
<FieldDecl
>(D
)) &&
6613 StringRef ContextName
, BaseName
;
6615 std::tie(ContextName
, BaseName
) = Name
.split('.');
6616 if (BaseName
.empty()) {
6617 BaseName
= ContextName
;
6618 ContextName
= StringRef();
6619 } else if (!isValidAsciiIdentifier(ContextName
)) {
6620 Diag(Loc
, diag::warn_attr_swift_name_invalid_identifier
) << AL
6625 if (!isValidAsciiIdentifier(BaseName
)) {
6626 Diag(Loc
, diag::warn_attr_swift_name_invalid_identifier
) << AL
6631 Diag(Loc
, diag::warn_attr_swift_name_decl_kind
) << AL
;
6637 static void handleSwiftName(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6640 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Name
, &Loc
))
6643 if (!S
.DiagnoseSwiftName(D
, Name
, Loc
, AL
, /*IsAsync=*/false))
6646 D
->addAttr(::new (S
.Context
) SwiftNameAttr(S
.Context
, AL
, Name
));
6649 static void handleSwiftAsyncName(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6652 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Name
, &Loc
))
6655 if (!S
.DiagnoseSwiftName(D
, Name
, Loc
, AL
, /*IsAsync=*/true))
6658 D
->addAttr(::new (S
.Context
) SwiftAsyncNameAttr(S
.Context
, AL
, Name
));
6661 static void handleSwiftNewType(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6662 // Make sure that there is an identifier as the annotation's single argument.
6663 if (!AL
.checkExactlyNumArgs(S
, 1))
6666 if (!AL
.isArgIdent(0)) {
6667 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
6668 << AL
<< AANT_ArgumentIdentifier
;
6672 SwiftNewTypeAttr::NewtypeKind Kind
;
6673 IdentifierInfo
*II
= AL
.getArgAsIdent(0)->Ident
;
6674 if (!SwiftNewTypeAttr::ConvertStrToNewtypeKind(II
->getName(), Kind
)) {
6675 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
) << AL
<< II
;
6679 if (!isa
<TypedefNameDecl
>(D
)) {
6680 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type_str
)
6681 << AL
<< "typedefs";
6685 D
->addAttr(::new (S
.Context
) SwiftNewTypeAttr(S
.Context
, AL
, Kind
));
6688 static void handleSwiftAsyncAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6689 if (!AL
.isArgIdent(0)) {
6690 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_n_type
)
6691 << AL
<< 1 << AANT_ArgumentIdentifier
;
6695 SwiftAsyncAttr::Kind Kind
;
6696 IdentifierInfo
*II
= AL
.getArgAsIdent(0)->Ident
;
6697 if (!SwiftAsyncAttr::ConvertStrToKind(II
->getName(), Kind
)) {
6698 S
.Diag(AL
.getLoc(), diag::err_swift_async_no_access
) << AL
<< II
;
6703 if (Kind
== SwiftAsyncAttr::None
) {
6704 // If this is 'none', then there shouldn't be any additional arguments.
6705 if (!AL
.checkExactlyNumArgs(S
, 1))
6708 // Non-none swift_async requires a completion handler index argument.
6709 if (!AL
.checkExactlyNumArgs(S
, 2))
6712 Expr
*HandlerIdx
= AL
.getArgAsExpr(1);
6713 if (!checkFunctionOrMethodParameterIndex(S
, D
, AL
, 2, HandlerIdx
, Idx
))
6716 const ParmVarDecl
*CompletionBlock
=
6717 getFunctionOrMethodParam(D
, Idx
.getASTIndex());
6718 QualType CompletionBlockType
= CompletionBlock
->getType();
6719 if (!CompletionBlockType
->isBlockPointerType()) {
6720 S
.Diag(CompletionBlock
->getLocation(),
6721 diag::err_swift_async_bad_block_type
)
6722 << CompletionBlock
->getType();
6726 CompletionBlockType
->castAs
<BlockPointerType
>()->getPointeeType();
6727 if (!BlockTy
->castAs
<FunctionType
>()->getReturnType()->isVoidType()) {
6728 S
.Diag(CompletionBlock
->getLocation(),
6729 diag::err_swift_async_bad_block_type
)
6730 << CompletionBlock
->getType();
6736 ::new (S
.Context
) SwiftAsyncAttr(S
.Context
, AL
, Kind
, Idx
);
6737 D
->addAttr(AsyncAttr
);
6739 if (auto *ErrorAttr
= D
->getAttr
<SwiftAsyncErrorAttr
>())
6740 checkSwiftAsyncErrorBlock(S
, D
, ErrorAttr
, AsyncAttr
);
6743 //===----------------------------------------------------------------------===//
6744 // Microsoft specific attribute handlers.
6745 //===----------------------------------------------------------------------===//
6747 UuidAttr
*Sema::mergeUuidAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
6748 StringRef UuidAsWritten
, MSGuidDecl
*GuidDecl
) {
6749 if (const auto *UA
= D
->getAttr
<UuidAttr
>()) {
6750 if (declaresSameEntity(UA
->getGuidDecl(), GuidDecl
))
6752 if (!UA
->getGuid().empty()) {
6753 Diag(UA
->getLocation(), diag::err_mismatched_uuid
);
6754 Diag(CI
.getLoc(), diag::note_previous_uuid
);
6755 D
->dropAttr
<UuidAttr
>();
6759 return ::new (Context
) UuidAttr(Context
, CI
, UuidAsWritten
, GuidDecl
);
6762 static void handleUuidAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6763 if (!S
.LangOpts
.CPlusPlus
) {
6764 S
.Diag(AL
.getLoc(), diag::err_attribute_not_supported_in_lang
)
6765 << AL
<< AttributeLangSupport::C
;
6769 StringRef OrigStrRef
;
6770 SourceLocation LiteralLoc
;
6771 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, OrigStrRef
, &LiteralLoc
))
6774 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
6775 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
6776 StringRef StrRef
= OrigStrRef
;
6777 if (StrRef
.size() == 38 && StrRef
.front() == '{' && StrRef
.back() == '}')
6778 StrRef
= StrRef
.drop_front().drop_back();
6780 // Validate GUID length.
6781 if (StrRef
.size() != 36) {
6782 S
.Diag(LiteralLoc
, diag::err_attribute_uuid_malformed_guid
);
6786 for (unsigned i
= 0; i
< 36; ++i
) {
6787 if (i
== 8 || i
== 13 || i
== 18 || i
== 23) {
6788 if (StrRef
[i
] != '-') {
6789 S
.Diag(LiteralLoc
, diag::err_attribute_uuid_malformed_guid
);
6792 } else if (!isHexDigit(StrRef
[i
])) {
6793 S
.Diag(LiteralLoc
, diag::err_attribute_uuid_malformed_guid
);
6798 // Convert to our parsed format and canonicalize.
6799 MSGuidDecl::Parts Parsed
;
6800 StrRef
.substr(0, 8).getAsInteger(16, Parsed
.Part1
);
6801 StrRef
.substr(9, 4).getAsInteger(16, Parsed
.Part2
);
6802 StrRef
.substr(14, 4).getAsInteger(16, Parsed
.Part3
);
6803 for (unsigned i
= 0; i
!= 8; ++i
)
6804 StrRef
.substr(19 + 2 * i
+ (i
>= 2 ? 1 : 0), 2)
6805 .getAsInteger(16, Parsed
.Part4And5
[i
]);
6806 MSGuidDecl
*Guid
= S
.Context
.getMSGuidDecl(Parsed
);
6808 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
6809 // the only thing in the [] list, the [] too), and add an insertion of
6810 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
6811 // separating attributes nor of the [ and the ] are in the AST.
6812 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
6814 if (AL
.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
6815 S
.Diag(AL
.getLoc(), diag::warn_atl_uuid_deprecated
);
6817 UuidAttr
*UA
= S
.mergeUuidAttr(D
, AL
, OrigStrRef
, Guid
);
6822 static void handleHLSLNumThreadsAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6824 Triple Target
= S
.Context
.getTargetInfo().getTriple();
6825 if (!llvm::is_contained({Triple::Compute
, Triple::Mesh
, Triple::Amplification
,
6827 Target
.getEnvironment())) {
6829 (uint32_t)S
.Context
.getTargetInfo().getTriple().getEnvironment() -
6830 (uint32_t)llvm::Triple::Pixel
;
6831 S
.Diag(AL
.getLoc(), diag::err_hlsl_attr_unsupported_in_stage
)
6832 << AL
<< Pipeline
<< "Compute, Amplification, Mesh or Library";
6836 llvm::VersionTuple SMVersion
= Target
.getOSVersion();
6837 uint32_t ZMax
= 1024;
6838 uint32_t ThreadMax
= 1024;
6839 if (SMVersion
.getMajor() <= 4) {
6842 } else if (SMVersion
.getMajor() == 5) {
6848 if (!checkUInt32Argument(S
, AL
, AL
.getArgAsExpr(0), X
))
6851 S
.Diag(AL
.getArgAsExpr(0)->getExprLoc(),
6852 diag::err_hlsl_numthreads_argument_oor
) << 0 << 1024;
6856 if (!checkUInt32Argument(S
, AL
, AL
.getArgAsExpr(1), Y
))
6859 S
.Diag(AL
.getArgAsExpr(1)->getExprLoc(),
6860 diag::err_hlsl_numthreads_argument_oor
) << 1 << 1024;
6864 if (!checkUInt32Argument(S
, AL
, AL
.getArgAsExpr(2), Z
))
6867 S
.Diag(AL
.getArgAsExpr(2)->getExprLoc(),
6868 diag::err_hlsl_numthreads_argument_oor
) << 2 << ZMax
;
6872 if (X
* Y
* Z
> ThreadMax
) {
6873 S
.Diag(AL
.getLoc(), diag::err_hlsl_numthreads_invalid
) << ThreadMax
;
6877 HLSLNumThreadsAttr
*NewAttr
= S
.mergeHLSLNumThreadsAttr(D
, AL
, X
, Y
, Z
);
6879 D
->addAttr(NewAttr
);
6882 HLSLNumThreadsAttr
*Sema::mergeHLSLNumThreadsAttr(Decl
*D
,
6883 const AttributeCommonInfo
&AL
,
6884 int X
, int Y
, int Z
) {
6885 if (HLSLNumThreadsAttr
*NT
= D
->getAttr
<HLSLNumThreadsAttr
>()) {
6886 if (NT
->getX() != X
|| NT
->getY() != Y
|| NT
->getZ() != Z
) {
6887 Diag(NT
->getLocation(), diag::err_hlsl_attribute_param_mismatch
) << AL
;
6888 Diag(AL
.getLoc(), diag::note_conflicting_attribute
);
6892 return ::new (Context
) HLSLNumThreadsAttr(Context
, AL
, X
, Y
, Z
);
6895 static void handleHLSLSVGroupIndexAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6897 Triple Target
= S
.Context
.getTargetInfo().getTriple();
6898 if (Target
.getEnvironment() != Triple::Compute
) {
6900 (uint32_t)S
.Context
.getTargetInfo().getTriple().getEnvironment() -
6901 (uint32_t)llvm::Triple::Pixel
;
6902 S
.Diag(AL
.getLoc(), diag::err_hlsl_attr_unsupported_in_stage
)
6903 << AL
<< Pipeline
<< "Compute";
6907 D
->addAttr(::new (S
.Context
) HLSLSV_GroupIndexAttr(S
.Context
, AL
));
6910 static void handleHLSLShaderAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6912 SourceLocation ArgLoc
;
6913 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &ArgLoc
))
6916 HLSLShaderAttr::ShaderType ShaderType
;
6917 if (!HLSLShaderAttr::ConvertStrToShaderType(Str
, ShaderType
) ||
6918 // Library is added to help convert HLSLShaderAttr::ShaderType to
6919 // llvm::Triple::EnviromentType. It is not a legal
6920 // HLSLShaderAttr::ShaderType.
6921 ShaderType
== HLSLShaderAttr::Library
) {
6922 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
)
6923 << AL
<< Str
<< ArgLoc
;
6927 // FIXME: check function match the shader stage.
6929 HLSLShaderAttr
*NewAttr
= S
.mergeHLSLShaderAttr(D
, AL
, ShaderType
);
6931 D
->addAttr(NewAttr
);
6935 Sema::mergeHLSLShaderAttr(Decl
*D
, const AttributeCommonInfo
&AL
,
6936 HLSLShaderAttr::ShaderType ShaderType
) {
6937 if (HLSLShaderAttr
*NT
= D
->getAttr
<HLSLShaderAttr
>()) {
6938 if (NT
->getType() != ShaderType
) {
6939 Diag(NT
->getLocation(), diag::err_hlsl_attribute_param_mismatch
) << AL
;
6940 Diag(AL
.getLoc(), diag::note_conflicting_attribute
);
6944 return HLSLShaderAttr::Create(Context
, ShaderType
, AL
);
6947 static void handleMSInheritanceAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6948 if (!S
.LangOpts
.CPlusPlus
) {
6949 S
.Diag(AL
.getLoc(), diag::err_attribute_not_supported_in_lang
)
6950 << AL
<< AttributeLangSupport::C
;
6953 MSInheritanceAttr
*IA
= S
.mergeMSInheritanceAttr(
6954 D
, AL
, /*BestCase=*/true, (MSInheritanceModel
)AL
.getSemanticSpelling());
6957 S
.Consumer
.AssignInheritanceModel(cast
<CXXRecordDecl
>(D
));
6961 static void handleDeclspecThreadAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6962 const auto *VD
= cast
<VarDecl
>(D
);
6963 if (!S
.Context
.getTargetInfo().isTLSSupported()) {
6964 S
.Diag(AL
.getLoc(), diag::err_thread_unsupported
);
6967 if (VD
->getTSCSpec() != TSCS_unspecified
) {
6968 S
.Diag(AL
.getLoc(), diag::err_declspec_thread_on_thread_variable
);
6971 if (VD
->hasLocalStorage()) {
6972 S
.Diag(AL
.getLoc(), diag::err_thread_non_global
) << "__declspec(thread)";
6975 D
->addAttr(::new (S
.Context
) ThreadAttr(S
.Context
, AL
));
6978 static void handleAbiTagAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
6979 SmallVector
<StringRef
, 4> Tags
;
6980 for (unsigned I
= 0, E
= AL
.getNumArgs(); I
!= E
; ++I
) {
6982 if (!S
.checkStringLiteralArgumentAttr(AL
, I
, Tag
))
6984 Tags
.push_back(Tag
);
6987 if (const auto *NS
= dyn_cast
<NamespaceDecl
>(D
)) {
6988 if (!NS
->isInline()) {
6989 S
.Diag(AL
.getLoc(), diag::warn_attr_abi_tag_namespace
) << 0;
6992 if (NS
->isAnonymousNamespace()) {
6993 S
.Diag(AL
.getLoc(), diag::warn_attr_abi_tag_namespace
) << 1;
6996 if (AL
.getNumArgs() == 0)
6997 Tags
.push_back(NS
->getName());
6998 } else if (!AL
.checkAtLeastNumArgs(S
, 1))
7001 // Store tags sorted and without duplicates.
7003 Tags
.erase(std::unique(Tags
.begin(), Tags
.end()), Tags
.end());
7005 D
->addAttr(::new (S
.Context
)
7006 AbiTagAttr(S
.Context
, AL
, Tags
.data(), Tags
.size()));
7009 static void handleARMInterruptAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7010 // Check the attribute arguments.
7011 if (AL
.getNumArgs() > 1) {
7012 S
.Diag(AL
.getLoc(), diag::err_attribute_too_many_arguments
) << AL
<< 1;
7017 SourceLocation ArgLoc
;
7019 if (AL
.getNumArgs() == 0)
7021 else if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &ArgLoc
))
7024 ARMInterruptAttr::InterruptType Kind
;
7025 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str
, Kind
)) {
7026 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
) << AL
<< Str
7031 D
->addAttr(::new (S
.Context
) ARMInterruptAttr(S
.Context
, AL
, Kind
));
7034 static void handleMSP430InterruptAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7035 // MSP430 'interrupt' attribute is applied to
7036 // a function with no parameters and void return type.
7037 if (!isFunctionOrMethod(D
)) {
7038 S
.Diag(D
->getLocation(), diag::warn_attribute_wrong_decl_type
)
7039 << "'interrupt'" << ExpectedFunctionOrMethod
;
7043 if (hasFunctionProto(D
) && getFunctionOrMethodNumParams(D
) != 0) {
7044 S
.Diag(D
->getLocation(), diag::warn_interrupt_attribute_invalid
)
7045 << /*MSP430*/ 1 << 0;
7049 if (!getFunctionOrMethodResultType(D
)->isVoidType()) {
7050 S
.Diag(D
->getLocation(), diag::warn_interrupt_attribute_invalid
)
7051 << /*MSP430*/ 1 << 1;
7055 // The attribute takes one integer argument.
7056 if (!AL
.checkExactlyNumArgs(S
, 1))
7059 if (!AL
.isArgExpr(0)) {
7060 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
7061 << AL
<< AANT_ArgumentIntegerConstant
;
7065 Expr
*NumParamsExpr
= static_cast<Expr
*>(AL
.getArgAsExpr(0));
7066 Optional
<llvm::APSInt
> NumParams
= llvm::APSInt(32);
7067 if (!(NumParams
= NumParamsExpr
->getIntegerConstantExpr(S
.Context
))) {
7068 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
7069 << AL
<< AANT_ArgumentIntegerConstant
7070 << NumParamsExpr
->getSourceRange();
7073 // The argument should be in range 0..63.
7074 unsigned Num
= NumParams
->getLimitedValue(255);
7076 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_bounds
)
7077 << AL
<< (int)NumParams
->getSExtValue()
7078 << NumParamsExpr
->getSourceRange();
7082 D
->addAttr(::new (S
.Context
) MSP430InterruptAttr(S
.Context
, AL
, Num
));
7083 D
->addAttr(UsedAttr::CreateImplicit(S
.Context
));
7086 static void handleMipsInterruptAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7087 // Only one optional argument permitted.
7088 if (AL
.getNumArgs() > 1) {
7089 S
.Diag(AL
.getLoc(), diag::err_attribute_too_many_arguments
) << AL
<< 1;
7094 SourceLocation ArgLoc
;
7096 if (AL
.getNumArgs() == 0)
7098 else if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &ArgLoc
))
7101 // Semantic checks for a function with the 'interrupt' attribute for MIPS:
7102 // a) Must be a function.
7103 // b) Must have no parameters.
7104 // c) Must have the 'void' return type.
7105 // d) Cannot have the 'mips16' attribute, as that instruction set
7106 // lacks the 'eret' instruction.
7107 // e) The attribute itself must either have no argument or one of the
7108 // valid interrupt types, see [MipsInterruptDocs].
7110 if (!isFunctionOrMethod(D
)) {
7111 S
.Diag(D
->getLocation(), diag::warn_attribute_wrong_decl_type
)
7112 << "'interrupt'" << ExpectedFunctionOrMethod
;
7116 if (hasFunctionProto(D
) && getFunctionOrMethodNumParams(D
) != 0) {
7117 S
.Diag(D
->getLocation(), diag::warn_interrupt_attribute_invalid
)
7122 if (!getFunctionOrMethodResultType(D
)->isVoidType()) {
7123 S
.Diag(D
->getLocation(), diag::warn_interrupt_attribute_invalid
)
7128 // We still have to do this manually because the Interrupt attributes are
7129 // a bit special due to sharing their spellings across targets.
7130 if (checkAttrMutualExclusion
<Mips16Attr
>(S
, D
, AL
))
7133 MipsInterruptAttr::InterruptType Kind
;
7134 if (!MipsInterruptAttr::ConvertStrToInterruptType(Str
, Kind
)) {
7135 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
)
7136 << AL
<< "'" + std::string(Str
) + "'";
7140 D
->addAttr(::new (S
.Context
) MipsInterruptAttr(S
.Context
, AL
, Kind
));
7143 static void handleM68kInterruptAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7144 if (!AL
.checkExactlyNumArgs(S
, 1))
7147 if (!AL
.isArgExpr(0)) {
7148 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
7149 << AL
<< AANT_ArgumentIntegerConstant
;
7153 // FIXME: Check for decl - it should be void ()(void).
7155 Expr
*NumParamsExpr
= static_cast<Expr
*>(AL
.getArgAsExpr(0));
7156 auto MaybeNumParams
= NumParamsExpr
->getIntegerConstantExpr(S
.Context
);
7157 if (!MaybeNumParams
) {
7158 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
7159 << AL
<< AANT_ArgumentIntegerConstant
7160 << NumParamsExpr
->getSourceRange();
7164 unsigned Num
= MaybeNumParams
->getLimitedValue(255);
7165 if ((Num
& 1) || Num
> 30) {
7166 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_bounds
)
7167 << AL
<< (int)MaybeNumParams
->getSExtValue()
7168 << NumParamsExpr
->getSourceRange();
7172 D
->addAttr(::new (S
.Context
) M68kInterruptAttr(S
.Context
, AL
, Num
));
7173 D
->addAttr(UsedAttr::CreateImplicit(S
.Context
));
7176 static void handleAnyX86InterruptAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7177 // Semantic checks for a function with the 'interrupt' attribute.
7178 // a) Must be a function.
7179 // b) Must have the 'void' return type.
7180 // c) Must take 1 or 2 arguments.
7181 // d) The 1st argument must be a pointer.
7182 // e) The 2nd argument (if any) must be an unsigned integer.
7183 if (!isFunctionOrMethod(D
) || !hasFunctionProto(D
) || isInstanceMethod(D
) ||
7184 CXXMethodDecl::isStaticOverloadedOperator(
7185 cast
<NamedDecl
>(D
)->getDeclName().getCXXOverloadedOperator())) {
7186 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
)
7187 << AL
<< ExpectedFunctionWithProtoType
;
7190 // Interrupt handler must have void return type.
7191 if (!getFunctionOrMethodResultType(D
)->isVoidType()) {
7192 S
.Diag(getFunctionOrMethodResultSourceRange(D
).getBegin(),
7193 diag::err_anyx86_interrupt_attribute
)
7194 << (S
.Context
.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
7200 // Interrupt handler must have 1 or 2 parameters.
7201 unsigned NumParams
= getFunctionOrMethodNumParams(D
);
7202 if (NumParams
< 1 || NumParams
> 2) {
7203 S
.Diag(D
->getBeginLoc(), diag::err_anyx86_interrupt_attribute
)
7204 << (S
.Context
.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
7210 // The first argument must be a pointer.
7211 if (!getFunctionOrMethodParamType(D
, 0)->isPointerType()) {
7212 S
.Diag(getFunctionOrMethodParamRange(D
, 0).getBegin(),
7213 diag::err_anyx86_interrupt_attribute
)
7214 << (S
.Context
.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
7220 // The second argument, if present, must be an unsigned integer.
7222 S
.Context
.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
7225 if (NumParams
== 2 &&
7226 (!getFunctionOrMethodParamType(D
, 1)->isUnsignedIntegerType() ||
7227 S
.Context
.getTypeSize(getFunctionOrMethodParamType(D
, 1)) != TypeSize
)) {
7228 S
.Diag(getFunctionOrMethodParamRange(D
, 1).getBegin(),
7229 diag::err_anyx86_interrupt_attribute
)
7230 << (S
.Context
.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
7233 << 3 << S
.Context
.getIntTypeForBitwidth(TypeSize
, /*Signed=*/false);
7236 D
->addAttr(::new (S
.Context
) AnyX86InterruptAttr(S
.Context
, AL
));
7237 D
->addAttr(UsedAttr::CreateImplicit(S
.Context
));
7240 static void handleAVRInterruptAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7241 if (!isFunctionOrMethod(D
)) {
7242 S
.Diag(D
->getLocation(), diag::warn_attribute_wrong_decl_type
)
7243 << "'interrupt'" << ExpectedFunction
;
7247 if (!AL
.checkExactlyNumArgs(S
, 0))
7250 handleSimpleAttribute
<AVRInterruptAttr
>(S
, D
, AL
);
7253 static void handleAVRSignalAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7254 if (!isFunctionOrMethod(D
)) {
7255 S
.Diag(D
->getLocation(), diag::warn_attribute_wrong_decl_type
)
7256 << "'signal'" << ExpectedFunction
;
7260 if (!AL
.checkExactlyNumArgs(S
, 0))
7263 handleSimpleAttribute
<AVRSignalAttr
>(S
, D
, AL
);
7266 static void handleBPFPreserveAIRecord(Sema
&S
, RecordDecl
*RD
) {
7267 // Add preserve_access_index attribute to all fields and inner records.
7268 for (auto *D
: RD
->decls()) {
7269 if (D
->hasAttr
<BPFPreserveAccessIndexAttr
>())
7272 D
->addAttr(BPFPreserveAccessIndexAttr::CreateImplicit(S
.Context
));
7273 if (auto *Rec
= dyn_cast
<RecordDecl
>(D
))
7274 handleBPFPreserveAIRecord(S
, Rec
);
7278 static void handleBPFPreserveAccessIndexAttr(Sema
&S
, Decl
*D
,
7279 const ParsedAttr
&AL
) {
7280 auto *Rec
= cast
<RecordDecl
>(D
);
7281 handleBPFPreserveAIRecord(S
, Rec
);
7282 Rec
->addAttr(::new (S
.Context
) BPFPreserveAccessIndexAttr(S
.Context
, AL
));
7285 static bool hasBTFDeclTagAttr(Decl
*D
, StringRef Tag
) {
7286 for (const auto *I
: D
->specific_attrs
<BTFDeclTagAttr
>()) {
7287 if (I
->getBTFDeclTag() == Tag
)
7293 static void handleBTFDeclTagAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7295 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
7297 if (hasBTFDeclTagAttr(D
, Str
))
7300 D
->addAttr(::new (S
.Context
) BTFDeclTagAttr(S
.Context
, AL
, Str
));
7303 BTFDeclTagAttr
*Sema::mergeBTFDeclTagAttr(Decl
*D
, const BTFDeclTagAttr
&AL
) {
7304 if (hasBTFDeclTagAttr(D
, AL
.getBTFDeclTag()))
7306 return ::new (Context
) BTFDeclTagAttr(Context
, AL
, AL
.getBTFDeclTag());
7309 static void handleWebAssemblyExportNameAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7310 if (!isFunctionOrMethod(D
)) {
7311 S
.Diag(D
->getLocation(), diag::warn_attribute_wrong_decl_type
)
7312 << "'export_name'" << ExpectedFunction
;
7316 auto *FD
= cast
<FunctionDecl
>(D
);
7317 if (FD
->isThisDeclarationADefinition()) {
7318 S
.Diag(D
->getLocation(), diag::err_alias_is_definition
) << FD
<< 0;
7323 SourceLocation ArgLoc
;
7324 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &ArgLoc
))
7327 D
->addAttr(::new (S
.Context
) WebAssemblyExportNameAttr(S
.Context
, AL
, Str
));
7328 D
->addAttr(UsedAttr::CreateImplicit(S
.Context
));
7331 WebAssemblyImportModuleAttr
*
7332 Sema::mergeImportModuleAttr(Decl
*D
, const WebAssemblyImportModuleAttr
&AL
) {
7333 auto *FD
= cast
<FunctionDecl
>(D
);
7335 if (const auto *ExistingAttr
= FD
->getAttr
<WebAssemblyImportModuleAttr
>()) {
7336 if (ExistingAttr
->getImportModule() == AL
.getImportModule())
7338 Diag(ExistingAttr
->getLocation(), diag::warn_mismatched_import
) << 0
7339 << ExistingAttr
->getImportModule() << AL
.getImportModule();
7340 Diag(AL
.getLoc(), diag::note_previous_attribute
);
7343 if (FD
->hasBody()) {
7344 Diag(AL
.getLoc(), diag::warn_import_on_definition
) << 0;
7347 return ::new (Context
) WebAssemblyImportModuleAttr(Context
, AL
,
7348 AL
.getImportModule());
7351 WebAssemblyImportNameAttr
*
7352 Sema::mergeImportNameAttr(Decl
*D
, const WebAssemblyImportNameAttr
&AL
) {
7353 auto *FD
= cast
<FunctionDecl
>(D
);
7355 if (const auto *ExistingAttr
= FD
->getAttr
<WebAssemblyImportNameAttr
>()) {
7356 if (ExistingAttr
->getImportName() == AL
.getImportName())
7358 Diag(ExistingAttr
->getLocation(), diag::warn_mismatched_import
) << 1
7359 << ExistingAttr
->getImportName() << AL
.getImportName();
7360 Diag(AL
.getLoc(), diag::note_previous_attribute
);
7363 if (FD
->hasBody()) {
7364 Diag(AL
.getLoc(), diag::warn_import_on_definition
) << 1;
7367 return ::new (Context
) WebAssemblyImportNameAttr(Context
, AL
,
7368 AL
.getImportName());
7372 handleWebAssemblyImportModuleAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7373 auto *FD
= cast
<FunctionDecl
>(D
);
7376 SourceLocation ArgLoc
;
7377 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &ArgLoc
))
7379 if (FD
->hasBody()) {
7380 S
.Diag(AL
.getLoc(), diag::warn_import_on_definition
) << 0;
7384 FD
->addAttr(::new (S
.Context
)
7385 WebAssemblyImportModuleAttr(S
.Context
, AL
, Str
));
7389 handleWebAssemblyImportNameAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7390 auto *FD
= cast
<FunctionDecl
>(D
);
7393 SourceLocation ArgLoc
;
7394 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &ArgLoc
))
7396 if (FD
->hasBody()) {
7397 S
.Diag(AL
.getLoc(), diag::warn_import_on_definition
) << 1;
7401 FD
->addAttr(::new (S
.Context
) WebAssemblyImportNameAttr(S
.Context
, AL
, Str
));
7404 static void handleRISCVInterruptAttr(Sema
&S
, Decl
*D
,
7405 const ParsedAttr
&AL
) {
7406 // Warn about repeated attributes.
7407 if (const auto *A
= D
->getAttr
<RISCVInterruptAttr
>()) {
7408 S
.Diag(AL
.getRange().getBegin(),
7409 diag::warn_riscv_repeated_interrupt_attribute
);
7410 S
.Diag(A
->getLocation(), diag::note_riscv_repeated_interrupt_attribute
);
7414 // Check the attribute argument. Argument is optional.
7415 if (!AL
.checkAtMostNumArgs(S
, 1))
7419 SourceLocation ArgLoc
;
7421 // 'machine'is the default interrupt mode.
7422 if (AL
.getNumArgs() == 0)
7424 else if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Str
, &ArgLoc
))
7427 // Semantic checks for a function with the 'interrupt' attribute:
7428 // - Must be a function.
7429 // - Must have no parameters.
7430 // - Must have the 'void' return type.
7431 // - The attribute itself must either have no argument or one of the
7432 // valid interrupt types, see [RISCVInterruptDocs].
7434 if (D
->getFunctionType() == nullptr) {
7435 S
.Diag(D
->getLocation(), diag::warn_attribute_wrong_decl_type
)
7436 << "'interrupt'" << ExpectedFunction
;
7440 if (hasFunctionProto(D
) && getFunctionOrMethodNumParams(D
) != 0) {
7441 S
.Diag(D
->getLocation(), diag::warn_interrupt_attribute_invalid
)
7442 << /*RISC-V*/ 2 << 0;
7446 if (!getFunctionOrMethodResultType(D
)->isVoidType()) {
7447 S
.Diag(D
->getLocation(), diag::warn_interrupt_attribute_invalid
)
7448 << /*RISC-V*/ 2 << 1;
7452 RISCVInterruptAttr::InterruptType Kind
;
7453 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str
, Kind
)) {
7454 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
) << AL
<< Str
7459 D
->addAttr(::new (S
.Context
) RISCVInterruptAttr(S
.Context
, AL
, Kind
));
7462 static void handleInterruptAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7463 // Dispatch the interrupt attribute based on the current target.
7464 switch (S
.Context
.getTargetInfo().getTriple().getArch()) {
7465 case llvm::Triple::msp430
:
7466 handleMSP430InterruptAttr(S
, D
, AL
);
7468 case llvm::Triple::mipsel
:
7469 case llvm::Triple::mips
:
7470 handleMipsInterruptAttr(S
, D
, AL
);
7472 case llvm::Triple::m68k
:
7473 handleM68kInterruptAttr(S
, D
, AL
);
7475 case llvm::Triple::x86
:
7476 case llvm::Triple::x86_64
:
7477 handleAnyX86InterruptAttr(S
, D
, AL
);
7479 case llvm::Triple::avr
:
7480 handleAVRInterruptAttr(S
, D
, AL
);
7482 case llvm::Triple::riscv32
:
7483 case llvm::Triple::riscv64
:
7484 handleRISCVInterruptAttr(S
, D
, AL
);
7487 handleARMInterruptAttr(S
, D
, AL
);
7493 checkAMDGPUFlatWorkGroupSizeArguments(Sema
&S
, Expr
*MinExpr
, Expr
*MaxExpr
,
7494 const AMDGPUFlatWorkGroupSizeAttr
&Attr
) {
7495 // Accept template arguments for now as they depend on something else.
7496 // We'll get to check them when they eventually get instantiated.
7497 if (MinExpr
->isValueDependent() || MaxExpr
->isValueDependent())
7501 if (!checkUInt32Argument(S
, Attr
, MinExpr
, Min
, 0))
7505 if (!checkUInt32Argument(S
, Attr
, MaxExpr
, Max
, 1))
7508 if (Min
== 0 && Max
!= 0) {
7509 S
.Diag(Attr
.getLocation(), diag::err_attribute_argument_invalid
)
7514 S
.Diag(Attr
.getLocation(), diag::err_attribute_argument_invalid
)
7522 void Sema::addAMDGPUFlatWorkGroupSizeAttr(Decl
*D
,
7523 const AttributeCommonInfo
&CI
,
7524 Expr
*MinExpr
, Expr
*MaxExpr
) {
7525 AMDGPUFlatWorkGroupSizeAttr
TmpAttr(Context
, CI
, MinExpr
, MaxExpr
);
7527 if (checkAMDGPUFlatWorkGroupSizeArguments(*this, MinExpr
, MaxExpr
, TmpAttr
))
7530 D
->addAttr(::new (Context
)
7531 AMDGPUFlatWorkGroupSizeAttr(Context
, CI
, MinExpr
, MaxExpr
));
7534 static void handleAMDGPUFlatWorkGroupSizeAttr(Sema
&S
, Decl
*D
,
7535 const ParsedAttr
&AL
) {
7536 Expr
*MinExpr
= AL
.getArgAsExpr(0);
7537 Expr
*MaxExpr
= AL
.getArgAsExpr(1);
7539 S
.addAMDGPUFlatWorkGroupSizeAttr(D
, AL
, MinExpr
, MaxExpr
);
7542 static bool checkAMDGPUWavesPerEUArguments(Sema
&S
, Expr
*MinExpr
,
7544 const AMDGPUWavesPerEUAttr
&Attr
) {
7545 if (S
.DiagnoseUnexpandedParameterPack(MinExpr
) ||
7546 (MaxExpr
&& S
.DiagnoseUnexpandedParameterPack(MaxExpr
)))
7549 // Accept template arguments for now as they depend on something else.
7550 // We'll get to check them when they eventually get instantiated.
7551 if (MinExpr
->isValueDependent() || (MaxExpr
&& MaxExpr
->isValueDependent()))
7555 if (!checkUInt32Argument(S
, Attr
, MinExpr
, Min
, 0))
7559 if (MaxExpr
&& !checkUInt32Argument(S
, Attr
, MaxExpr
, Max
, 1))
7562 if (Min
== 0 && Max
!= 0) {
7563 S
.Diag(Attr
.getLocation(), diag::err_attribute_argument_invalid
)
7567 if (Max
!= 0 && Min
> Max
) {
7568 S
.Diag(Attr
.getLocation(), diag::err_attribute_argument_invalid
)
7576 void Sema::addAMDGPUWavesPerEUAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
7577 Expr
*MinExpr
, Expr
*MaxExpr
) {
7578 AMDGPUWavesPerEUAttr
TmpAttr(Context
, CI
, MinExpr
, MaxExpr
);
7580 if (checkAMDGPUWavesPerEUArguments(*this, MinExpr
, MaxExpr
, TmpAttr
))
7583 D
->addAttr(::new (Context
)
7584 AMDGPUWavesPerEUAttr(Context
, CI
, MinExpr
, MaxExpr
));
7587 static void handleAMDGPUWavesPerEUAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7588 if (!AL
.checkAtLeastNumArgs(S
, 1) || !AL
.checkAtMostNumArgs(S
, 2))
7591 Expr
*MinExpr
= AL
.getArgAsExpr(0);
7592 Expr
*MaxExpr
= (AL
.getNumArgs() > 1) ? AL
.getArgAsExpr(1) : nullptr;
7594 S
.addAMDGPUWavesPerEUAttr(D
, AL
, MinExpr
, MaxExpr
);
7597 static void handleAMDGPUNumSGPRAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7598 uint32_t NumSGPR
= 0;
7599 Expr
*NumSGPRExpr
= AL
.getArgAsExpr(0);
7600 if (!checkUInt32Argument(S
, AL
, NumSGPRExpr
, NumSGPR
))
7603 D
->addAttr(::new (S
.Context
) AMDGPUNumSGPRAttr(S
.Context
, AL
, NumSGPR
));
7606 static void handleAMDGPUNumVGPRAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7607 uint32_t NumVGPR
= 0;
7608 Expr
*NumVGPRExpr
= AL
.getArgAsExpr(0);
7609 if (!checkUInt32Argument(S
, AL
, NumVGPRExpr
, NumVGPR
))
7612 D
->addAttr(::new (S
.Context
) AMDGPUNumVGPRAttr(S
.Context
, AL
, NumVGPR
));
7615 static void handleX86ForceAlignArgPointerAttr(Sema
&S
, Decl
*D
,
7616 const ParsedAttr
&AL
) {
7617 // If we try to apply it to a function pointer, don't warn, but don't
7618 // do anything, either. It doesn't matter anyway, because there's nothing
7619 // special about calling a force_align_arg_pointer function.
7620 const auto *VD
= dyn_cast
<ValueDecl
>(D
);
7621 if (VD
&& VD
->getType()->isFunctionPointerType())
7623 // Also don't warn on function pointer typedefs.
7624 const auto *TD
= dyn_cast
<TypedefNameDecl
>(D
);
7625 if (TD
&& (TD
->getUnderlyingType()->isFunctionPointerType() ||
7626 TD
->getUnderlyingType()->isFunctionType()))
7628 // Attribute can only be applied to function types.
7629 if (!isa
<FunctionDecl
>(D
)) {
7630 S
.Diag(AL
.getLoc(), diag::warn_attribute_wrong_decl_type
)
7631 << AL
<< ExpectedFunction
;
7635 D
->addAttr(::new (S
.Context
) X86ForceAlignArgPointerAttr(S
.Context
, AL
));
7638 static void handleLayoutVersion(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7640 Expr
*VersionExpr
= static_cast<Expr
*>(AL
.getArgAsExpr(0));
7641 if (!checkUInt32Argument(S
, AL
, AL
.getArgAsExpr(0), Version
))
7644 // TODO: Investigate what happens with the next major version of MSVC.
7645 if (Version
!= LangOptions::MSVC2015
/ 100) {
7646 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_out_of_bounds
)
7647 << AL
<< Version
<< VersionExpr
->getSourceRange();
7651 // The attribute expects a "major" version number like 19, but new versions of
7652 // MSVC have moved to updating the "minor", or less significant numbers, so we
7653 // have to multiply by 100 now.
7656 D
->addAttr(::new (S
.Context
) LayoutVersionAttr(S
.Context
, AL
, Version
));
7659 DLLImportAttr
*Sema::mergeDLLImportAttr(Decl
*D
,
7660 const AttributeCommonInfo
&CI
) {
7661 if (D
->hasAttr
<DLLExportAttr
>()) {
7662 Diag(CI
.getLoc(), diag::warn_attribute_ignored
) << "'dllimport'";
7666 if (D
->hasAttr
<DLLImportAttr
>())
7669 return ::new (Context
) DLLImportAttr(Context
, CI
);
7672 DLLExportAttr
*Sema::mergeDLLExportAttr(Decl
*D
,
7673 const AttributeCommonInfo
&CI
) {
7674 if (DLLImportAttr
*Import
= D
->getAttr
<DLLImportAttr
>()) {
7675 Diag(Import
->getLocation(), diag::warn_attribute_ignored
) << Import
;
7676 D
->dropAttr
<DLLImportAttr
>();
7679 if (D
->hasAttr
<DLLExportAttr
>())
7682 return ::new (Context
) DLLExportAttr(Context
, CI
);
7685 static void handleDLLAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&A
) {
7686 if (isa
<ClassTemplatePartialSpecializationDecl
>(D
) &&
7687 (S
.Context
.getTargetInfo().shouldDLLImportComdatSymbols())) {
7688 S
.Diag(A
.getRange().getBegin(), diag::warn_attribute_ignored
) << A
;
7692 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
7693 if (FD
->isInlined() && A
.getKind() == ParsedAttr::AT_DLLImport
&&
7694 !(S
.Context
.getTargetInfo().shouldDLLImportComdatSymbols())) {
7695 // MinGW doesn't allow dllimport on inline functions.
7696 S
.Diag(A
.getRange().getBegin(), diag::warn_attribute_ignored_on_inline
)
7702 if (const auto *MD
= dyn_cast
<CXXMethodDecl
>(D
)) {
7703 if ((S
.Context
.getTargetInfo().shouldDLLImportComdatSymbols()) &&
7704 MD
->getParent()->isLambda()) {
7705 S
.Diag(A
.getRange().getBegin(), diag::err_attribute_dll_lambda
) << A
;
7710 Attr
*NewAttr
= A
.getKind() == ParsedAttr::AT_DLLExport
7711 ? (Attr
*)S
.mergeDLLExportAttr(D
, A
)
7712 : (Attr
*)S
.mergeDLLImportAttr(D
, A
);
7714 D
->addAttr(NewAttr
);
7718 Sema::mergeMSInheritanceAttr(Decl
*D
, const AttributeCommonInfo
&CI
,
7720 MSInheritanceModel Model
) {
7721 if (MSInheritanceAttr
*IA
= D
->getAttr
<MSInheritanceAttr
>()) {
7722 if (IA
->getInheritanceModel() == Model
)
7724 Diag(IA
->getLocation(), diag::err_mismatched_ms_inheritance
)
7725 << 1 /*previous declaration*/;
7726 Diag(CI
.getLoc(), diag::note_previous_ms_inheritance
);
7727 D
->dropAttr
<MSInheritanceAttr
>();
7730 auto *RD
= cast
<CXXRecordDecl
>(D
);
7731 if (RD
->hasDefinition()) {
7732 if (checkMSInheritanceAttrOnDefinition(RD
, CI
.getRange(), BestCase
,
7737 if (isa
<ClassTemplatePartialSpecializationDecl
>(RD
)) {
7738 Diag(CI
.getLoc(), diag::warn_ignored_ms_inheritance
)
7739 << 1 /*partial specialization*/;
7742 if (RD
->getDescribedClassTemplate()) {
7743 Diag(CI
.getLoc(), diag::warn_ignored_ms_inheritance
)
7744 << 0 /*primary template*/;
7749 return ::new (Context
) MSInheritanceAttr(Context
, CI
, BestCase
);
7752 static void handleCapabilityAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7753 // The capability attributes take a single string parameter for the name of
7754 // the capability they represent. The lockable attribute does not take any
7755 // parameters. However, semantically, both attributes represent the same
7756 // concept, and so they use the same semantic attribute. Eventually, the
7757 // lockable attribute will be removed.
7759 // For backward compatibility, any capability which has no specified string
7760 // literal will be considered a "mutex."
7761 StringRef
N("mutex");
7762 SourceLocation LiteralLoc
;
7763 if (AL
.getKind() == ParsedAttr::AT_Capability
&&
7764 !S
.checkStringLiteralArgumentAttr(AL
, 0, N
, &LiteralLoc
))
7767 D
->addAttr(::new (S
.Context
) CapabilityAttr(S
.Context
, AL
, N
));
7770 static void handleAssertCapabilityAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7771 SmallVector
<Expr
*, 1> Args
;
7772 if (!checkLockFunAttrCommon(S
, D
, AL
, Args
))
7775 D
->addAttr(::new (S
.Context
)
7776 AssertCapabilityAttr(S
.Context
, AL
, Args
.data(), Args
.size()));
7779 static void handleAcquireCapabilityAttr(Sema
&S
, Decl
*D
,
7780 const ParsedAttr
&AL
) {
7781 SmallVector
<Expr
*, 1> Args
;
7782 if (!checkLockFunAttrCommon(S
, D
, AL
, Args
))
7785 D
->addAttr(::new (S
.Context
) AcquireCapabilityAttr(S
.Context
, AL
, Args
.data(),
7789 static void handleTryAcquireCapabilityAttr(Sema
&S
, Decl
*D
,
7790 const ParsedAttr
&AL
) {
7791 SmallVector
<Expr
*, 2> Args
;
7792 if (!checkTryLockFunAttrCommon(S
, D
, AL
, Args
))
7795 D
->addAttr(::new (S
.Context
) TryAcquireCapabilityAttr(
7796 S
.Context
, AL
, AL
.getArgAsExpr(0), Args
.data(), Args
.size()));
7799 static void handleReleaseCapabilityAttr(Sema
&S
, Decl
*D
,
7800 const ParsedAttr
&AL
) {
7801 // Check that all arguments are lockable objects.
7802 SmallVector
<Expr
*, 1> Args
;
7803 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
, 0, true);
7805 D
->addAttr(::new (S
.Context
) ReleaseCapabilityAttr(S
.Context
, AL
, Args
.data(),
7809 static void handleRequiresCapabilityAttr(Sema
&S
, Decl
*D
,
7810 const ParsedAttr
&AL
) {
7811 if (!AL
.checkAtLeastNumArgs(S
, 1))
7814 // check that all arguments are lockable objects
7815 SmallVector
<Expr
*, 1> Args
;
7816 checkAttrArgsAreCapabilityObjs(S
, D
, AL
, Args
);
7820 RequiresCapabilityAttr
*RCA
= ::new (S
.Context
)
7821 RequiresCapabilityAttr(S
.Context
, AL
, Args
.data(), Args
.size());
7826 static void handleDeprecatedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7827 if (const auto *NSD
= dyn_cast
<NamespaceDecl
>(D
)) {
7828 if (NSD
->isAnonymousNamespace()) {
7829 S
.Diag(AL
.getLoc(), diag::warn_deprecated_anonymous_namespace
);
7830 // Do not want to attach the attribute to the namespace because that will
7831 // cause confusing diagnostic reports for uses of declarations within the
7835 } else if (isa
<UsingDecl
, UnresolvedUsingTypenameDecl
,
7836 UnresolvedUsingValueDecl
>(D
)) {
7837 S
.Diag(AL
.getRange().getBegin(), diag::warn_deprecated_ignored_on_using
)
7842 // Handle the cases where the attribute has a text message.
7843 StringRef Str
, Replacement
;
7844 if (AL
.isArgExpr(0) && AL
.getArgAsExpr(0) &&
7845 !S
.checkStringLiteralArgumentAttr(AL
, 0, Str
))
7848 // Support a single optional message only for Declspec and [[]] spellings.
7849 if (AL
.isDeclspecAttribute() || AL
.isStandardAttributeSyntax())
7850 AL
.checkAtMostNumArgs(S
, 1);
7851 else if (AL
.isArgExpr(1) && AL
.getArgAsExpr(1) &&
7852 !S
.checkStringLiteralArgumentAttr(AL
, 1, Replacement
))
7855 if (!S
.getLangOpts().CPlusPlus14
&& AL
.isCXX11Attribute() && !AL
.isGNUScope())
7856 S
.Diag(AL
.getLoc(), diag::ext_cxx14_attr
) << AL
;
7858 D
->addAttr(::new (S
.Context
) DeprecatedAttr(S
.Context
, AL
, Str
, Replacement
));
7861 static bool isGlobalVar(const Decl
*D
) {
7862 if (const auto *S
= dyn_cast
<VarDecl
>(D
))
7863 return S
->hasGlobalStorage();
7867 static bool isSanitizerAttributeAllowedOnGlobals(StringRef Sanitizer
) {
7868 return Sanitizer
== "address" || Sanitizer
== "hwaddress" ||
7869 Sanitizer
== "memtag";
7872 static void handleNoSanitizeAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7873 if (!AL
.checkAtLeastNumArgs(S
, 1))
7876 std::vector
<StringRef
> Sanitizers
;
7878 for (unsigned I
= 0, E
= AL
.getNumArgs(); I
!= E
; ++I
) {
7879 StringRef SanitizerName
;
7880 SourceLocation LiteralLoc
;
7882 if (!S
.checkStringLiteralArgumentAttr(AL
, I
, SanitizerName
, &LiteralLoc
))
7885 if (parseSanitizerValue(SanitizerName
, /*AllowGroups=*/true) ==
7887 SanitizerName
!= "coverage")
7888 S
.Diag(LiteralLoc
, diag::warn_unknown_sanitizer_ignored
) << SanitizerName
;
7889 else if (isGlobalVar(D
) && !isSanitizerAttributeAllowedOnGlobals(SanitizerName
))
7890 S
.Diag(D
->getLocation(), diag::warn_attribute_type_not_supported_global
)
7891 << AL
<< SanitizerName
;
7892 Sanitizers
.push_back(SanitizerName
);
7895 D
->addAttr(::new (S
.Context
) NoSanitizeAttr(S
.Context
, AL
, Sanitizers
.data(),
7896 Sanitizers
.size()));
7899 static void handleNoSanitizeSpecificAttr(Sema
&S
, Decl
*D
,
7900 const ParsedAttr
&AL
) {
7901 StringRef AttrName
= AL
.getAttrName()->getName();
7902 normalizeName(AttrName
);
7903 StringRef SanitizerName
= llvm::StringSwitch
<StringRef
>(AttrName
)
7904 .Case("no_address_safety_analysis", "address")
7905 .Case("no_sanitize_address", "address")
7906 .Case("no_sanitize_thread", "thread")
7907 .Case("no_sanitize_memory", "memory");
7908 if (isGlobalVar(D
) && SanitizerName
!= "address")
7909 S
.Diag(D
->getLocation(), diag::err_attribute_wrong_decl_type
)
7910 << AL
<< ExpectedFunction
;
7912 // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
7913 // NoSanitizeAttr object; but we need to calculate the correct spelling list
7914 // index rather than incorrectly assume the index for NoSanitizeSpecificAttr
7915 // has the same spellings as the index for NoSanitizeAttr. We don't have a
7916 // general way to "translate" between the two, so this hack attempts to work
7917 // around the issue with hard-coded indices. This is critical for calling
7918 // getSpelling() or prettyPrint() on the resulting semantic attribute object
7919 // without failing assertions.
7920 unsigned TranslatedSpellingIndex
= 0;
7921 if (AL
.isStandardAttributeSyntax())
7922 TranslatedSpellingIndex
= 1;
7924 AttributeCommonInfo Info
= AL
;
7925 Info
.setAttributeSpellingListIndex(TranslatedSpellingIndex
);
7926 D
->addAttr(::new (S
.Context
)
7927 NoSanitizeAttr(S
.Context
, Info
, &SanitizerName
, 1));
7930 static void handleInternalLinkageAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7931 if (InternalLinkageAttr
*Internal
= S
.mergeInternalLinkageAttr(D
, AL
))
7932 D
->addAttr(Internal
);
7935 static void handleOpenCLNoSVMAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7936 if (S
.LangOpts
.getOpenCLCompatibleVersion() < 200)
7937 S
.Diag(AL
.getLoc(), diag::err_attribute_requires_opencl_version
)
7938 << AL
<< "2.0" << 1;
7940 S
.Diag(AL
.getLoc(), diag::warn_opencl_attr_deprecated_ignored
)
7941 << AL
<< S
.LangOpts
.getOpenCLVersionString();
7944 static void handleOpenCLAccessAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7945 if (D
->isInvalidDecl())
7948 // Check if there is only one access qualifier.
7949 if (D
->hasAttr
<OpenCLAccessAttr
>()) {
7950 if (D
->getAttr
<OpenCLAccessAttr
>()->getSemanticSpelling() ==
7951 AL
.getSemanticSpelling()) {
7952 S
.Diag(AL
.getLoc(), diag::warn_duplicate_declspec
)
7953 << AL
.getAttrName()->getName() << AL
.getRange();
7955 S
.Diag(AL
.getLoc(), diag::err_opencl_multiple_access_qualifiers
)
7956 << D
->getSourceRange();
7957 D
->setInvalidDecl(true);
7962 // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that
7963 // an image object can be read and written. OpenCL v2.0 s6.13.6 - A kernel
7964 // cannot read from and write to the same pipe object. Using the read_write
7965 // (or __read_write) qualifier with the pipe qualifier is a compilation error.
7966 // OpenCL v3.0 s6.8 - For OpenCL C 2.0, or with the
7967 // __opencl_c_read_write_images feature, image objects specified as arguments
7968 // to a kernel can additionally be declared to be read-write.
7969 // C++ for OpenCL 1.0 inherits rule from OpenCL C v2.0.
7970 // C++ for OpenCL 2021 inherits rule from OpenCL C v3.0.
7971 if (const auto *PDecl
= dyn_cast
<ParmVarDecl
>(D
)) {
7972 const Type
*DeclTy
= PDecl
->getType().getCanonicalType().getTypePtr();
7973 if (AL
.getAttrName()->getName().contains("read_write")) {
7974 bool ReadWriteImagesUnsupported
=
7975 (S
.getLangOpts().getOpenCLCompatibleVersion() < 200) ||
7976 (S
.getLangOpts().getOpenCLCompatibleVersion() == 300 &&
7977 !S
.getOpenCLOptions().isSupported("__opencl_c_read_write_images",
7979 if (ReadWriteImagesUnsupported
|| DeclTy
->isPipeType()) {
7980 S
.Diag(AL
.getLoc(), diag::err_opencl_invalid_read_write
)
7981 << AL
<< PDecl
->getType() << DeclTy
->isImageType();
7982 D
->setInvalidDecl(true);
7988 D
->addAttr(::new (S
.Context
) OpenCLAccessAttr(S
.Context
, AL
));
7991 static void handleZeroCallUsedRegsAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
7992 // Check that the argument is a string literal.
7994 SourceLocation LiteralLoc
;
7995 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, KindStr
, &LiteralLoc
))
7998 ZeroCallUsedRegsAttr::ZeroCallUsedRegsKind Kind
;
7999 if (!ZeroCallUsedRegsAttr::ConvertStrToZeroCallUsedRegsKind(KindStr
, Kind
)) {
8000 S
.Diag(LiteralLoc
, diag::warn_attribute_type_not_supported
)
8005 D
->dropAttr
<ZeroCallUsedRegsAttr
>();
8006 D
->addAttr(ZeroCallUsedRegsAttr::Create(S
.Context
, Kind
, AL
));
8009 static void handleFunctionReturnThunksAttr(Sema
&S
, Decl
*D
,
8010 const ParsedAttr
&AL
) {
8012 SourceLocation LiteralLoc
;
8013 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, KindStr
, &LiteralLoc
))
8016 FunctionReturnThunksAttr::Kind Kind
;
8017 if (!FunctionReturnThunksAttr::ConvertStrToKind(KindStr
, Kind
)) {
8018 S
.Diag(LiteralLoc
, diag::warn_attribute_type_not_supported
)
8022 // FIXME: it would be good to better handle attribute merging rather than
8023 // silently replacing the existing attribute, so long as it does not break
8024 // the expected codegen tests.
8025 D
->dropAttr
<FunctionReturnThunksAttr
>();
8026 D
->addAttr(FunctionReturnThunksAttr::Create(S
.Context
, Kind
, AL
));
8029 static void handleSYCLKernelAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8030 // The 'sycl_kernel' attribute applies only to function templates.
8031 const auto *FD
= cast
<FunctionDecl
>(D
);
8032 const FunctionTemplateDecl
*FT
= FD
->getDescribedFunctionTemplate();
8033 assert(FT
&& "Function template is expected");
8035 // Function template must have at least two template parameters.
8036 const TemplateParameterList
*TL
= FT
->getTemplateParameters();
8037 if (TL
->size() < 2) {
8038 S
.Diag(FT
->getLocation(), diag::warn_sycl_kernel_num_of_template_params
);
8042 // Template parameters must be typenames.
8043 for (unsigned I
= 0; I
< 2; ++I
) {
8044 const NamedDecl
*TParam
= TL
->getParam(I
);
8045 if (isa
<NonTypeTemplateParmDecl
>(TParam
)) {
8046 S
.Diag(FT
->getLocation(),
8047 diag::warn_sycl_kernel_invalid_template_param_type
);
8052 // Function must have at least one argument.
8053 if (getFunctionOrMethodNumParams(D
) != 1) {
8054 S
.Diag(FT
->getLocation(), diag::warn_sycl_kernel_num_of_function_params
);
8058 // Function must return void.
8059 QualType RetTy
= getFunctionOrMethodResultType(D
);
8060 if (!RetTy
->isVoidType()) {
8061 S
.Diag(FT
->getLocation(), diag::warn_sycl_kernel_return_type
);
8065 handleSimpleAttribute
<SYCLKernelAttr
>(S
, D
, AL
);
8068 static void handleDestroyAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&A
) {
8069 if (!cast
<VarDecl
>(D
)->hasGlobalStorage()) {
8070 S
.Diag(D
->getLocation(), diag::err_destroy_attr_on_non_static_var
)
8071 << (A
.getKind() == ParsedAttr::AT_AlwaysDestroy
);
8075 if (A
.getKind() == ParsedAttr::AT_AlwaysDestroy
)
8076 handleSimpleAttribute
<AlwaysDestroyAttr
>(S
, D
, A
);
8078 handleSimpleAttribute
<NoDestroyAttr
>(S
, D
, A
);
8081 static void handleUninitializedAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8082 assert(cast
<VarDecl
>(D
)->getStorageDuration() == SD_Automatic
&&
8083 "uninitialized is only valid on automatic duration variables");
8084 D
->addAttr(::new (S
.Context
) UninitializedAttr(S
.Context
, AL
));
8087 static bool tryMakeVariablePseudoStrong(Sema
&S
, VarDecl
*VD
,
8088 bool DiagnoseFailure
) {
8089 QualType Ty
= VD
->getType();
8090 if (!Ty
->isObjCRetainableType()) {
8091 if (DiagnoseFailure
) {
8092 S
.Diag(VD
->getBeginLoc(), diag::warn_ignored_objc_externally_retained
)
8098 Qualifiers::ObjCLifetime LifetimeQual
= Ty
.getQualifiers().getObjCLifetime();
8100 // Sema::inferObjCARCLifetime must run after processing decl attributes
8101 // (because __block lowers to an attribute), so if the lifetime hasn't been
8102 // explicitly specified, infer it locally now.
8103 if (LifetimeQual
== Qualifiers::OCL_None
)
8104 LifetimeQual
= Ty
->getObjCARCImplicitLifetime();
8106 // The attributes only really makes sense for __strong variables; ignore any
8107 // attempts to annotate a parameter with any other lifetime qualifier.
8108 if (LifetimeQual
!= Qualifiers::OCL_Strong
) {
8109 if (DiagnoseFailure
) {
8110 S
.Diag(VD
->getBeginLoc(), diag::warn_ignored_objc_externally_retained
)
8116 // Tampering with the type of a VarDecl here is a bit of a hack, but we need
8117 // to ensure that the variable is 'const' so that we can error on
8118 // modification, which can otherwise over-release.
8119 VD
->setType(Ty
.withConst());
8120 VD
->setARCPseudoStrong(true);
8124 static void handleObjCExternallyRetainedAttr(Sema
&S
, Decl
*D
,
8125 const ParsedAttr
&AL
) {
8126 if (auto *VD
= dyn_cast
<VarDecl
>(D
)) {
8127 assert(!isa
<ParmVarDecl
>(VD
) && "should be diagnosed automatically");
8128 if (!VD
->hasLocalStorage()) {
8129 S
.Diag(D
->getBeginLoc(), diag::warn_ignored_objc_externally_retained
)
8134 if (!tryMakeVariablePseudoStrong(S
, VD
, /*DiagnoseFailure=*/true))
8137 handleSimpleAttribute
<ObjCExternallyRetainedAttr
>(S
, D
, AL
);
8141 // If D is a function-like declaration (method, block, or function), then we
8142 // make every parameter psuedo-strong.
8143 unsigned NumParams
=
8144 hasFunctionProto(D
) ? getFunctionOrMethodNumParams(D
) : 0;
8145 for (unsigned I
= 0; I
!= NumParams
; ++I
) {
8146 auto *PVD
= const_cast<ParmVarDecl
*>(getFunctionOrMethodParam(D
, I
));
8147 QualType Ty
= PVD
->getType();
8149 // If a user wrote a parameter with __strong explicitly, then assume they
8150 // want "real" strong semantics for that parameter. This works because if
8151 // the parameter was written with __strong, then the strong qualifier will
8153 if (Ty
.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
8154 Qualifiers::OCL_Strong
)
8157 tryMakeVariablePseudoStrong(S
, PVD
, /*DiagnoseFailure=*/false);
8159 handleSimpleAttribute
<ObjCExternallyRetainedAttr
>(S
, D
, AL
);
8162 static void handleMIGServerRoutineAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8163 // Check that the return type is a `typedef int kern_return_t` or a typedef
8164 // around it, because otherwise MIG convention checks make no sense.
8165 // BlockDecl doesn't store a return type, so it's annoying to check,
8166 // so let's skip it for now.
8167 if (!isa
<BlockDecl
>(D
)) {
8168 QualType T
= getFunctionOrMethodResultType(D
);
8169 bool IsKernReturnT
= false;
8170 while (const auto *TT
= T
->getAs
<TypedefType
>()) {
8171 IsKernReturnT
= (TT
->getDecl()->getName() == "kern_return_t");
8174 if (!IsKernReturnT
|| T
.getCanonicalType() != S
.getASTContext().IntTy
) {
8175 S
.Diag(D
->getBeginLoc(),
8176 diag::warn_mig_server_routine_does_not_return_kern_return_t
);
8181 handleSimpleAttribute
<MIGServerRoutineAttr
>(S
, D
, AL
);
8184 static void handleMSAllocatorAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8185 // Warn if the return type is not a pointer or reference type.
8186 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
8187 QualType RetTy
= FD
->getReturnType();
8188 if (!RetTy
->isPointerType() && !RetTy
->isReferenceType()) {
8189 S
.Diag(AL
.getLoc(), diag::warn_declspec_allocator_nonpointer
)
8190 << AL
.getRange() << RetTy
;
8195 handleSimpleAttribute
<MSAllocatorAttr
>(S
, D
, AL
);
8198 static void handleAcquireHandleAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8199 if (AL
.isUsedAsTypeAttr())
8201 // Warn if the parameter is definitely not an output parameter.
8202 if (const auto *PVD
= dyn_cast
<ParmVarDecl
>(D
)) {
8203 if (PVD
->getType()->isIntegerType()) {
8204 S
.Diag(AL
.getLoc(), diag::err_attribute_output_parameter
)
8210 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Argument
))
8212 D
->addAttr(AcquireHandleAttr::Create(S
.Context
, Argument
, AL
));
8215 template<typename Attr
>
8216 static void handleHandleAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8218 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Argument
))
8220 D
->addAttr(Attr::Create(S
.Context
, Argument
, AL
));
8223 static void handleCFGuardAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8224 // The guard attribute takes a single identifier argument.
8226 if (!AL
.isArgIdent(0)) {
8227 S
.Diag(AL
.getLoc(), diag::err_attribute_argument_type
)
8228 << AL
<< AANT_ArgumentIdentifier
;
8232 CFGuardAttr::GuardArg Arg
;
8233 IdentifierInfo
*II
= AL
.getArgAsIdent(0)->Ident
;
8234 if (!CFGuardAttr::ConvertStrToGuardArg(II
->getName(), Arg
)) {
8235 S
.Diag(AL
.getLoc(), diag::warn_attribute_type_not_supported
) << AL
<< II
;
8239 D
->addAttr(::new (S
.Context
) CFGuardAttr(S
.Context
, AL
, Arg
));
8243 template <typename AttrTy
>
8244 static const AttrTy
*findEnforceTCBAttrByName(Decl
*D
, StringRef Name
) {
8245 auto Attrs
= D
->specific_attrs
<AttrTy
>();
8246 auto I
= llvm::find_if(Attrs
,
8247 [Name
](const AttrTy
*A
) {
8248 return A
->getTCBName() == Name
;
8250 return I
== Attrs
.end() ? nullptr : *I
;
8253 template <typename AttrTy
, typename ConflictingAttrTy
>
8254 static void handleEnforceTCBAttr(Sema
&S
, Decl
*D
, const ParsedAttr
&AL
) {
8256 if (!S
.checkStringLiteralArgumentAttr(AL
, 0, Argument
))
8259 // A function cannot be have both regular and leaf membership in the same TCB.
8260 if (const ConflictingAttrTy
*ConflictingAttr
=
8261 findEnforceTCBAttrByName
<ConflictingAttrTy
>(D
, Argument
)) {
8262 // We could attach a note to the other attribute but in this case
8263 // there's no need given how the two are very close to each other.
8264 S
.Diag(AL
.getLoc(), diag::err_tcb_conflicting_attributes
)
8265 << AL
.getAttrName()->getName() << ConflictingAttr
->getAttrName()->getName()
8268 // Error recovery: drop the non-leaf attribute so that to suppress
8269 // all future warnings caused by erroneous attributes. The leaf attribute
8270 // needs to be kept because it can only suppresses warnings, not cause them.
8271 D
->dropAttr
<EnforceTCBAttr
>();
8275 D
->addAttr(AttrTy::Create(S
.Context
, Argument
, AL
));
8278 template <typename AttrTy
, typename ConflictingAttrTy
>
8279 static AttrTy
*mergeEnforceTCBAttrImpl(Sema
&S
, Decl
*D
, const AttrTy
&AL
) {
8280 // Check if the new redeclaration has different leaf-ness in the same TCB.
8281 StringRef TCBName
= AL
.getTCBName();
8282 if (const ConflictingAttrTy
*ConflictingAttr
=
8283 findEnforceTCBAttrByName
<ConflictingAttrTy
>(D
, TCBName
)) {
8284 S
.Diag(ConflictingAttr
->getLoc(), diag::err_tcb_conflicting_attributes
)
8285 << ConflictingAttr
->getAttrName()->getName()
8286 << AL
.getAttrName()->getName() << TCBName
;
8288 // Add a note so that the user could easily find the conflicting attribute.
8289 S
.Diag(AL
.getLoc(), diag::note_conflicting_attribute
);
8291 // More error recovery.
8292 D
->dropAttr
<EnforceTCBAttr
>();
8296 ASTContext
&Context
= S
.getASTContext();
8297 return ::new(Context
) AttrTy(Context
, AL
, AL
.getTCBName());
8300 EnforceTCBAttr
*Sema::mergeEnforceTCBAttr(Decl
*D
, const EnforceTCBAttr
&AL
) {
8301 return mergeEnforceTCBAttrImpl
<EnforceTCBAttr
, EnforceTCBLeafAttr
>(
8305 EnforceTCBLeafAttr
*Sema::mergeEnforceTCBLeafAttr(
8306 Decl
*D
, const EnforceTCBLeafAttr
&AL
) {
8307 return mergeEnforceTCBAttrImpl
<EnforceTCBLeafAttr
, EnforceTCBAttr
>(
8311 //===----------------------------------------------------------------------===//
8312 // Top Level Sema Entry Points
8313 //===----------------------------------------------------------------------===//
8315 // Returns true if the attribute must delay setting its arguments until after
8316 // template instantiation, and false otherwise.
8317 static bool MustDelayAttributeArguments(const ParsedAttr
&AL
) {
8318 // Only attributes that accept expression parameter packs can delay arguments.
8319 if (!AL
.acceptsExprPack())
8322 bool AttrHasVariadicArg
= AL
.hasVariadicArg();
8323 unsigned AttrNumArgs
= AL
.getNumArgMembers();
8324 for (size_t I
= 0; I
< std::min(AL
.getNumArgs(), AttrNumArgs
); ++I
) {
8325 bool IsLastAttrArg
= I
== (AttrNumArgs
- 1);
8326 // If the argument is the last argument and it is variadic it can contain
8328 if (IsLastAttrArg
&& AttrHasVariadicArg
)
8330 Expr
*E
= AL
.getArgAsExpr(I
);
8331 bool ArgMemberCanHoldExpr
= AL
.isParamExpr(I
);
8332 // If the expression is a pack expansion then arguments must be delayed
8333 // unless the argument is an expression and it is the last argument of the
8335 if (isa
<PackExpansionExpr
>(E
))
8336 return !(IsLastAttrArg
&& ArgMemberCanHoldExpr
);
8337 // Last case is if the expression is value dependent then it must delay
8338 // arguments unless the corresponding argument is able to hold the
8340 if (E
->isValueDependent() && !ArgMemberCanHoldExpr
)
8346 /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
8347 /// the attribute applies to decls. If the attribute is a type attribute, just
8348 /// silently ignore it if a GNU attribute.
8350 ProcessDeclAttribute(Sema
&S
, Scope
*scope
, Decl
*D
, const ParsedAttr
&AL
,
8351 const Sema::ProcessDeclAttributeOptions
&Options
) {
8352 if (AL
.isInvalid() || AL
.getKind() == ParsedAttr::IgnoredAttribute
)
8355 // Ignore C++11 attributes on declarator chunks: they appertain to the type
8357 // FIXME: We currently check the attribute syntax directly instead of using
8358 // isCXX11Attribute(), which currently erroneously classifies the C11
8359 // `_Alignas` attribute as a C++11 attribute. `_Alignas` can appear on the
8360 // `DeclSpec`, so we need to let it through here to make sure it is processed
8361 // appropriately. Once the behavior of isCXX11Attribute() is fixed, we can
8362 // go back to using that here.
8363 if (AL
.getSyntax() == ParsedAttr::AS_CXX11
&& !Options
.IncludeCXX11Attributes
)
8366 // Unknown attributes are automatically warned on. Target-specific attributes
8367 // which do not apply to the current target architecture are treated as
8368 // though they were unknown attributes.
8369 if (AL
.getKind() == ParsedAttr::UnknownAttribute
||
8370 !AL
.existsInTarget(S
.Context
.getTargetInfo())) {
8372 AL
.isDeclspecAttribute()
8373 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
8374 : (unsigned)diag::warn_unknown_attribute_ignored
)
8375 << AL
<< AL
.getRange();
8379 // Check if argument population must delayed to after template instantiation.
8380 bool MustDelayArgs
= MustDelayAttributeArguments(AL
);
8382 // Argument number check must be skipped if arguments are delayed.
8383 if (S
.checkCommonAttributeFeatures(D
, AL
, MustDelayArgs
))
8386 if (MustDelayArgs
) {
8387 AL
.handleAttrWithDelayedArgs(S
, D
);
8391 switch (AL
.getKind()) {
8393 if (AL
.getInfo().handleDeclAttribute(S
, D
, AL
) != ParsedAttrInfo::NotHandled
)
8395 if (!AL
.isStmtAttr()) {
8396 assert(AL
.isTypeAttr() && "Non-type attribute not handled");
8398 if (AL
.isTypeAttr()) {
8399 if (Options
.IgnoreTypeAttributes
)
8401 if (!AL
.isStandardAttributeSyntax()) {
8402 // Non-[[]] type attributes are handled in processTypeAttrs(); silently
8407 // According to the C and C++ standards, we should never see a
8408 // [[]] type attribute on a declaration. However, we have in the past
8409 // allowed some type attributes to "slide" to the `DeclSpec`, so we need
8410 // to continue to support this legacy behavior. We only do this, however,
8412 // - we actually have a `DeclSpec`, i.e. if we're looking at a
8413 // `DeclaratorDecl`, or
8414 // - we are looking at an alias-declaration, where historically we have
8415 // allowed type attributes after the identifier to slide to the type.
8416 if (AL
.slidesFromDeclToDeclSpecLegacyBehavior() &&
8417 isa
<DeclaratorDecl
, TypeAliasDecl
>(D
)) {
8418 // Suggest moving the attribute to the type instead, but only for our
8419 // own vendor attributes; moving other vendors' attributes might hurt
8421 if (AL
.isClangScope()) {
8422 S
.Diag(AL
.getLoc(), diag::warn_type_attribute_deprecated_on_decl
)
8423 << AL
<< D
->getLocation();
8426 // Allow this type attribute to be handled in processTypeAttrs();
8427 // silently move on.
8431 if (AL
.getKind() == ParsedAttr::AT_Regparm
) {
8432 // `regparm` is a special case: It's a type attribute but we still want
8433 // to treat it as if it had been written on the declaration because that
8434 // way we'll be able to handle it directly in `processTypeAttr()`.
8435 // If we treated `regparm` it as if it had been written on the
8436 // `DeclSpec`, the logic in `distributeFunctionTypeAttrFromDeclSepc()`
8437 // would try to move it to the declarator, but that doesn't work: We
8438 // can't remove the attribute from the list of declaration attributes
8439 // because it might be needed by other declarators in the same
8444 if (AL
.getKind() == ParsedAttr::AT_VectorSize
) {
8445 // `vector_size` is a special case: It's a type attribute semantically,
8446 // but GCC expects the [[]] syntax to be written on the declaration (and
8447 // warns that the attribute has no effect if it is placed on the
8448 // decl-specifier-seq).
8449 // Silently move on and allow the attribute to be handled in
8450 // processTypeAttr().
8454 if (AL
.getKind() == ParsedAttr::AT_NoDeref
) {
8455 // FIXME: `noderef` currently doesn't work correctly in [[]] syntax.
8456 // See https://github.com/llvm/llvm-project/issues/55790 for details.
8457 // We allow processTypeAttrs() to emit a warning and silently move on.
8461 // N.B., ClangAttrEmitter.cpp emits a diagnostic helper that ensures a
8462 // statement attribute is not written on a declaration, but this code is
8463 // needed for type attributes as well as statement attributes in Attr.td
8464 // that do not list any subjects.
8465 S
.Diag(AL
.getLoc(), diag::err_attribute_invalid_on_decl
)
8466 << AL
<< D
->getLocation();
8468 case ParsedAttr::AT_Interrupt
:
8469 handleInterruptAttr(S
, D
, AL
);
8471 case ParsedAttr::AT_X86ForceAlignArgPointer
:
8472 handleX86ForceAlignArgPointerAttr(S
, D
, AL
);
8474 case ParsedAttr::AT_DLLExport
:
8475 case ParsedAttr::AT_DLLImport
:
8476 handleDLLAttr(S
, D
, AL
);
8478 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize
:
8479 handleAMDGPUFlatWorkGroupSizeAttr(S
, D
, AL
);
8481 case ParsedAttr::AT_AMDGPUWavesPerEU
:
8482 handleAMDGPUWavesPerEUAttr(S
, D
, AL
);
8484 case ParsedAttr::AT_AMDGPUNumSGPR
:
8485 handleAMDGPUNumSGPRAttr(S
, D
, AL
);
8487 case ParsedAttr::AT_AMDGPUNumVGPR
:
8488 handleAMDGPUNumVGPRAttr(S
, D
, AL
);
8490 case ParsedAttr::AT_AVRSignal
:
8491 handleAVRSignalAttr(S
, D
, AL
);
8493 case ParsedAttr::AT_BPFPreserveAccessIndex
:
8494 handleBPFPreserveAccessIndexAttr(S
, D
, AL
);
8496 case ParsedAttr::AT_BTFDeclTag
:
8497 handleBTFDeclTagAttr(S
, D
, AL
);
8499 case ParsedAttr::AT_WebAssemblyExportName
:
8500 handleWebAssemblyExportNameAttr(S
, D
, AL
);
8502 case ParsedAttr::AT_WebAssemblyImportModule
:
8503 handleWebAssemblyImportModuleAttr(S
, D
, AL
);
8505 case ParsedAttr::AT_WebAssemblyImportName
:
8506 handleWebAssemblyImportNameAttr(S
, D
, AL
);
8508 case ParsedAttr::AT_IBOutlet
:
8509 handleIBOutlet(S
, D
, AL
);
8511 case ParsedAttr::AT_IBOutletCollection
:
8512 handleIBOutletCollection(S
, D
, AL
);
8514 case ParsedAttr::AT_IFunc
:
8515 handleIFuncAttr(S
, D
, AL
);
8517 case ParsedAttr::AT_Alias
:
8518 handleAliasAttr(S
, D
, AL
);
8520 case ParsedAttr::AT_Aligned
:
8521 handleAlignedAttr(S
, D
, AL
);
8523 case ParsedAttr::AT_AlignValue
:
8524 handleAlignValueAttr(S
, D
, AL
);
8526 case ParsedAttr::AT_AllocSize
:
8527 handleAllocSizeAttr(S
, D
, AL
);
8529 case ParsedAttr::AT_AlwaysInline
:
8530 handleAlwaysInlineAttr(S
, D
, AL
);
8532 case ParsedAttr::AT_AnalyzerNoReturn
:
8533 handleAnalyzerNoReturnAttr(S
, D
, AL
);
8535 case ParsedAttr::AT_TLSModel
:
8536 handleTLSModelAttr(S
, D
, AL
);
8538 case ParsedAttr::AT_Annotate
:
8539 handleAnnotateAttr(S
, D
, AL
);
8541 case ParsedAttr::AT_Availability
:
8542 handleAvailabilityAttr(S
, D
, AL
);
8544 case ParsedAttr::AT_CarriesDependency
:
8545 handleDependencyAttr(S
, scope
, D
, AL
);
8547 case ParsedAttr::AT_CPUDispatch
:
8548 case ParsedAttr::AT_CPUSpecific
:
8549 handleCPUSpecificAttr(S
, D
, AL
);
8551 case ParsedAttr::AT_Common
:
8552 handleCommonAttr(S
, D
, AL
);
8554 case ParsedAttr::AT_CUDAConstant
:
8555 handleConstantAttr(S
, D
, AL
);
8557 case ParsedAttr::AT_PassObjectSize
:
8558 handlePassObjectSizeAttr(S
, D
, AL
);
8560 case ParsedAttr::AT_Constructor
:
8561 handleConstructorAttr(S
, D
, AL
);
8563 case ParsedAttr::AT_Deprecated
:
8564 handleDeprecatedAttr(S
, D
, AL
);
8566 case ParsedAttr::AT_Destructor
:
8567 handleDestructorAttr(S
, D
, AL
);
8569 case ParsedAttr::AT_EnableIf
:
8570 handleEnableIfAttr(S
, D
, AL
);
8572 case ParsedAttr::AT_Error
:
8573 handleErrorAttr(S
, D
, AL
);
8575 case ParsedAttr::AT_DiagnoseIf
:
8576 handleDiagnoseIfAttr(S
, D
, AL
);
8578 case ParsedAttr::AT_DiagnoseAsBuiltin
:
8579 handleDiagnoseAsBuiltinAttr(S
, D
, AL
);
8581 case ParsedAttr::AT_NoBuiltin
:
8582 handleNoBuiltinAttr(S
, D
, AL
);
8584 case ParsedAttr::AT_ExtVectorType
:
8585 handleExtVectorTypeAttr(S
, D
, AL
);
8587 case ParsedAttr::AT_ExternalSourceSymbol
:
8588 handleExternalSourceSymbolAttr(S
, D
, AL
);
8590 case ParsedAttr::AT_MinSize
:
8591 handleMinSizeAttr(S
, D
, AL
);
8593 case ParsedAttr::AT_OptimizeNone
:
8594 handleOptimizeNoneAttr(S
, D
, AL
);
8596 case ParsedAttr::AT_EnumExtensibility
:
8597 handleEnumExtensibilityAttr(S
, D
, AL
);
8599 case ParsedAttr::AT_SYCLKernel
:
8600 handleSYCLKernelAttr(S
, D
, AL
);
8602 case ParsedAttr::AT_SYCLSpecialClass
:
8603 handleSimpleAttribute
<SYCLSpecialClassAttr
>(S
, D
, AL
);
8605 case ParsedAttr::AT_Format
:
8606 handleFormatAttr(S
, D
, AL
);
8608 case ParsedAttr::AT_FormatArg
:
8609 handleFormatArgAttr(S
, D
, AL
);
8611 case ParsedAttr::AT_Callback
:
8612 handleCallbackAttr(S
, D
, AL
);
8614 case ParsedAttr::AT_CalledOnce
:
8615 handleCalledOnceAttr(S
, D
, AL
);
8617 case ParsedAttr::AT_CUDAGlobal
:
8618 handleGlobalAttr(S
, D
, AL
);
8620 case ParsedAttr::AT_CUDADevice
:
8621 handleDeviceAttr(S
, D
, AL
);
8623 case ParsedAttr::AT_HIPManaged
:
8624 handleManagedAttr(S
, D
, AL
);
8626 case ParsedAttr::AT_GNUInline
:
8627 handleGNUInlineAttr(S
, D
, AL
);
8629 case ParsedAttr::AT_CUDALaunchBounds
:
8630 handleLaunchBoundsAttr(S
, D
, AL
);
8632 case ParsedAttr::AT_Restrict
:
8633 handleRestrictAttr(S
, D
, AL
);
8635 case ParsedAttr::AT_Mode
:
8636 handleModeAttr(S
, D
, AL
);
8638 case ParsedAttr::AT_NonNull
:
8639 if (auto *PVD
= dyn_cast
<ParmVarDecl
>(D
))
8640 handleNonNullAttrParameter(S
, PVD
, AL
);
8642 handleNonNullAttr(S
, D
, AL
);
8644 case ParsedAttr::AT_ReturnsNonNull
:
8645 handleReturnsNonNullAttr(S
, D
, AL
);
8647 case ParsedAttr::AT_NoEscape
:
8648 handleNoEscapeAttr(S
, D
, AL
);
8650 case ParsedAttr::AT_MaybeUndef
:
8651 handleSimpleAttribute
<MaybeUndefAttr
>(S
, D
, AL
);
8653 case ParsedAttr::AT_AssumeAligned
:
8654 handleAssumeAlignedAttr(S
, D
, AL
);
8656 case ParsedAttr::AT_AllocAlign
:
8657 handleAllocAlignAttr(S
, D
, AL
);
8659 case ParsedAttr::AT_Ownership
:
8660 handleOwnershipAttr(S
, D
, AL
);
8662 case ParsedAttr::AT_Naked
:
8663 handleNakedAttr(S
, D
, AL
);
8665 case ParsedAttr::AT_NoReturn
:
8666 handleNoReturnAttr(S
, D
, AL
);
8668 case ParsedAttr::AT_CXX11NoReturn
:
8669 handleStandardNoReturnAttr(S
, D
, AL
);
8671 case ParsedAttr::AT_AnyX86NoCfCheck
:
8672 handleNoCfCheckAttr(S
, D
, AL
);
8674 case ParsedAttr::AT_NoThrow
:
8675 if (!AL
.isUsedAsTypeAttr())
8676 handleSimpleAttribute
<NoThrowAttr
>(S
, D
, AL
);
8678 case ParsedAttr::AT_CUDAShared
:
8679 handleSharedAttr(S
, D
, AL
);
8681 case ParsedAttr::AT_VecReturn
:
8682 handleVecReturnAttr(S
, D
, AL
);
8684 case ParsedAttr::AT_ObjCOwnership
:
8685 handleObjCOwnershipAttr(S
, D
, AL
);
8687 case ParsedAttr::AT_ObjCPreciseLifetime
:
8688 handleObjCPreciseLifetimeAttr(S
, D
, AL
);
8690 case ParsedAttr::AT_ObjCReturnsInnerPointer
:
8691 handleObjCReturnsInnerPointerAttr(S
, D
, AL
);
8693 case ParsedAttr::AT_ObjCRequiresSuper
:
8694 handleObjCRequiresSuperAttr(S
, D
, AL
);
8696 case ParsedAttr::AT_ObjCBridge
:
8697 handleObjCBridgeAttr(S
, D
, AL
);
8699 case ParsedAttr::AT_ObjCBridgeMutable
:
8700 handleObjCBridgeMutableAttr(S
, D
, AL
);
8702 case ParsedAttr::AT_ObjCBridgeRelated
:
8703 handleObjCBridgeRelatedAttr(S
, D
, AL
);
8705 case ParsedAttr::AT_ObjCDesignatedInitializer
:
8706 handleObjCDesignatedInitializer(S
, D
, AL
);
8708 case ParsedAttr::AT_ObjCRuntimeName
:
8709 handleObjCRuntimeName(S
, D
, AL
);
8711 case ParsedAttr::AT_ObjCBoxable
:
8712 handleObjCBoxable(S
, D
, AL
);
8714 case ParsedAttr::AT_NSErrorDomain
:
8715 handleNSErrorDomain(S
, D
, AL
);
8717 case ParsedAttr::AT_CFConsumed
:
8718 case ParsedAttr::AT_NSConsumed
:
8719 case ParsedAttr::AT_OSConsumed
:
8720 S
.AddXConsumedAttr(D
, AL
, parsedAttrToRetainOwnershipKind(AL
),
8721 /*IsTemplateInstantiation=*/false);
8723 case ParsedAttr::AT_OSReturnsRetainedOnZero
:
8724 handleSimpleAttributeOrDiagnose
<OSReturnsRetainedOnZeroAttr
>(
8725 S
, D
, AL
, isValidOSObjectOutParameter(D
),
8726 diag::warn_ns_attribute_wrong_parameter_type
,
8727 /*Extra Args=*/AL
, /*pointer-to-OSObject-pointer*/ 3, AL
.getRange());
8729 case ParsedAttr::AT_OSReturnsRetainedOnNonZero
:
8730 handleSimpleAttributeOrDiagnose
<OSReturnsRetainedOnNonZeroAttr
>(
8731 S
, D
, AL
, isValidOSObjectOutParameter(D
),
8732 diag::warn_ns_attribute_wrong_parameter_type
,
8733 /*Extra Args=*/AL
, /*pointer-to-OSObject-poointer*/ 3, AL
.getRange());
8735 case ParsedAttr::AT_NSReturnsAutoreleased
:
8736 case ParsedAttr::AT_NSReturnsNotRetained
:
8737 case ParsedAttr::AT_NSReturnsRetained
:
8738 case ParsedAttr::AT_CFReturnsNotRetained
:
8739 case ParsedAttr::AT_CFReturnsRetained
:
8740 case ParsedAttr::AT_OSReturnsNotRetained
:
8741 case ParsedAttr::AT_OSReturnsRetained
:
8742 handleXReturnsXRetainedAttr(S
, D
, AL
);
8744 case ParsedAttr::AT_WorkGroupSizeHint
:
8745 handleWorkGroupSize
<WorkGroupSizeHintAttr
>(S
, D
, AL
);
8747 case ParsedAttr::AT_ReqdWorkGroupSize
:
8748 handleWorkGroupSize
<ReqdWorkGroupSizeAttr
>(S
, D
, AL
);
8750 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize
:
8751 handleSubGroupSize(S
, D
, AL
);
8753 case ParsedAttr::AT_VecTypeHint
:
8754 handleVecTypeHint(S
, D
, AL
);
8756 case ParsedAttr::AT_InitPriority
:
8757 handleInitPriorityAttr(S
, D
, AL
);
8759 case ParsedAttr::AT_Packed
:
8760 handlePackedAttr(S
, D
, AL
);
8762 case ParsedAttr::AT_PreferredName
:
8763 handlePreferredName(S
, D
, AL
);
8765 case ParsedAttr::AT_Section
:
8766 handleSectionAttr(S
, D
, AL
);
8768 case ParsedAttr::AT_RandomizeLayout
:
8769 handleRandomizeLayoutAttr(S
, D
, AL
);
8771 case ParsedAttr::AT_NoRandomizeLayout
:
8772 handleNoRandomizeLayoutAttr(S
, D
, AL
);
8774 case ParsedAttr::AT_CodeSeg
:
8775 handleCodeSegAttr(S
, D
, AL
);
8777 case ParsedAttr::AT_Target
:
8778 handleTargetAttr(S
, D
, AL
);
8780 case ParsedAttr::AT_TargetClones
:
8781 handleTargetClonesAttr(S
, D
, AL
);
8783 case ParsedAttr::AT_MinVectorWidth
:
8784 handleMinVectorWidthAttr(S
, D
, AL
);
8786 case ParsedAttr::AT_Unavailable
:
8787 handleAttrWithMessage
<UnavailableAttr
>(S
, D
, AL
);
8789 case ParsedAttr::AT_Assumption
:
8790 handleAssumumptionAttr(S
, D
, AL
);
8792 case ParsedAttr::AT_ObjCDirect
:
8793 handleObjCDirectAttr(S
, D
, AL
);
8795 case ParsedAttr::AT_ObjCDirectMembers
:
8796 handleObjCDirectMembersAttr(S
, D
, AL
);
8797 handleSimpleAttribute
<ObjCDirectMembersAttr
>(S
, D
, AL
);
8799 case ParsedAttr::AT_ObjCExplicitProtocolImpl
:
8800 handleObjCSuppresProtocolAttr(S
, D
, AL
);
8802 case ParsedAttr::AT_Unused
:
8803 handleUnusedAttr(S
, D
, AL
);
8805 case ParsedAttr::AT_Visibility
:
8806 handleVisibilityAttr(S
, D
, AL
, false);
8808 case ParsedAttr::AT_TypeVisibility
:
8809 handleVisibilityAttr(S
, D
, AL
, true);
8811 case ParsedAttr::AT_WarnUnusedResult
:
8812 handleWarnUnusedResult(S
, D
, AL
);
8814 case ParsedAttr::AT_WeakRef
:
8815 handleWeakRefAttr(S
, D
, AL
);
8817 case ParsedAttr::AT_WeakImport
:
8818 handleWeakImportAttr(S
, D
, AL
);
8820 case ParsedAttr::AT_TransparentUnion
:
8821 handleTransparentUnionAttr(S
, D
, AL
);
8823 case ParsedAttr::AT_ObjCMethodFamily
:
8824 handleObjCMethodFamilyAttr(S
, D
, AL
);
8826 case ParsedAttr::AT_ObjCNSObject
:
8827 handleObjCNSObject(S
, D
, AL
);
8829 case ParsedAttr::AT_ObjCIndependentClass
:
8830 handleObjCIndependentClass(S
, D
, AL
);
8832 case ParsedAttr::AT_Blocks
:
8833 handleBlocksAttr(S
, D
, AL
);
8835 case ParsedAttr::AT_Sentinel
:
8836 handleSentinelAttr(S
, D
, AL
);
8838 case ParsedAttr::AT_Cleanup
:
8839 handleCleanupAttr(S
, D
, AL
);
8841 case ParsedAttr::AT_NoDebug
:
8842 handleNoDebugAttr(S
, D
, AL
);
8844 case ParsedAttr::AT_CmseNSEntry
:
8845 handleCmseNSEntryAttr(S
, D
, AL
);
8847 case ParsedAttr::AT_StdCall
:
8848 case ParsedAttr::AT_CDecl
:
8849 case ParsedAttr::AT_FastCall
:
8850 case ParsedAttr::AT_ThisCall
:
8851 case ParsedAttr::AT_Pascal
:
8852 case ParsedAttr::AT_RegCall
:
8853 case ParsedAttr::AT_SwiftCall
:
8854 case ParsedAttr::AT_SwiftAsyncCall
:
8855 case ParsedAttr::AT_VectorCall
:
8856 case ParsedAttr::AT_MSABI
:
8857 case ParsedAttr::AT_SysVABI
:
8858 case ParsedAttr::AT_Pcs
:
8859 case ParsedAttr::AT_IntelOclBicc
:
8860 case ParsedAttr::AT_PreserveMost
:
8861 case ParsedAttr::AT_PreserveAll
:
8862 case ParsedAttr::AT_AArch64VectorPcs
:
8863 case ParsedAttr::AT_AArch64SVEPcs
:
8864 case ParsedAttr::AT_AMDGPUKernelCall
:
8865 handleCallConvAttr(S
, D
, AL
);
8867 case ParsedAttr::AT_Suppress
:
8868 handleSuppressAttr(S
, D
, AL
);
8870 case ParsedAttr::AT_Owner
:
8871 case ParsedAttr::AT_Pointer
:
8872 handleLifetimeCategoryAttr(S
, D
, AL
);
8874 case ParsedAttr::AT_OpenCLAccess
:
8875 handleOpenCLAccessAttr(S
, D
, AL
);
8877 case ParsedAttr::AT_OpenCLNoSVM
:
8878 handleOpenCLNoSVMAttr(S
, D
, AL
);
8880 case ParsedAttr::AT_SwiftContext
:
8881 S
.AddParameterABIAttr(D
, AL
, ParameterABI::SwiftContext
);
8883 case ParsedAttr::AT_SwiftAsyncContext
:
8884 S
.AddParameterABIAttr(D
, AL
, ParameterABI::SwiftAsyncContext
);
8886 case ParsedAttr::AT_SwiftErrorResult
:
8887 S
.AddParameterABIAttr(D
, AL
, ParameterABI::SwiftErrorResult
);
8889 case ParsedAttr::AT_SwiftIndirectResult
:
8890 S
.AddParameterABIAttr(D
, AL
, ParameterABI::SwiftIndirectResult
);
8892 case ParsedAttr::AT_InternalLinkage
:
8893 handleInternalLinkageAttr(S
, D
, AL
);
8895 case ParsedAttr::AT_ZeroCallUsedRegs
:
8896 handleZeroCallUsedRegsAttr(S
, D
, AL
);
8898 case ParsedAttr::AT_FunctionReturnThunks
:
8899 handleFunctionReturnThunksAttr(S
, D
, AL
);
8902 // Microsoft attributes:
8903 case ParsedAttr::AT_LayoutVersion
:
8904 handleLayoutVersion(S
, D
, AL
);
8906 case ParsedAttr::AT_Uuid
:
8907 handleUuidAttr(S
, D
, AL
);
8909 case ParsedAttr::AT_MSInheritance
:
8910 handleMSInheritanceAttr(S
, D
, AL
);
8912 case ParsedAttr::AT_Thread
:
8913 handleDeclspecThreadAttr(S
, D
, AL
);
8917 case ParsedAttr::AT_HLSLNumThreads
:
8918 handleHLSLNumThreadsAttr(S
, D
, AL
);
8920 case ParsedAttr::AT_HLSLSV_GroupIndex
:
8921 handleHLSLSVGroupIndexAttr(S
, D
, AL
);
8923 case ParsedAttr::AT_HLSLShader
:
8924 handleHLSLShaderAttr(S
, D
, AL
);
8927 case ParsedAttr::AT_AbiTag
:
8928 handleAbiTagAttr(S
, D
, AL
);
8930 case ParsedAttr::AT_CFGuard
:
8931 handleCFGuardAttr(S
, D
, AL
);
8934 // Thread safety attributes:
8935 case ParsedAttr::AT_AssertExclusiveLock
:
8936 handleAssertExclusiveLockAttr(S
, D
, AL
);
8938 case ParsedAttr::AT_AssertSharedLock
:
8939 handleAssertSharedLockAttr(S
, D
, AL
);
8941 case ParsedAttr::AT_PtGuardedVar
:
8942 handlePtGuardedVarAttr(S
, D
, AL
);
8944 case ParsedAttr::AT_NoSanitize
:
8945 handleNoSanitizeAttr(S
, D
, AL
);
8947 case ParsedAttr::AT_NoSanitizeSpecific
:
8948 handleNoSanitizeSpecificAttr(S
, D
, AL
);
8950 case ParsedAttr::AT_GuardedBy
:
8951 handleGuardedByAttr(S
, D
, AL
);
8953 case ParsedAttr::AT_PtGuardedBy
:
8954 handlePtGuardedByAttr(S
, D
, AL
);
8956 case ParsedAttr::AT_ExclusiveTrylockFunction
:
8957 handleExclusiveTrylockFunctionAttr(S
, D
, AL
);
8959 case ParsedAttr::AT_LockReturned
:
8960 handleLockReturnedAttr(S
, D
, AL
);
8962 case ParsedAttr::AT_LocksExcluded
:
8963 handleLocksExcludedAttr(S
, D
, AL
);
8965 case ParsedAttr::AT_SharedTrylockFunction
:
8966 handleSharedTrylockFunctionAttr(S
, D
, AL
);
8968 case ParsedAttr::AT_AcquiredBefore
:
8969 handleAcquiredBeforeAttr(S
, D
, AL
);
8971 case ParsedAttr::AT_AcquiredAfter
:
8972 handleAcquiredAfterAttr(S
, D
, AL
);
8975 // Capability analysis attributes.
8976 case ParsedAttr::AT_Capability
:
8977 case ParsedAttr::AT_Lockable
:
8978 handleCapabilityAttr(S
, D
, AL
);
8980 case ParsedAttr::AT_RequiresCapability
:
8981 handleRequiresCapabilityAttr(S
, D
, AL
);
8984 case ParsedAttr::AT_AssertCapability
:
8985 handleAssertCapabilityAttr(S
, D
, AL
);
8987 case ParsedAttr::AT_AcquireCapability
:
8988 handleAcquireCapabilityAttr(S
, D
, AL
);
8990 case ParsedAttr::AT_ReleaseCapability
:
8991 handleReleaseCapabilityAttr(S
, D
, AL
);
8993 case ParsedAttr::AT_TryAcquireCapability
:
8994 handleTryAcquireCapabilityAttr(S
, D
, AL
);
8997 // Consumed analysis attributes.
8998 case ParsedAttr::AT_Consumable
:
8999 handleConsumableAttr(S
, D
, AL
);
9001 case ParsedAttr::AT_CallableWhen
:
9002 handleCallableWhenAttr(S
, D
, AL
);
9004 case ParsedAttr::AT_ParamTypestate
:
9005 handleParamTypestateAttr(S
, D
, AL
);
9007 case ParsedAttr::AT_ReturnTypestate
:
9008 handleReturnTypestateAttr(S
, D
, AL
);
9010 case ParsedAttr::AT_SetTypestate
:
9011 handleSetTypestateAttr(S
, D
, AL
);
9013 case ParsedAttr::AT_TestTypestate
:
9014 handleTestTypestateAttr(S
, D
, AL
);
9017 // Type safety attributes.
9018 case ParsedAttr::AT_ArgumentWithTypeTag
:
9019 handleArgumentWithTypeTagAttr(S
, D
, AL
);
9021 case ParsedAttr::AT_TypeTagForDatatype
:
9022 handleTypeTagForDatatypeAttr(S
, D
, AL
);
9025 // Swift attributes.
9026 case ParsedAttr::AT_SwiftAsyncName
:
9027 handleSwiftAsyncName(S
, D
, AL
);
9029 case ParsedAttr::AT_SwiftAttr
:
9030 handleSwiftAttrAttr(S
, D
, AL
);
9032 case ParsedAttr::AT_SwiftBridge
:
9033 handleSwiftBridge(S
, D
, AL
);
9035 case ParsedAttr::AT_SwiftError
:
9036 handleSwiftError(S
, D
, AL
);
9038 case ParsedAttr::AT_SwiftName
:
9039 handleSwiftName(S
, D
, AL
);
9041 case ParsedAttr::AT_SwiftNewType
:
9042 handleSwiftNewType(S
, D
, AL
);
9044 case ParsedAttr::AT_SwiftAsync
:
9045 handleSwiftAsyncAttr(S
, D
, AL
);
9047 case ParsedAttr::AT_SwiftAsyncError
:
9048 handleSwiftAsyncError(S
, D
, AL
);
9052 case ParsedAttr::AT_XRayLogArgs
:
9053 handleXRayLogArgsAttr(S
, D
, AL
);
9056 case ParsedAttr::AT_PatchableFunctionEntry
:
9057 handlePatchableFunctionEntryAttr(S
, D
, AL
);
9060 case ParsedAttr::AT_AlwaysDestroy
:
9061 case ParsedAttr::AT_NoDestroy
:
9062 handleDestroyAttr(S
, D
, AL
);
9065 case ParsedAttr::AT_Uninitialized
:
9066 handleUninitializedAttr(S
, D
, AL
);
9069 case ParsedAttr::AT_ObjCExternallyRetained
:
9070 handleObjCExternallyRetainedAttr(S
, D
, AL
);
9073 case ParsedAttr::AT_MIGServerRoutine
:
9074 handleMIGServerRoutineAttr(S
, D
, AL
);
9077 case ParsedAttr::AT_MSAllocator
:
9078 handleMSAllocatorAttr(S
, D
, AL
);
9081 case ParsedAttr::AT_ArmBuiltinAlias
:
9082 handleArmBuiltinAliasAttr(S
, D
, AL
);
9085 case ParsedAttr::AT_AcquireHandle
:
9086 handleAcquireHandleAttr(S
, D
, AL
);
9089 case ParsedAttr::AT_ReleaseHandle
:
9090 handleHandleAttr
<ReleaseHandleAttr
>(S
, D
, AL
);
9093 case ParsedAttr::AT_UseHandle
:
9094 handleHandleAttr
<UseHandleAttr
>(S
, D
, AL
);
9097 case ParsedAttr::AT_EnforceTCB
:
9098 handleEnforceTCBAttr
<EnforceTCBAttr
, EnforceTCBLeafAttr
>(S
, D
, AL
);
9101 case ParsedAttr::AT_EnforceTCBLeaf
:
9102 handleEnforceTCBAttr
<EnforceTCBLeafAttr
, EnforceTCBAttr
>(S
, D
, AL
);
9105 case ParsedAttr::AT_BuiltinAlias
:
9106 handleBuiltinAliasAttr(S
, D
, AL
);
9109 case ParsedAttr::AT_UsingIfExists
:
9110 handleSimpleAttribute
<UsingIfExistsAttr
>(S
, D
, AL
);
9115 /// ProcessDeclAttributeList - Apply all the decl attributes in the specified
9116 /// attribute list to the specified decl, ignoring any type attributes.
9117 void Sema::ProcessDeclAttributeList(
9118 Scope
*S
, Decl
*D
, const ParsedAttributesView
&AttrList
,
9119 const ProcessDeclAttributeOptions
&Options
) {
9120 if (AttrList
.empty())
9123 for (const ParsedAttr
&AL
: AttrList
)
9124 ProcessDeclAttribute(*this, S
, D
, AL
, Options
);
9126 // FIXME: We should be able to handle these cases in TableGen.
9128 // static int a9 __attribute__((weakref));
9129 // but that looks really pointless. We reject it.
9130 if (D
->hasAttr
<WeakRefAttr
>() && !D
->hasAttr
<AliasAttr
>()) {
9131 Diag(AttrList
.begin()->getLoc(), diag::err_attribute_weakref_without_alias
)
9132 << cast
<NamedDecl
>(D
);
9133 D
->dropAttr
<WeakRefAttr
>();
9137 // FIXME: We should be able to handle this in TableGen as well. It would be
9138 // good to have a way to specify "these attributes must appear as a group",
9139 // for these. Additionally, it would be good to have a way to specify "these
9140 // attribute must never appear as a group" for attributes like cold and hot.
9141 if (!D
->hasAttr
<OpenCLKernelAttr
>()) {
9142 // These attributes cannot be applied to a non-kernel function.
9143 if (const auto *A
= D
->getAttr
<ReqdWorkGroupSizeAttr
>()) {
9144 // FIXME: This emits a different error message than
9145 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
9146 Diag(D
->getLocation(), diag::err_opencl_kernel_attr
) << A
;
9147 D
->setInvalidDecl();
9148 } else if (const auto *A
= D
->getAttr
<WorkGroupSizeHintAttr
>()) {
9149 Diag(D
->getLocation(), diag::err_opencl_kernel_attr
) << A
;
9150 D
->setInvalidDecl();
9151 } else if (const auto *A
= D
->getAttr
<VecTypeHintAttr
>()) {
9152 Diag(D
->getLocation(), diag::err_opencl_kernel_attr
) << A
;
9153 D
->setInvalidDecl();
9154 } else if (const auto *A
= D
->getAttr
<OpenCLIntelReqdSubGroupSizeAttr
>()) {
9155 Diag(D
->getLocation(), diag::err_opencl_kernel_attr
) << A
;
9156 D
->setInvalidDecl();
9157 } else if (!D
->hasAttr
<CUDAGlobalAttr
>()) {
9158 if (const auto *A
= D
->getAttr
<AMDGPUFlatWorkGroupSizeAttr
>()) {
9159 Diag(D
->getLocation(), diag::err_attribute_wrong_decl_type
)
9160 << A
<< ExpectedKernelFunction
;
9161 D
->setInvalidDecl();
9162 } else if (const auto *A
= D
->getAttr
<AMDGPUWavesPerEUAttr
>()) {
9163 Diag(D
->getLocation(), diag::err_attribute_wrong_decl_type
)
9164 << A
<< ExpectedKernelFunction
;
9165 D
->setInvalidDecl();
9166 } else if (const auto *A
= D
->getAttr
<AMDGPUNumSGPRAttr
>()) {
9167 Diag(D
->getLocation(), diag::err_attribute_wrong_decl_type
)
9168 << A
<< ExpectedKernelFunction
;
9169 D
->setInvalidDecl();
9170 } else if (const auto *A
= D
->getAttr
<AMDGPUNumVGPRAttr
>()) {
9171 Diag(D
->getLocation(), diag::err_attribute_wrong_decl_type
)
9172 << A
<< ExpectedKernelFunction
;
9173 D
->setInvalidDecl();
9178 // Do this check after processing D's attributes because the attribute
9179 // objc_method_family can change whether the given method is in the init
9180 // family, and it can be applied after objc_designated_initializer. This is a
9181 // bit of a hack, but we need it to be compatible with versions of clang that
9182 // processed the attribute list in the wrong order.
9183 if (D
->hasAttr
<ObjCDesignatedInitializerAttr
>() &&
9184 cast
<ObjCMethodDecl
>(D
)->getMethodFamily() != OMF_init
) {
9185 Diag(D
->getLocation(), diag::err_designated_init_attr_non_init
);
9186 D
->dropAttr
<ObjCDesignatedInitializerAttr
>();
9190 // Helper for delayed processing TransparentUnion or BPFPreserveAccessIndexAttr
9192 void Sema::ProcessDeclAttributeDelayed(Decl
*D
,
9193 const ParsedAttributesView
&AttrList
) {
9194 for (const ParsedAttr
&AL
: AttrList
)
9195 if (AL
.getKind() == ParsedAttr::AT_TransparentUnion
) {
9196 handleTransparentUnionAttr(*this, D
, AL
);
9200 // For BPFPreserveAccessIndexAttr, we want to populate the attributes
9201 // to fields and inner records as well.
9202 if (D
&& D
->hasAttr
<BPFPreserveAccessIndexAttr
>())
9203 handleBPFPreserveAIRecord(*this, cast
<RecordDecl
>(D
));
9206 // Annotation attributes are the only attributes allowed after an access
9208 bool Sema::ProcessAccessDeclAttributeList(
9209 AccessSpecDecl
*ASDecl
, const ParsedAttributesView
&AttrList
) {
9210 for (const ParsedAttr
&AL
: AttrList
) {
9211 if (AL
.getKind() == ParsedAttr::AT_Annotate
) {
9212 ProcessDeclAttribute(*this, nullptr, ASDecl
, AL
,
9213 ProcessDeclAttributeOptions());
9215 Diag(AL
.getLoc(), diag::err_only_annotate_after_access_spec
);
9222 /// checkUnusedDeclAttributes - Check a list of attributes to see if it
9223 /// contains any decl attributes that we should warn about.
9224 static void checkUnusedDeclAttributes(Sema
&S
, const ParsedAttributesView
&A
) {
9225 for (const ParsedAttr
&AL
: A
) {
9226 // Only warn if the attribute is an unignored, non-type attribute.
9227 if (AL
.isUsedAsTypeAttr() || AL
.isInvalid())
9229 if (AL
.getKind() == ParsedAttr::IgnoredAttribute
)
9232 if (AL
.getKind() == ParsedAttr::UnknownAttribute
) {
9233 S
.Diag(AL
.getLoc(), diag::warn_unknown_attribute_ignored
)
9234 << AL
<< AL
.getRange();
9236 S
.Diag(AL
.getLoc(), diag::warn_attribute_not_on_decl
) << AL
9242 /// checkUnusedDeclAttributes - Given a declarator which is not being
9243 /// used to build a declaration, complain about any decl attributes
9244 /// which might be lying around on it.
9245 void Sema::checkUnusedDeclAttributes(Declarator
&D
) {
9246 ::checkUnusedDeclAttributes(*this, D
.getDeclarationAttributes());
9247 ::checkUnusedDeclAttributes(*this, D
.getDeclSpec().getAttributes());
9248 ::checkUnusedDeclAttributes(*this, D
.getAttributes());
9249 for (unsigned i
= 0, e
= D
.getNumTypeObjects(); i
!= e
; ++i
)
9250 ::checkUnusedDeclAttributes(*this, D
.getTypeObject(i
).getAttrs());
9253 /// DeclClonePragmaWeak - clone existing decl (maybe definition),
9254 /// \#pragma weak needs a non-definition decl and source may not have one.
9255 NamedDecl
*Sema::DeclClonePragmaWeak(NamedDecl
*ND
, const IdentifierInfo
*II
,
9256 SourceLocation Loc
) {
9257 assert(isa
<FunctionDecl
>(ND
) || isa
<VarDecl
>(ND
));
9258 NamedDecl
*NewD
= nullptr;
9259 if (auto *FD
= dyn_cast
<FunctionDecl
>(ND
)) {
9260 FunctionDecl
*NewFD
;
9261 // FIXME: Missing call to CheckFunctionDeclaration().
9263 // FIXME: Is the qualifier info correct?
9264 // FIXME: Is the DeclContext correct?
9265 NewFD
= FunctionDecl::Create(
9266 FD
->getASTContext(), FD
->getDeclContext(), Loc
, Loc
,
9267 DeclarationName(II
), FD
->getType(), FD
->getTypeSourceInfo(), SC_None
,
9268 getCurFPFeatures().isFPConstrained(), false /*isInlineSpecified*/,
9269 FD
->hasPrototype(), ConstexprSpecKind::Unspecified
,
9270 FD
->getTrailingRequiresClause());
9273 if (FD
->getQualifier())
9274 NewFD
->setQualifierInfo(FD
->getQualifierLoc());
9276 // Fake up parameter variables; they are declared as if this were
9278 QualType FDTy
= FD
->getType();
9279 if (const auto *FT
= FDTy
->getAs
<FunctionProtoType
>()) {
9280 SmallVector
<ParmVarDecl
*, 16> Params
;
9281 for (const auto &AI
: FT
->param_types()) {
9282 ParmVarDecl
*Param
= BuildParmVarDeclForTypedef(NewFD
, Loc
, AI
);
9283 Param
->setScopeInfo(0, Params
.size());
9284 Params
.push_back(Param
);
9286 NewFD
->setParams(Params
);
9288 } else if (auto *VD
= dyn_cast
<VarDecl
>(ND
)) {
9289 NewD
= VarDecl::Create(VD
->getASTContext(), VD
->getDeclContext(),
9290 VD
->getInnerLocStart(), VD
->getLocation(), II
,
9291 VD
->getType(), VD
->getTypeSourceInfo(),
9292 VD
->getStorageClass());
9293 if (VD
->getQualifier())
9294 cast
<VarDecl
>(NewD
)->setQualifierInfo(VD
->getQualifierLoc());
9299 /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak
9300 /// applied to it, possibly with an alias.
9301 void Sema::DeclApplyPragmaWeak(Scope
*S
, NamedDecl
*ND
, const WeakInfo
&W
) {
9302 if (W
.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
9303 IdentifierInfo
*NDId
= ND
->getIdentifier();
9304 NamedDecl
*NewD
= DeclClonePragmaWeak(ND
, W
.getAlias(), W
.getLocation());
9306 AliasAttr::CreateImplicit(Context
, NDId
->getName(), W
.getLocation()));
9307 NewD
->addAttr(WeakAttr::CreateImplicit(Context
, W
.getLocation(),
9308 AttributeCommonInfo::AS_Pragma
));
9309 WeakTopLevelDecl
.push_back(NewD
);
9310 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
9311 // to insert Decl at TU scope, sorry.
9312 DeclContext
*SavedContext
= CurContext
;
9313 CurContext
= Context
.getTranslationUnitDecl();
9314 NewD
->setDeclContext(CurContext
);
9315 NewD
->setLexicalDeclContext(CurContext
);
9316 PushOnScopeChains(NewD
, S
);
9317 CurContext
= SavedContext
;
9318 } else { // just add weak to existing
9319 ND
->addAttr(WeakAttr::CreateImplicit(Context
, W
.getLocation(),
9320 AttributeCommonInfo::AS_Pragma
));
9324 void Sema::ProcessPragmaWeak(Scope
*S
, Decl
*D
) {
9325 // It's valid to "forward-declare" #pragma weak, in which case we
9327 LoadExternalWeakUndeclaredIdentifiers();
9328 if (WeakUndeclaredIdentifiers
.empty())
9330 NamedDecl
*ND
= nullptr;
9331 if (auto *VD
= dyn_cast
<VarDecl
>(D
))
9332 if (VD
->isExternC())
9334 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
))
9335 if (FD
->isExternC())
9339 if (IdentifierInfo
*Id
= ND
->getIdentifier()) {
9340 auto I
= WeakUndeclaredIdentifiers
.find(Id
);
9341 if (I
!= WeakUndeclaredIdentifiers
.end()) {
9342 auto &WeakInfos
= I
->second
;
9343 for (const auto &W
: WeakInfos
)
9344 DeclApplyPragmaWeak(S
, ND
, W
);
9345 std::remove_reference_t
<decltype(WeakInfos
)> EmptyWeakInfos
;
9346 WeakInfos
.swap(EmptyWeakInfos
);
9351 /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
9352 /// it, apply them to D. This is a bit tricky because PD can have attributes
9353 /// specified in many different places, and we need to find and apply them all.
9354 void Sema::ProcessDeclAttributes(Scope
*S
, Decl
*D
, const Declarator
&PD
) {
9355 // Ordering of attributes can be important, so we take care to process
9356 // attributes in the order in which they appeared in the source code.
9358 // First, process attributes that appeared on the declaration itself (but
9359 // only if they don't have the legacy behavior of "sliding" to the DeclSepc).
9360 ParsedAttributesView NonSlidingAttrs
;
9361 for (ParsedAttr
&AL
: PD
.getDeclarationAttributes()) {
9362 if (AL
.slidesFromDeclToDeclSpecLegacyBehavior()) {
9363 // Skip processing the attribute, but do check if it appertains to the
9364 // declaration. This is needed for the `MatrixType` attribute, which,
9365 // despite being a type attribute, defines a `SubjectList` that only
9366 // allows it to be used on typedef declarations.
9367 AL
.diagnoseAppertainsTo(*this, D
);
9369 NonSlidingAttrs
.addAtEnd(&AL
);
9372 ProcessDeclAttributeList(S
, D
, NonSlidingAttrs
);
9374 // Apply decl attributes from the DeclSpec if present.
9375 if (!PD
.getDeclSpec().getAttributes().empty()) {
9376 ProcessDeclAttributeList(S
, D
, PD
.getDeclSpec().getAttributes(),
9377 ProcessDeclAttributeOptions()
9378 .WithIncludeCXX11Attributes(false)
9379 .WithIgnoreTypeAttributes(true));
9382 // Walk the declarator structure, applying decl attributes that were in a type
9383 // position to the decl itself. This handles cases like:
9384 // int *__attr__(x)** D;
9385 // when X is a decl attribute.
9386 for (unsigned i
= 0, e
= PD
.getNumTypeObjects(); i
!= e
; ++i
) {
9387 ProcessDeclAttributeList(S
, D
, PD
.getTypeObject(i
).getAttrs(),
9388 ProcessDeclAttributeOptions()
9389 .WithIncludeCXX11Attributes(false)
9390 .WithIgnoreTypeAttributes(true));
9393 // Finally, apply any attributes on the decl itself.
9394 ProcessDeclAttributeList(S
, D
, PD
.getAttributes());
9396 // Apply additional attributes specified by '#pragma clang attribute'.
9397 AddPragmaAttributes(S
, D
);
9400 /// Is the given declaration allowed to use a forbidden type?
9401 /// If so, it'll still be annotated with an attribute that makes it
9402 /// illegal to actually use.
9403 static bool isForbiddenTypeAllowed(Sema
&S
, Decl
*D
,
9404 const DelayedDiagnostic
&diag
,
9405 UnavailableAttr::ImplicitReason
&reason
) {
9406 // Private ivars are always okay. Unfortunately, people don't
9407 // always properly make their ivars private, even in system headers.
9408 // Plus we need to make fields okay, too.
9409 if (!isa
<FieldDecl
>(D
) && !isa
<ObjCPropertyDecl
>(D
) &&
9410 !isa
<FunctionDecl
>(D
))
9413 // Silently accept unsupported uses of __weak in both user and system
9414 // declarations when it's been disabled, for ease of integration with
9415 // -fno-objc-arc files. We do have to take some care against attempts
9416 // to define such things; for now, we've only done that for ivars
9418 if ((isa
<ObjCIvarDecl
>(D
) || isa
<ObjCPropertyDecl
>(D
))) {
9419 if (diag
.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled
||
9420 diag
.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime
) {
9421 reason
= UnavailableAttr::IR_ForbiddenWeak
;
9426 // Allow all sorts of things in system headers.
9427 if (S
.Context
.getSourceManager().isInSystemHeader(D
->getLocation())) {
9428 // Currently, all the failures dealt with this way are due to ARC
9430 reason
= UnavailableAttr::IR_ARCForbiddenType
;
9437 /// Handle a delayed forbidden-type diagnostic.
9438 static void handleDelayedForbiddenType(Sema
&S
, DelayedDiagnostic
&DD
,
9440 auto Reason
= UnavailableAttr::IR_None
;
9441 if (D
&& isForbiddenTypeAllowed(S
, D
, DD
, Reason
)) {
9442 assert(Reason
&& "didn't set reason?");
9443 D
->addAttr(UnavailableAttr::CreateImplicit(S
.Context
, "", Reason
, DD
.Loc
));
9446 if (S
.getLangOpts().ObjCAutoRefCount
)
9447 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
9448 // FIXME: we may want to suppress diagnostics for all
9449 // kind of forbidden type messages on unavailable functions.
9450 if (FD
->hasAttr
<UnavailableAttr
>() &&
9451 DD
.getForbiddenTypeDiagnostic() ==
9452 diag::err_arc_array_param_no_ownership
) {
9453 DD
.Triggered
= true;
9458 S
.Diag(DD
.Loc
, DD
.getForbiddenTypeDiagnostic())
9459 << DD
.getForbiddenTypeOperand() << DD
.getForbiddenTypeArgument();
9460 DD
.Triggered
= true;
9464 void Sema::PopParsingDeclaration(ParsingDeclState state
, Decl
*decl
) {
9465 assert(DelayedDiagnostics
.getCurrentPool());
9466 DelayedDiagnosticPool
&poppedPool
= *DelayedDiagnostics
.getCurrentPool();
9467 DelayedDiagnostics
.popWithoutEmitting(state
);
9469 // When delaying diagnostics to run in the context of a parsed
9470 // declaration, we only want to actually emit anything if parsing
9474 // We emit all the active diagnostics in this pool or any of its
9475 // parents. In general, we'll get one pool for the decl spec
9476 // and a child pool for each declarator; in a decl group like:
9477 // deprecated_typedef foo, *bar, baz();
9478 // only the declarator pops will be passed decls. This is correct;
9479 // we really do need to consider delayed diagnostics from the decl spec
9480 // for each of the different declarations.
9481 const DelayedDiagnosticPool
*pool
= &poppedPool
;
9483 bool AnyAccessFailures
= false;
9484 for (DelayedDiagnosticPool::pool_iterator
9485 i
= pool
->pool_begin(), e
= pool
->pool_end(); i
!= e
; ++i
) {
9486 // This const_cast is a bit lame. Really, Triggered should be mutable.
9487 DelayedDiagnostic
&diag
= const_cast<DelayedDiagnostic
&>(*i
);
9491 switch (diag
.Kind
) {
9492 case DelayedDiagnostic::Availability
:
9493 // Don't bother giving deprecation/unavailable diagnostics if
9494 // the decl is invalid.
9495 if (!decl
->isInvalidDecl())
9496 handleDelayedAvailabilityCheck(diag
, decl
);
9499 case DelayedDiagnostic::Access
:
9500 // Only produce one access control diagnostic for a structured binding
9501 // declaration: we don't need to tell the user that all the fields are
9502 // inaccessible one at a time.
9503 if (AnyAccessFailures
&& isa
<DecompositionDecl
>(decl
))
9505 HandleDelayedAccessCheck(diag
, decl
);
9507 AnyAccessFailures
= true;
9510 case DelayedDiagnostic::ForbiddenType
:
9511 handleDelayedForbiddenType(*this, diag
, decl
);
9515 } while ((pool
= pool
->getParent()));
9518 /// Given a set of delayed diagnostics, re-emit them as if they had
9519 /// been delayed in the current context instead of in the given pool.
9520 /// Essentially, this just moves them to the current pool.
9521 void Sema::redelayDiagnostics(DelayedDiagnosticPool
&pool
) {
9522 DelayedDiagnosticPool
*curPool
= DelayedDiagnostics
.getCurrentPool();
9523 assert(curPool
&& "re-emitting in undelayed context not supported");
9524 curPool
->steal(pool
);