[docs] Fix build-docs.sh
[llvm-project.git] / clang / lib / Sema / SemaDeclAttr.cpp
blob128eefbf7197bd6546323b8ba3ae7c6364a3a095
1 //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file 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;
51 using namespace sema;
53 namespace AttributeLangSupport {
54 enum LANG {
56 Cpp,
57 ObjC
59 } // end namespace AttributeLangSupport
61 //===----------------------------------------------------------------------===//
62 // Helper functions
63 //===----------------------------------------------------------------------===//
65 /// isFunctionOrMethod - Return true if the given decl has function
66 /// type (function or function-typed variable) or an Objective-C
67 /// method.
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,
107 unsigned Idx) {
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);
114 return nullptr;
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();
157 return false;
160 static inline bool isNSStringType(QualType T, ASTContext &Ctx,
161 bool AllowNSAttributedString = false) {
162 const auto *PT = T->getAs<ObjCObjectPointerType>();
163 if (!PT)
164 return false;
166 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
167 if (!Cls)
168 return false;
170 IdentifierInfo* ClsName = Cls->getIdentifier();
172 if (AllowNSAttributedString &&
173 ClsName == &Ctx.Idents.get("NSAttributedString"))
174 return true;
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>();
182 if (!PT)
183 return false;
185 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
186 if (!RT)
187 return false;
189 const RecordDecl *RD = RT->getDecl();
190 if (RD->getTagKind() != TTK_Struct)
191 return false;
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))) {
222 if (Idx != UINT_MAX)
223 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
224 << &AI << Idx << AANT_ArgumentIntegerConstant
225 << Expr->getSourceRange();
226 else
227 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
228 << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
229 return false;
232 if (!I->isIntN(32)) {
233 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
234 << toString(*I, 10, false) << 32 << /* Unsigned */ 1;
235 return false;
238 if (StrictlyUnsigned && I->isSigned() && I->isNegative()) {
239 S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
240 << &AI << /*non-negative*/ 1;
241 return false;
244 Val = (uint32_t)I->getZExtValue();
245 return true;
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) {
254 uint32_t UVal;
255 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
256 return false;
258 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
259 llvm::APSInt I(32); // for toString
260 I = UVal;
261 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
262 << toString(I, 10, false) << 32 << /* Unsigned */ 0;
263 return false;
266 Val = UVal;
267 return true;
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);
277 return true;
279 return false;
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
286 << A;
287 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
288 return true;
290 return false;
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);
308 unsigned NumParams =
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();
317 return false;
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();
324 return false;
326 if (HasImplicitThisParam && !CanIndexImplicitThis) {
327 if (IdxSource == 1) {
328 S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
329 << &AI << IdxExpr->getSourceRange();
330 return false;
334 Idx = ParamIdx(IdxSource, D);
335 return true;
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
340 /// and return true.
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());
345 if (ArgLocation)
346 *ArgLocation = E->getBeginLoc();
348 if (!Literal || !Literal->isOrdinary()) {
349 Diag(E->getBeginLoc(), diag::err_attribute_argument_type)
350 << CI << AANT_ArgumentString;
351 return false;
354 Str = Literal->getString();
355 return true;
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
361 /// literal.
362 bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
363 StringRef &Str,
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();
373 if (ArgLocation)
374 *ArgLocation = Loc->Loc;
375 return true;
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) {
394 return 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) {
414 if (!PassesCheck) {
415 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
416 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
417 return;
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)
443 return true;
445 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
446 if (!CXXRecord)
447 return false;
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)
459 return true;
461 return false;
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())
472 return true;
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())
479 return true;
481 if (threadSafetyCheckIsSmartPointer(S, RT))
482 return true;
485 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
486 return false;
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>())
493 return RT;
495 // Now check if we point to record type.
496 if (const auto *PT = QT->getAs<PointerType>())
497 return PT->getPointeeType()->getAs<RecordType>();
499 return nullptr;
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>())
506 return true;
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>();
513 return true;
515 return false;
518 static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
519 const RecordType *RT = getRecordType(Ty);
521 if (!RT)
522 return false;
524 // Don't check for the capability if the class hasn't been defined yet.
525 if (RT->isIncompleteType())
526 return true;
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))
531 return true;
533 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
536 static bool checkTypedefTypeForCapability(QualType Ty) {
537 const auto *TD = Ty->getAs<TypedefType>();
538 if (!TD)
539 return false;
541 TypedefNameDecl *TN = TD->getDecl();
542 if (!TN)
543 return false;
545 return TN->hasAttr<CapabilityAttr>();
548 static bool typeHasCapability(Sema &S, QualType Ty) {
549 if (checkTypedefTypeForCapability(Ty))
550 return true;
552 if (checkRecordTypeForCapability(S, Ty))
553 return true;
555 return false;
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());
572 return false;
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());
577 return false;
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
587 /// parameter list.
588 static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
589 const ParsedAttr &AL,
590 SmallVectorImpl<Expr *> &Args,
591 unsigned Sidx = 0,
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))
603 S.Diag(AL.getLoc(),
604 diag::warn_thread_attribute_not_on_capability_member)
605 << AL << MD->getParent();
606 } else {
607 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
608 << AL;
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);
618 continue;
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);
627 continue;
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);
634 continue;
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);
654 if(FD && IL) {
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) {
660 S.Diag(AL.getLoc(),
661 diag::err_attribute_argument_out_of_bounds_extra_info)
662 << AL << Idx + 1 << NumParams;
663 continue;
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)
675 << AL << ArgTy;
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))
687 return;
689 D->addAttr(::new (S.Context) PtGuardedVarAttr(S.Context, AL));
692 static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
693 Expr *&Arg) {
694 SmallVector<Expr *, 1> Args;
695 // check that all arguments are lockable objects
696 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
697 unsigned Size = Args.size();
698 if (Size != 1)
699 return false;
701 Arg = Args[0];
703 return true;
706 static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
707 Expr *Arg = nullptr;
708 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
709 return;
711 D->addAttr(::new (S.Context) GuardedByAttr(S.Context, AL, Arg));
714 static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
715 Expr *Arg = nullptr;
716 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
717 return;
719 if (!threadSafetyCheckIsPointer(S, D, AL))
720 return;
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))
728 return false;
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;
734 return false;
737 // Check that all arguments are lockable objects.
738 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
739 if (Args.empty())
740 return false;
742 return true;
745 static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
746 SmallVector<Expr *, 1> Args;
747 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
748 return;
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))
758 return;
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);
771 return true;
774 static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
775 SmallVector<Expr *, 1> Args;
776 if (!checkLockFunAttrCommon(S, D, AL, Args))
777 return;
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))
789 return;
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
799 /// false.
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);
807 ParamIdx Idx;
808 if (!checkFunctionOrMethodParameterIndex(S, D, AI, AttrArgNo + 1, AttrArg,
809 Idx))
810 return false;
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());
817 return false;
819 return true;
822 static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
823 if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 2))
824 return;
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;
831 return;
834 const Expr *SizeExpr = AL.getArgAsExpr(0);
835 int SizeArgNoVal;
836 // Parameter indices are 1-indexed, hence Index=1
837 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1))
838 return;
839 if (!checkParamIsIntegerType(S, D, AL, /*AttrArgNo=*/0))
840 return;
841 ParamIdx SizeArgNo(SizeArgNoVal, D);
843 ParamIdx NumberArgNo;
844 if (AL.getNumArgs() == 2) {
845 const Expr *NumberExpr = AL.getArgAsExpr(1);
846 int Val;
847 // Parameter indices are 1-based, hence Index=2
848 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2))
849 return;
850 if (!checkParamIsIntegerType(S, D, AL, /*AttrArgNo=*/1))
851 return;
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))
862 return false;
864 if (!isIntOrBool(AL.getArgAsExpr(0))) {
865 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
866 << AL << 1 << AANT_ArgumentIntOrBool;
867 return false;
870 // check that all arguments are lockable objects
871 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
873 return true;
876 static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
877 const ParsedAttr &AL) {
878 SmallVector<Expr*, 2> Args;
879 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
880 return;
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))
890 return;
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();
901 if (Size == 0)
902 return;
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))
909 return;
911 // check that all arguments are lockable objects
912 SmallVector<Expr*, 1> Args;
913 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
914 unsigned Size = Args.size();
915 if (Size == 0)
916 return;
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())
929 return false;
930 Cond = Converted.get();
933 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
934 return false;
936 if (Msg.empty())
937 Msg = "<no message provided>";
939 SmallVector<PartialDiagnosticAt, 8> Diags;
940 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
941 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
942 Diags)) {
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);
946 return false;
948 return true;
951 static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
952 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
954 Expr *Cond;
955 StringRef Msg;
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))
963 return;
964 if (ErrorAttr *EA = S.mergeErrorAttr(D, AL, NewUserDiagnostic))
965 D->addAttr(EA);
968 namespace {
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> {
973 #ifndef NDEBUG
974 const CXXRecordDecl *ClassType;
975 #endif
976 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
977 bool Result;
979 public:
980 ArgumentDependenceChecker(const FunctionDecl *FD) {
981 #ifndef NDEBUG
982 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
983 ClassType = MD->getParent();
984 else
985 ClassType = nullptr;
986 #endif
987 Parms.insert(FD->param_begin(), FD->param_end());
990 bool referencesArgs(Expr *E) {
991 Result = false;
992 TraverseStmt(E);
993 return Result;
996 bool VisitCXXThisExpr(CXXThisExpr *E) {
997 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
998 "`this` doesn't refer to the enclosing class?");
999 Result = true;
1000 return false;
1003 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1004 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1005 if (Parms.count(PVD)) {
1006 Result = true;
1007 return false;
1009 return true;
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;
1021 return;
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;
1030 }();
1032 S.Diag(Loc, diag::err_attribute_argument_n_type) << AL << Index << T;
1035 FunctionDecl *AttrFD = [&]() -> FunctionDecl * {
1036 if (!AL.isArgExpr(0))
1037 return nullptr;
1038 auto *F = dyn_cast_or_null<DeclRefExpr>(AL.getArgAsExpr(0));
1039 if (!F)
1040 return nullptr;
1041 return dyn_cast_or_null<FunctionDecl>(F->getFoundDecl());
1042 }();
1044 if (!AttrFD || !AttrFD->getBuiltinID(true)) {
1045 DiagnoseType(1, AANT_ArgumentBuiltinFunction);
1046 return;
1049 if (AttrFD->getNumParams() != AL.getNumArgs() - 1) {
1050 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments_for)
1051 << AL << AttrFD << AttrFD->getNumParams();
1052 return;
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);
1060 return;
1063 const Expr *IndexExpr = AL.getArgAsExpr(I);
1064 uint32_t Index;
1066 if (!checkUInt32Argument(S, AL, IndexExpr, Index, I + 1, false))
1067 return;
1069 if (Index > DeclFD->getNumParams()) {
1070 S.Diag(AL.getLoc(), diag::err_attribute_bounds_for_function)
1071 << AL << Index << DeclFD << DeclFD->getNumParams();
1072 return;
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;
1082 return;
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);
1095 Expr *Cond;
1096 StringRef Msg;
1097 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
1098 return;
1100 StringRef DiagTypeStr;
1101 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
1102 return;
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);
1108 return;
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)
1126 HasWildcard = true;
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);
1138 else
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))
1143 return;
1145 if (Builtin::Context::isBuiltinFunc(BuiltinName))
1146 AddBuiltinName(BuiltinName);
1147 else
1148 S.Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name)
1149 << BuiltinName << AL;
1152 // Repeating the same attribute is fine.
1153 llvm::sort(Names);
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)
1160 << AL;
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;
1171 return;
1174 Expr *E = AL.getArgAsExpr(0);
1175 uint32_t Type;
1176 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
1177 return;
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].
1182 if (Type > 3) {
1183 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
1184 << AL << 0 << 3 << E->getSourceRange();
1185 return;
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;
1194 return;
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(),
1206 DefaultState)) {
1207 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1208 << IL->Ident;
1209 return;
1211 } else {
1212 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1213 << AL << AANT_ArgumentIdentifier;
1214 return;
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;
1228 return false;
1232 return true;
1235 static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1236 if (!AL.checkAtLeastNumArgs(S, 1))
1237 return;
1239 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
1240 return;
1242 SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1243 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
1244 CallableWhenAttr::ConsumedState CallableState;
1246 StringRef StateString;
1247 SourceLocation Loc;
1248 if (AL.isArgIdent(ArgIndex)) {
1249 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
1250 StateString = Ident->Ident->getName();
1251 Loc = Ident->Loc;
1252 } else {
1253 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
1254 return;
1257 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
1258 CallableState)) {
1259 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
1260 return;
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,
1278 ParamState)) {
1279 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1280 << AL << StateString;
1281 return;
1283 } else {
1284 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1285 << AL << AANT_ArgumentIdentifier;
1286 return;
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();
1298 // return;
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(),
1310 ReturnState)) {
1311 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1312 << IL->Ident;
1313 return;
1315 } else {
1316 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1317 << AL << AANT_ArgumentIdentifier;
1318 return;
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();
1333 //} else {
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();
1343 // return;
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))
1351 return;
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
1359 << Param;
1360 return;
1362 } else {
1363 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1364 << AL << AANT_ArgumentIdentifier;
1365 return;
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))
1373 return;
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
1381 << Param;
1382 return;
1384 } else {
1385 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1386 << AL << AANT_ArgumentIdentifier;
1387 return;
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() &&
1404 FD->isBitField() &&
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();
1412 else
1413 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1414 } else {
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));
1422 } else
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);
1434 if (!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>();
1446 if (TST)
1447 Template = TST->getTemplateName().getAsTemplateDecl();
1450 if (Template && declaresSameEntity(Template, CTD)) {
1451 D->addAttr(::new (S.Context) PreferredNameAttr(S.Context, AL, TSI));
1452 return;
1456 S.Diag(AL.getLoc(), diag::err_attribute_preferred_name_arg_invalid)
1457 << T << CTD;
1458 if (const auto *TT = T->getAs<TypedefType>())
1459 S.Diag(TT->getDecl()->getLocation(), diag::note_entity_declared_at)
1460 << TT->getDecl();
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;
1471 return false;
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;
1478 return false;
1481 else {
1482 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
1483 return false;
1486 return true;
1489 static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
1490 if (!checkIBOutletCommon(S, D, AL))
1491 return;
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;
1501 return;
1504 if (!checkIBOutletCommon(S, D, AL))
1505 return;
1507 ParsedType PT;
1509 if (AL.hasParsedType())
1510 PT = AL.getTypeArg();
1511 else {
1512 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
1513 S.getScopeForContext(D->getDeclContext()->getParent()));
1514 if (!PT) {
1515 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1516 return;
1520 TypeSourceInfo *QTLoc = nullptr;
1521 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1522 if (!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()) {
1530 S.Diag(AL.getLoc(),
1531 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1532 : diag::err_iboutletcollection_type) << QT;
1533 return;
1536 D->addAttr(::new (S.Context) IBOutletCollectionAttr(S.Context, AL, QTLoc));
1539 bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1540 if (RefOkay) {
1541 if (T->isReferenceType())
1542 return true;
1543 } else {
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())
1555 return true;
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)) {
1568 if (isReturnValue)
1569 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1570 << AL << AttrParmRange << TypeRange;
1571 else
1572 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1573 << AL << AttrParmRange << TypeRange << 0;
1574 return false;
1576 return true;
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);
1583 ParamIdx Idx;
1584 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
1585 return;
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())))
1593 continue;
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
1601 // instantiation.
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))
1609 AnyPointers = true;
1612 if (!AnyPointers)
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);
1627 } else {
1628 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1629 << D->getSourceRange();
1631 return;
1634 // Is the argument a pointer type?
1635 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
1636 D->getSourceRange()))
1637 return;
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))
1647 return;
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())
1654 return;
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;
1661 return;
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,
1678 Expr *OE) {
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;
1688 return;
1691 if (!E->isValueDependent()) {
1692 Optional<llvm::APSInt> I = llvm::APSInt(64);
1693 if (!(I = E->getIntegerConstantExpr(Context))) {
1694 if (OE)
1695 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1696 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1697 << E->getSourceRange();
1698 else
1699 Diag(AttrLoc, diag::err_attribute_argument_type)
1700 << &TmpAttr << AANT_ArgumentIntegerConstant
1701 << E->getSourceRange();
1702 return;
1705 if (!I->isPowerOf2()) {
1706 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1707 << E->getSourceRange();
1708 return;
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();
1720 return;
1723 D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE));
1726 void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
1727 Expr *ParamExpr) {
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);
1737 return;
1740 ParamIdx Idx;
1741 const auto *FuncDecl = cast<FunctionDecl>(D);
1742 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
1743 /*AttrArgNum=*/1, ParamExpr, Idx))
1744 return;
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)
1750 << &TmpAttr
1751 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
1752 return;
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))
1762 return;
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;
1778 else
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.
1784 StringRef Str;
1785 SourceLocation AttrStrLoc;
1786 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &AttrStrLoc))
1787 return;
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);
1800 return true;
1802 return false;
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;
1817 return;
1820 // Figure out our Kind.
1821 OwnershipAttr::OwnershipKind K =
1822 OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind();
1824 // Check arguments.
1825 switch (K) {
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;
1830 return;
1832 break;
1833 case OwnershipAttr::Returns:
1834 if (AL.getNumArgs() > 2) {
1835 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
1836 return;
1838 break;
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);
1851 ParamIdx Idx;
1852 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
1853 return;
1855 // Is the function argument a pointer type?
1856 QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
1857 int Err = -1; // No error
1858 switch (K) {
1859 case OwnershipAttr::Takes:
1860 case OwnershipAttr::Holds:
1861 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1862 Err = 0;
1863 break;
1864 case OwnershipAttr::Returns:
1865 if (!T->isIntegerType())
1866 Err = 1;
1867 break;
1869 if (-1 != Err) {
1870 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1871 << Ex->getSourceRange();
1872 return;
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
1878 // index.
1879 if (I->getOwnKind() != K && llvm::is_contained(I->args(), Idx)) {
1880 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
1881 return;
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();
1889 if (I->args_size())
1890 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1891 << Idx.getSourceIndex() << Ex->getSourceRange();
1892 return;
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;
1910 return;
1913 // gcc rejects
1914 // class c {
1915 // static int a __attribute__((weakref ("v2")));
1916 // static int b() __attribute__((weakref ("f3")));
1917 // };
1918 // and ignores the attributes of
1919 // void f(void) {
1920 // static int a __attribute__((weakref ("v2")));
1921 // }
1922 // we reject them
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);
1927 return;
1930 // The GCC manual says
1932 // At present, a declaration to which `weakref' is attached can only
1933 // be `static'.
1935 // It also says
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));
1943 // as
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.
1948 // GCC rejects
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.
1955 StringRef Str;
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) {
1965 StringRef Str;
1966 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1967 return;
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;
1973 return;
1976 D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str));
1979 static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1980 StringRef Str;
1981 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1982 return;
1984 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
1985 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
1986 return;
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;
1996 return;
1998 } else {
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;
2002 return;
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) {
2021 StringRef Model;
2022 SourceLocation LiteralLoc;
2023 // Check that it is a string.
2024 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
2025 return;
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);
2031 return;
2034 if (S.Context.getTargetInfo().getTriple().isOSAIX() &&
2035 Model != "global-dynamic") {
2036 S.Diag(LiteralLoc, diag::err_aix_attr_unsupported_tls_model) << Model;
2037 return;
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));
2047 return;
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))
2059 return;
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);
2064 return;
2066 } else if (AL.getParsedKind() == ParsedAttr::AT_CPUSpecific) {
2067 if (checkAttrMutualExclusion<CPUDispatchAttr>(S, D, AL))
2068 return;
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);
2073 return;
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;
2082 return;
2086 if (!AL.checkAtLeastNumArgs(S, 1))
2087 return;
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;
2094 return;
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);
2103 return;
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());
2110 })) {
2111 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
2112 return;
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()));
2121 else
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;
2130 return;
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;
2139 return;
2142 const auto *FD = cast<FunctionDecl>(D);
2143 if (!FD->isExternallyVisible()) {
2144 S.Diag(AL.getLoc(), diag::warn_attribute_cmse_entry_static);
2145 return;
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();
2159 return;
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";
2167 return;
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;
2180 return;
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);
2204 else
2205 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
2208 bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
2209 if (!Attrs.checkExactlyNumArgs(*this, 0)) {
2210 Attrs.setInvalid();
2211 return true;
2214 return false;
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();
2222 AL.setInvalid();
2223 return true;
2226 return false;
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;
2241 return;
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
2255 function.
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.
2260 Example:
2262 struct Vector
2264 __vector float xyzw;
2265 } __attribute__((vecreturn));
2267 Vector Add(Vector lhs, Vector rhs)
2269 Vector result;
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;
2276 return;
2279 const auto *R = cast<RecordDecl>(D);
2280 int count = 0;
2282 if (!isa<CXXRecordDecl>(R)) {
2283 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2284 return;
2287 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2288 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
2289 return;
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);
2295 return;
2297 count++;
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)) {
2309 S.Diag(AL.getLoc(),
2310 diag::err_carries_dependency_param_not_function_decl);
2311 return;
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);
2333 return;
2335 if (AL.getNumArgs() &&
2336 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
2337 return;
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))
2346 return;
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.
2354 StringRef Str;
2355 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
2356 return;
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();
2366 return;
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();
2389 return true;
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();
2397 return true;
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();
2405 return true;
2408 return false;
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())
2418 return true;
2420 if (X == Y)
2421 return true;
2423 if (BeforeIsOkay && X < Y)
2424 return true;
2426 return false;
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,
2434 int Priority) {
2435 VersionTuple MergedIntroduced = Introduced;
2436 VersionTuple MergedDeprecated = Deprecated;
2437 VersionTuple MergedObsoleted = Obsoleted;
2438 bool FoundAny = false;
2439 bool OverrideOrImpl = false;
2440 switch (AMK) {
2441 case AMK_None:
2442 case AMK_Redeclaration:
2443 OverrideOrImpl = false;
2444 break;
2446 case AMK_Override:
2447 case AMK_ProtocolImplementation:
2448 case AMK_OptionalProtocolImplementation:
2449 OverrideOrImpl = true;
2450 break;
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]);
2457 if (!OldAA) {
2458 ++i;
2459 continue;
2462 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2463 if (OldPlatform != Platform) {
2464 ++i;
2465 continue;
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
2470 // attribute.
2471 if (OldAA->getPriority() < Priority)
2472 return nullptr;
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);
2479 --e;
2480 continue;
2483 FoundAny = true;
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) {
2495 int Which = -1;
2496 VersionTuple FirstVersion;
2497 VersionTuple SecondVersion;
2498 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
2499 Which = 0;
2500 FirstVersion = OldIntroduced;
2501 SecondVersion = Introduced;
2502 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
2503 Which = 1;
2504 FirstVersion = Deprecated;
2505 SecondVersion = OldDeprecated;
2506 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
2507 Which = 2;
2508 FirstVersion = Obsoleted;
2509 SecondVersion = OldObsoleted;
2512 if (Which == -1) {
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.
2523 ++i;
2524 continue;
2525 } else {
2526 Diag(OldAA->getLocation(),
2527 diag::warn_mismatched_availability_override)
2528 << Which
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);
2535 else
2536 Diag(CI.getLoc(), diag::note_protocol_method);
2537 } else {
2538 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2539 Diag(CI.getLoc(), diag::note_previous_attribute);
2542 Attrs.erase(Attrs.begin() + i);
2543 --e;
2544 continue;
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);
2562 --e;
2563 continue;
2566 MergedIntroduced = MergedIntroduced2;
2567 MergedDeprecated = MergedDeprecated2;
2568 MergedObsoleted = MergedObsoleted2;
2569 ++i;
2573 if (FoundAny &&
2574 MergedIntroduced == Introduced &&
2575 MergedDeprecated == Deprecated &&
2576 MergedObsoleted == Obsoleted)
2577 return nullptr;
2579 // Only create a new attribute if !OverrideOrImpl, but we want to do
2580 // the checking.
2581 if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
2582 MergedDeprecated, MergedObsoleted) &&
2583 !OverrideOrImpl) {
2584 auto *Avail = ::new (Context) AvailabilityAttr(
2585 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2586 Message, IsStrict, Replacement, Priority);
2587 Avail->setImplicit(Implicit);
2588 return Avail;
2590 return nullptr;
2593 static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2594 if (isa<UsingDecl, UnresolvedUsingTypenameDecl, UnresolvedUsingValueDecl>(
2595 D)) {
2596 S.Diag(AL.getRange().getBegin(), diag::warn_deprecated_ignored_on_using)
2597 << AL;
2598 return;
2601 if (!AL.checkExactlyNumArgs(S, 1))
2602 return;
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)
2608 << Platform->Ident;
2610 auto *ND = dyn_cast<NamedDecl>(D);
2611 if (!ND) // We warned about this already, so just return.
2612 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();
2619 StringRef Str;
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())) {
2629 S.Diag(AL.getLoc(),
2630 diag::warn_availability_swift_unavailable_deprecated_only);
2631 return;
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);
2640 return;
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);
2651 if (NewAttr)
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");
2663 if (NewII) {
2664 const auto *SDKInfo = S.getDarwinSDKInfoForAvailabilityChecking();
2665 const auto *IOSToWatchOSMapping =
2666 SDKInfo ? SDKInfo->getVersionMapping(
2667 DarwinSDKInfo::OSEnvPair::iOStoWatchOSPair())
2668 : nullptr;
2670 auto adjustWatchOSVersion =
2671 [IOSToWatchOSMapping](VersionTuple Version) -> VersionTuple {
2672 if (Version.empty())
2673 return Version;
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());
2690 else
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,
2706 Sema::AMK_None,
2707 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
2708 if (NewAttr)
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");
2720 if (NewII) {
2721 const auto *SDKInfo = S.getDarwinSDKInfoForAvailabilityChecking();
2722 const auto *IOSToTvOSMapping =
2723 SDKInfo ? SDKInfo->getVersionMapping(
2724 DarwinSDKInfo::OSEnvPair::iOStoTvOSPair())
2725 : nullptr;
2727 auto AdjustTvOSVersion =
2728 [IOSToTvOSMapping](VersionTuple Version) -> VersionTuple {
2729 if (Version.empty())
2730 return Version;
2732 if (IOSToTvOSMapping) {
2733 if (auto MappedVersion =
2734 IOSToTvOSMapping->map(Version, VersionTuple(0, 0), None)) {
2735 return *MappedVersion;
2738 return Version;
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,
2748 Sema::AMK_None,
2749 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
2750 if (NewAttr)
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(),
2758 "macOS");
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");
2767 if (NewII) {
2768 auto MinMacCatalystVersion = [](const VersionTuple &V) {
2769 if (V.empty())
2770 return 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.
2774 return V;
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);
2783 if (NewAttr)
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> {
2798 if (V.empty())
2799 return None;
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),
2808 NewDeprecated =
2809 RemapMacOSVersion(Deprecated.Version),
2810 NewObsoleted =
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);
2825 if (NewAttr)
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))
2836 return;
2838 StringRef Language;
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));
2850 template <class T>
2851 static T *mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI,
2852 typename T::VisibilityType value) {
2853 T *existingAttr = D->getAttr<T>();
2854 if (existingAttr) {
2855 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2856 if (existingValue == value)
2857 return nullptr;
2858 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2859 S.Diag(CI.getLoc(), diag::note_previous_attribute);
2860 D->dropAttr<T>();
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;
2882 return;
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;
2892 return;
2895 // Check that the argument is a string literal.
2896 StringRef TypeStr;
2897 SourceLocation LiteralLoc;
2898 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
2899 return;
2901 VisibilityAttr::VisibilityType type;
2902 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
2903 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2904 << TypeStr;
2905 return;
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;
2916 Attr *newAttr;
2917 if (isTypeVisibility) {
2918 newAttr = S.mergeTypeVisibilityAttr(
2919 D, AL, (TypeVisibilityAttr::VisibilityType)type);
2920 } else {
2921 newAttr = S.mergeVisibilityAttr(D, AL, type);
2923 if (newAttr)
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;
2931 return;
2934 if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
2935 handleSimpleAttribute<ObjCDirectAttr>(S, D, AL);
2936 } else {
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);
2945 } else {
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;
2955 return;
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;
2962 return;
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.
2970 return;
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);
2981 return;
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);
2988 return;
2991 else {
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
2997 // case.
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);
3008 return;
3010 } else {
3011 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
3012 return;
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;
3021 return;
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;
3028 return;
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();
3042 return;
3045 if (Idx->isSigned() && Idx->isNegative()) {
3046 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
3047 << E->getSourceRange();
3048 return;
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();
3061 return;
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();
3070 return;
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);
3078 return;
3081 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
3082 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
3083 return;
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;
3088 return;
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;
3093 return;
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>()
3101 ->getPointeeType()
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;
3106 return;
3108 } else {
3109 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3110 << AL << ExpectedFunctionMethodOrBlock;
3111 return;
3113 } else {
3114 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3115 << AL << ExpectedFunctionMethodOrBlock;
3116 return;
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;
3126 return;
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;
3131 return;
3134 StringRef Str;
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
3154 // extension.
3155 // FIXME: C2x should support this feature as well, even as an extension.
3156 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
3157 return;
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)
3166 << AL.isGNUScope();
3167 return;
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.
3175 bool isDef = false;
3176 if (!D->canBeWeakImported(isDef)) {
3177 if (isDef)
3178 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
3179 << "weak_import";
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.
3184 } else
3185 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3186 << AL << ExpectedVariableOrFunction;
3188 return;
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) {
3197 uint32_t WGSize[3];
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))
3202 return;
3203 if (WGSize[i] == 0) {
3204 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
3205 << AL << E->getSourceRange();
3206 return;
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) {
3222 uint32_t SGSize;
3223 const Expr *E = AL.getArgAsExpr(0);
3224 if (!checkUInt32Argument(S, AL, E, SGSize))
3225 return;
3226 if (SGSize == 0) {
3227 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
3228 << AL << E->getSourceRange();
3229 return;
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;
3244 return;
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;
3255 return;
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;
3261 return;
3265 D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
3268 SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI,
3269 StringRef Name) {
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())
3275 return nullptr;
3277 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
3278 if (ExistingAttr->getName() == Name)
3279 return nullptr;
3280 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3281 << 1 /*section*/;
3282 Diag(CI.getLoc(), diag::note_previous_attribute);
3283 return nullptr;
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;
3304 bool HasTAA;
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'*/;
3313 return false;
3315 return true;
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
3320 // argument.
3321 StringRef Str;
3322 SourceLocation LiteralLoc;
3323 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3324 return;
3326 if (!S.checkSectionName(LiteralLoc, Str))
3327 return;
3329 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
3330 if (NewAttr) {
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'*/;
3347 return false;
3350 return true;
3353 CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI,
3354 StringRef Name) {
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())
3359 return nullptr;
3361 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3362 if (ExistingAttr->getName() == Name)
3363 return nullptr;
3364 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3365 << 0 /*codeseg*/;
3366 Diag(CI.getLoc(), diag::note_previous_attribute);
3367 return nullptr;
3369 return ::new (Context) CodeSegAttr(Context, CI, Name);
3372 static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3373 StringRef Str;
3374 SourceLocation LiteralLoc;
3375 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3376 return;
3377 if (!checkCodeSegName(S, LiteralLoc, Str))
3378 return;
3379 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3380 if (!ExistingAttr->isImplicit()) {
3381 S.Diag(AL.getLoc(),
3382 ExistingAttr->getName() == Str
3383 ? diag::warn_duplicate_codeseg_attribute
3384 : diag::err_conflicting_codeseg_attribute);
3385 return;
3387 D->dropAttr<CodeSegAttr>();
3389 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
3390 D->addAttr(CSA);
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;
3436 StringRef DiagMsg;
3437 if (ParsedAttrs.BranchProtection.empty())
3438 return false;
3439 if (!Context.getTargetInfo().validateBranchProtection(
3440 ParsedAttrs.BranchProtection, ParsedAttrs.Architecture, BPI,
3441 DiagMsg)) {
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)
3446 << DiagMsg;
3448 if (!DiagMsg.empty())
3449 Diag(LiteralLoc, diag::warn_unsupported_branch_protection_spec) << DiagMsg;
3451 return false;
3454 static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3455 StringRef Str;
3456 SourceLocation LiteralLoc;
3457 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
3458 S.checkTargetAttr(LiteralLoc, Str))
3459 return;
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;
3487 if (Cur.empty())
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;
3499 HasDefault = true;
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;
3513 return false;
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);
3522 return;
3524 if (checkAttrMutualExclusion<TargetClonesAttr>(S, D, AL))
3525 return;
3527 SmallVector<StringRef, 2> Strings;
3528 bool HasCommas = false, HasDefault = false;
3530 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
3531 StringRef CurStr;
3532 SourceLocation LiteralLoc;
3533 if (!S.checkStringLiteralArgumentAttr(AL, I, CurStr, &LiteralLoc) ||
3534 S.checkTargetClonesAttrString(
3535 LiteralLoc, CurStr,
3536 cast<StringLiteral>(AL.getArgAsExpr(I)->IgnoreParenCasts()),
3537 HasDefault, HasCommas, Strings))
3538 return;
3541 if (HasCommas && AL.getNumArgs() > 1)
3542 S.Diag(AL.getLoc(), diag::warn_target_clone_mixed_values);
3544 if (!HasDefault) {
3545 S.Diag(AL.getLoc(), diag::err_target_clone_must_have_default);
3546 return;
3549 // FIXME: We could probably figure out how to get this to work for lambdas
3550 // someday.
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)
3555 << /*Lambda*/ 9;
3556 return;
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);
3568 uint32_t VecWidth;
3569 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3570 AL.setInvalid();
3571 return;
3574 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3575 if (Existing && Existing->getVectorWidth() != VecWidth) {
3576 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3577 return;
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();
3596 if (!FD) {
3597 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3598 << NI.getName();
3599 return;
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();
3606 if (!FD) {
3607 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3608 << NI.getName();
3609 if (ULE->getType() == S.Context.OverloadTy)
3610 S.NoteAllOverloadCandidates(ULE);
3611 return;
3613 } else {
3614 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
3615 return;
3618 if (FD->getNumParams() != 1) {
3619 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3620 << NI.getName();
3621 return;
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;
3632 return;
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;
3643 return;
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;
3651 return;
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);
3662 ParamIdx Idx;
3663 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
3664 return;
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);
3676 return;
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);
3693 return;
3696 D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
3699 enum FormatAttrKind {
3700 CFStringFormat,
3701 NSStringFormat,
3702 StrftimeFormat,
3703 SupportedFormat,
3704 IgnoredFormat,
3705 InvalidFormat
3708 /// getFormatAttrKind - Map from format attribute names to supported format
3709 /// types.
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;
3734 return;
3737 if (S.getLangOpts().HLSL) {
3738 S.Diag(AL.getLoc(), diag::err_hlsl_init_priority_unsupported);
3739 return;
3742 if (S.getCurFunctionOrMethodDecl()) {
3743 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3744 AL.setInvalid();
3745 return;
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);
3752 AL.setInvalid();
3753 return;
3756 Expr *E = AL.getArgAsExpr(0);
3757 uint32_t prioritynum;
3758 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3759 AL.setInvalid();
3760 return;
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;
3770 AL.setInvalid();
3771 return;
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");
3784 if (!Match) {
3785 Diag(EA->getLocation(), diag::err_attributes_are_not_compatible)
3786 << CI << EA;
3787 Diag(CI.getLoc(), diag::note_conflicting_attribute);
3788 return nullptr;
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,
3801 int FirstArg) {
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
3808 // location.
3809 if (F->getLocation().isInvalid())
3810 F->setRange(CI.getRange());
3811 return nullptr;
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;
3824 return;
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)
3844 return;
3846 if (Kind == InvalidFormat) {
3847 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3848 << AL << II->getName();
3849 return;
3852 // checks for the 2nd argument
3853 Expr *IdxExpr = AL.getArgAsExpr(1);
3854 uint32_t Idx;
3855 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
3856 return;
3858 if (Idx < 1 || Idx > NumArgs) {
3859 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3860 << AL << 2 << IdxExpr->getSourceRange();
3861 return;
3864 // FIXME: Do we need to bounds check?
3865 unsigned ArgIdx = Idx - 1;
3867 if (HasImplicitThisParam) {
3868 if (ArgIdx == 0) {
3869 S.Diag(AL.getLoc(),
3870 diag::err_format_attribute_implicit_this_format_string)
3871 << IdxExpr->getSourceRange();
3872 return;
3874 ArgIdx--;
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);
3886 return;
3889 // check the 3rd argument
3890 Expr *FirstArgExpr = AL.getArgAsExpr(2);
3891 uint32_t FirstArg;
3892 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
3893 return;
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 ...
3899 else
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();
3909 return;
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();
3915 return;
3918 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg);
3919 if (NewAttr)
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)
3928 << AL.getRange();
3929 return;
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;
3943 int Idx = 1;
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) {
3951 SourceRange SR;
3952 int32_t ArgIdx;
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;
3960 return;
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,
3970 false)) {
3971 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3972 << AL << (I + 1) << IdxExpr->getSourceRange();
3973 return;
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();
3980 return;
3983 SR = IdxExpr->getSourceRange();
3984 } else {
3985 llvm_unreachable("Unexpected ParsedAttr argument type!");
3988 if (ArgIdx == 0 && !HasImplicitThisParam) {
3989 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3990 << (I + 1) << SR;
3991 return;
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)
3997 ArgIdx -= 1;
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)
4008 << AL.getRange();
4009 return;
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)
4016 .getTypePtr();
4017 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
4018 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
4019 << AL.getRange();
4020 return;
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)
4031 << AL.getRange();
4032 return;
4035 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
4036 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
4037 << AL << (unsigned)(EncodingIndices.size() - 1);
4038 return;
4041 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
4042 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
4043 << AL << (unsigned)(EncodingIndices.size() - 1);
4044 return;
4047 if (CalleeFnProtoType->isVariadic()) {
4048 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
4049 return;
4052 // Do not allow multiple callback attributes.
4053 if (D->hasAttr<CallbackAttr>()) {
4054 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
4055 return;
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);
4076 return;
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();
4088 else
4089 RD = dyn_cast<RecordDecl>(D);
4091 if (!RD || !RD->isUnion()) {
4092 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
4093 << ExpectedUnion;
4094 return;
4097 if (!RD->isCompleteDefinition()) {
4098 if (!RD->isBeingDefined())
4099 S.Diag(AL.getLoc(),
4100 diag::warn_transparent_union_attribute_not_definition);
4101 return;
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);
4108 return;
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;
4117 return;
4120 if (FirstType->isIncompleteType())
4121 return;
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())
4127 return;
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
4132 // as an example).
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;
4146 return;
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()))) {
4158 D->addAttr(Attr);
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
4164 // argument.
4165 StringRef Str;
4166 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
4167 return;
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();
4187 QualType T;
4188 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
4189 T = TD->getUnderlyingType();
4190 else if (const auto *VD = dyn_cast<ValueDecl>(D))
4191 T = VD->getType();
4192 else
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();
4199 return;
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())
4207 return;
4209 if (!Alignment.isPowerOf2()) {
4210 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
4211 << E->getSourceRange();
4212 return;
4215 D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
4216 return;
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;
4227 return;
4230 if (AL.getNumArgs() == 0) {
4231 D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
4232 return;
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);
4239 return;
4242 if (!AL.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E))
4243 return;
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.
4262 // CWG 2354:
4263 // CWG agreed to remove permission for alignas to be applied to
4264 // enumerations.
4265 // C11 6.7.5/2:
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.
4269 int DiagKind = -1;
4270 if (isa<ParmVarDecl>(D)) {
4271 DiagKind = 0;
4272 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
4273 if (VD->getStorageClass() == SC_Register)
4274 DiagKind = 1;
4275 if (VD->isExceptionVariable())
4276 DiagKind = 2;
4277 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
4278 if (FD->isBitField())
4279 DiagKind = 3;
4280 } else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
4281 if (ED->getLangOpts().CPlusPlus)
4282 DiagKind = 4;
4283 } else if (!isa<TagDecl>(D)) {
4284 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
4285 << (TmpAttr.isC11() ? ExpectedVariableOrField
4286 : ExpectedVariableFieldOrTag);
4287 return;
4289 if (DiagKind != -1) {
4290 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
4291 << &TmpAttr << DiagKind;
4292 return;
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();
4304 return;
4308 // Save dependent expressions in the AST to be instantiated.
4309 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
4310 AA->setPackExpansion(IsPackExpansion);
4311 D->addAttr(AA);
4312 return;
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())
4320 return;
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
4326 // C11 6.7.5p6:
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();
4332 return;
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();
4342 return;
4345 const auto *VD = dyn_cast<VarDecl>(D);
4346 if (VD && Context.getTargetInfo().isTLSSupported()) {
4347 unsigned MaxTLSAlign =
4348 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
4349 .getQuantity();
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;
4354 return;
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;
4365 return;
4369 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
4370 AA->setPackExpansion(IsPackExpansion);
4371 D->addAttr(AA);
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);
4380 D->addAttr(AA);
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();
4389 } else {
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())
4395 return;
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;
4403 unsigned Align = 0;
4404 for (auto *I : D->specific_attrs<AlignedAttr>()) {
4405 if (I->isAlignmentDependent())
4406 return;
4407 if (I->isAlignas())
4408 AlignasAttr = I;
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())
4433 return false;
4435 // The unspecified model never matches what a definition could need.
4436 if (ExplicitModel == MSInheritanceModel::Unspecified)
4437 return false;
4439 if (BestCase) {
4440 if (RD->calculateInheritanceModel() == ExplicitModel)
4441 return false;
4442 } else {
4443 if (RD->calculateInheritanceModel() <= ExplicitModel)
4444 return false;
4447 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
4448 << 0 /*definition*/;
4449 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) << RD;
4450 return true;
4453 /// parseModeAttrArg - Parses attribute mode string and returns parsed type
4454 /// attribute.
4455 static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
4456 bool &IntegerMode, bool &ComplexMode,
4457 FloatModeKind &ExplicitType) {
4458 IntegerMode = true;
4459 ComplexMode = false;
4460 ExplicitType = FloatModeKind::NoFloat;
4461 switch (Str.size()) {
4462 case 2:
4463 switch (Str[0]) {
4464 case 'Q':
4465 DestWidth = 8;
4466 break;
4467 case 'H':
4468 DestWidth = 16;
4469 break;
4470 case 'S':
4471 DestWidth = 32;
4472 break;
4473 case 'D':
4474 DestWidth = 64;
4475 break;
4476 case 'X':
4477 DestWidth = 96;
4478 break;
4479 case 'K': // KFmode - IEEE quad precision (__float128)
4480 ExplicitType = FloatModeKind::Float128;
4481 DestWidth = Str[1] == 'I' ? 0 : 128;
4482 break;
4483 case 'T':
4484 ExplicitType = FloatModeKind::LongDouble;
4485 DestWidth = 128;
4486 break;
4487 case 'I':
4488 ExplicitType = FloatModeKind::Ibm128;
4489 DestWidth = Str[1] == 'I' ? 0 : 128;
4490 break;
4492 if (Str[1] == 'F') {
4493 IntegerMode = false;
4494 } else if (Str[1] == 'C') {
4495 IntegerMode = false;
4496 ComplexMode = true;
4497 } else if (Str[1] != 'I') {
4498 DestWidth = 0;
4500 break;
4501 case 4:
4502 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
4503 // pointer on PIC16 and other embedded platforms.
4504 if (Str == "word")
4505 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
4506 else if (Str == "byte")
4507 DestWidth = S.Context.getTargetInfo().getCharWidth();
4508 break;
4509 case 7:
4510 if (Str == "pointer")
4511 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
4512 break;
4513 case 11:
4514 if (Str == "unwind_word")
4515 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
4516 break;
4520 /// handleModeAttr - This attribute modifies the width of a decl with primitive
4521 /// type.
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;
4532 return;
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();
4543 normalizeName(Str);
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);
4566 } else {
4567 VectorSize = 0;
4571 if (!VectorSize)
4572 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode,
4573 ExplicitType);
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
4579 if (!DestWidth) {
4580 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4581 return;
4584 QualType OldTy;
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();
4591 if (OldTy.isNull())
4592 OldTy = Context.IntTy;
4593 } else
4594 OldTy = cast<ValueDecl>(D)->getType();
4596 if (OldTy->isDependentType()) {
4597 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
4598 return;
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();
4613 return;
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);
4628 } else {
4629 if (!OldElemTy->isFloatingType())
4630 Diag(AttrLoc, diag::err_mode_wrong_type);
4633 QualType NewElemTy;
4635 if (IntegerMode)
4636 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4637 OldElemTy->isSignedIntegerType());
4638 else
4639 NewElemTy = Context.getRealTypeForBitwidth(DestWidth, ExplicitType);
4641 if (NewElemTy.isNull()) {
4642 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
4643 return;
4646 if (ComplexMode) {
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.
4656 if (ComplexMode) {
4657 Diag(AttrLoc, diag::err_complex_mode_vector_type);
4658 return;
4660 unsigned NumElements = Context.getTypeSize(OldElemTy) *
4661 OldVT->getNumElements() /
4662 Context.getTypeSize(NewElemTy);
4663 NewTy =
4664 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
4667 if (NewTy.isNull()) {
4668 Diag(AttrLoc, diag::err_mode_wrong_type);
4669 return;
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);
4677 else
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);
4693 return nullptr;
4696 if (D->hasAttr<AlwaysInlineAttr>())
4697 return nullptr;
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);
4711 return nullptr;
4713 // Attribute does not apply to non-static local variables.
4714 if (VD->hasLocalStorage()) {
4715 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4716 return nullptr;
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);
4731 return nullptr;
4733 // Attribute does not apply to non-static local variables.
4734 if (VD->hasLocalStorage()) {
4735 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4736 return nullptr;
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);
4747 return nullptr;
4750 if (D->hasAttr<MinSizeAttr>())
4751 return nullptr;
4753 return ::new (Context) MinSizeAttr(Context, CI);
4756 SwiftNameAttr *Sema::mergeSwiftNameAttr(Decl *D, const SwiftNameAttr &SNA,
4757 StringRef Name) {
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)
4761 << PrevSNA << &SNA;
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>())
4784 return nullptr;
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()))
4792 D->addAttr(Inline);
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);
4809 return;
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())
4815 return;
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.
4824 // "int x[]").
4825 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
4826 !isa<IncompleteArrayType>(VD->getType())) {
4827 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
4828 return;
4830 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
4831 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
4832 << S.CurrentCUDATarget())
4833 return;
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)
4844 << FD->getType()
4845 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4846 : FixItHint());
4847 return;
4849 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4850 if (Method->isInstance()) {
4851 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
4852 << Method;
4853 return;
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);
4874 return;
4878 if (auto *A = D->getAttr<CUDADeviceAttr>()) {
4879 if (!A->isImplicit())
4880 return;
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);
4890 return;
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);
4903 return;
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.
4917 CallingConv CC;
4918 if (S.CheckCallingConvAttr(AL, CC, /*FD*/nullptr))
4919 return;
4921 if (!isa<ObjCMethodDecl>(D)) {
4922 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4923 << AL << ExpectedFunctionOrMethod;
4924 return;
4927 switch (AL.getKind()) {
4928 case ParsedAttr::AT_FastCall:
4929 D->addAttr(::new (S.Context) FastCallAttr(S.Context, AL));
4930 return;
4931 case ParsedAttr::AT_StdCall:
4932 D->addAttr(::new (S.Context) StdCallAttr(S.Context, AL));
4933 return;
4934 case ParsedAttr::AT_ThisCall:
4935 D->addAttr(::new (S.Context) ThisCallAttr(S.Context, AL));
4936 return;
4937 case ParsedAttr::AT_CDecl:
4938 D->addAttr(::new (S.Context) CDeclAttr(S.Context, AL));
4939 return;
4940 case ParsedAttr::AT_Pascal:
4941 D->addAttr(::new (S.Context) PascalAttr(S.Context, AL));
4942 return;
4943 case ParsedAttr::AT_SwiftCall:
4944 D->addAttr(::new (S.Context) SwiftCallAttr(S.Context, AL));
4945 return;
4946 case ParsedAttr::AT_SwiftAsyncCall:
4947 D->addAttr(::new (S.Context) SwiftAsyncCallAttr(S.Context, AL));
4948 return;
4949 case ParsedAttr::AT_VectorCall:
4950 D->addAttr(::new (S.Context) VectorCallAttr(S.Context, AL));
4951 return;
4952 case ParsedAttr::AT_MSABI:
4953 D->addAttr(::new (S.Context) MSABIAttr(S.Context, AL));
4954 return;
4955 case ParsedAttr::AT_SysVABI:
4956 D->addAttr(::new (S.Context) SysVABIAttr(S.Context, AL));
4957 return;
4958 case ParsedAttr::AT_RegCall:
4959 D->addAttr(::new (S.Context) RegCallAttr(S.Context, AL));
4960 return;
4961 case ParsedAttr::AT_Pcs: {
4962 PcsAttr::PCSType PCS;
4963 switch (CC) {
4964 case CC_AAPCS:
4965 PCS = PcsAttr::AAPCS;
4966 break;
4967 case CC_AAPCS_VFP:
4968 PCS = PcsAttr::AAPCS_VFP;
4969 break;
4970 default:
4971 llvm_unreachable("unexpected calling convention in pcs attribute");
4974 D->addAttr(::new (S.Context) PcsAttr(S.Context, AL, PCS));
4975 return;
4977 case ParsedAttr::AT_AArch64VectorPcs:
4978 D->addAttr(::new (S.Context) AArch64VectorPcsAttr(S.Context, AL));
4979 return;
4980 case ParsedAttr::AT_AArch64SVEPcs:
4981 D->addAttr(::new (S.Context) AArch64SVEPcsAttr(S.Context, AL));
4982 return;
4983 case ParsedAttr::AT_AMDGPUKernelCall:
4984 D->addAttr(::new (S.Context) AMDGPUKernelCallAttr(S.Context, AL));
4985 return;
4986 case ParsedAttr::AT_IntelOclBicc:
4987 D->addAttr(::new (S.Context) IntelOclBiccAttr(S.Context, AL));
4988 return;
4989 case ParsedAttr::AT_PreserveMost:
4990 D->addAttr(::new (S.Context) PreserveMostAttr(S.Context, AL));
4991 return;
4992 case ParsedAttr::AT_PreserveAll:
4993 D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL));
4994 return;
4995 default:
4996 llvm_unreachable("unexpected attribute kind");
5000 static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5001 if (!AL.checkAtLeastNumArgs(S, 1))
5002 return;
5004 std::vector<StringRef> DiagnosticIdentifiers;
5005 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
5006 StringRef RuleName;
5008 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
5009 return;
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;
5022 QualType ParmType;
5023 if (AL.hasParsedType()) {
5024 ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc);
5026 unsigned SelectIdx = ~0U;
5027 if (ParmType->isReferenceType())
5028 SelectIdx = 0;
5029 else if (ParmType->isArrayType())
5030 SelectIdx = 1;
5032 if (SelectIdx != ~0U) {
5033 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument)
5034 << SelectIdx << AL;
5035 return;
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
5043 // declaration.
5044 D = D->getCanonicalDecl();
5045 if (AL.getKind() == ParsedAttr::AT_Owner) {
5046 if (checkAttrMutualExclusion<PointerAttr>(S, D, AL))
5047 return;
5048 if (const auto *OAttr = D->getAttr<OwnerAttr>()) {
5049 const Type *ExistingDerefType = OAttr->getDerefTypeLoc()
5050 ? OAttr->getDerefType().getTypePtr()
5051 : nullptr;
5052 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
5053 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
5054 << AL << OAttr;
5055 S.Diag(OAttr->getLocation(), diag::note_conflicting_attribute);
5057 return;
5059 for (Decl *Redecl : D->redecls()) {
5060 Redecl->addAttr(::new (S.Context) OwnerAttr(S.Context, AL, DerefTypeLoc));
5062 } else {
5063 if (checkAttrMutualExclusion<OwnerAttr>(S, D, AL))
5064 return;
5065 if (const auto *PAttr = D->getAttr<PointerAttr>()) {
5066 const Type *ExistingDerefType = PAttr->getDerefTypeLoc()
5067 ? PAttr->getDerefType().getTypePtr()
5068 : nullptr;
5069 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
5070 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
5071 << AL << PAttr;
5072 S.Diag(PAttr->getLocation(), diag::note_conflicting_attribute);
5074 return;
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))
5085 return;
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))
5093 return;
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())
5101 return true;
5103 if (Attrs.hasProcessingCache()) {
5104 CC = (CallingConv) Attrs.getProcessingCache();
5105 return false;
5108 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
5109 if (!Attrs.checkExactlyNumArgs(*this, ReqArgs)) {
5110 Attrs.setInvalid();
5111 return true;
5114 // TODO: diagnose uses of these conventions on the wrong target.
5115 switch (Attrs.getKind()) {
5116 case ParsedAttr::AT_CDecl:
5117 CC = CC_C;
5118 break;
5119 case ParsedAttr::AT_FastCall:
5120 CC = CC_X86FastCall;
5121 break;
5122 case ParsedAttr::AT_StdCall:
5123 CC = CC_X86StdCall;
5124 break;
5125 case ParsedAttr::AT_ThisCall:
5126 CC = CC_X86ThisCall;
5127 break;
5128 case ParsedAttr::AT_Pascal:
5129 CC = CC_X86Pascal;
5130 break;
5131 case ParsedAttr::AT_SwiftCall:
5132 CC = CC_Swift;
5133 break;
5134 case ParsedAttr::AT_SwiftAsyncCall:
5135 CC = CC_SwiftAsync;
5136 break;
5137 case ParsedAttr::AT_VectorCall:
5138 CC = CC_X86VectorCall;
5139 break;
5140 case ParsedAttr::AT_AArch64VectorPcs:
5141 CC = CC_AArch64VectorCall;
5142 break;
5143 case ParsedAttr::AT_AArch64SVEPcs:
5144 CC = CC_AArch64SVEPCS;
5145 break;
5146 case ParsedAttr::AT_AMDGPUKernelCall:
5147 CC = CC_AMDGPUKernelCall;
5148 break;
5149 case ParsedAttr::AT_RegCall:
5150 CC = CC_X86RegCall;
5151 break;
5152 case ParsedAttr::AT_MSABI:
5153 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
5154 CC_Win64;
5155 break;
5156 case ParsedAttr::AT_SysVABI:
5157 CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
5158 CC_C;
5159 break;
5160 case ParsedAttr::AT_Pcs: {
5161 StringRef StrRef;
5162 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
5163 Attrs.setInvalid();
5164 return true;
5166 if (StrRef == "aapcs") {
5167 CC = CC_AAPCS;
5168 break;
5169 } else if (StrRef == "aapcs-vfp") {
5170 CC = CC_AAPCS_VFP;
5171 break;
5174 Attrs.setInvalid();
5175 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
5176 return true;
5178 case ParsedAttr::AT_IntelOclBicc:
5179 CC = CC_IntelOclBicc;
5180 break;
5181 case ParsedAttr::AT_PreserveMost:
5182 CC = CC_PreserveMost;
5183 break;
5184 case ParsedAttr::AT_PreserveAll:
5185 CC = CC_PreserveAll;
5186 break;
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:
5202 CheckHost = true;
5203 CheckDevice = true;
5204 break;
5205 case CFT_Host:
5206 CheckHost = true;
5207 break;
5208 case CFT_Device:
5209 case CFT_Global:
5210 CheckDevice = true;
5211 break;
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);
5221 } else {
5222 A = TI.checkCallingConvention(CC);
5225 switch (A) {
5226 case TargetInfo::CCCR_OK:
5227 break;
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.
5234 CC = CC_C;
5235 break;
5237 case TargetInfo::CCCR_Error:
5238 Diag(Attrs.getLoc(), diag::error_cconv_unsupported)
5239 << Attrs << (int)CallingConventionIgnoredReason::ForThisTarget;
5240 break;
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;
5249 if (FD) {
5250 IsCXXMethod = FD->isCXXInstanceMember();
5251 IsVariadic = FD->isVariadic();
5253 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
5254 break;
5258 Attrs.setProcessingCache((unsigned) CC);
5259 return false;
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();
5275 } else {
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();
5287 } else {
5288 return Ty->isDependentType();
5290 if (!Ty.getQualifiers().empty())
5291 return false;
5292 return isValidSwiftContextType(Ty);
5295 void Sema::AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI,
5296 ParameterABI abi) {
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);
5305 return;
5309 switch (abi) {
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));
5319 return;
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));
5327 return;
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));
5335 return;
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));
5343 return;
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) {
5351 if (AL.isInvalid())
5352 return true;
5354 if (!AL.checkExactlyNumArgs(*this, 1)) {
5355 AL.setInvalid();
5356 return true;
5359 uint32_t NP;
5360 Expr *NumParamsExpr = AL.getArgAsExpr(0);
5361 if (!checkUInt32Argument(*this, AL, NumParamsExpr, NP)) {
5362 AL.setInvalid();
5363 return true;
5366 if (Context.getTargetInfo().getRegParmMax() == 0) {
5367 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
5368 << NumParamsExpr->getSourceRange();
5369 AL.setInvalid();
5370 return true;
5373 numParams = NP;
5374 if (numParams > Context.getTargetInfo().getRegParmMax()) {
5375 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
5376 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
5377 AL.setInvalid();
5378 return true;
5381 return false;
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))
5392 return nullptr;
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())
5397 return E;
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();
5403 return nullptr;
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;
5409 return nullptr;
5411 if (*I < 0)
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)
5430 return;
5432 if (MinBlocks) {
5433 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
5434 if (MinBlocks == nullptr)
5435 return;
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))
5444 return;
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;
5455 return;
5458 ParamIdx ArgumentIdx;
5459 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, AL.getArgAsExpr(1),
5460 ArgumentIdx))
5461 return;
5463 ParamIdx TypeTagIdx;
5464 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 3, AL.getArgAsExpr(2),
5465 TypeTagIdx))
5466 return;
5468 bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag";
5469 if (IsPointer) {
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,
5479 IsPointer));
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;
5487 return;
5490 if (!AL.checkExactlyNumArgs(S, 1))
5491 return;
5493 if (!isa<VarDecl>(D)) {
5494 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
5495 << AL << ExpectedVariable;
5496 return;
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) {
5510 ParamIdx ArgCount;
5512 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, AL.getArgAsExpr(0),
5513 ArgCount,
5514 true /* CanIndexImplicitThis */))
5515 return;
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))
5526 return;
5527 if (AL.getNumArgs() == 2) {
5528 Expr *Arg = AL.getArgAsExpr(1);
5529 if (!checkUInt32Argument(S, AL, Arg, Offset, 1, true))
5530 return;
5531 if (Count < Offset) {
5532 S.Diag(getAttrLoc(AL), diag::err_attribute_argument_out_of_range)
5533 << &AL << 0 << Count << Arg->getBeginLoc();
5534 return;
5537 D->addAttr(::new (S.Context)
5538 PatchableFunctionEntryAttr(S.Context, AL, Count, Offset));
5541 namespace {
5542 struct IntrinToName {
5543 uint32_t Id;
5544 int32_t FullName;
5545 int32_t ShortName;
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) {
5556 return L.Id < Id;
5558 if (It == Map.end() || It->Id != BuiltinID)
5559 return false;
5560 StringRef FullName(&IntrinNames[It->FullName]);
5561 if (AliasName == FullName)
5562 return true;
5563 if (It->ShortName == -1)
5564 return false;
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;
5594 return;
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);
5606 return;
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;
5622 return;
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;
5639 return;
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())
5664 return true;
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);
5673 switch (K) {
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);
5679 return;
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);
5692 return;
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);
5698 return;
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;
5722 default:
5723 llvm_unreachable("Wrong argument supplied");
5727 bool Sema::checkNSReturnsRetainedReturnType(SourceLocation Loc, QualType QT) {
5728 if (isValidSubjectOfNSReturnsRetainedAttribute(QT))
5729 return false;
5731 Diag(Loc, diag::warn_ns_attribute_wrong_return_type)
5732 << "'ns_returns_retained'" << 0 << 0;
5733 return true;
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);
5739 if (!PVD)
5740 return false;
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();
5770 return;
5772 } else if (AL.isUsedAsTypeAttr()) {
5773 return;
5774 } else {
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;
5782 break;
5784 case ParsedAttr::AT_OSReturnsRetained:
5785 case ParsedAttr::AT_OSReturnsNotRetained:
5786 case ParsedAttr::AT_CFReturnsRetained:
5787 case ParsedAttr::AT_CFReturnsNotRetained:
5788 ExpectedDeclKind = ExpectedFunctionMethodOrParameter;
5789 break;
5791 S.Diag(D->getBeginLoc(), diag::warn_attribute_wrong_decl_type)
5792 << AL.getRange() << AL << ExpectedDeclKind;
5793 return;
5796 bool TypeOK;
5797 bool Cf;
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);
5803 Cf = false;
5804 break;
5806 case ParsedAttr::AT_NSReturnsAutoreleased:
5807 case ParsedAttr::AT_NSReturnsNotRetained:
5808 TypeOK = isValidSubjectOfNSAttribute(ReturnType);
5809 Cf = false;
5810 break;
5812 case ParsedAttr::AT_CFReturnsRetained:
5813 case ParsedAttr::AT_CFReturnsNotRetained:
5814 TypeOK = isValidSubjectOfCFAttribute(ReturnType);
5815 Cf = true;
5816 break;
5818 case ParsedAttr::AT_OSReturnsRetained:
5819 case ParsedAttr::AT_OSReturnsNotRetained:
5820 TypeOK = isValidSubjectOfOSAttribute(ReturnType);
5821 Cf = true;
5822 ParmDiagID = 3; // Pointer-to-OSObject-pointer
5823 break;
5826 if (!TypeOK) {
5827 if (AL.isUsedAsTypeAttr())
5828 return;
5830 if (isa<ParmVarDecl>(D)) {
5831 S.Diag(D->getBeginLoc(), diag::warn_ns_attribute_wrong_parameter_type)
5832 << AL << ParmDiagID << AL.getRange();
5833 } else {
5834 // Needs to be kept in sync with warn_ns_attribute_wrong_return_type.
5835 enum : unsigned {
5836 Function,
5837 Method,
5838 Property
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();
5847 return;
5850 switch (AL.getKind()) {
5851 default:
5852 llvm_unreachable("invalid ownership attribute");
5853 case ParsedAttr::AT_NSReturnsAutoreleased:
5854 handleSimpleAttribute<NSReturnsAutoreleasedAttr>(S, D, AL);
5855 return;
5856 case ParsedAttr::AT_CFReturnsNotRetained:
5857 handleSimpleAttribute<CFReturnsNotRetainedAttr>(S, D, AL);
5858 return;
5859 case ParsedAttr::AT_NSReturnsNotRetained:
5860 handleSimpleAttribute<NSReturnsNotRetainedAttr>(S, D, AL);
5861 return;
5862 case ParsedAttr::AT_CFReturnsRetained:
5863 handleSimpleAttribute<CFReturnsRetainedAttr>(S, D, AL);
5864 return;
5865 case ParsedAttr::AT_NSReturnsRetained:
5866 handleSimpleAttribute<NSReturnsRetainedAttr>(S, D, AL);
5867 return;
5868 case ParsedAttr::AT_OSReturnsRetained:
5869 handleSimpleAttribute<OSReturnsRetainedAttr>(S, D, AL);
5870 return;
5871 case ParsedAttr::AT_OSReturnsNotRetained:
5872 handleSimpleAttribute<OSReturnsNotRetainedAttr>(S, D, AL);
5873 return;
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();
5886 else
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.
5897 return;
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
5910 << 0;
5911 S.Diag(PDecl->getLocation(), diag::note_protocol_decl);
5912 return;
5914 if (Method->getMethodFamily() == OMF_dealloc) {
5915 S.Diag(D->getBeginLoc(), diag::warn_objc_requires_super_protocol) << Attrs
5916 << 1;
5917 return;
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));
5928 if (!DRE) {
5929 S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 0;
5930 return;
5933 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
5934 if (!VD) {
5935 S.Diag(Loc, diag::err_nserrordomain_invalid_decl) << 1 << DRE->getDecl();
5936 return;
5939 if (!isNSStringType(VD->getType(), S.Context) &&
5940 !isCFStringType(VD->getType(), S.Context)) {
5941 S.Diag(Loc, diag::err_nserrordomain_wrong_type) << VD;
5942 return;
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;
5951 if (!Parm) {
5952 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
5953 return;
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;
5960 return;
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);
5967 return;
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;
5978 if (!Parm) {
5979 S.Diag(D->getBeginLoc(), diag::err_objc_attr_not_id) << AL << 0;
5980 return;
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;
5993 return;
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
6008 // extensions.
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);
6013 return;
6016 ObjCInterfaceDecl *IFace;
6017 if (auto *CatDecl = dyn_cast<ObjCCategoryDecl>(Ctx))
6018 IFace = CatDecl->getClassInterface();
6019 else
6020 IFace = cast<ObjCInterfaceDecl>(Ctx);
6022 if (!IFace)
6023 return;
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))
6032 return;
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();
6047 notify = true;
6050 if (RD) {
6051 ObjCBoxableAttr *BoxableAttr =
6052 ::new (S.Context) ObjCBoxableAttr(S.Context, AL);
6053 RD->addAttr(BoxableAttr);
6054 if (notify) {
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)
6078 << QT;
6079 return;
6082 Qualifiers::ObjCLifetime Lifetime = QT.getObjCLifetime();
6084 // If we have no lifetime yet, check the lifetime we're presumably
6085 // going to infer.
6086 if (Lifetime == Qualifiers::OCL_None && !QT->isDependentType())
6087 Lifetime = QT->getObjCARCImplicitLifetime();
6089 switch (Lifetime) {
6090 case Qualifiers::OCL_None:
6091 assert(QT->isDependentType() &&
6092 "didn't infer lifetime for non-dependent type?");
6093 break;
6095 case Qualifiers::OCL_Weak: // meaningful
6096 case Qualifiers::OCL_Strong: // meaningful
6097 break;
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);
6103 break;
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
6111 // argument.
6112 StringRef Str;
6113 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
6114 return;
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
6121 // argument.
6122 StringRef BT;
6123 if (!S.checkStringLiteralArgumentAttr(AL, 0, BT))
6124 return;
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;
6131 return;
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>();
6139 if (!PT)
6140 return false;
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())
6148 return true;
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()))
6154 return true;
6156 return false;
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)))
6163 return true;
6166 S.Diag(AL.getLoc(), diag::err_attr_swift_error_no_error_parameter)
6167 << AL << isa<ObjCMethodDecl>(D);
6168 return false;
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())
6177 return true;
6179 S.Diag(AL.getLoc(), diag::err_attr_swift_error_return_type)
6180 << AL << AL.getArgAsIdent(0)->Ident->getName() << isa<ObjCMethodDecl>(D)
6181 << /*pointer*/ 1;
6182 return false;
6185 auto hasIntegerResult = [](Sema &S, Decl *D, const ParsedAttr &AL) -> bool {
6186 QualType RT = getFunctionOrMethodResultType(D);
6187 if (RT->isIntegralType(S.Context))
6188 return true;
6190 S.Diag(AL.getLoc(), diag::err_attr_swift_error_return_type)
6191 << AL << AL.getArgAsIdent(0)->Ident->getName() << isa<ObjCMethodDecl>(D)
6192 << /*integral*/ 0;
6193 return false;
6196 if (D->isInvalidDecl())
6197 return;
6199 IdentifierLoc *Loc = AL.getArgAsIdent(0);
6200 SwiftErrorAttr::ConventionKind Convention;
6201 if (!SwiftErrorAttr::ConvertStrToConventionKind(Loc->Ident->getName(),
6202 Convention)) {
6203 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
6204 << AL << Loc->Ident;
6205 return;
6208 switch (Convention) {
6209 case SwiftErrorAttr::None:
6210 // No additional validation required.
6211 break;
6213 case SwiftErrorAttr::NonNullError:
6214 if (!hasErrorParameter(S, D, AL))
6215 return;
6216 break;
6218 case SwiftErrorAttr::NullResult:
6219 if (!hasErrorParameter(S, D, AL) || !hasPointerResult(S, D, AL))
6220 return;
6221 break;
6223 case SwiftErrorAttr::NonZeroResult:
6224 case SwiftErrorAttr::ZeroResult:
6225 if (!hasErrorParameter(S, D, AL) || !hasIntegerResult(S, D, AL))
6226 return;
6227 break;
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);
6242 return;
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>()
6251 ->getPointeeType()
6252 ->getAs<FunctionProtoType>();
6253 ArrayRef<QualType> BlockParams;
6254 if (FuncTy)
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;
6264 return;
6266 QualType ErrorParam = BlockParams[ParamIdx - 1];
6267 if (!ErrorParam->isIntegralType(S.Context)) {
6268 StringRef ConvStr =
6269 ErrorAttr->getConvention() == SwiftAsyncErrorAttr::ZeroArgument
6270 ? "zero_argument"
6271 : "nonzero_argument";
6272 S.Diag(ErrorAttr->getLocation(), diag::err_swift_async_error_non_integral)
6273 << ErrorAttr << ConvStr << ParamIdx << ErrorParam;
6274 return;
6276 break;
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;
6286 break;
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;
6295 break;
6301 if (!AnyErrorParams) {
6302 S.Diag(ErrorAttr->getLocation(),
6303 diag::err_swift_async_error_no_error_parameter)
6304 << ErrorAttr << isa<ObjCMethodDecl>(D);
6305 return;
6307 break;
6309 case SwiftAsyncErrorAttr::None:
6310 break;
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(),
6318 ConvKind)) {
6319 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
6320 << AL << IDLoc->Ident;
6321 return;
6324 uint32_t ParamIdx = 0;
6325 switch (ConvKind) {
6326 case SwiftAsyncErrorAttr::ZeroArgument:
6327 case SwiftAsyncErrorAttr::NonZeroArgument: {
6328 if (!AL.checkExactlyNumArgs(S, 2))
6329 return;
6331 Expr *IdxExpr = AL.getArgAsExpr(1);
6332 if (!checkUInt32Argument(S, AL, IdxExpr, ParamIdx))
6333 return;
6334 break;
6336 case SwiftAsyncErrorAttr::NonNullError:
6337 case SwiftAsyncErrorAttr::None: {
6338 if (!AL.checkExactlyNumArgs(S, 1))
6339 return;
6340 break;
6344 auto *ErrorAttr =
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.
6360 static bool
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:")) {
6370 IsGetter = true;
6371 Name = Name.substr(7);
6372 } else if (Name.startswith("setter:")) {
6373 IsSetter = true;
6374 Name = Name.substr(7);
6377 if (Name.back() != ')') {
6378 S.Diag(Loc, diag::warn_attr_swift_name_function) << AL;
6379 return false;
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;
6396 return false;
6397 } else {
6398 IsMember = true;
6401 if (!isValidAsciiIdentifier(BaseName) || BaseName == "_") {
6402 S.Diag(Loc, diag::warn_attr_swift_name_invalid_identifier)
6403 << AL << /*basename*/ 0;
6404 return false;
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;
6412 return false;
6415 if (Parameters.empty()) {
6416 S.Diag(Loc, diag::warn_attr_swift_name_missing_parameters) << AL;
6417 return false;
6420 assert(Parameters.back() == ')' && "expected ')'");
6421 Parameters = Parameters.drop_back(); // ')'
6423 if (Parameters.empty()) {
6424 // Setters and subscripts must have at least one parameter.
6425 if (IsSubscript) {
6426 S.Diag(Loc, diag::warn_attr_swift_name_subscript_invalid_parameter)
6427 << AL << /* have at least one parameter */1;
6428 return false;
6431 if (IsSetter) {
6432 S.Diag(Loc, diag::warn_attr_swift_name_setter_parameters) << AL;
6433 return false;
6436 return true;
6439 if (Parameters.back() != ':') {
6440 S.Diag(Loc, diag::warn_attr_swift_name_function) << AL;
6441 return false;
6444 StringRef CurrentParam;
6445 llvm::Optional<unsigned> SelfLocation;
6446 unsigned NewValueCount = 0;
6447 llvm::Optional<unsigned> NewValueLocation;
6448 do {
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;
6454 return false;
6457 if (IsMember && CurrentParam == "self") {
6458 // "self" indicates the "self" argument for a member.
6460 // More than one "self"?
6461 if (SelfLocation) {
6462 S.Diag(Loc, diag::warn_attr_swift_name_multiple_selfs) << AL;
6463 return false;
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.
6473 ++NewValueCount;
6475 NewValueLocation = SwiftParamCount;
6478 ++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;
6485 return false;
6488 IsSingleParamInit =
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".
6500 if (SelfLocation)
6501 ++NumExpectedParams;
6503 // Subscripts may have additional parameters beyond the expected params for
6504 // the index.
6505 if (IsSubscript) {
6506 if (SwiftParamCount < NumExpectedParams) {
6507 S.Diag(Loc, ParamDiag) << AL;
6508 return false;
6511 // A subscript setter must explicitly label its newValue parameter to
6512 // distinguish it from index parameters.
6513 if (IsSetter) {
6514 if (!NewValueLocation) {
6515 S.Diag(Loc, diag::warn_attr_swift_name_subscript_setter_no_newValue)
6516 << AL;
6517 return false;
6519 if (NewValueCount > 1) {
6520 S.Diag(Loc, diag::warn_attr_swift_name_subscript_setter_multiple_newValues)
6521 << AL;
6522 return false;
6524 } else {
6525 // Subscript getters should have no 'newValue:' parameter.
6526 if (NewValueLocation) {
6527 S.Diag(Loc, diag::warn_attr_swift_name_subscript_getter_newValue)
6528 << AL;
6529 return false;
6532 } else {
6533 // Property accessors must have exactly the number of expected params.
6534 if (SwiftParamCount != NumExpectedParams) {
6535 S.Diag(Loc, ParamDiag) << AL;
6536 return false;
6541 return true;
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);
6553 } else {
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;
6562 return false;
6566 // The async name drops the last callback parameter.
6567 if (IsAsync) {
6568 if (ParamCount == 0) {
6569 Diag(Loc, diag::warn_attr_swift_name_decl_missing_params)
6570 << AL << isa<ObjCMethodDecl>(D);
6571 return false;
6573 ParamCount -= 1;
6576 unsigned SwiftParamCount;
6577 bool IsSingleParamInit;
6578 if (!validateSwiftFunctionName(*this, AL, Loc, Name,
6579 SwiftParamCount, IsSingleParamInit))
6580 return false;
6582 bool ParamCountValid;
6583 if (SwiftParamCount == ParamCount) {
6584 ParamCountValid = true;
6585 } else if (SwiftParamCount > ParamCount) {
6586 ParamCountValid = IsSingleParamInit && ParamCount == 0;
6587 } else {
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();
6596 return false;
6599 ParamCountValid = SwiftParamCount + MaybeOutParamCount >= ParamCount;
6602 if (!ParamCountValid) {
6603 Diag(Loc, diag::warn_attr_swift_name_num_params)
6604 << (SwiftParamCount > ParamCount) << AL << ParamCount
6605 << SwiftParamCount;
6606 return false;
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)) &&
6612 !IsAsync) {
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
6621 << /*context*/1;
6622 return false;
6625 if (!isValidAsciiIdentifier(BaseName)) {
6626 Diag(Loc, diag::warn_attr_swift_name_invalid_identifier) << AL
6627 << /*basename*/0;
6628 return false;
6630 } else {
6631 Diag(Loc, diag::warn_attr_swift_name_decl_kind) << AL;
6632 return false;
6634 return true;
6637 static void handleSwiftName(Sema &S, Decl *D, const ParsedAttr &AL) {
6638 StringRef Name;
6639 SourceLocation Loc;
6640 if (!S.checkStringLiteralArgumentAttr(AL, 0, Name, &Loc))
6641 return;
6643 if (!S.DiagnoseSwiftName(D, Name, Loc, AL, /*IsAsync=*/false))
6644 return;
6646 D->addAttr(::new (S.Context) SwiftNameAttr(S.Context, AL, Name));
6649 static void handleSwiftAsyncName(Sema &S, Decl *D, const ParsedAttr &AL) {
6650 StringRef Name;
6651 SourceLocation Loc;
6652 if (!S.checkStringLiteralArgumentAttr(AL, 0, Name, &Loc))
6653 return;
6655 if (!S.DiagnoseSwiftName(D, Name, Loc, AL, /*IsAsync=*/true))
6656 return;
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))
6664 return;
6666 if (!AL.isArgIdent(0)) {
6667 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
6668 << AL << AANT_ArgumentIdentifier;
6669 return;
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;
6676 return;
6679 if (!isa<TypedefNameDecl>(D)) {
6680 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
6681 << AL << "typedefs";
6682 return;
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;
6692 return;
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;
6699 return;
6702 ParamIdx Idx;
6703 if (Kind == SwiftAsyncAttr::None) {
6704 // If this is 'none', then there shouldn't be any additional arguments.
6705 if (!AL.checkExactlyNumArgs(S, 1))
6706 return;
6707 } else {
6708 // Non-none swift_async requires a completion handler index argument.
6709 if (!AL.checkExactlyNumArgs(S, 2))
6710 return;
6712 Expr *HandlerIdx = AL.getArgAsExpr(1);
6713 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 2, HandlerIdx, Idx))
6714 return;
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();
6723 return;
6725 QualType BlockTy =
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();
6731 return;
6735 auto *AsyncAttr =
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))
6751 return nullptr;
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;
6766 return;
6769 StringRef OrigStrRef;
6770 SourceLocation LiteralLoc;
6771 if (!S.checkStringLiteralArgumentAttr(AL, 0, OrigStrRef, &LiteralLoc))
6772 return;
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);
6783 return;
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);
6790 return;
6792 } else if (!isHexDigit(StrRef[i])) {
6793 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
6794 return;
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"
6813 // on cfe-dev.
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);
6818 if (UA)
6819 D->addAttr(UA);
6822 static void handleHLSLNumThreadsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6823 using llvm::Triple;
6824 Triple Target = S.Context.getTargetInfo().getTriple();
6825 if (!llvm::is_contained({Triple::Compute, Triple::Mesh, Triple::Amplification,
6826 Triple::Library},
6827 Target.getEnvironment())) {
6828 uint32_t Pipeline =
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";
6833 return;
6836 llvm::VersionTuple SMVersion = Target.getOSVersion();
6837 uint32_t ZMax = 1024;
6838 uint32_t ThreadMax = 1024;
6839 if (SMVersion.getMajor() <= 4) {
6840 ZMax = 1;
6841 ThreadMax = 768;
6842 } else if (SMVersion.getMajor() == 5) {
6843 ZMax = 64;
6844 ThreadMax = 1024;
6847 uint32_t X;
6848 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), X))
6849 return;
6850 if (X > 1024) {
6851 S.Diag(AL.getArgAsExpr(0)->getExprLoc(),
6852 diag::err_hlsl_numthreads_argument_oor) << 0 << 1024;
6853 return;
6855 uint32_t Y;
6856 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(1), Y))
6857 return;
6858 if (Y > 1024) {
6859 S.Diag(AL.getArgAsExpr(1)->getExprLoc(),
6860 diag::err_hlsl_numthreads_argument_oor) << 1 << 1024;
6861 return;
6863 uint32_t Z;
6864 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(2), Z))
6865 return;
6866 if (Z > ZMax) {
6867 S.Diag(AL.getArgAsExpr(2)->getExprLoc(),
6868 diag::err_hlsl_numthreads_argument_oor) << 2 << ZMax;
6869 return;
6872 if (X * Y * Z > ThreadMax) {
6873 S.Diag(AL.getLoc(), diag::err_hlsl_numthreads_invalid) << ThreadMax;
6874 return;
6877 HLSLNumThreadsAttr *NewAttr = S.mergeHLSLNumThreadsAttr(D, AL, X, Y, Z);
6878 if (NewAttr)
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);
6890 return nullptr;
6892 return ::new (Context) HLSLNumThreadsAttr(Context, AL, X, Y, Z);
6895 static void handleHLSLSVGroupIndexAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6896 using llvm::Triple;
6897 Triple Target = S.Context.getTargetInfo().getTriple();
6898 if (Target.getEnvironment() != Triple::Compute) {
6899 uint32_t Pipeline =
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";
6904 return;
6907 D->addAttr(::new (S.Context) HLSLSV_GroupIndexAttr(S.Context, AL));
6910 static void handleHLSLShaderAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6911 StringRef Str;
6912 SourceLocation ArgLoc;
6913 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
6914 return;
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;
6924 return;
6927 // FIXME: check function match the shader stage.
6929 HLSLShaderAttr *NewAttr = S.mergeHLSLShaderAttr(D, AL, ShaderType);
6930 if (NewAttr)
6931 D->addAttr(NewAttr);
6934 HLSLShaderAttr *
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);
6942 return nullptr;
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;
6951 return;
6953 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
6954 D, AL, /*BestCase=*/true, (MSInheritanceModel)AL.getSemanticSpelling());
6955 if (IA) {
6956 D->addAttr(IA);
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);
6965 return;
6967 if (VD->getTSCSpec() != TSCS_unspecified) {
6968 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
6969 return;
6971 if (VD->hasLocalStorage()) {
6972 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
6973 return;
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) {
6981 StringRef Tag;
6982 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
6983 return;
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;
6990 return;
6992 if (NS->isAnonymousNamespace()) {
6993 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
6994 return;
6996 if (AL.getNumArgs() == 0)
6997 Tags.push_back(NS->getName());
6998 } else if (!AL.checkAtLeastNumArgs(S, 1))
6999 return;
7001 // Store tags sorted and without duplicates.
7002 llvm::sort(Tags);
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;
7013 return;
7016 StringRef Str;
7017 SourceLocation ArgLoc;
7019 if (AL.getNumArgs() == 0)
7020 Str = "";
7021 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
7022 return;
7024 ARMInterruptAttr::InterruptType Kind;
7025 if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
7026 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
7027 << ArgLoc;
7028 return;
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;
7040 return;
7043 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
7044 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
7045 << /*MSP430*/ 1 << 0;
7046 return;
7049 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
7050 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
7051 << /*MSP430*/ 1 << 1;
7052 return;
7055 // The attribute takes one integer argument.
7056 if (!AL.checkExactlyNumArgs(S, 1))
7057 return;
7059 if (!AL.isArgExpr(0)) {
7060 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
7061 << AL << AANT_ArgumentIntegerConstant;
7062 return;
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();
7071 return;
7073 // The argument should be in range 0..63.
7074 unsigned Num = NumParams->getLimitedValue(255);
7075 if (Num > 63) {
7076 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
7077 << AL << (int)NumParams->getSExtValue()
7078 << NumParamsExpr->getSourceRange();
7079 return;
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;
7090 return;
7093 StringRef Str;
7094 SourceLocation ArgLoc;
7096 if (AL.getNumArgs() == 0)
7097 Str = "";
7098 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
7099 return;
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;
7113 return;
7116 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
7117 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
7118 << /*MIPS*/ 0 << 0;
7119 return;
7122 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
7123 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
7124 << /*MIPS*/ 0 << 1;
7125 return;
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))
7131 return;
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) + "'";
7137 return;
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))
7145 return;
7147 if (!AL.isArgExpr(0)) {
7148 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
7149 << AL << AANT_ArgumentIntegerConstant;
7150 return;
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();
7161 return;
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();
7169 return;
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;
7188 return;
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
7196 : 1)
7197 << 0;
7198 return;
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
7206 : 1)
7207 << 1;
7208 return;
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
7216 : 1)
7217 << 2;
7218 return;
7220 // The second argument, if present, must be an unsigned integer.
7221 unsigned TypeSize =
7222 S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64
7223 ? 64
7224 : 32;
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
7232 : 1)
7233 << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false);
7234 return;
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;
7244 return;
7247 if (!AL.checkExactlyNumArgs(S, 0))
7248 return;
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;
7257 return;
7260 if (!AL.checkExactlyNumArgs(S, 0))
7261 return;
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>())
7270 continue;
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)
7288 return true;
7290 return false;
7293 static void handleBTFDeclTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7294 StringRef Str;
7295 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
7296 return;
7297 if (hasBTFDeclTagAttr(D, Str))
7298 return;
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()))
7305 return nullptr;
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;
7313 return;
7316 auto *FD = cast<FunctionDecl>(D);
7317 if (FD->isThisDeclarationADefinition()) {
7318 S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
7319 return;
7322 StringRef Str;
7323 SourceLocation ArgLoc;
7324 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
7325 return;
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())
7337 return nullptr;
7338 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_import) << 0
7339 << ExistingAttr->getImportModule() << AL.getImportModule();
7340 Diag(AL.getLoc(), diag::note_previous_attribute);
7341 return nullptr;
7343 if (FD->hasBody()) {
7344 Diag(AL.getLoc(), diag::warn_import_on_definition) << 0;
7345 return nullptr;
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())
7357 return nullptr;
7358 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_import) << 1
7359 << ExistingAttr->getImportName() << AL.getImportName();
7360 Diag(AL.getLoc(), diag::note_previous_attribute);
7361 return nullptr;
7363 if (FD->hasBody()) {
7364 Diag(AL.getLoc(), diag::warn_import_on_definition) << 1;
7365 return nullptr;
7367 return ::new (Context) WebAssemblyImportNameAttr(Context, AL,
7368 AL.getImportName());
7371 static void
7372 handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7373 auto *FD = cast<FunctionDecl>(D);
7375 StringRef Str;
7376 SourceLocation ArgLoc;
7377 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
7378 return;
7379 if (FD->hasBody()) {
7380 S.Diag(AL.getLoc(), diag::warn_import_on_definition) << 0;
7381 return;
7384 FD->addAttr(::new (S.Context)
7385 WebAssemblyImportModuleAttr(S.Context, AL, Str));
7388 static void
7389 handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7390 auto *FD = cast<FunctionDecl>(D);
7392 StringRef Str;
7393 SourceLocation ArgLoc;
7394 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
7395 return;
7396 if (FD->hasBody()) {
7397 S.Diag(AL.getLoc(), diag::warn_import_on_definition) << 1;
7398 return;
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);
7411 return;
7414 // Check the attribute argument. Argument is optional.
7415 if (!AL.checkAtMostNumArgs(S, 1))
7416 return;
7418 StringRef Str;
7419 SourceLocation ArgLoc;
7421 // 'machine'is the default interrupt mode.
7422 if (AL.getNumArgs() == 0)
7423 Str = "machine";
7424 else if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
7425 return;
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;
7437 return;
7440 if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) {
7441 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
7442 << /*RISC-V*/ 2 << 0;
7443 return;
7446 if (!getFunctionOrMethodResultType(D)->isVoidType()) {
7447 S.Diag(D->getLocation(), diag::warn_interrupt_attribute_invalid)
7448 << /*RISC-V*/ 2 << 1;
7449 return;
7452 RISCVInterruptAttr::InterruptType Kind;
7453 if (!RISCVInterruptAttr::ConvertStrToInterruptType(Str, Kind)) {
7454 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << Str
7455 << ArgLoc;
7456 return;
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);
7467 break;
7468 case llvm::Triple::mipsel:
7469 case llvm::Triple::mips:
7470 handleMipsInterruptAttr(S, D, AL);
7471 break;
7472 case llvm::Triple::m68k:
7473 handleM68kInterruptAttr(S, D, AL);
7474 break;
7475 case llvm::Triple::x86:
7476 case llvm::Triple::x86_64:
7477 handleAnyX86InterruptAttr(S, D, AL);
7478 break;
7479 case llvm::Triple::avr:
7480 handleAVRInterruptAttr(S, D, AL);
7481 break;
7482 case llvm::Triple::riscv32:
7483 case llvm::Triple::riscv64:
7484 handleRISCVInterruptAttr(S, D, AL);
7485 break;
7486 default:
7487 handleARMInterruptAttr(S, D, AL);
7488 break;
7492 static bool
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())
7498 return false;
7500 uint32_t Min = 0;
7501 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
7502 return true;
7504 uint32_t Max = 0;
7505 if (!checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
7506 return true;
7508 if (Min == 0 && Max != 0) {
7509 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
7510 << &Attr << 0;
7511 return true;
7513 if (Min > Max) {
7514 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
7515 << &Attr << 1;
7516 return true;
7519 return false;
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))
7528 return;
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,
7543 Expr *MaxExpr,
7544 const AMDGPUWavesPerEUAttr &Attr) {
7545 if (S.DiagnoseUnexpandedParameterPack(MinExpr) ||
7546 (MaxExpr && S.DiagnoseUnexpandedParameterPack(MaxExpr)))
7547 return true;
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()))
7552 return false;
7554 uint32_t Min = 0;
7555 if (!checkUInt32Argument(S, Attr, MinExpr, Min, 0))
7556 return true;
7558 uint32_t Max = 0;
7559 if (MaxExpr && !checkUInt32Argument(S, Attr, MaxExpr, Max, 1))
7560 return true;
7562 if (Min == 0 && Max != 0) {
7563 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
7564 << &Attr << 0;
7565 return true;
7567 if (Max != 0 && Min > Max) {
7568 S.Diag(Attr.getLocation(), diag::err_attribute_argument_invalid)
7569 << &Attr << 1;
7570 return true;
7573 return false;
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))
7581 return;
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))
7589 return;
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))
7601 return;
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))
7610 return;
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())
7622 return;
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()))
7627 return;
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;
7632 return;
7635 D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(S.Context, AL));
7638 static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
7639 uint32_t Version;
7640 Expr *VersionExpr = static_cast<Expr *>(AL.getArgAsExpr(0));
7641 if (!checkUInt32Argument(S, AL, AL.getArgAsExpr(0), Version))
7642 return;
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();
7648 return;
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.
7654 Version *= 100;
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'";
7663 return nullptr;
7666 if (D->hasAttr<DLLImportAttr>())
7667 return nullptr;
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>())
7680 return nullptr;
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;
7689 return;
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)
7697 << A;
7698 return;
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;
7706 return;
7710 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
7711 ? (Attr *)S.mergeDLLExportAttr(D, A)
7712 : (Attr *)S.mergeDLLImportAttr(D, A);
7713 if (NewAttr)
7714 D->addAttr(NewAttr);
7717 MSInheritanceAttr *
7718 Sema::mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI,
7719 bool BestCase,
7720 MSInheritanceModel Model) {
7721 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
7722 if (IA->getInheritanceModel() == Model)
7723 return nullptr;
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,
7733 Model)) {
7734 return nullptr;
7736 } else {
7737 if (isa<ClassTemplatePartialSpecializationDecl>(RD)) {
7738 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
7739 << 1 /*partial specialization*/;
7740 return nullptr;
7742 if (RD->getDescribedClassTemplate()) {
7743 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
7744 << 0 /*primary template*/;
7745 return nullptr;
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))
7765 return;
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))
7773 return;
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))
7783 return;
7785 D->addAttr(::new (S.Context) AcquireCapabilityAttr(S.Context, AL, Args.data(),
7786 Args.size()));
7789 static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D,
7790 const ParsedAttr &AL) {
7791 SmallVector<Expr*, 2> Args;
7792 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
7793 return;
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(),
7806 Args.size()));
7809 static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
7810 const ParsedAttr &AL) {
7811 if (!AL.checkAtLeastNumArgs(S, 1))
7812 return;
7814 // check that all arguments are lockable objects
7815 SmallVector<Expr*, 1> Args;
7816 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
7817 if (Args.empty())
7818 return;
7820 RequiresCapabilityAttr *RCA = ::new (S.Context)
7821 RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size());
7823 D->addAttr(RCA);
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
7832 // namespace.
7833 return;
7835 } else if (isa<UsingDecl, UnresolvedUsingTypenameDecl,
7836 UnresolvedUsingValueDecl>(D)) {
7837 S.Diag(AL.getRange().getBegin(), diag::warn_deprecated_ignored_on_using)
7838 << AL;
7839 return;
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))
7846 return;
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))
7853 return;
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();
7864 return false;
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))
7874 return;
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))
7883 return;
7885 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) ==
7886 SanitizerMask() &&
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;
7939 else
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())
7946 return;
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();
7954 } else {
7955 S.Diag(AL.getLoc(), diag::err_opencl_multiple_access_qualifiers)
7956 << D->getSourceRange();
7957 D->setInvalidDecl(true);
7958 return;
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",
7978 S.getLangOpts()));
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);
7983 return;
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.
7993 StringRef KindStr;
7994 SourceLocation LiteralLoc;
7995 if (!S.checkStringLiteralArgumentAttr(AL, 0, KindStr, &LiteralLoc))
7996 return;
7998 ZeroCallUsedRegsAttr::ZeroCallUsedRegsKind Kind;
7999 if (!ZeroCallUsedRegsAttr::ConvertStrToZeroCallUsedRegsKind(KindStr, Kind)) {
8000 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
8001 << AL << KindStr;
8002 return;
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) {
8011 StringRef KindStr;
8012 SourceLocation LiteralLoc;
8013 if (!S.checkStringLiteralArgumentAttr(AL, 0, KindStr, &LiteralLoc))
8014 return;
8016 FunctionReturnThunksAttr::Kind Kind;
8017 if (!FunctionReturnThunksAttr::ConvertStrToKind(KindStr, Kind)) {
8018 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
8019 << AL << KindStr;
8020 return;
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);
8039 return;
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);
8048 return;
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);
8055 return;
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);
8062 return;
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);
8072 return;
8075 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
8076 handleSimpleAttribute<AlwaysDestroyAttr>(S, D, A);
8077 else
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)
8093 << 0;
8095 return false;
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)
8111 << 1;
8113 return false;
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);
8121 return 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)
8130 << 0;
8131 return;
8134 if (!tryMakeVariablePseudoStrong(S, VD, /*DiagnoseFailure=*/true))
8135 return;
8137 handleSimpleAttribute<ObjCExternallyRetainedAttr>(S, D, AL);
8138 return;
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
8152 // be non-local.
8153 if (Ty.getLocalUnqualifiedType().getQualifiers().getObjCLifetime() ==
8154 Qualifiers::OCL_Strong)
8155 continue;
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");
8172 T = TT->desugar();
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);
8177 return;
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;
8191 return;
8195 handleSimpleAttribute<MSAllocatorAttr>(S, D, AL);
8198 static void handleAcquireHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
8199 if (AL.isUsedAsTypeAttr())
8200 return;
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)
8205 << AL.getRange();
8206 return;
8209 StringRef Argument;
8210 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
8211 return;
8212 D->addAttr(AcquireHandleAttr::Create(S.Context, Argument, AL));
8215 template<typename Attr>
8216 static void handleHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
8217 StringRef Argument;
8218 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
8219 return;
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;
8229 return;
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;
8236 return;
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) {
8255 StringRef Argument;
8256 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
8257 return;
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()
8266 << Argument;
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>();
8272 return;
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>();
8293 return nullptr;
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>(
8302 *this, D, AL);
8305 EnforceTCBLeafAttr *Sema::mergeEnforceTCBLeafAttr(
8306 Decl *D, const EnforceTCBLeafAttr &AL) {
8307 return mergeEnforceTCBAttrImpl<EnforceTCBLeafAttr, EnforceTCBAttr>(
8308 *this, D, AL);
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())
8320 return false;
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
8327 // any expression.
8328 if (IsLastAttrArg && AttrHasVariadicArg)
8329 return false;
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
8334 // attribute.
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
8339 // expression.
8340 if (E->isValueDependent() && !ArgMemberCanHoldExpr)
8341 return true;
8343 return false;
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.
8349 static void
8350 ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
8351 const Sema::ProcessDeclAttributeOptions &Options) {
8352 if (AL.isInvalid() || AL.getKind() == ParsedAttr::IgnoredAttribute)
8353 return;
8355 // Ignore C++11 attributes on declarator chunks: they appertain to the type
8356 // instead.
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)
8364 return;
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())) {
8371 S.Diag(AL.getLoc(),
8372 AL.isDeclspecAttribute()
8373 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
8374 : (unsigned)diag::warn_unknown_attribute_ignored)
8375 << AL << AL.getRange();
8376 return;
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))
8384 return;
8386 if (MustDelayArgs) {
8387 AL.handleAttrWithDelayedArgs(S, D);
8388 return;
8391 switch (AL.getKind()) {
8392 default:
8393 if (AL.getInfo().handleDeclAttribute(S, D, AL) != ParsedAttrInfo::NotHandled)
8394 break;
8395 if (!AL.isStmtAttr()) {
8396 assert(AL.isTypeAttr() && "Non-type attribute not handled");
8398 if (AL.isTypeAttr()) {
8399 if (Options.IgnoreTypeAttributes)
8400 break;
8401 if (!AL.isStandardAttributeSyntax()) {
8402 // Non-[[]] type attributes are handled in processTypeAttrs(); silently
8403 // move on.
8404 break;
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,
8411 // if
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
8420 // portability.
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.
8428 break;
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
8440 // declaration.
8441 break;
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().
8451 break;
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.
8458 break;
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();
8467 break;
8468 case ParsedAttr::AT_Interrupt:
8469 handleInterruptAttr(S, D, AL);
8470 break;
8471 case ParsedAttr::AT_X86ForceAlignArgPointer:
8472 handleX86ForceAlignArgPointerAttr(S, D, AL);
8473 break;
8474 case ParsedAttr::AT_DLLExport:
8475 case ParsedAttr::AT_DLLImport:
8476 handleDLLAttr(S, D, AL);
8477 break;
8478 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
8479 handleAMDGPUFlatWorkGroupSizeAttr(S, D, AL);
8480 break;
8481 case ParsedAttr::AT_AMDGPUWavesPerEU:
8482 handleAMDGPUWavesPerEUAttr(S, D, AL);
8483 break;
8484 case ParsedAttr::AT_AMDGPUNumSGPR:
8485 handleAMDGPUNumSGPRAttr(S, D, AL);
8486 break;
8487 case ParsedAttr::AT_AMDGPUNumVGPR:
8488 handleAMDGPUNumVGPRAttr(S, D, AL);
8489 break;
8490 case ParsedAttr::AT_AVRSignal:
8491 handleAVRSignalAttr(S, D, AL);
8492 break;
8493 case ParsedAttr::AT_BPFPreserveAccessIndex:
8494 handleBPFPreserveAccessIndexAttr(S, D, AL);
8495 break;
8496 case ParsedAttr::AT_BTFDeclTag:
8497 handleBTFDeclTagAttr(S, D, AL);
8498 break;
8499 case ParsedAttr::AT_WebAssemblyExportName:
8500 handleWebAssemblyExportNameAttr(S, D, AL);
8501 break;
8502 case ParsedAttr::AT_WebAssemblyImportModule:
8503 handleWebAssemblyImportModuleAttr(S, D, AL);
8504 break;
8505 case ParsedAttr::AT_WebAssemblyImportName:
8506 handleWebAssemblyImportNameAttr(S, D, AL);
8507 break;
8508 case ParsedAttr::AT_IBOutlet:
8509 handleIBOutlet(S, D, AL);
8510 break;
8511 case ParsedAttr::AT_IBOutletCollection:
8512 handleIBOutletCollection(S, D, AL);
8513 break;
8514 case ParsedAttr::AT_IFunc:
8515 handleIFuncAttr(S, D, AL);
8516 break;
8517 case ParsedAttr::AT_Alias:
8518 handleAliasAttr(S, D, AL);
8519 break;
8520 case ParsedAttr::AT_Aligned:
8521 handleAlignedAttr(S, D, AL);
8522 break;
8523 case ParsedAttr::AT_AlignValue:
8524 handleAlignValueAttr(S, D, AL);
8525 break;
8526 case ParsedAttr::AT_AllocSize:
8527 handleAllocSizeAttr(S, D, AL);
8528 break;
8529 case ParsedAttr::AT_AlwaysInline:
8530 handleAlwaysInlineAttr(S, D, AL);
8531 break;
8532 case ParsedAttr::AT_AnalyzerNoReturn:
8533 handleAnalyzerNoReturnAttr(S, D, AL);
8534 break;
8535 case ParsedAttr::AT_TLSModel:
8536 handleTLSModelAttr(S, D, AL);
8537 break;
8538 case ParsedAttr::AT_Annotate:
8539 handleAnnotateAttr(S, D, AL);
8540 break;
8541 case ParsedAttr::AT_Availability:
8542 handleAvailabilityAttr(S, D, AL);
8543 break;
8544 case ParsedAttr::AT_CarriesDependency:
8545 handleDependencyAttr(S, scope, D, AL);
8546 break;
8547 case ParsedAttr::AT_CPUDispatch:
8548 case ParsedAttr::AT_CPUSpecific:
8549 handleCPUSpecificAttr(S, D, AL);
8550 break;
8551 case ParsedAttr::AT_Common:
8552 handleCommonAttr(S, D, AL);
8553 break;
8554 case ParsedAttr::AT_CUDAConstant:
8555 handleConstantAttr(S, D, AL);
8556 break;
8557 case ParsedAttr::AT_PassObjectSize:
8558 handlePassObjectSizeAttr(S, D, AL);
8559 break;
8560 case ParsedAttr::AT_Constructor:
8561 handleConstructorAttr(S, D, AL);
8562 break;
8563 case ParsedAttr::AT_Deprecated:
8564 handleDeprecatedAttr(S, D, AL);
8565 break;
8566 case ParsedAttr::AT_Destructor:
8567 handleDestructorAttr(S, D, AL);
8568 break;
8569 case ParsedAttr::AT_EnableIf:
8570 handleEnableIfAttr(S, D, AL);
8571 break;
8572 case ParsedAttr::AT_Error:
8573 handleErrorAttr(S, D, AL);
8574 break;
8575 case ParsedAttr::AT_DiagnoseIf:
8576 handleDiagnoseIfAttr(S, D, AL);
8577 break;
8578 case ParsedAttr::AT_DiagnoseAsBuiltin:
8579 handleDiagnoseAsBuiltinAttr(S, D, AL);
8580 break;
8581 case ParsedAttr::AT_NoBuiltin:
8582 handleNoBuiltinAttr(S, D, AL);
8583 break;
8584 case ParsedAttr::AT_ExtVectorType:
8585 handleExtVectorTypeAttr(S, D, AL);
8586 break;
8587 case ParsedAttr::AT_ExternalSourceSymbol:
8588 handleExternalSourceSymbolAttr(S, D, AL);
8589 break;
8590 case ParsedAttr::AT_MinSize:
8591 handleMinSizeAttr(S, D, AL);
8592 break;
8593 case ParsedAttr::AT_OptimizeNone:
8594 handleOptimizeNoneAttr(S, D, AL);
8595 break;
8596 case ParsedAttr::AT_EnumExtensibility:
8597 handleEnumExtensibilityAttr(S, D, AL);
8598 break;
8599 case ParsedAttr::AT_SYCLKernel:
8600 handleSYCLKernelAttr(S, D, AL);
8601 break;
8602 case ParsedAttr::AT_SYCLSpecialClass:
8603 handleSimpleAttribute<SYCLSpecialClassAttr>(S, D, AL);
8604 break;
8605 case ParsedAttr::AT_Format:
8606 handleFormatAttr(S, D, AL);
8607 break;
8608 case ParsedAttr::AT_FormatArg:
8609 handleFormatArgAttr(S, D, AL);
8610 break;
8611 case ParsedAttr::AT_Callback:
8612 handleCallbackAttr(S, D, AL);
8613 break;
8614 case ParsedAttr::AT_CalledOnce:
8615 handleCalledOnceAttr(S, D, AL);
8616 break;
8617 case ParsedAttr::AT_CUDAGlobal:
8618 handleGlobalAttr(S, D, AL);
8619 break;
8620 case ParsedAttr::AT_CUDADevice:
8621 handleDeviceAttr(S, D, AL);
8622 break;
8623 case ParsedAttr::AT_HIPManaged:
8624 handleManagedAttr(S, D, AL);
8625 break;
8626 case ParsedAttr::AT_GNUInline:
8627 handleGNUInlineAttr(S, D, AL);
8628 break;
8629 case ParsedAttr::AT_CUDALaunchBounds:
8630 handleLaunchBoundsAttr(S, D, AL);
8631 break;
8632 case ParsedAttr::AT_Restrict:
8633 handleRestrictAttr(S, D, AL);
8634 break;
8635 case ParsedAttr::AT_Mode:
8636 handleModeAttr(S, D, AL);
8637 break;
8638 case ParsedAttr::AT_NonNull:
8639 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
8640 handleNonNullAttrParameter(S, PVD, AL);
8641 else
8642 handleNonNullAttr(S, D, AL);
8643 break;
8644 case ParsedAttr::AT_ReturnsNonNull:
8645 handleReturnsNonNullAttr(S, D, AL);
8646 break;
8647 case ParsedAttr::AT_NoEscape:
8648 handleNoEscapeAttr(S, D, AL);
8649 break;
8650 case ParsedAttr::AT_MaybeUndef:
8651 handleSimpleAttribute<MaybeUndefAttr>(S, D, AL);
8652 break;
8653 case ParsedAttr::AT_AssumeAligned:
8654 handleAssumeAlignedAttr(S, D, AL);
8655 break;
8656 case ParsedAttr::AT_AllocAlign:
8657 handleAllocAlignAttr(S, D, AL);
8658 break;
8659 case ParsedAttr::AT_Ownership:
8660 handleOwnershipAttr(S, D, AL);
8661 break;
8662 case ParsedAttr::AT_Naked:
8663 handleNakedAttr(S, D, AL);
8664 break;
8665 case ParsedAttr::AT_NoReturn:
8666 handleNoReturnAttr(S, D, AL);
8667 break;
8668 case ParsedAttr::AT_CXX11NoReturn:
8669 handleStandardNoReturnAttr(S, D, AL);
8670 break;
8671 case ParsedAttr::AT_AnyX86NoCfCheck:
8672 handleNoCfCheckAttr(S, D, AL);
8673 break;
8674 case ParsedAttr::AT_NoThrow:
8675 if (!AL.isUsedAsTypeAttr())
8676 handleSimpleAttribute<NoThrowAttr>(S, D, AL);
8677 break;
8678 case ParsedAttr::AT_CUDAShared:
8679 handleSharedAttr(S, D, AL);
8680 break;
8681 case ParsedAttr::AT_VecReturn:
8682 handleVecReturnAttr(S, D, AL);
8683 break;
8684 case ParsedAttr::AT_ObjCOwnership:
8685 handleObjCOwnershipAttr(S, D, AL);
8686 break;
8687 case ParsedAttr::AT_ObjCPreciseLifetime:
8688 handleObjCPreciseLifetimeAttr(S, D, AL);
8689 break;
8690 case ParsedAttr::AT_ObjCReturnsInnerPointer:
8691 handleObjCReturnsInnerPointerAttr(S, D, AL);
8692 break;
8693 case ParsedAttr::AT_ObjCRequiresSuper:
8694 handleObjCRequiresSuperAttr(S, D, AL);
8695 break;
8696 case ParsedAttr::AT_ObjCBridge:
8697 handleObjCBridgeAttr(S, D, AL);
8698 break;
8699 case ParsedAttr::AT_ObjCBridgeMutable:
8700 handleObjCBridgeMutableAttr(S, D, AL);
8701 break;
8702 case ParsedAttr::AT_ObjCBridgeRelated:
8703 handleObjCBridgeRelatedAttr(S, D, AL);
8704 break;
8705 case ParsedAttr::AT_ObjCDesignatedInitializer:
8706 handleObjCDesignatedInitializer(S, D, AL);
8707 break;
8708 case ParsedAttr::AT_ObjCRuntimeName:
8709 handleObjCRuntimeName(S, D, AL);
8710 break;
8711 case ParsedAttr::AT_ObjCBoxable:
8712 handleObjCBoxable(S, D, AL);
8713 break;
8714 case ParsedAttr::AT_NSErrorDomain:
8715 handleNSErrorDomain(S, D, AL);
8716 break;
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);
8722 break;
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());
8728 break;
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());
8734 break;
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);
8743 break;
8744 case ParsedAttr::AT_WorkGroupSizeHint:
8745 handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, AL);
8746 break;
8747 case ParsedAttr::AT_ReqdWorkGroupSize:
8748 handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, AL);
8749 break;
8750 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
8751 handleSubGroupSize(S, D, AL);
8752 break;
8753 case ParsedAttr::AT_VecTypeHint:
8754 handleVecTypeHint(S, D, AL);
8755 break;
8756 case ParsedAttr::AT_InitPriority:
8757 handleInitPriorityAttr(S, D, AL);
8758 break;
8759 case ParsedAttr::AT_Packed:
8760 handlePackedAttr(S, D, AL);
8761 break;
8762 case ParsedAttr::AT_PreferredName:
8763 handlePreferredName(S, D, AL);
8764 break;
8765 case ParsedAttr::AT_Section:
8766 handleSectionAttr(S, D, AL);
8767 break;
8768 case ParsedAttr::AT_RandomizeLayout:
8769 handleRandomizeLayoutAttr(S, D, AL);
8770 break;
8771 case ParsedAttr::AT_NoRandomizeLayout:
8772 handleNoRandomizeLayoutAttr(S, D, AL);
8773 break;
8774 case ParsedAttr::AT_CodeSeg:
8775 handleCodeSegAttr(S, D, AL);
8776 break;
8777 case ParsedAttr::AT_Target:
8778 handleTargetAttr(S, D, AL);
8779 break;
8780 case ParsedAttr::AT_TargetClones:
8781 handleTargetClonesAttr(S, D, AL);
8782 break;
8783 case ParsedAttr::AT_MinVectorWidth:
8784 handleMinVectorWidthAttr(S, D, AL);
8785 break;
8786 case ParsedAttr::AT_Unavailable:
8787 handleAttrWithMessage<UnavailableAttr>(S, D, AL);
8788 break;
8789 case ParsedAttr::AT_Assumption:
8790 handleAssumumptionAttr(S, D, AL);
8791 break;
8792 case ParsedAttr::AT_ObjCDirect:
8793 handleObjCDirectAttr(S, D, AL);
8794 break;
8795 case ParsedAttr::AT_ObjCDirectMembers:
8796 handleObjCDirectMembersAttr(S, D, AL);
8797 handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
8798 break;
8799 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
8800 handleObjCSuppresProtocolAttr(S, D, AL);
8801 break;
8802 case ParsedAttr::AT_Unused:
8803 handleUnusedAttr(S, D, AL);
8804 break;
8805 case ParsedAttr::AT_Visibility:
8806 handleVisibilityAttr(S, D, AL, false);
8807 break;
8808 case ParsedAttr::AT_TypeVisibility:
8809 handleVisibilityAttr(S, D, AL, true);
8810 break;
8811 case ParsedAttr::AT_WarnUnusedResult:
8812 handleWarnUnusedResult(S, D, AL);
8813 break;
8814 case ParsedAttr::AT_WeakRef:
8815 handleWeakRefAttr(S, D, AL);
8816 break;
8817 case ParsedAttr::AT_WeakImport:
8818 handleWeakImportAttr(S, D, AL);
8819 break;
8820 case ParsedAttr::AT_TransparentUnion:
8821 handleTransparentUnionAttr(S, D, AL);
8822 break;
8823 case ParsedAttr::AT_ObjCMethodFamily:
8824 handleObjCMethodFamilyAttr(S, D, AL);
8825 break;
8826 case ParsedAttr::AT_ObjCNSObject:
8827 handleObjCNSObject(S, D, AL);
8828 break;
8829 case ParsedAttr::AT_ObjCIndependentClass:
8830 handleObjCIndependentClass(S, D, AL);
8831 break;
8832 case ParsedAttr::AT_Blocks:
8833 handleBlocksAttr(S, D, AL);
8834 break;
8835 case ParsedAttr::AT_Sentinel:
8836 handleSentinelAttr(S, D, AL);
8837 break;
8838 case ParsedAttr::AT_Cleanup:
8839 handleCleanupAttr(S, D, AL);
8840 break;
8841 case ParsedAttr::AT_NoDebug:
8842 handleNoDebugAttr(S, D, AL);
8843 break;
8844 case ParsedAttr::AT_CmseNSEntry:
8845 handleCmseNSEntryAttr(S, D, AL);
8846 break;
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);
8866 break;
8867 case ParsedAttr::AT_Suppress:
8868 handleSuppressAttr(S, D, AL);
8869 break;
8870 case ParsedAttr::AT_Owner:
8871 case ParsedAttr::AT_Pointer:
8872 handleLifetimeCategoryAttr(S, D, AL);
8873 break;
8874 case ParsedAttr::AT_OpenCLAccess:
8875 handleOpenCLAccessAttr(S, D, AL);
8876 break;
8877 case ParsedAttr::AT_OpenCLNoSVM:
8878 handleOpenCLNoSVMAttr(S, D, AL);
8879 break;
8880 case ParsedAttr::AT_SwiftContext:
8881 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftContext);
8882 break;
8883 case ParsedAttr::AT_SwiftAsyncContext:
8884 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftAsyncContext);
8885 break;
8886 case ParsedAttr::AT_SwiftErrorResult:
8887 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftErrorResult);
8888 break;
8889 case ParsedAttr::AT_SwiftIndirectResult:
8890 S.AddParameterABIAttr(D, AL, ParameterABI::SwiftIndirectResult);
8891 break;
8892 case ParsedAttr::AT_InternalLinkage:
8893 handleInternalLinkageAttr(S, D, AL);
8894 break;
8895 case ParsedAttr::AT_ZeroCallUsedRegs:
8896 handleZeroCallUsedRegsAttr(S, D, AL);
8897 break;
8898 case ParsedAttr::AT_FunctionReturnThunks:
8899 handleFunctionReturnThunksAttr(S, D, AL);
8900 break;
8902 // Microsoft attributes:
8903 case ParsedAttr::AT_LayoutVersion:
8904 handleLayoutVersion(S, D, AL);
8905 break;
8906 case ParsedAttr::AT_Uuid:
8907 handleUuidAttr(S, D, AL);
8908 break;
8909 case ParsedAttr::AT_MSInheritance:
8910 handleMSInheritanceAttr(S, D, AL);
8911 break;
8912 case ParsedAttr::AT_Thread:
8913 handleDeclspecThreadAttr(S, D, AL);
8914 break;
8916 // HLSL attributes:
8917 case ParsedAttr::AT_HLSLNumThreads:
8918 handleHLSLNumThreadsAttr(S, D, AL);
8919 break;
8920 case ParsedAttr::AT_HLSLSV_GroupIndex:
8921 handleHLSLSVGroupIndexAttr(S, D, AL);
8922 break;
8923 case ParsedAttr::AT_HLSLShader:
8924 handleHLSLShaderAttr(S, D, AL);
8925 break;
8927 case ParsedAttr::AT_AbiTag:
8928 handleAbiTagAttr(S, D, AL);
8929 break;
8930 case ParsedAttr::AT_CFGuard:
8931 handleCFGuardAttr(S, D, AL);
8932 break;
8934 // Thread safety attributes:
8935 case ParsedAttr::AT_AssertExclusiveLock:
8936 handleAssertExclusiveLockAttr(S, D, AL);
8937 break;
8938 case ParsedAttr::AT_AssertSharedLock:
8939 handleAssertSharedLockAttr(S, D, AL);
8940 break;
8941 case ParsedAttr::AT_PtGuardedVar:
8942 handlePtGuardedVarAttr(S, D, AL);
8943 break;
8944 case ParsedAttr::AT_NoSanitize:
8945 handleNoSanitizeAttr(S, D, AL);
8946 break;
8947 case ParsedAttr::AT_NoSanitizeSpecific:
8948 handleNoSanitizeSpecificAttr(S, D, AL);
8949 break;
8950 case ParsedAttr::AT_GuardedBy:
8951 handleGuardedByAttr(S, D, AL);
8952 break;
8953 case ParsedAttr::AT_PtGuardedBy:
8954 handlePtGuardedByAttr(S, D, AL);
8955 break;
8956 case ParsedAttr::AT_ExclusiveTrylockFunction:
8957 handleExclusiveTrylockFunctionAttr(S, D, AL);
8958 break;
8959 case ParsedAttr::AT_LockReturned:
8960 handleLockReturnedAttr(S, D, AL);
8961 break;
8962 case ParsedAttr::AT_LocksExcluded:
8963 handleLocksExcludedAttr(S, D, AL);
8964 break;
8965 case ParsedAttr::AT_SharedTrylockFunction:
8966 handleSharedTrylockFunctionAttr(S, D, AL);
8967 break;
8968 case ParsedAttr::AT_AcquiredBefore:
8969 handleAcquiredBeforeAttr(S, D, AL);
8970 break;
8971 case ParsedAttr::AT_AcquiredAfter:
8972 handleAcquiredAfterAttr(S, D, AL);
8973 break;
8975 // Capability analysis attributes.
8976 case ParsedAttr::AT_Capability:
8977 case ParsedAttr::AT_Lockable:
8978 handleCapabilityAttr(S, D, AL);
8979 break;
8980 case ParsedAttr::AT_RequiresCapability:
8981 handleRequiresCapabilityAttr(S, D, AL);
8982 break;
8984 case ParsedAttr::AT_AssertCapability:
8985 handleAssertCapabilityAttr(S, D, AL);
8986 break;
8987 case ParsedAttr::AT_AcquireCapability:
8988 handleAcquireCapabilityAttr(S, D, AL);
8989 break;
8990 case ParsedAttr::AT_ReleaseCapability:
8991 handleReleaseCapabilityAttr(S, D, AL);
8992 break;
8993 case ParsedAttr::AT_TryAcquireCapability:
8994 handleTryAcquireCapabilityAttr(S, D, AL);
8995 break;
8997 // Consumed analysis attributes.
8998 case ParsedAttr::AT_Consumable:
8999 handleConsumableAttr(S, D, AL);
9000 break;
9001 case ParsedAttr::AT_CallableWhen:
9002 handleCallableWhenAttr(S, D, AL);
9003 break;
9004 case ParsedAttr::AT_ParamTypestate:
9005 handleParamTypestateAttr(S, D, AL);
9006 break;
9007 case ParsedAttr::AT_ReturnTypestate:
9008 handleReturnTypestateAttr(S, D, AL);
9009 break;
9010 case ParsedAttr::AT_SetTypestate:
9011 handleSetTypestateAttr(S, D, AL);
9012 break;
9013 case ParsedAttr::AT_TestTypestate:
9014 handleTestTypestateAttr(S, D, AL);
9015 break;
9017 // Type safety attributes.
9018 case ParsedAttr::AT_ArgumentWithTypeTag:
9019 handleArgumentWithTypeTagAttr(S, D, AL);
9020 break;
9021 case ParsedAttr::AT_TypeTagForDatatype:
9022 handleTypeTagForDatatypeAttr(S, D, AL);
9023 break;
9025 // Swift attributes.
9026 case ParsedAttr::AT_SwiftAsyncName:
9027 handleSwiftAsyncName(S, D, AL);
9028 break;
9029 case ParsedAttr::AT_SwiftAttr:
9030 handleSwiftAttrAttr(S, D, AL);
9031 break;
9032 case ParsedAttr::AT_SwiftBridge:
9033 handleSwiftBridge(S, D, AL);
9034 break;
9035 case ParsedAttr::AT_SwiftError:
9036 handleSwiftError(S, D, AL);
9037 break;
9038 case ParsedAttr::AT_SwiftName:
9039 handleSwiftName(S, D, AL);
9040 break;
9041 case ParsedAttr::AT_SwiftNewType:
9042 handleSwiftNewType(S, D, AL);
9043 break;
9044 case ParsedAttr::AT_SwiftAsync:
9045 handleSwiftAsyncAttr(S, D, AL);
9046 break;
9047 case ParsedAttr::AT_SwiftAsyncError:
9048 handleSwiftAsyncError(S, D, AL);
9049 break;
9051 // XRay attributes.
9052 case ParsedAttr::AT_XRayLogArgs:
9053 handleXRayLogArgsAttr(S, D, AL);
9054 break;
9056 case ParsedAttr::AT_PatchableFunctionEntry:
9057 handlePatchableFunctionEntryAttr(S, D, AL);
9058 break;
9060 case ParsedAttr::AT_AlwaysDestroy:
9061 case ParsedAttr::AT_NoDestroy:
9062 handleDestroyAttr(S, D, AL);
9063 break;
9065 case ParsedAttr::AT_Uninitialized:
9066 handleUninitializedAttr(S, D, AL);
9067 break;
9069 case ParsedAttr::AT_ObjCExternallyRetained:
9070 handleObjCExternallyRetainedAttr(S, D, AL);
9071 break;
9073 case ParsedAttr::AT_MIGServerRoutine:
9074 handleMIGServerRoutineAttr(S, D, AL);
9075 break;
9077 case ParsedAttr::AT_MSAllocator:
9078 handleMSAllocatorAttr(S, D, AL);
9079 break;
9081 case ParsedAttr::AT_ArmBuiltinAlias:
9082 handleArmBuiltinAliasAttr(S, D, AL);
9083 break;
9085 case ParsedAttr::AT_AcquireHandle:
9086 handleAcquireHandleAttr(S, D, AL);
9087 break;
9089 case ParsedAttr::AT_ReleaseHandle:
9090 handleHandleAttr<ReleaseHandleAttr>(S, D, AL);
9091 break;
9093 case ParsedAttr::AT_UseHandle:
9094 handleHandleAttr<UseHandleAttr>(S, D, AL);
9095 break;
9097 case ParsedAttr::AT_EnforceTCB:
9098 handleEnforceTCBAttr<EnforceTCBAttr, EnforceTCBLeafAttr>(S, D, AL);
9099 break;
9101 case ParsedAttr::AT_EnforceTCBLeaf:
9102 handleEnforceTCBAttr<EnforceTCBLeafAttr, EnforceTCBAttr>(S, D, AL);
9103 break;
9105 case ParsedAttr::AT_BuiltinAlias:
9106 handleBuiltinAliasAttr(S, D, AL);
9107 break;
9109 case ParsedAttr::AT_UsingIfExists:
9110 handleSimpleAttribute<UsingIfExistsAttr>(S, D, AL);
9111 break;
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())
9121 return;
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.
9127 // GCC accepts
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>();
9134 return;
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
9191 // attribute.
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);
9197 break;
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
9207 // specifier.
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());
9214 } else {
9215 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
9216 return true;
9219 return false;
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())
9228 continue;
9229 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
9230 continue;
9232 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
9233 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
9234 << AL << AL.getRange();
9235 } else {
9236 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
9237 << AL.getRange();
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().
9262 // FIXME: Mangling?
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());
9271 NewD = NewFD;
9273 if (FD->getQualifier())
9274 NewFD->setQualifierInfo(FD->getQualifierLoc());
9276 // Fake up parameter variables; they are declared as if this were
9277 // a typedef.
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());
9296 return NewD;
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());
9305 NewD->addAttr(
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
9326 // have to do this.
9327 LoadExternalWeakUndeclaredIdentifiers();
9328 if (WeakUndeclaredIdentifiers.empty())
9329 return;
9330 NamedDecl *ND = nullptr;
9331 if (auto *VD = dyn_cast<VarDecl>(D))
9332 if (VD->isExternC())
9333 ND = VD;
9334 if (auto *FD = dyn_cast<FunctionDecl>(D))
9335 if (FD->isExternC())
9336 ND = FD;
9337 if (!ND)
9338 return;
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);
9368 } else {
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))
9411 return false;
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
9417 // and properties.
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;
9422 return true;
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
9429 // restrictions.
9430 reason = UnavailableAttr::IR_ARCForbiddenType;
9431 return true;
9434 return false;
9437 /// Handle a delayed forbidden-type diagnostic.
9438 static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD,
9439 Decl *D) {
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));
9444 return;
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;
9454 return;
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
9471 // succeeds.
9472 if (!decl) return;
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;
9482 do {
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);
9488 if (diag.Triggered)
9489 continue;
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);
9497 break;
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))
9504 continue;
9505 HandleDelayedAccessCheck(diag, decl);
9506 if (diag.Triggered)
9507 AnyAccessFailures = true;
9508 break;
9510 case DelayedDiagnostic::ForbiddenType:
9511 handleDelayedForbiddenType(*this, diag, decl);
9512 break;
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);