clang 17.0.0git
SemaDeclAttr.cpp
Go to the documentation of this file.
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.
10//
11//===----------------------------------------------------------------------===//
12
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/Mangle.h"
24#include "clang/AST/Type.h"
34#include "clang/Sema/DeclSpec.h"
37#include "clang/Sema/Lookup.h"
39#include "clang/Sema/Scope.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"
49#include <optional>
50
51using namespace clang;
52using namespace sema;
53
55 enum LANG {
58 ObjC
59 };
60} // end namespace AttributeLangSupport
61
62//===----------------------------------------------------------------------===//
63// Helper functions
64//===----------------------------------------------------------------------===//
65
66/// isFunctionOrMethod - Return true if the given decl has function
67/// type (function or function-typed variable) or an Objective-C
68/// method.
69static bool isFunctionOrMethod(const Decl *D) {
70 return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D);
71}
72
73/// Return true if the given decl has function type (function or
74/// function-typed variable) or an Objective-C method or a block.
75static bool isFunctionOrMethodOrBlock(const Decl *D) {
76 return isFunctionOrMethod(D) || isa<BlockDecl>(D);
77}
78
79/// Return true if the given decl has a declarator that should have
80/// been processed by Sema::GetTypeForDeclarator.
81static bool hasDeclarator(const Decl *D) {
82 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
83 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
84 isa<ObjCPropertyDecl>(D);
85}
86
87/// hasFunctionProto - Return true if the given decl has a argument
88/// information. This decl should have already passed
89/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
90static bool hasFunctionProto(const Decl *D) {
91 if (const FunctionType *FnTy = D->getFunctionType())
92 return isa<FunctionProtoType>(FnTy);
93 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
94}
95
96/// getFunctionOrMethodNumParams - Return number of function or method
97/// parameters. It is an error to call this on a K&R function (use
98/// hasFunctionProto first).
99static unsigned getFunctionOrMethodNumParams(const Decl *D) {
100 if (const FunctionType *FnTy = D->getFunctionType())
101 return cast<FunctionProtoType>(FnTy)->getNumParams();
102 if (const auto *BD = dyn_cast<BlockDecl>(D))
103 return BD->getNumParams();
104 return cast<ObjCMethodDecl>(D)->param_size();
105}
106
108 unsigned Idx) {
109 if (const auto *FD = dyn_cast<FunctionDecl>(D))
110 return FD->getParamDecl(Idx);
111 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
112 return MD->getParamDecl(Idx);
113 if (const auto *BD = dyn_cast<BlockDecl>(D))
114 return BD->getParamDecl(Idx);
115 return nullptr;
116}
117
118static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
119 if (const FunctionType *FnTy = D->getFunctionType())
120 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
121 if (const auto *BD = dyn_cast<BlockDecl>(D))
122 return BD->getParamDecl(Idx)->getType();
123
124 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
125}
126
127static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
128 if (auto *PVD = getFunctionOrMethodParam(D, Idx))
129 return PVD->getSourceRange();
130 return SourceRange();
131}
132
134 if (const FunctionType *FnTy = D->getFunctionType())
135 return FnTy->getReturnType();
136 return cast<ObjCMethodDecl>(D)->getReturnType();
137}
138
140 if (const auto *FD = dyn_cast<FunctionDecl>(D))
141 return FD->getReturnTypeSourceRange();
142 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
143 return MD->getReturnTypeSourceRange();
144 return SourceRange();
145}
146
147static bool isFunctionOrMethodVariadic(const Decl *D) {
148 if (const FunctionType *FnTy = D->getFunctionType())
149 return cast<FunctionProtoType>(FnTy)->isVariadic();
150 if (const auto *BD = dyn_cast<BlockDecl>(D))
151 return BD->isVariadic();
152 return cast<ObjCMethodDecl>(D)->isVariadic();
153}
154
155static bool isInstanceMethod(const Decl *D) {
156 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
157 return MethodDecl->isInstance();
158 return false;
159}
160
161static inline bool isNSStringType(QualType T, ASTContext &Ctx,
162 bool AllowNSAttributedString = false) {
163 const auto *PT = T->getAs<ObjCObjectPointerType>();
164 if (!PT)
165 return false;
166
167 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
168 if (!Cls)
169 return false;
170
171 IdentifierInfo* ClsName = Cls->getIdentifier();
172
173 if (AllowNSAttributedString &&
174 ClsName == &Ctx.Idents.get("NSAttributedString"))
175 return true;
176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
182 const auto *PT = T->getAs<PointerType>();
183 if (!PT)
184 return false;
185
186 const auto *RT = PT->getPointeeType()->getAs<RecordType>();
187 if (!RT)
188 return false;
189
190 const RecordDecl *RD = RT->getDecl();
191 if (RD->getTagKind() != TTK_Struct)
192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
197static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
198 // FIXME: Include the type in the argument list.
199 return AL.getNumArgs() + AL.hasParsedType();
200}
201
202/// A helper function to provide Attribute Location for the Attr types
203/// AND the ParsedAttr.
204template <typename AttrInfo>
205static std::enable_if_t<std::is_base_of_v<Attr, AttrInfo>, SourceLocation>
206getAttrLoc(const AttrInfo &AL) {
207 return AL.getLocation();
208}
209static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
210
211/// If Expr is a valid integer constant, get the value of the integer
212/// expression and return success or failure. May output an error.
213///
214/// Negative argument is implicitly converted to unsigned, unless
215/// \p StrictlyUnsigned is true.
216template <typename AttrInfo>
217static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
218 uint32_t &Val, unsigned Idx = UINT_MAX,
219 bool StrictlyUnsigned = false) {
220 std::optional<llvm::APSInt> I = llvm::APSInt(32);
221 if (Expr->isTypeDependent() ||
223 if (Idx != UINT_MAX)
224 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
225 << &AI << Idx << AANT_ArgumentIntegerConstant
226 << Expr->getSourceRange();
227 else
228 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
230 return false;
231 }
232
233 if (!I->isIntN(32)) {
234 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
235 << toString(*I, 10, false) << 32 << /* Unsigned */ 1;
236 return false;
237 }
238
239 if (StrictlyUnsigned && I->isSigned() && I->isNegative()) {
240 S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
241 << &AI << /*non-negative*/ 1;
242 return false;
243 }
244
245 Val = (uint32_t)I->getZExtValue();
246 return true;
247}
248
249/// Wrapper around checkUInt32Argument, with an extra check to be sure
250/// that the result will fit into a regular (signed) int. All args have the same
251/// purpose as they do in checkUInt32Argument.
252template <typename AttrInfo>
253static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
254 int &Val, unsigned Idx = UINT_MAX) {
255 uint32_t UVal;
256 if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
257 return false;
258
259 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
260 llvm::APSInt I(32); // for toString
261 I = UVal;
262 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
263 << toString(I, 10, false) << 32 << /* Unsigned */ 0;
264 return false;
265 }
266
267 Val = UVal;
268 return true;
269}
270
271/// Diagnose mutually exclusive attributes when present on a given
272/// declaration. Returns true if diagnosed.
273template <typename AttrTy>
274static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
275 if (const auto *A = D->getAttr<AttrTy>()) {
276 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
277 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
278 return true;
279 }
280 return false;
281}
282
283template <typename AttrTy>
284static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
285 if (const auto *A = D->getAttr<AttrTy>()) {
286 S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
287 << A;
288 S.Diag(A->getLocation(), diag::note_conflicting_attribute);
289 return true;
290 }
291 return false;
292}
293
294/// Check if IdxExpr is a valid parameter index for a function or
295/// instance method D. May output an error.
296///
297/// \returns true if IdxExpr is a valid index.
298template <typename AttrInfo>
300 Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
301 const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
302 assert(isFunctionOrMethodOrBlock(D));
303
304 // In C++ the implicit 'this' function parameter also counts.
305 // Parameters are counted from one.
306 bool HP = hasFunctionProto(D);
307 bool HasImplicitThisParam = isInstanceMethod(D);
308 bool IV = HP && isFunctionOrMethodVariadic(D);
309 unsigned NumParams =
310 (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam;
311
312 std::optional<llvm::APSInt> IdxInt;
313 if (IdxExpr->isTypeDependent() ||
314 !(IdxInt = IdxExpr->getIntegerConstantExpr(S.Context))) {
315 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
316 << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
317 << IdxExpr->getSourceRange();
318 return false;
319 }
320
321 unsigned IdxSource = IdxInt->getLimitedValue(UINT_MAX);
322 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
323 S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
324 << &AI << AttrArgNum << IdxExpr->getSourceRange();
325 return false;
326 }
327 if (HasImplicitThisParam && !CanIndexImplicitThis) {
328 if (IdxSource == 1) {
329 S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
330 << &AI << IdxExpr->getSourceRange();
331 return false;
332 }
333 }
334
335 Idx = ParamIdx(IdxSource, D);
336 return true;
337}
338
339/// Check if the argument \p E is a ASCII string literal. If not emit an error
340/// and return false, otherwise set \p Str to the value of the string literal
341/// and return true.
343 const Expr *E, StringRef &Str,
344 SourceLocation *ArgLocation) {
345 const auto *Literal = dyn_cast<StringLiteral>(E->IgnoreParenCasts());
346 if (ArgLocation)
347 *ArgLocation = E->getBeginLoc();
348
349 if (!Literal || !Literal->isOrdinary()) {
350 Diag(E->getBeginLoc(), diag::err_attribute_argument_type)
351 << CI << AANT_ArgumentString;
352 return false;
353 }
354
355 Str = Literal->getString();
356 return true;
357}
358
359/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
360/// If not emit an error and return false. If the argument is an identifier it
361/// will emit an error with a fixit hint and treat it as if it was a string
362/// literal.
363bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
364 StringRef &Str,
365 SourceLocation *ArgLocation) {
366 // Look for identifiers. If we have one emit a hint to fix it to a literal.
367 if (AL.isArgIdent(ArgNum)) {
368 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
369 Diag(Loc->Loc, diag::err_attribute_argument_type)
370 << AL << AANT_ArgumentString
371 << FixItHint::CreateInsertion(Loc->Loc, "\"")
373 Str = Loc->Ident->getName();
374 if (ArgLocation)
375 *ArgLocation = Loc->Loc;
376 return true;
377 }
378
379 // Now check for an actual string literal.
380 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
381 return checkStringLiteralArgumentAttr(AL, ArgExpr, Str, ArgLocation);
382}
383
384/// Applies the given attribute to the Decl without performing any
385/// additional semantic checking.
386template <typename AttrType>
387static void handleSimpleAttribute(Sema &S, Decl *D,
388 const AttributeCommonInfo &CI) {
389 D->addAttr(::new (S.Context) AttrType(S.Context, CI));
390}
391
392template <typename... DiagnosticArgs>
393static const Sema::SemaDiagnosticBuilder&
395 return Bldr;
396}
397
398template <typename T, typename... DiagnosticArgs>
399static const Sema::SemaDiagnosticBuilder&
401 DiagnosticArgs &&... ExtraArgs) {
402 return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
403 std::forward<DiagnosticArgs>(ExtraArgs)...);
404}
405
406/// Add an attribute @c AttrType to declaration @c D, provided that
407/// @c PassesCheck is true.
408/// Otherwise, emit diagnostic @c DiagID, passing in all parameters
409/// specified in @c ExtraArgs.
410template <typename AttrType, typename... DiagnosticArgs>
412 const AttributeCommonInfo &CI,
413 bool PassesCheck, unsigned DiagID,
414 DiagnosticArgs &&... ExtraArgs) {
415 if (!PassesCheck) {
416 Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
417 appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
418 return;
419 }
420 handleSimpleAttribute<AttrType>(S, D, CI);
421}
422
423/// Check if the passed-in expression is of type int or bool.
424static bool isIntOrBool(Expr *Exp) {
425 QualType QT = Exp->getType();
426 return QT->isBooleanType() || QT->isIntegerType();
427}
428
429
430// Check to see if the type is a smart pointer of some kind. We assume
431// it's a smart pointer if it defines both operator-> and operator*.
433 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
436 Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
437 return !Result.empty();
438 };
439
440 const RecordDecl *Record = RT->getDecl();
441 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
442 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
443 if (foundStarOperator && foundArrowOperator)
444 return true;
445
446 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
447 if (!CXXRecord)
448 return false;
449
450 for (auto BaseSpecifier : CXXRecord->bases()) {
451 if (!foundStarOperator)
452 foundStarOperator = IsOverloadedOperatorPresent(
453 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
454 if (!foundArrowOperator)
455 foundArrowOperator = IsOverloadedOperatorPresent(
456 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
457 }
458
459 if (foundStarOperator && foundArrowOperator)
460 return true;
461
462 return false;
463}
464
465/// Check if passed in Decl is a pointer type.
466/// Note that this function may produce an error message.
467/// \return true if the Decl is a pointer type; false otherwise
468static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
469 const ParsedAttr &AL) {
470 const auto *VD = cast<ValueDecl>(D);
471 QualType QT = VD->getType();
472 if (QT->isAnyPointerType())
473 return true;
474
475 if (const auto *RT = QT->getAs<RecordType>()) {
476 // If it's an incomplete type, it could be a smart pointer; skip it.
477 // (We don't want to force template instantiation if we can avoid it,
478 // since that would alter the order in which templates are instantiated.)
479 if (RT->isIncompleteType())
480 return true;
481
483 return true;
484 }
485
486 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
487 return false;
488}
489
490/// Checks that the passed in QualType either is of RecordType or points
491/// to RecordType. Returns the relevant RecordType, null if it does not exit.
493 if (const auto *RT = QT->getAs<RecordType>())
494 return RT;
495
496 // Now check if we point to record type.
497 if (const auto *PT = QT->getAs<PointerType>())
498 return PT->getPointeeType()->getAs<RecordType>();
499
500 return nullptr;
501}
502
503template <typename AttrType>
504static bool checkRecordDeclForAttr(const RecordDecl *RD) {
505 // Check if the record itself has the attribute.
506 if (RD->hasAttr<AttrType>())
507 return true;
508
509 // Else check if any base classes have the attribute.
510 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
511 if (!CRD->forallBases([](const CXXRecordDecl *Base) {
512 return !Base->hasAttr<AttrType>();
513 }))
514 return true;
515 }
516 return false;
517}
518
520 const RecordType *RT = getRecordType(Ty);
521
522 if (!RT)
523 return false;
524
525 // Don't check for the capability if the class hasn't been defined yet.
526 if (RT->isIncompleteType())
527 return true;
528
529 // Allow smart pointers to be used as capability objects.
530 // FIXME -- Check the type that the smart pointer points to.
532 return true;
533
534 return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
535}
536
538 const auto *TD = Ty->getAs<TypedefType>();
539 if (!TD)
540 return false;
541
542 TypedefNameDecl *TN = TD->getDecl();
543 if (!TN)
544 return false;
545
546 return TN->hasAttr<CapabilityAttr>();
547}
548
549static bool typeHasCapability(Sema &S, QualType Ty) {
551 return true;
552
554 return true;
555
556 return false;
557}
558
559static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
560 // Capability expressions are simple expressions involving the boolean logic
561 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
562 // a DeclRefExpr is found, its type should be checked to determine whether it
563 // is a capability or not.
564
565 if (const auto *E = dyn_cast<CastExpr>(Ex))
566 return isCapabilityExpr(S, E->getSubExpr());
567 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
568 return isCapabilityExpr(S, E->getSubExpr());
569 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
570 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
571 E->getOpcode() == UO_Deref)
572 return isCapabilityExpr(S, E->getSubExpr());
573 return false;
574 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
575 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
576 return isCapabilityExpr(S, E->getLHS()) &&
577 isCapabilityExpr(S, E->getRHS());
578 return false;
579 }
580
581 return typeHasCapability(S, Ex->getType());
582}
583
584/// Checks that all attribute arguments, starting from Sidx, resolve to
585/// a capability object.
586/// \param Sidx The attribute argument index to start checking with.
587/// \param ParamIdxOk Whether an argument can be indexing into a function
588/// parameter list.
590 const ParsedAttr &AL,
592 unsigned Sidx = 0,
593 bool ParamIdxOk = false) {
594 if (Sidx == AL.getNumArgs()) {
595 // If we don't have any capability arguments, the attribute implicitly
596 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
597 // a non-static method, and that the class is a (scoped) capability.
598 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
599 if (MD && !MD->isStatic()) {
600 const CXXRecordDecl *RD = MD->getParent();
601 // FIXME -- need to check this again on template instantiation
602 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
603 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
604 S.Diag(AL.getLoc(),
605 diag::warn_thread_attribute_not_on_capability_member)
606 << AL << MD->getParent();
607 } else {
608 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
609 << AL;
610 }
611 }
612
613 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
614 Expr *ArgExp = AL.getArgAsExpr(Idx);
615
616 if (ArgExp->isTypeDependent()) {
617 // FIXME -- need to check this again on template instantiation
618 Args.push_back(ArgExp);
619 continue;
620 }
621
622 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
623 if (StrLit->getLength() == 0 ||
624 (StrLit->isOrdinary() && StrLit->getString() == StringRef("*"))) {
625 // Pass empty strings to the analyzer without warnings.
626 // Treat "*" as the universal lock.
627 Args.push_back(ArgExp);
628 continue;
629 }
630
631 // We allow constant strings to be used as a placeholder for expressions
632 // that are not valid C++ syntax, but warn that they are ignored.
633 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
634 Args.push_back(ArgExp);
635 continue;
636 }
637
638 QualType ArgTy = ArgExp->getType();
639
640 // A pointer to member expression of the form &MyClass::mu is treated
641 // specially -- we need to look at the type of the member.
642 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
643 if (UOp->getOpcode() == UO_AddrOf)
644 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
645 if (DRE->getDecl()->isCXXInstanceMember())
646 ArgTy = DRE->getDecl()->getType();
647
648 // First see if we can just cast to record type, or pointer to record type.
649 const RecordType *RT = getRecordType(ArgTy);
650
651 // Now check if we index into a record type function param.
652 if(!RT && ParamIdxOk) {
653 const auto *FD = dyn_cast<FunctionDecl>(D);
654 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
655 if(FD && IL) {
656 unsigned int NumParams = FD->getNumParams();
657 llvm::APInt ArgValue = IL->getValue();
658 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
659 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
660 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
661 S.Diag(AL.getLoc(),
662 diag::err_attribute_argument_out_of_bounds_extra_info)
663 << AL << Idx + 1 << NumParams;
664 continue;
665 }
666 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
667 }
668 }
669
670 // If the type does not have a capability, see if the components of the
671 // expression have capabilities. This allows for writing C code where the
672 // capability may be on the type, and the expression is a capability
673 // boolean logic expression. Eg) requires_capability(A || B && !C)
674 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
675 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
676 << AL << ArgTy;
677
678 Args.push_back(ArgExp);
679 }
680}
681
682//===----------------------------------------------------------------------===//
683// Attribute Implementations
684//===----------------------------------------------------------------------===//
685
686static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
687 if (!threadSafetyCheckIsPointer(S, D, AL))
688 return;
689
690 D->addAttr(::new (S.Context) PtGuardedVarAttr(S.Context, AL));
691}
692
693static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
694 Expr *&Arg) {
696 // check that all arguments are lockable objects
697 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
698 unsigned Size = Args.size();
699 if (Size != 1)
700 return false;
701
702 Arg = Args[0];
703
704 return true;
705}
706
707static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
708 Expr *Arg = nullptr;
709 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
710 return;
711
712 D->addAttr(::new (S.Context) GuardedByAttr(S.Context, AL, Arg));
713}
714
715static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
716 Expr *Arg = nullptr;
717 if (!checkGuardedByAttrCommon(S, D, AL, Arg))
718 return;
719
720 if (!threadSafetyCheckIsPointer(S, D, AL))
721 return;
722
723 D->addAttr(::new (S.Context) PtGuardedByAttr(S.Context, AL, Arg));
724}
725
726static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
728 if (!AL.checkAtLeastNumArgs(S, 1))
729 return false;
730
731 // Check that this attribute only applies to lockable types.
732 QualType QT = cast<ValueDecl>(D)->getType();
733 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
734 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
735 return false;
736 }
737
738 // Check that all arguments are lockable objects.
739 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
740 if (Args.empty())
741 return false;
742
743 return true;
744}
745
746static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
748 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
749 return;
750
751 Expr **StartArg = &Args[0];
752 D->addAttr(::new (S.Context)
753 AcquiredAfterAttr(S.Context, AL, StartArg, Args.size()));
754}
755
756static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
758 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
759 return;
760
761 Expr **StartArg = &Args[0];
762 D->addAttr(::new (S.Context)
763 AcquiredBeforeAttr(S.Context, AL, StartArg, Args.size()));
764}
765
766static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
768 // zero or more arguments ok
769 // check that all arguments are lockable objects
770 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
771
772 return true;
773}
774
775static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
777 if (!checkLockFunAttrCommon(S, D, AL, Args))
778 return;
779
780 unsigned Size = Args.size();
781 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
782 D->addAttr(::new (S.Context)
783 AssertSharedLockAttr(S.Context, AL, StartArg, Size));
784}
785
787 const ParsedAttr &AL) {
789 if (!checkLockFunAttrCommon(S, D, AL, Args))
790 return;
791
792 unsigned Size = Args.size();
793 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
794 D->addAttr(::new (S.Context)
795 AssertExclusiveLockAttr(S.Context, AL, StartArg, Size));
796}
797
798/// Checks to be sure that the given parameter number is in bounds, and
799/// is an integral type. Will emit appropriate diagnostics if this returns
800/// false.
801///
802/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
803template <typename AttrInfo>
804static bool checkParamIsIntegerType(Sema &S, const Decl *D, const AttrInfo &AI,
805 unsigned AttrArgNo) {
806 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
807 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
808 ParamIdx Idx;
809 if (!checkFunctionOrMethodParameterIndex(S, D, AI, AttrArgNo + 1, AttrArg,
810 Idx))
811 return false;
812
814 if (!ParamTy->isIntegerType() && !ParamTy->isCharType()) {
815 SourceLocation SrcLoc = AttrArg->getBeginLoc();
816 S.Diag(SrcLoc, diag::err_attribute_integers_only)
818 return false;
819 }
820 return true;
821}
822
823static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
824 if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 2))
825 return;
826
827 assert(isFunctionOrMethod(D) && hasFunctionProto(D));
828
830 if (!RetTy->isPointerType()) {
831 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
832 return;
833 }
834
835 const Expr *SizeExpr = AL.getArgAsExpr(0);
836 int SizeArgNoVal;
837 // Parameter indices are 1-indexed, hence Index=1
838 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1))
839 return;
840 if (!checkParamIsIntegerType(S, D, AL, /*AttrArgNo=*/0))
841 return;
842 ParamIdx SizeArgNo(SizeArgNoVal, D);
843
844 ParamIdx NumberArgNo;
845 if (AL.getNumArgs() == 2) {
846 const Expr *NumberExpr = AL.getArgAsExpr(1);
847 int Val;
848 // Parameter indices are 1-based, hence Index=2
849 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2))
850 return;
851 if (!checkParamIsIntegerType(S, D, AL, /*AttrArgNo=*/1))
852 return;
853 NumberArgNo = ParamIdx(Val, D);
854 }
855
856 D->addAttr(::new (S.Context)
857 AllocSizeAttr(S.Context, AL, SizeArgNo, NumberArgNo));
858}
859
860static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
862 if (!AL.checkAtLeastNumArgs(S, 1))
863 return false;
864
865 if (!isIntOrBool(AL.getArgAsExpr(0))) {
866 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
867 << AL << 1 << AANT_ArgumentIntOrBool;
868 return false;
869 }
870
871 // check that all arguments are lockable objects
872 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
873
874 return true;
875}
876
878 const ParsedAttr &AL) {
880 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
881 return;
882
883 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
884 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
885}
886
888 const ParsedAttr &AL) {
890 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
891 return;
892
893 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
894 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
895}
896
897static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
898 // check that the argument is lockable object
900 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
901 unsigned Size = Args.size();
902 if (Size == 0)
903 return;
904
905 D->addAttr(::new (S.Context) LockReturnedAttr(S.Context, AL, Args[0]));
906}
907
908static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
909 if (!AL.checkAtLeastNumArgs(S, 1))
910 return;
911
912 // check that all arguments are lockable objects
914 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
915 unsigned Size = Args.size();
916 if (Size == 0)
917 return;
918 Expr **StartArg = &Args[0];
919
920 D->addAttr(::new (S.Context)
921 LocksExcludedAttr(S.Context, AL, StartArg, Size));
922}
923
924static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
925 Expr *&Cond, StringRef &Msg) {
926 Cond = AL.getArgAsExpr(0);
927 if (!Cond->isTypeDependent()) {
929 if (Converted.isInvalid())
930 return false;
931 Cond = Converted.get();
932 }
933
934 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
935 return false;
936
937 if (Msg.empty())
938 Msg = "<no message provided>";
939
941 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
942 !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
943 Diags)) {
944 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
945 for (const PartialDiagnosticAt &PDiag : Diags)
946 S.Diag(PDiag.first, PDiag.second);
947 return false;
948 }
949 return true;
950}
951
952static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
953 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
954
955 Expr *Cond;
956 StringRef Msg;
957 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
958 D->addAttr(::new (S.Context) EnableIfAttr(S.Context, AL, Cond, Msg));
959}
960
961static void handleErrorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
962 StringRef NewUserDiagnostic;
963 if (!S.checkStringLiteralArgumentAttr(AL, 0, NewUserDiagnostic))
964 return;
965 if (ErrorAttr *EA = S.mergeErrorAttr(D, AL, NewUserDiagnostic))
966 D->addAttr(EA);
967}
968
969namespace {
970/// Determines if a given Expr references any of the given function's
971/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
972class ArgumentDependenceChecker
973 : public RecursiveASTVisitor<ArgumentDependenceChecker> {
974#ifndef NDEBUG
975 const CXXRecordDecl *ClassType;
976#endif
978 bool Result;
979
980public:
981 ArgumentDependenceChecker(const FunctionDecl *FD) {
982#ifndef NDEBUG
983 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
984 ClassType = MD->getParent();
985 else
986 ClassType = nullptr;
987#endif
988 Parms.insert(FD->param_begin(), FD->param_end());
989 }
990
991 bool referencesArgs(Expr *E) {
992 Result = false;
993 TraverseStmt(E);
994 return Result;
995 }
996
997 bool VisitCXXThisExpr(CXXThisExpr *E) {
998 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
999 "`this` doesn't refer to the enclosing class?");
1000 Result = true;
1001 return false;
1002 }
1003
1004 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1005 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1006 if (Parms.count(PVD)) {
1007 Result = true;
1008 return false;
1009 }
1010 return true;
1011 }
1012};
1013}
1014
1016 const ParsedAttr &AL) {
1017 const auto *DeclFD = cast<FunctionDecl>(D);
1018
1019 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(DeclFD))
1020 if (!MethodDecl->isStatic()) {
1021 S.Diag(AL.getLoc(), diag::err_attribute_no_member_function) << AL;
1022 return;
1023 }
1024
1025 auto DiagnoseType = [&](unsigned Index, AttributeArgumentNType T) {
1026 SourceLocation Loc = [&]() {
1027 auto Union = AL.getArg(Index - 1);
1028 if (Union.is<Expr *>())
1029 return Union.get<Expr *>()->getBeginLoc();
1030 return Union.get<IdentifierLoc *>()->Loc;
1031 }();
1032
1033 S.Diag(Loc, diag::err_attribute_argument_n_type) << AL << Index << T;
1034 };
1035
1036 FunctionDecl *AttrFD = [&]() -> FunctionDecl * {
1037 if (!AL.isArgExpr(0))
1038 return nullptr;
1039 auto *F = dyn_cast_or_null<DeclRefExpr>(AL.getArgAsExpr(0));
1040 if (!F)
1041 return nullptr;
1042 return dyn_cast_or_null<FunctionDecl>(F->getFoundDecl());
1043 }();
1044
1045 if (!AttrFD || !AttrFD->getBuiltinID(true)) {
1046 DiagnoseType(1, AANT_ArgumentBuiltinFunction);
1047 return;
1048 }
1049
1050 if (AttrFD->getNumParams() != AL.getNumArgs() - 1) {
1051 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments_for)
1052 << AL << AttrFD << AttrFD->getNumParams();
1053 return;
1054 }
1055
1057
1058 for (unsigned I = 1; I < AL.getNumArgs(); ++I) {
1059 if (!AL.isArgExpr(I)) {
1060 DiagnoseType(I + 1, AANT_ArgumentIntegerConstant);
1061 return;
1062 }
1063
1064 const Expr *IndexExpr = AL.getArgAsExpr(I);
1065 uint32_t Index;
1066
1067 if (!checkUInt32Argument(S, AL, IndexExpr, Index, I + 1, false))
1068 return;
1069
1070 if (Index > DeclFD->getNumParams()) {
1071 S.Diag(AL.getLoc(), diag::err_attribute_bounds_for_function)
1072 << AL << Index << DeclFD << DeclFD->getNumParams();
1073 return;
1074 }
1075
1076 QualType T1 = AttrFD->getParamDecl(I - 1)->getType();
1077 QualType T2 = DeclFD->getParamDecl(Index - 1)->getType();
1078
1081 S.Diag(IndexExpr->getBeginLoc(), diag::err_attribute_parameter_types)
1082 << AL << Index << DeclFD << T2 << I << AttrFD << T1;
1083 return;
1084 }
1085
1086 Indices.push_back(Index - 1);
1087 }
1088
1089 D->addAttr(::new (S.Context) DiagnoseAsBuiltinAttr(
1090 S.Context, AL, AttrFD, Indices.data(), Indices.size()));
1091}
1092
1093static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1094 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
1095
1096 Expr *Cond;
1097 StringRef Msg;
1098 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
1099 return;
1100
1101 StringRef DiagTypeStr;
1102 if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
1103 return;
1104
1105 DiagnoseIfAttr::DiagnosticType DiagType;
1106 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
1107 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
1108 diag::err_diagnose_if_invalid_diagnostic_type);
1109 return;
1110 }
1111
1112 bool ArgDependent = false;
1113 if (const auto *FD = dyn_cast<FunctionDecl>(D))
1114 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
1115 D->addAttr(::new (S.Context) DiagnoseIfAttr(
1116 S.Context, AL, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D)));
1117}
1118
1119static void handleNoBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1120 static constexpr const StringRef kWildcard = "*";
1121
1123 bool HasWildcard = false;
1124
1125 const auto AddBuiltinName = [&Names, &HasWildcard](StringRef Name) {
1126 if (Name == kWildcard)
1127 HasWildcard = true;
1128 Names.push_back(Name);
1129 };
1130
1131 // Add previously defined attributes.
1132 if (const auto *NBA = D->getAttr<NoBuiltinAttr>())
1133 for (StringRef BuiltinName : NBA->builtinNames())
1134 AddBuiltinName(BuiltinName);
1135
1136 // Add current attributes.
1137 if (AL.getNumArgs() == 0)
1138 AddBuiltinName(kWildcard);
1139 else
1140 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
1141 StringRef BuiltinName;
1142 SourceLocation LiteralLoc;
1143 if (!S.checkStringLiteralArgumentAttr(AL, I, BuiltinName, &LiteralLoc))
1144 return;
1145
1146 if (Builtin::Context::isBuiltinFunc(BuiltinName))
1147 AddBuiltinName(BuiltinName);
1148 else
1149 S.Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name)
1150 << BuiltinName << AL;
1151 }
1152
1153 // Repeating the same attribute is fine.
1154 llvm::sort(Names);
1155 Names.erase(std::unique(Names.begin(), Names.end()), Names.end());
1156
1157 // Empty no_builtin must be on its own.
1158 if (HasWildcard && Names.size() > 1)
1159 S.Diag(D->getLocation(),
1160 diag::err_attribute_no_builtin_wildcard_or_builtin_name)
1161 << AL;
1162
1163 if (D->hasAttr<NoBuiltinAttr>())
1164 D->dropAttr<NoBuiltinAttr>();
1165 D->addAttr(::new (S.Context)
1166 NoBuiltinAttr(S.Context, AL, Names.data(), Names.size()));
1167}
1168
1169static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1170 if (D->hasAttr<PassObjectSizeAttr>()) {
1171 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
1172 return;
1173 }
1174
1175 Expr *E = AL.getArgAsExpr(0);
1176 uint32_t Type;
1177 if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
1178 return;
1179
1180 // pass_object_size's argument is passed in as the second argument of
1181 // __builtin_object_size. So, it has the same constraints as that second
1182 // argument; namely, it must be in the range [0, 3].
1183 if (Type > 3) {
1184 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
1185 << AL << 0 << 3 << E->getSourceRange();
1186 return;
1187 }
1188
1189 // pass_object_size is only supported on constant pointer parameters; as a
1190 // kindness to users, we allow the parameter to be non-const for declarations.
1191 // At this point, we have no clue if `D` belongs to a function declaration or
1192 // definition, so we defer the constness check until later.
1193 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
1194 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
1195 return;
1196 }
1197
1198 D->addAttr(::new (S.Context) PassObjectSizeAttr(S.Context, AL, (int)Type));
1199}
1200
1201static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1202 ConsumableAttr::ConsumedState DefaultState;
1203
1204 if (AL.isArgIdent(0)) {
1205 IdentifierLoc *IL = AL.getArgAsIdent(0);
1206 if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1207 DefaultState)) {
1208 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1209 << IL->Ident;
1210 return;
1211 }
1212 } else {
1213 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1214 << AL << AANT_ArgumentIdentifier;
1215 return;
1216 }
1217
1218 D->addAttr(::new (S.Context) ConsumableAttr(S.Context, AL, DefaultState));
1219}
1220
1222 const ParsedAttr &AL) {
1223 QualType ThisType = MD->getThisType()->getPointeeType();
1224
1225 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1226 if (!RD->hasAttr<ConsumableAttr>()) {
1227 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << RD;
1228
1229 return false;
1230 }
1231 }
1232
1233 return true;
1234}
1235
1236static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1237 if (!AL.checkAtLeastNumArgs(S, 1))
1238 return;
1239
1240 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
1241 return;
1242
1244 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
1245 CallableWhenAttr::ConsumedState CallableState;
1246
1247 StringRef StateString;
1248 SourceLocation Loc;
1249 if (AL.isArgIdent(ArgIndex)) {
1250 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
1251 StateString = Ident->Ident->getName();
1252 Loc = Ident->Loc;
1253 } else {
1254 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
1255 return;
1256 }
1257
1258 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
1259 CallableState)) {
1260 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
1261 return;
1262 }
1263
1264 States.push_back(CallableState);
1265 }
1266
1267 D->addAttr(::new (S.Context)
1268 CallableWhenAttr(S.Context, AL, States.data(), States.size()));
1269}
1270
1271static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1272 ParamTypestateAttr::ConsumedState ParamState;
1273
1274 if (AL.isArgIdent(0)) {
1275 IdentifierLoc *Ident = AL.getArgAsIdent(0);
1276 StringRef StateString = Ident->Ident->getName();
1277
1278 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1279 ParamState)) {
1280 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1281 << AL << StateString;
1282 return;
1283 }
1284 } else {
1285 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1286 << AL << AANT_ArgumentIdentifier;
1287 return;
1288 }
1289
1290 // FIXME: This check is currently being done in the analysis. It can be
1291 // enabled here only after the parser propagates attributes at
1292 // template specialization definition, not declaration.
1293 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1294 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1295 //
1296 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1297 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1298 // ReturnType.getAsString();
1299 // return;
1300 //}
1301
1302 D->addAttr(::new (S.Context) ParamTypestateAttr(S.Context, AL, ParamState));
1303}
1304
1305static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1306 ReturnTypestateAttr::ConsumedState ReturnState;
1307
1308 if (AL.isArgIdent(0)) {
1309 IdentifierLoc *IL = AL.getArgAsIdent(0);
1310 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1311 ReturnState)) {
1312 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1313 << IL->Ident;
1314 return;
1315 }
1316 } else {
1317 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1318 << AL << AANT_ArgumentIdentifier;
1319 return;
1320 }
1321
1322 // FIXME: This check is currently being done in the analysis. It can be
1323 // enabled here only after the parser propagates attributes at
1324 // template specialization definition, not declaration.
1325 //QualType ReturnType;
1326 //
1327 //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1328 // ReturnType = Param->getType();
1329 //
1330 //} else if (const CXXConstructorDecl *Constructor =
1331 // dyn_cast<CXXConstructorDecl>(D)) {
1332 // ReturnType = Constructor->getThisType()->getPointeeType();
1333 //
1334 //} else {
1335 //
1336 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1337 //}
1338 //
1339 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1340 //
1341 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1342 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1343 // ReturnType.getAsString();
1344 // return;
1345 //}
1346
1347 D->addAttr(::new (S.Context) ReturnTypestateAttr(S.Context, AL, ReturnState));
1348}
1349
1350static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1351 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
1352 return;
1353
1354 SetTypestateAttr::ConsumedState NewState;
1355 if (AL.isArgIdent(0)) {
1356 IdentifierLoc *Ident = AL.getArgAsIdent(0);
1357 StringRef Param = Ident->Ident->getName();
1358 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1359 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1360 << Param;
1361 return;
1362 }
1363 } else {
1364 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1365 << AL << AANT_ArgumentIdentifier;
1366 return;
1367 }
1368
1369 D->addAttr(::new (S.Context) SetTypestateAttr(S.Context, AL, NewState));
1370}
1371
1372static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1373 if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
1374 return;
1375
1376 TestTypestateAttr::ConsumedState TestState;
1377 if (AL.isArgIdent(0)) {
1378 IdentifierLoc *Ident = AL.getArgAsIdent(0);
1379 StringRef Param = Ident->Ident->getName();
1380 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
1381 S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1382 << Param;
1383 return;
1384 }
1385 } else {
1386 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1387 << AL << AANT_ArgumentIdentifier;
1388 return;
1389 }
1390
1391 D->addAttr(::new (S.Context) TestTypestateAttr(S.Context, AL, TestState));
1392}
1393
1394static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1395 // Remember this typedef decl, we will need it later for diagnostics.
1396 S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
1397}
1398
1399static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1400 if (auto *TD = dyn_cast<TagDecl>(D))
1401 TD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1402 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
1403 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1404 !FD->getType()->isIncompleteType() &&
1405 FD->isBitField() &&
1406 S.Context.getTypeAlign(FD->getType()) <= 8);
1407
1408 if (S.getASTContext().getTargetInfo().getTriple().isPS()) {
1409 if (BitfieldByteAligned)
1410 // The PS4/PS5 targets need to maintain ABI backwards compatibility.
1411 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
1412 << AL << FD->getType();
1413 else
1414 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1415 } else {
1416 // Report warning about changed offset in the newer compiler versions.
1417 if (BitfieldByteAligned)
1418 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
1419
1420 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1421 }
1422
1423 } else
1424 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
1425}
1426
1427static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
1428 auto *RD = cast<CXXRecordDecl>(D);
1429 ClassTemplateDecl *CTD = RD->getDescribedClassTemplate();
1430 assert(CTD && "attribute does not appertain to this declaration");
1431
1432 ParsedType PT = AL.getTypeArg();
1433 TypeSourceInfo *TSI = nullptr;
1434 QualType T = S.GetTypeFromParser(PT, &TSI);
1435 if (!TSI)
1436 TSI = S.Context.getTrivialTypeSourceInfo(T, AL.getLoc());
1437
1438 if (!T.hasQualifiers() && T->isTypedefNameType()) {
1439 // Find the template name, if this type names a template specialization.
1440 const TemplateDecl *Template = nullptr;
1441 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(
1442 T->getAsCXXRecordDecl())) {
1443 Template = CTSD->getSpecializedTemplate();
1444 } else if (const auto *TST = T->getAs<TemplateSpecializationType>()) {
1445 while (TST && TST->isTypeAlias())
1446 TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
1447 if (TST)
1448 Template = TST->getTemplateName().getAsTemplateDecl();
1449 }
1450
1451 if (Template && declaresSameEntity(Template, CTD)) {
1452 D->addAttr(::new (S.Context) PreferredNameAttr(S.Context, AL, TSI));
1453 return;
1454 }
1455 }
1456
1457 S.Diag(AL.getLoc(), diag::err_attribute_preferred_name_arg_invalid)
1458 << T << CTD;
1459 if (const auto *TT = T->getAs<TypedefType>())
1460 S.Diag(TT->getDecl()->getLocation(), diag::note_entity_declared_at)
1461 << TT->getDecl();
1462}
1463
1464static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
1465 // The IBOutlet/IBOutletCollection attributes only apply to instance
1466 // variables or properties of Objective-C classes. The outlet must also
1467 // have an object reference type.
1468 if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
1469 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
1470 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1471 << AL << VD->getType() << 0;
1472 return false;
1473 }
1474 }
1475 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1476 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
1477 S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1478 << AL << PD->getType() << 1;
1479 return false;
1480 }
1481 }
1482 else {
1483 S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
1484 return false;
1485 }
1486
1487 return true;
1488}
1489
1490static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
1491 if (!checkIBOutletCommon(S, D, AL))
1492 return;
1493
1494 D->addAttr(::new (S.Context) IBOutletAttr(S.Context, AL));
1495}
1496
1497static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
1498
1499 // The iboutletcollection attribute can have zero or one arguments.
1500 if (AL.getNumArgs() > 1) {
1501 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1502 return;
1503 }
1504
1505 if (!checkIBOutletCommon(S, D, AL))
1506 return;
1507
1508 ParsedType PT;
1509
1510 if (AL.hasParsedType())
1511 PT = AL.getTypeArg();
1512 else {
1513 PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
1515 if (!PT) {
1516 S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1517 return;
1518 }
1519 }
1520
1521 TypeSourceInfo *QTLoc = nullptr;
1522 QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1523 if (!QTLoc)
1524 QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
1525
1526 // Diagnose use of non-object type in iboutletcollection attribute.
1527 // FIXME. Gnu attribute extension ignores use of builtin types in
1528 // attributes. So, __attribute__((iboutletcollection(char))) will be
1529 // treated as __attribute__((iboutletcollection())).
1530 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
1531 S.Diag(AL.getLoc(),
1532 QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1533 : diag::err_iboutletcollection_type) << QT;
1534 return;
1535 }
1536
1537 D->addAttr(::new (S.Context) IBOutletCollectionAttr(S.Context, AL, QTLoc));
1538}
1539
1541 if (RefOkay) {
1542 if (T->isReferenceType())
1543 return true;
1544 } else {
1545 T = T.getNonReferenceType();
1546 }
1547
1548 // The nonnull attribute, and other similar attributes, can be applied to a
1549 // transparent union that contains a pointer type.
1550 if (const RecordType *UT = T->getAsUnionType()) {
1551 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1552 RecordDecl *UD = UT->getDecl();
1553 for (const auto *I : UD->fields()) {
1554 QualType QT = I->getType();
1555 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1556 return true;
1557 }
1558 }
1559 }
1560
1561 return T->isAnyPointerType() || T->isBlockPointerType();
1562}
1563
1564static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
1565 SourceRange AttrParmRange,
1566 SourceRange TypeRange,
1567 bool isReturnValue = false) {
1568 if (!S.isValidPointerAttrType(T)) {
1569 if (isReturnValue)
1570 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1571 << AL << AttrParmRange << TypeRange;
1572 else
1573 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1574 << AL << AttrParmRange << TypeRange << 0;
1575 return false;
1576 }
1577 return true;
1578}
1579
1580static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1581 SmallVector<ParamIdx, 8> NonNullArgs;
1582 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1583 Expr *Ex = AL.getArgAsExpr(I);
1584 ParamIdx Idx;
1585 if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
1586 return;
1587
1588 // Is the function argument a pointer type?
1592 Ex->getSourceRange(),
1594 continue;
1595
1596 NonNullArgs.push_back(Idx);
1597 }
1598
1599 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1600 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1601 // check if the attribute came from a macro expansion or a template
1602 // instantiation.
1603 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
1605 bool AnyPointers = isFunctionOrMethodVariadic(D);
1606 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1607 I != E && !AnyPointers; ++I) {
1609 if (T->isDependentType() || S.isValidPointerAttrType(T))
1610 AnyPointers = true;
1611 }
1612
1613 if (!AnyPointers)
1614 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
1615 }
1616
1617 ParamIdx *Start = NonNullArgs.data();
1618 unsigned Size = NonNullArgs.size();
1619 llvm::array_pod_sort(Start, Start + Size);
1620 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, Start, Size));
1621}
1622
1624 const ParsedAttr &AL) {
1625 if (AL.getNumArgs() > 0) {
1626 if (D->getFunctionType()) {
1627 handleNonNullAttr(S, D, AL);
1628 } else {
1629 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1630 << D->getSourceRange();
1631 }
1632 return;
1633 }
1634
1635 // Is the argument a pointer type?
1636 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
1637 D->getSourceRange()))
1638 return;
1639
1640 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, nullptr, 0));
1641}
1642
1643static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1646 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
1647 /* isReturnValue */ true))
1648 return;
1649
1650 D->addAttr(::new (S.Context) ReturnsNonNullAttr(S.Context, AL));
1651}
1652
1653static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1654 if (D->isInvalidDecl())
1655 return;
1656
1657 // noescape only applies to pointer types.
1658 QualType T = cast<ParmVarDecl>(D)->getType();
1659 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
1660 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1661 << AL << AL.getRange() << 0;
1662 return;
1663 }
1664
1665 D->addAttr(::new (S.Context) NoEscapeAttr(S.Context, AL));
1666}
1667
1668static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1669 Expr *E = AL.getArgAsExpr(0),
1670 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1671 S.AddAssumeAlignedAttr(D, AL, E, OE);
1672}
1673
1674static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1675 S.AddAllocAlignAttr(D, AL, AL.getArgAsExpr(0));
1676}
1677
1679 Expr *OE) {
1682
1683 AssumeAlignedAttr TmpAttr(Context, CI, E, OE);
1684 SourceLocation AttrLoc = TmpAttr.getLocation();
1685
1686 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1687 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1688 << &TmpAttr << TmpAttr.getRange() << SR;
1689 return;
1690 }
1691
1692 if (!E->isValueDependent()) {
1693 std::optional<llvm::APSInt> I = llvm::APSInt(64);
1694 if (!(I = E->getIntegerConstantExpr(Context))) {
1695 if (OE)
1696 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1697 << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1698 << E->getSourceRange();
1699 else
1700 Diag(AttrLoc, diag::err_attribute_argument_type)
1701 << &TmpAttr << AANT_ArgumentIntegerConstant
1702 << E->getSourceRange();
1703 return;
1704 }
1705
1706 if (!I->isPowerOf2()) {
1707 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1708 << E->getSourceRange();
1709 return;
1710 }
1711
1712 if (*I > Sema::MaximumAlignment)
1713 Diag(CI.getLoc(), diag::warn_assume_aligned_too_great)
1715 }
1716
1717 if (OE && !OE->isValueDependent() && !OE->isIntegerConstantExpr(Context)) {
1718 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1719 << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1720 << OE->getSourceRange();
1721 return;
1722 }
1723
1724 D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE));
1725}
1726
1728 Expr *ParamExpr) {
1730
1731 AllocAlignAttr TmpAttr(Context, CI, ParamIdx());
1732 SourceLocation AttrLoc = CI.getLoc();
1733
1734 if (!ResultType->isDependentType() &&
1735 !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1736 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1737 << &TmpAttr << CI.getRange() << getFunctionOrMethodResultSourceRange(D);
1738 return;
1739 }
1740
1741 ParamIdx Idx;
1742 const auto *FuncDecl = cast<FunctionDecl>(D);
1743 if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
1744 /*AttrArgNum=*/1, ParamExpr, Idx))
1745 return;
1746
1748 if (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1749 !Ty->isAlignValT()) {
1750 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
1751 << &TmpAttr
1752 << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
1753 return;
1754 }
1755
1756 D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx));
1757}
1758
1759/// Check if \p AssumptionStr is a known assumption and warn if not.
1761 StringRef AssumptionStr) {
1762 if (llvm::KnownAssumptionStrings.count(AssumptionStr))
1763 return;
1764
1765 unsigned BestEditDistance = 3;
1766 StringRef Suggestion;
1767 for (const auto &KnownAssumptionIt : llvm::KnownAssumptionStrings) {
1768 unsigned EditDistance =
1769 AssumptionStr.edit_distance(KnownAssumptionIt.getKey());
1770 if (EditDistance < BestEditDistance) {
1771 Suggestion = KnownAssumptionIt.getKey();
1772 BestEditDistance = EditDistance;
1773 }
1774 }
1775
1776 if (!Suggestion.empty())
1777 S.Diag(Loc, diag::warn_assume_attribute_string_unknown_suggested)
1778 << AssumptionStr << Suggestion;
1779 else
1780 S.Diag(Loc, diag::warn_assume_attribute_string_unknown) << AssumptionStr;
1781}
1782
1783static void handleAssumumptionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1784 // Handle the case where the attribute has a text message.
1785 StringRef Str;
1786 SourceLocation AttrStrLoc;
1787 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &AttrStrLoc))
1788 return;
1789
1790 checkAssumptionAttr(S, AttrStrLoc, Str);
1791
1792 D->addAttr(::new (S.Context) AssumptionAttr(S.Context, AL, Str));
1793}
1794
1795/// Normalize the attribute, __foo__ becomes foo.
1796/// Returns true if normalization was applied.
1797static bool normalizeName(StringRef &AttrName) {
1798 if (AttrName.size() > 4 && AttrName.startswith("__") &&
1799 AttrName.endswith("__")) {
1800 AttrName = AttrName.drop_front(2).drop_back(2);
1801 return true;
1802 }
1803 return false;
1804}
1805
1806static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1807 // This attribute must be applied to a function declaration. The first
1808 // argument to the attribute must be an identifier, the name of the resource,
1809 // for example: malloc. The following arguments must be argument indexes, the
1810 // arguments must be of integer type for Returns, otherwise of pointer type.
1811 // The difference between Holds and Takes is that a pointer may still be used
1812 // after being held. free() should be __attribute((ownership_takes)), whereas
1813 // a list append function may well be __attribute((ownership_holds)).
1814
1815 if (!AL.isArgIdent(0)) {
1816 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
1817 << AL << 1 << AANT_ArgumentIdentifier;
1818 return;
1819 }
1820
1821 // Figure out our Kind.
1822 OwnershipAttr::OwnershipKind K =
1823 OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind();
1824
1825 // Check arguments.
1826 switch (K) {
1827 case OwnershipAttr::Takes:
1828 case OwnershipAttr::Holds:
1829 if (AL.getNumArgs() < 2) {
1830 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
1831 return;
1832 }
1833 break;
1834 case OwnershipAttr::Returns:
1835 if (AL.getNumArgs() > 2) {
1836 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
1837 return;
1838 }
1839 break;
1840 }
1841
1843
1844 StringRef ModuleName = Module->getName();
1845 if (normalizeName(ModuleName)) {
1846 Module = &S.PP.getIdentifierTable().get(ModuleName);
1847 }
1848
1849 SmallVector<ParamIdx, 8> OwnershipArgs;
1850 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1851 Expr *Ex = AL.getArgAsExpr(i);
1852 ParamIdx Idx;
1853 if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
1854 return;
1855
1856 // Is the function argument a pointer type?
1858 int Err = -1; // No error
1859 switch (K) {
1860 case OwnershipAttr::Takes:
1861 case OwnershipAttr::Holds:
1862 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1863 Err = 0;
1864 break;
1865 case OwnershipAttr::Returns:
1866 if (!T->isIntegerType())
1867 Err = 1;
1868 break;
1869 }
1870 if (-1 != Err) {
1871 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1872 << Ex->getSourceRange();
1873 return;
1874 }
1875
1876 // Check we don't have a conflict with another ownership attribute.
1877 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
1878 // Cannot have two ownership attributes of different kinds for the same
1879 // index.
1880 if (I->getOwnKind() != K && llvm::is_contained(I->args(), Idx)) {
1881 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
1882 return;
1883 } else if (K == OwnershipAttr::Returns &&
1884 I->getOwnKind() == OwnershipAttr::Returns) {
1885 // A returns attribute conflicts with any other returns attribute using
1886 // a different index.
1887 if (!llvm::is_contained(I->args(), Idx)) {
1888 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1889 << I->args_begin()->getSourceIndex();
1890 if (I->args_size())
1891 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1892 << Idx.getSourceIndex() << Ex->getSourceRange();
1893 return;
1894 }
1895 }
1896 }
1897 OwnershipArgs.push_back(Idx);
1898 }
1899
1900 ParamIdx *Start = OwnershipArgs.data();
1901 unsigned Size = OwnershipArgs.size();
1902 llvm::array_pod_sort(Start, Start + Size);
1903 D->addAttr(::new (S.Context)
1904 OwnershipAttr(S.Context, AL, Module, Start, Size));
1905}
1906
1907static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1908 // Check the attribute arguments.
1909 if (AL.getNumArgs() > 1) {
1910 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1911 return;
1912 }
1913
1914 // gcc rejects
1915 // class c {
1916 // static int a __attribute__((weakref ("v2")));
1917 // static int b() __attribute__((weakref ("f3")));
1918 // };
1919 // and ignores the attributes of
1920 // void f(void) {
1921 // static int a __attribute__((weakref ("v2")));
1922 // }
1923 // we reject them
1924 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
1925 if (!Ctx->isFileContext()) {
1926 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1927 << cast<NamedDecl>(D);
1928 return;
1929 }
1930
1931 // The GCC manual says
1932 //
1933 // At present, a declaration to which `weakref' is attached can only
1934 // be `static'.
1935 //
1936 // It also says
1937 //
1938 // Without a TARGET,
1939 // given as an argument to `weakref' or to `alias', `weakref' is
1940 // equivalent to `weak'.
1941 //
1942 // gcc 4.4.1 will accept
1943 // int a7 __attribute__((weakref));
1944 // as
1945 // int a7 __attribute__((weak));
1946 // This looks like a bug in gcc. We reject that for now. We should revisit
1947 // it if this behaviour is actually used.
1948
1949 // GCC rejects
1950 // static ((alias ("y"), weakref)).
1951 // Should we? How to check that weakref is before or after alias?
1952
1953 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1954 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1955 // StringRef parameter it was given anyway.
1956 StringRef Str;
1957 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
1958 // GCC will accept anything as the argument of weakref. Should we
1959 // check for an existing decl?
1960 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
1961
1962 D->addAttr(::new (S.Context) WeakRefAttr(S.Context, AL));
1963}
1964
1965static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1966 StringRef Str;
1967 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1968 return;
1969
1970 // Aliases should be on declarations, not definitions.
1971 const auto *FD = cast<FunctionDecl>(D);
1972 if (FD->isThisDeclarationADefinition()) {
1973 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
1974 return;
1975 }
1976
1977 D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str));
1978}
1979
1980static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1981 StringRef Str;
1982 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1983 return;
1984
1985 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
1986 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
1987 return;
1988 }
1989 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
1990 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
1991 }
1992
1993 // Aliases should be on declarations, not definitions.
1994 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1995 if (FD->isThisDeclarationADefinition()) {
1996 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
1997 return;
1998 }
1999 } else {
2000 const auto *VD = cast<VarDecl>(D);
2001 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
2002 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
2003 return;
2004 }
2005 }
2006
2007 // Mark target used to prevent unneeded-internal-declaration warnings.
2008 if (!S.LangOpts.CPlusPlus) {
2009 // FIXME: demangle Str for C++, as the attribute refers to the mangled
2010 // linkage name, not the pre-mangled identifier.
2011 const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc());
2014 for (NamedDecl *ND : LR)
2015 ND->markUsed(S.Context);
2016 }
2017
2018 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
2019}
2020
2021static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2022 StringRef Model;
2023 SourceLocation LiteralLoc;
2024 // Check that it is a string.
2025 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
2026 return;
2027
2028 // Check that the value.
2029 if (Model != "global-dynamic" && Model != "local-dynamic"
2030 && Model != "initial-exec" && Model != "local-exec") {
2031 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
2032 return;
2033 }
2034
2035 if (S.Context.getTargetInfo().getTriple().isOSAIX() &&
2036 Model != "global-dynamic") {
2037 S.Diag(LiteralLoc, diag::err_aix_attr_unsupported_tls_model) << Model;
2038 return;
2039 }
2040
2041 D->addAttr(::new (S.Context) TLSModelAttr(S.Context, AL, Model));
2042}
2043
2044static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2046 if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
2047 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
2048 return;
2049 }
2050
2051 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
2053}
2054
2055static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2056 // Ensure we don't combine these with themselves, since that causes some
2057 // confusing behavior.
2058 if (AL.getParsedKind() == ParsedAttr::AT_CPUDispatch) {
2059 if (checkAttrMutualExclusion<CPUSpecificAttr>(S, D, AL))
2060 return;
2061
2062 if (const auto *Other = D->getAttr<CPUDispatchAttr>()) {
2063 S.Diag(AL.getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
2064 S.Diag(Other->getLocation(), diag::note_conflicting_attribute);
2065 return;
2066 }
2067 } else if (AL.getParsedKind() == ParsedAttr::AT_CPUSpecific) {
2068 if (checkAttrMutualExclusion<CPUDispatchAttr>(S, D, AL))
2069 return;
2070
2071 if (const auto *Other = D->getAttr<CPUSpecificAttr>()) {
2072 S.Diag(AL.getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
2073 S.Diag(Other->getLocation(), diag::note_conflicting_attribute);
2074 return;
2075 }
2076 }
2077
2078 FunctionDecl *FD = cast<FunctionDecl>(D);
2079
2080 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2081 if (MD->getParent()->isLambda()) {
2082 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
2083 return;
2084 }
2085 }
2086
2087 if (!AL.checkAtLeastNumArgs(S, 1))
2088 return;
2089
2091 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
2092 if (!AL.isArgIdent(ArgNo)) {
2093 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
2094 << AL << AANT_ArgumentIdentifier;
2095 return;
2096 }
2097
2098 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
2099 StringRef CPUName = CPUArg->Ident->getName().trim();
2100
2102 S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
2103 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
2104 return;
2105 }
2106
2108 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
2109 return Target.CPUSpecificManglingCharacter(CPUName) ==
2110 Target.CPUSpecificManglingCharacter(Cur->getName());
2111 })) {
2112 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
2113 return;
2114 }
2115 CPUs.push_back(CPUArg->Ident);
2116 }
2117
2118 FD->setIsMultiVersion(true);
2119 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
2120 D->addAttr(::new (S.Context)
2121 CPUSpecificAttr(S.Context, AL, CPUs.data(), CPUs.size()));
2122 else
2123 D->addAttr(::new (S.Context)
2124 CPUDispatchAttr(S.Context, AL, CPUs.data(), CPUs.size()));
2125}
2126
2127static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2128 if (S.LangOpts.CPlusPlus) {
2129 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
2131 return;
2132 }
2133
2134 D->addAttr(::new (S.Context) CommonAttr(S.Context, AL));
2135}
2136
2137static void handleCmseNSEntryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2138 if (S.LangOpts.CPlusPlus && !D->getDeclContext()->isExternCContext()) {
2139 S.Diag(AL.getLoc(), diag::err_attribute_not_clinkage) << AL;
2140 return;
2141 }
2142
2143 const auto *FD = cast<FunctionDecl>(D);
2144 if (!FD->isExternallyVisible()) {
2145 S.Diag(AL.getLoc(), diag::warn_attribute_cmse_entry_static);
2146 return;
2147 }
2148
2149 D->addAttr(::new (S.Context) CmseNSEntryAttr(S.Context, AL));
2150}
2151
2152static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2153 if (AL.isDeclspecAttribute()) {
2154 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2155 const auto &Arch = Triple.getArch();
2156 if (Arch != llvm::Triple::x86 &&
2157 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
2158 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
2159 << AL << Triple.getArchName();
2160 return;
2161 }
2162
2163 // This form is not allowed to be written on a member function (static or
2164 // nonstatic) when in Microsoft compatibility mode.
2165 if (S.getLangOpts().MSVCCompat && isa<CXXMethodDecl>(D)) {
2166 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type_str)
2167 << AL << "non-member functions";
2168 return;
2169 }
2170 }
2171
2172 D->addAttr(::new (S.Context) NakedAttr(S.Context, AL));
2173}
2174
2175static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
2176 if (hasDeclarator(D)) return;
2177
2178 if (!isa<ObjCMethodDecl>(D)) {
2179 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
2180 << Attrs << ExpectedFunctionOrMethod;
2181 return;
2182 }
2183
2184 D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs));
2185}
2186
2187static void handleStandardNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &A) {
2188 // The [[_Noreturn]] spelling is deprecated in C2x, so if that was used,
2189 // issue an appropriate diagnostic. However, don't issue a diagnostic if the
2190 // attribute name comes from a macro expansion. We don't want to punish users
2191 // who write [[noreturn]] after including <stdnoreturn.h> (where 'noreturn'
2192 // is defined as a macro which expands to '_Noreturn').
2193 if (!S.getLangOpts().CPlusPlus &&
2194 A.getSemanticSpelling() == CXX11NoReturnAttr::C2x_Noreturn &&
2195 !(A.getLoc().isMacroID() &&
2197 S.Diag(A.getLoc(), diag::warn_deprecated_noreturn_spelling) << A.getRange();
2198
2199 D->addAttr(::new (S.Context) CXX11NoReturnAttr(S.Context, A));
2200}
2201
2202static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
2203 if (!S.getLangOpts().CFProtectionBranch)
2204 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2205 else
2206 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
2207}
2208
2210 if (!Attrs.checkExactlyNumArgs(*this, 0)) {
2211 Attrs.setInvalid();
2212 return true;
2213 }
2214
2215 return false;
2216}
2217
2219 // Check whether the attribute is valid on the current target.
2221 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
2222 << AL << AL.getRange();
2223 AL.setInvalid();
2224 return true;
2225 }
2226
2227 return false;
2228}
2229
2230static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2231
2232 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2233 // because 'analyzer_noreturn' does not impact the type.
2234 if (!isFunctionOrMethodOrBlock(D)) {
2235 ValueDecl *VD = dyn_cast<ValueDecl>(D);
2236 if (!VD || (!VD->getType()->isBlockPointerType() &&
2237 !VD->getType()->isFunctionPointerType())) {
2239 ? diag::err_attribute_wrong_decl_type
2240 : diag::warn_attribute_wrong_decl_type)
2242 return;
2243 }
2244 }
2245
2246 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL));
2247}
2248
2249// PS3 PPU-specific.
2250static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2251 /*
2252 Returning a Vector Class in Registers
2253
2254 According to the PPU ABI specifications, a class with a single member of
2255 vector type is returned in memory when used as the return value of a
2256 function.
2257 This results in inefficient code when implementing vector classes. To return
2258 the value in a single vector register, add the vecreturn attribute to the
2259 class definition. This attribute is also applicable to struct types.
2260
2261 Example:
2262
2263 struct Vector
2264 {
2265 __vector float xyzw;
2266 } __attribute__((vecreturn));
2267
2268 Vector Add(Vector lhs, Vector rhs)
2269 {
2270 Vector result;
2271 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2272 return result; // This will be returned in a register
2273 }
2274 */
2275 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
2276 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
2277 return;
2278 }
2279
2280 const auto *R = cast<RecordDecl>(D);
2281 int count = 0;
2282
2283 if (!isa<CXXRecordDecl>(R)) {
2284 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2285 return;
2286 }
2287
2288 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2289 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
2290 return;
2291 }
2292
2293 for (const auto *I : R->fields()) {
2294 if ((count == 1) || !I->getType()->isVectorType()) {
2295 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2296 return;
2297 }
2298 count++;
2299 }
2300
2301 D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL));
2302}
2303
2305 const ParsedAttr &AL) {
2306 if (isa<ParmVarDecl>(D)) {
2307 // [[carries_dependency]] can only be applied to a parameter if it is a
2308 // parameter of a function declaration or lambda.
2310 S.Diag(AL.getLoc(),
2311 diag::err_carries_dependency_param_not_function_decl);
2312 return;
2313 }
2314 }
2315
2316 D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
2317}
2318
2319static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2320 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
2321
2322 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
2323 // about using it as an extension.
2324 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
2325 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2326
2327 D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
2328}
2329
2330static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2331 uint32_t priority = ConstructorAttr::DefaultPriority;
2332 if (S.getLangOpts().HLSL && AL.getNumArgs()) {
2333 S.Diag(AL.getLoc(), diag::err_hlsl_init_priority_unsupported);
2334 return;
2335 }
2336 if (AL.getNumArgs() &&
2337 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
2338 return;
2339
2340 D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
2341}
2342
2343static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2344 uint32_t priority = DestructorAttr::DefaultPriority;
2345 if (AL.getNumArgs() &&
2346 !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
2347 return;
2348
2349 D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
2350}
2351
2352template <typename AttrTy>
2353static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
2354 // Handle the case where the attribute has a text message.
2355 StringRef Str;
2356 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
2357 return;
2358
2359 D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str));
2360}
2361
2363 const ParsedAttr &AL) {
2364 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
2365 S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
2366 << AL << AL.getRange();
2367 return;
2368 }
2369
2370 D->addAttr(::new (S.Context) ObjCExplicitProtocolImplAttr(S.Context, AL));
2371}
2372
2374 IdentifierInfo *Platform,
2375 VersionTuple Introduced,
2376 VersionTuple Deprecated,
2377 VersionTuple Obsoleted) {
2378 StringRef PlatformName
2379 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2380 if (PlatformName.empty())
2381 PlatformName = Platform->getName();
2382
2383 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2384 // of these steps are needed).
2385 if (!Introduced.empty() && !Deprecated.empty() &&
2386 !(Introduced <= Deprecated)) {
2387 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2388 << 1 << PlatformName << Deprecated.getAsString()
2389 << 0 << Introduced.getAsString();
2390 return true;
2391 }
2392
2393 if (!Introduced.empty() && !Obsoleted.empty() &&
2394 !(Introduced <= Obsoleted)) {
2395 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2396 << 2 << PlatformName << Obsoleted.getAsString()
2397 << 0 << Introduced.getAsString();
2398 return true;
2399 }
2400
2401 if (!Deprecated.empty() && !Obsoleted.empty() &&
2402 !(Deprecated <= Obsoleted)) {
2403 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2404 << 2 << PlatformName << Obsoleted.getAsString()
2405 << 1 << Deprecated.getAsString();
2406 return true;
2407 }
2408
2409 return false;
2410}
2411
2412/// Check whether the two versions match.
2413///
2414/// If either version tuple is empty, then they are assumed to match. If
2415/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2416static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2417 bool BeforeIsOkay) {
2418 if (X.empty() || Y.empty())
2419 return true;
2420
2421 if (X == Y)
2422 return true;
2423
2424 if (BeforeIsOkay && X < Y)
2425 return true;
2426
2427 return false;
2428}
2429
2431 NamedDecl *D, const AttributeCommonInfo &CI, IdentifierInfo *Platform,
2432 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2433 VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2434 bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2435 int Priority) {
2436 VersionTuple MergedIntroduced = Introduced;
2437 VersionTuple MergedDeprecated = Deprecated;
2438 VersionTuple MergedObsoleted = Obsoleted;
2439 bool FoundAny = false;
2440 bool OverrideOrImpl = false;
2441 switch (AMK) {
2442 case AMK_None:
2443 case AMK_Redeclaration:
2444 OverrideOrImpl = false;
2445 break;
2446
2447 case AMK_Override:
2450 OverrideOrImpl = true;
2451 break;
2452 }
2453
2454 if (D->hasAttrs()) {
2455 AttrVec &Attrs = D->getAttrs();
2456 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2457 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2458 if (!OldAA) {
2459 ++i;
2460 continue;
2461 }
2462
2463 IdentifierInfo *OldPlatform = OldAA->getPlatform();
2464 if (OldPlatform != Platform) {
2465 ++i;
2466 continue;
2467 }
2468
2469 // If there is an existing availability attribute for this platform that
2470 // has a lower priority use the existing one and discard the new
2471 // attribute.
2472 if (OldAA->getPriority() < Priority)
2473 return nullptr;
2474
2475 // If there is an existing attribute for this platform that has a higher
2476 // priority than the new attribute then erase the old one and continue
2477 // processing the attributes.
2478 if (OldAA->getPriority() > Priority) {
2479 Attrs.erase(Attrs.begin() + i);
2480 --e;
2481 continue;
2482 }
2483
2484 FoundAny = true;
2485 VersionTuple OldIntroduced = OldAA->getIntroduced();
2486 VersionTuple OldDeprecated = OldAA->getDeprecated();
2487 VersionTuple OldObsoleted = OldAA->getObsoleted();
2488 bool OldIsUnavailable = OldAA->getUnavailable();
2489
2490 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2491 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2492 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
2493 !(OldIsUnavailable == IsUnavailable ||
2494 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2495 if (OverrideOrImpl) {
2496 int Which = -1;
2497 VersionTuple FirstVersion;
2498 VersionTuple SecondVersion;
2499 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
2500 Which = 0;
2501 FirstVersion = OldIntroduced;
2502 SecondVersion = Introduced;
2503 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
2504 Which = 1;
2505 FirstVersion = Deprecated;
2506 SecondVersion = OldDeprecated;
2507 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
2508 Which = 2;
2509 FirstVersion = Obsoleted;
2510 SecondVersion = OldObsoleted;
2511 }
2512
2513 if (Which == -1) {
2514 Diag(OldAA->getLocation(),
2515 diag::warn_mismatched_availability_override_unavail)
2516 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2517 << (AMK == AMK_Override);
2518 } else if (Which != 1 && AMK == AMK_OptionalProtocolImplementation) {
2519 // Allow different 'introduced' / 'obsoleted' availability versions
2520 // on a method that implements an optional protocol requirement. It
2521 // makes less sense to allow this for 'deprecated' as the user can't
2522 // see if the method is 'deprecated' as 'respondsToSelector' will
2523 // still return true when the method is deprecated.
2524 ++i;
2525 continue;
2526 } else {
2527 Diag(OldAA->getLocation(),
2528 diag::warn_mismatched_availability_override)
2529 << Which
2530 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2531 << FirstVersion.getAsString() << SecondVersion.getAsString()
2532 << (AMK == AMK_Override);
2533 }
2534 if (AMK == AMK_Override)
2535 Diag(CI.getLoc(), diag::note_overridden_method);
2536 else
2537 Diag(CI.getLoc(), diag::note_protocol_method);
2538 } else {
2539 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2540 Diag(CI.getLoc(), diag::note_previous_attribute);
2541 }
2542
2543 Attrs.erase(Attrs.begin() + i);
2544 --e;
2545 continue;
2546 }
2547
2548 VersionTuple MergedIntroduced2 = MergedIntroduced;
2549 VersionTuple MergedDeprecated2 = MergedDeprecated;
2550 VersionTuple MergedObsoleted2 = MergedObsoleted;
2551
2552 if (MergedIntroduced2.empty())
2553 MergedIntroduced2 = OldIntroduced;
2554 if (MergedDeprecated2.empty())
2555 MergedDeprecated2 = OldDeprecated;
2556 if (MergedObsoleted2.empty())
2557 MergedObsoleted2 = OldObsoleted;
2558
2559 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2560 MergedIntroduced2, MergedDeprecated2,
2561 MergedObsoleted2)) {
2562 Attrs.erase(Attrs.begin() + i);
2563 --e;
2564 continue;
2565 }
2566
2567 MergedIntroduced = MergedIntroduced2;
2568 MergedDeprecated = MergedDeprecated2;
2569 MergedObsoleted = MergedObsoleted2;
2570 ++i;
2571 }
2572 }
2573
2574 if (FoundAny &&
2575 MergedIntroduced == Introduced &&
2576 MergedDeprecated == Deprecated &&
2577 MergedObsoleted == Obsoleted)
2578 return nullptr;
2579
2580 // Only create a new attribute if !OverrideOrImpl, but we want to do
2581 // the checking.
2582 if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
2583 MergedDeprecated, MergedObsoleted) &&
2584 !OverrideOrImpl) {
2585 auto *Avail = ::new (Context) AvailabilityAttr(
2586 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2587 Message, IsStrict, Replacement, Priority);
2588 Avail->setImplicit(Implicit);
2589 return Avail;
2590 }
2591 return nullptr;
2592}
2593
2594static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2595 if (isa<UsingDecl, UnresolvedUsingTypenameDecl, UnresolvedUsingValueDecl>(
2596 D)) {
2597 S.Diag(AL.getRange().getBegin(), diag::warn_deprecated_ignored_on_using)
2598 << AL;
2599 return;
2600 }
2601
2602 if (!AL.checkExactlyNumArgs(S, 1))
2603 return;
2604 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2605
2606 IdentifierInfo *II = Platform->Ident;
2607 if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2608 S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2609 << Platform->Ident;
2610
2611 auto *ND = dyn_cast<NamedDecl>(D);
2612 if (!ND) // We warned about this already, so just return.
2613 return;
2614
2618 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2619 bool IsStrict = AL.getStrictLoc().isValid();
2620 StringRef Str;
2621 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
2622 Str = SE->getString();
2623 StringRef Replacement;
2624 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
2625 Replacement = SE->getString();
2626
2627 if (II->isStr("swift")) {
2628 if (Introduced.isValid() || Obsoleted.isValid() ||
2629 (!IsUnavailable && !Deprecated.isValid())) {
2630 S.Diag(AL.getLoc(),
2631 diag::warn_availability_swift_unavailable_deprecated_only);
2632 return;
2633 }
2634 }
2635
2636 if (II->isStr("fuchsia")) {
2637 std::optional<unsigned> Min, Sub;
2638 if ((Min = Introduced.Version.getMinor()) ||
2639 (Sub = Introduced.Version.getSubminor())) {
2640 S.Diag(AL.getLoc(), diag::warn_availability_fuchsia_unavailable_minor);
2641 return;
2642 }
2643 }
2644
2645 int PriorityModifier = AL.isPragmaClangAttribute()
2648 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2649 ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version,
2650 Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2651 Sema::AMK_None, PriorityModifier);
2652 if (NewAttr)
2653 D->addAttr(NewAttr);
2654
2655 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2656 // matches before the start of the watchOS platform.
2657 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2658 IdentifierInfo *NewII = nullptr;
2659 if (II->getName() == "ios")
2660 NewII = &S.Context.Idents.get("watchos");
2661 else if (II->getName() == "ios_app_extension")
2662 NewII = &S.Context.Idents.get("watchos_app_extension");
2663
2664 if (NewII) {
2665 const auto *SDKInfo = S.getDarwinSDKInfoForAvailabilityChecking();
2666 const auto *IOSToWatchOSMapping =
2667 SDKInfo ? SDKInfo->getVersionMapping(
2669 : nullptr;
2670
2671 auto adjustWatchOSVersion =
2672 [IOSToWatchOSMapping](VersionTuple Version) -> VersionTuple {
2673 if (Version.empty())
2674 return Version;
2675 auto MinimumWatchOSVersion = VersionTuple(2, 0);
2676
2677 if (IOSToWatchOSMapping) {
2678 if (auto MappedVersion = IOSToWatchOSMapping->map(
2679 Version, MinimumWatchOSVersion, std::nullopt)) {
2680 return *MappedVersion;
2681 }
2682 }
2683
2684 auto Major = Version.getMajor();
2685 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2686 if (NewMajor >= 2) {
2687 if (Version.getMinor()) {
2688 if (Version.getSubminor())
2689 return VersionTuple(NewMajor, *Version.getMinor(),
2690 *Version.getSubminor());
2691 else
2692 return VersionTuple(NewMajor, *Version.getMinor());
2693 }
2694 return VersionTuple(NewMajor);
2695 }
2696
2697 return MinimumWatchOSVersion;
2698 };
2699
2700 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2701 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2702 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2703
2704 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2705 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2706 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2708 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
2709 if (NewAttr)
2710 D->addAttr(NewAttr);
2711 }
2712 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2713 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2714 // matches before the start of the tvOS platform.
2715 IdentifierInfo *NewII = nullptr;
2716 if (II->getName() == "ios")
2717 NewII = &S.Context.Idents.get("tvos");
2718 else if (II->getName() == "ios_app_extension")
2719 NewII = &S.Context.Idents.get("tvos_app_extension");
2720
2721 if (NewII) {
2722 const auto *SDKInfo = S.getDarwinSDKInfoForAvailabilityChecking();
2723 const auto *IOSToTvOSMapping =
2724 SDKInfo ? SDKInfo->getVersionMapping(
2726 : nullptr;
2727
2728 auto AdjustTvOSVersion =
2729 [IOSToTvOSMapping](VersionTuple Version) -> VersionTuple {
2730 if (Version.empty())
2731 return Version;
2732
2733 if (IOSToTvOSMapping) {
2734 if (auto MappedVersion = IOSToTvOSMapping->map(
2735 Version, VersionTuple(0, 0), std::nullopt)) {
2736 return *MappedVersion;
2737 }
2738 }
2739 return Version;
2740 };
2741
2742 auto NewIntroduced = AdjustTvOSVersion(Introduced.Version);
2743 auto NewDeprecated = AdjustTvOSVersion(Deprecated.Version);
2744 auto NewObsoleted = AdjustTvOSVersion(Obsoleted.Version);
2745
2746 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2747 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2748 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2750 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
2751 if (NewAttr)
2752 D->addAttr(NewAttr);
2753 }
2754 } else if (S.Context.getTargetInfo().getTriple().getOS() ==
2755 llvm::Triple::IOS &&
2756 S.Context.getTargetInfo().getTriple().isMacCatalystEnvironment()) {
2757 auto GetSDKInfo = [&]() {
2759 "macOS");
2760 };
2761
2762 // Transcribe "ios" to "maccatalyst" (and add a new attribute).
2763 IdentifierInfo *NewII = nullptr;
2764 if (II->getName() == "ios")
2765 NewII = &S.Context.Idents.get("maccatalyst");
2766 else if (II->getName() == "ios_app_extension")
2767 NewII = &S.Context.Idents.get("maccatalyst_app_extension");
2768 if (NewII) {
2769 auto MinMacCatalystVersion = [](const VersionTuple &V) {
2770 if (V.empty())
2771 return V;
2772 if (V.getMajor() < 13 ||
2773 (V.getMajor() == 13 && V.getMinor() && *V.getMinor() < 1))
2774 return VersionTuple(13, 1); // The min Mac Catalyst version is 13.1.
2775 return V;
2776 };
2777 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2778 ND, AL.getRange(), NewII, true /*Implicit*/,
2779 MinMacCatalystVersion(Introduced.Version),
2780 MinMacCatalystVersion(Deprecated.Version),
2781 MinMacCatalystVersion(Obsoleted.Version), IsUnavailable, Str,
2782 IsStrict, Replacement, Sema::AMK_None,
2783 PriorityModifier + Sema::AP_InferredFromOtherPlatform);
2784 if (NewAttr)
2785 D->addAttr(NewAttr);
2786 } else if (II->getName() == "macos" && GetSDKInfo() &&
2787 (!Introduced.Version.empty() || !Deprecated.Version.empty() ||
2788 !Obsoleted.Version.empty())) {
2789 if (const auto *MacOStoMacCatalystMapping =
2790 GetSDKInfo()->getVersionMapping(
2792 // Infer Mac Catalyst availability from the macOS availability attribute
2793 // if it has versioned availability. Don't infer 'unavailable'. This
2794 // inferred availability has lower priority than the other availability
2795 // attributes that are inferred from 'ios'.
2796 NewII = &S.Context.Idents.get("maccatalyst");
2797 auto RemapMacOSVersion =
2798 [&](const VersionTuple &V) -> std::optional<VersionTuple> {
2799 if (V.empty())
2800 return std::nullopt;
2801 // API_TO_BE_DEPRECATED is 100000.
2802 if (V.getMajor() == 100000)
2803 return VersionTuple(100000);
2804 // The minimum iosmac version is 13.1
2805 return MacOStoMacCatalystMapping->map(V, VersionTuple(13, 1),
2806 std::nullopt);
2807 };
2808 std::optional<VersionTuple> NewIntroduced =
2809 RemapMacOSVersion(Introduced.Version),
2810 NewDeprecated =
2811 RemapMacOSVersion(Deprecated.Version),
2812 NewObsoleted =
2813 RemapMacOSVersion(Obsoleted.Version);
2814 if (NewIntroduced || NewDeprecated || NewObsoleted) {
2815 auto VersionOrEmptyVersion =
2816 [](const std::optional<VersionTuple> &V) -> VersionTuple {
2817 return V ? *V : VersionTuple();
2818 };
2819 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2820 ND, AL.getRange(), NewII, true /*Implicit*/,
2821 VersionOrEmptyVersion(NewIntroduced),
2822 VersionOrEmptyVersion(NewDeprecated),
2823 VersionOrEmptyVersion(NewObsoleted), /*IsUnavailable=*/false, Str,
2824 IsStrict, Replacement, Sema::AMK_None,
2825 PriorityModifier + Sema::AP_InferredFromOtherPlatform +
2827 if (NewAttr)
2828 D->addAttr(NewAttr);
2829 }
2830 }
2831 }
2832 }
2833}
2834
2836 const ParsedAttr &AL) {
2837 if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 4))
2838 return;
2839
2840 StringRef Language;
2841 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
2842 Language = SE->getString();
2843 StringRef DefinedIn;
2844 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
2845 DefinedIn = SE->getString();
2846 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
2847 StringRef USR;
2848 if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(3)))
2849 USR = SE->getString();
2850
2851 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
2852 S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration, USR));
2853}
2854
2855template <class T>
2857 typename T::VisibilityType value) {
2858 T *existingAttr = D->getAttr<T>();
2859 if (existingAttr) {
2860 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2861 if (existingValue == value)
2862 return nullptr;
2863 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2864 S.Diag(CI.getLoc(), diag::note_previous_attribute);
2865 D->dropAttr<T>();
2866 }
2867 return ::new (S.Context) T(S.Context, CI, value);
2868}
2869
2871 const AttributeCommonInfo &CI,
2872 VisibilityAttr::VisibilityType Vis) {
2873 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis);
2874}
2875
2876TypeVisibilityAttr *
2878 TypeVisibilityAttr::VisibilityType Vis) {
2879 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis);
2880}
2881
2882static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
2883 bool isTypeVisibility) {
2884 // Visibility attributes don't mean anything on a typedef.
2885 if (isa<TypedefNameDecl>(D)) {
2886 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
2887 return;
2888 }
2889
2890 // 'type_visibility' can only go on a type or namespace.
2891 if (isTypeVisibility &&
2892 !(isa<TagDecl>(D) ||
2893 isa<ObjCInterfaceDecl>(D) ||
2894 isa<NamespaceDecl>(D))) {
2895 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2896 << AL << ExpectedTypeOrNamespace;
2897 return;
2898 }
2899
2900 // Check that the argument is a string literal.
2901 StringRef TypeStr;
2902 SourceLocation LiteralLoc;
2903 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
2904 return;
2905
2906 VisibilityAttr::VisibilityType type;
2907 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
2908 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2909 << TypeStr;
2910 return;
2911 }
2912
2913 // Complain about attempts to use protected visibility on targets
2914 // (like Darwin) that don't support it.
2915 if (type == VisibilityAttr::Protected &&
2917 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
2918 type = VisibilityAttr::Default;
2919 }
2920
2921 Attr *newAttr;
2922 if (isTypeVisibility) {
2923 newAttr = S.mergeTypeVisibilityAttr(
2924 D, AL, (TypeVisibilityAttr::VisibilityType)type);
2925 } else {
2926 newAttr = S.mergeVisibilityAttr(D, AL, type);
2927 }
2928 if (newAttr)
2929 D->addAttr(newAttr);
2930}
2931
2932static void handleObjCDirectAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2933 // objc_direct cannot be set on methods declared in the context of a protocol
2934 if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
2935 S.Diag(AL.getLoc(), diag::err_objc_direct_on_protocol) << false;
2936 return;
2937 }
2938
2940 handleSimpleAttribute<ObjCDirectAttr>(S, D, AL);
2941 } else {
2942 S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2943 }
2944}
2945
2947 const ParsedAttr &AL) {
2949 handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
2950 } else {
2951 S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2952 }
2953}
2954
2955static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2956 const auto *M = cast<ObjCMethodDecl>(D);
2957 if (!AL.isArgIdent(0)) {
2958 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2959 << AL << 1 << AANT_ArgumentIdentifier;
2960 return;
2961 }
2962
2963 IdentifierLoc *IL = AL.getArgAsIdent(0);
2964 ObjCMethodFamilyAttr::FamilyKind F;
2965 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2966 S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
2967 return;
2968 }
2969
2970 if (F == ObjCMethodFamilyAttr::OMF_init &&
2971 !M->getReturnType()->isObjCObjectPointerType()) {
2972 S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2973 << M->getReturnType();
2974 // Ignore the attribute.
2975 return;
2976 }
2977
2978 D->addAttr(new (S.Context) ObjCMethodFamilyAttr(S.Context, AL, F));
2979}
2980
2981static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
2982 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
2983 QualType T = TD->getUnderlyingType();
2984 if (!T->isCARCBridgableType()) {
2985 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2986 return;
2987 }
2988 }
2989 else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2990 QualType T = PD->getType();
2991 if (!T->isCARCBridgableType()) {
2992 S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2993 return;
2994 }
2995 }
2996 else {
2997 // It is okay to include this attribute on properties, e.g.:
2998 //
2999 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
3000 //
3001 // In this case it follows tradition and suppresses an error in the above
3002 // case.
3003 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
3004 }
3005 D->addAttr(::new (S.Context) ObjCNSObjectAttr(S.Context, AL));
3006}
3007
3008static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
3009 if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
3010 QualType T = TD->getUnderlyingType();
3011 if (!T->isObjCObjectPointerType()) {
3012 S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
3013 return;
3014 }
3015 } else {
3016 S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
3017 return;
3018 }
3019 D->addAttr(::new (S.Context) ObjCIndependentClassAttr(S.Context, AL));
3020}
3021
3022static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3023 if (!AL.isArgIdent(0)) {
3024 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3025 << AL << 1 << AANT_ArgumentIdentifier;
3026 return;
3027 }
3028
3029 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
3030 BlocksAttr::BlockType type;
3031 if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
3032 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
3033 return;
3034 }
3035
3036 D->addAttr(::new (S.Context) BlocksAttr(S.Context, AL, type));
3037}
3038
3039static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3040 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
3041 if (AL.getNumArgs() > 0) {
3042 Expr *E = AL.getArgAsExpr(0);
3043 std::optional<llvm::APSInt> Idx = llvm::APSInt(32);
3044 if (E->isTypeDependent() || !(Idx = E->getIntegerConstantExpr(S.Context))) {
3045 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3046 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
3047 return;
3048 }
3049
3050 if (Idx->isSigned() && Idx->isNegative()) {
3051 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
3052 << E->getSourceRange();
3053 return;
3054 }
3055
3056 sentinel = Idx->getZExtValue();
3057 }
3058
3059 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
3060 if (AL.getNumArgs() > 1) {
3061 Expr *E = AL.getArgAsExpr(1);
3062 std::optional<llvm::APSInt> Idx = llvm::APSInt(32);
3063 if (E->isTypeDependent() || !(Idx = E->getIntegerConstantExpr(S.Context))) {
3064 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3065 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
3066 return;
3067 }
3068 nullPos = Idx->getZExtValue();
3069
3070 if ((Idx->isSigned() && Idx->isNegative()) || nullPos > 1) {
3071 // FIXME: This error message could be improved, it would be nice
3072 // to say what the bounds actually are.
3073 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
3074 << E->getSourceRange();
3075 return;
3076 }
3077 }
3078
3079 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3080 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
3081 if (isa<FunctionNoProtoType>(FT)) {
3082 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
3083 return;
3084 }
3085
3086 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
3087 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
3088 return;
3089 }
3090 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
3091 if (!MD->isVariadic()) {
3092 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
3093 return;
3094 }
3095 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
3096 if (!BD->isVariadic()) {
3097 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
3098 return;
3099 }
3100 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
3101 QualType Ty = V->getType();
3102 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
3103 const FunctionType *FT = Ty->isFunctionPointerType()
3104 ? D->getFunctionType()
3105 : Ty->castAs<BlockPointerType>()
3106 ->getPointeeType()
3107 ->castAs<FunctionType>();
3108 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
3109 int m = Ty->isFunctionPointerType() ? 0 : 1;
3110 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
3111 return;
3112 }
3113 } else {
3114 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3116 return;
3117 }
3118 } else {
3119 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3121 return;
3122 }
3123 D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos));
3124}
3125
3126static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
3127 if (D->getFunctionType() &&
3129 !isa<CXXConstructorDecl>(D)) {
3130 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
3131 return;
3132 }
3133 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
3134 if (MD->getReturnType()->isVoidType()) {
3135 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
3136 return;
3137 }
3138
3139 StringRef Str;
3140 if (AL.isStandardAttributeSyntax() && !AL.getScopeName()) {
3141 // The standard attribute cannot be applied to variable declarations such
3142 // as a function pointer.
3143 if (isa<VarDecl>(D))
3144 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
3145 << AL << "functions, classes, or enumerations";
3146
3147 // If this is spelled as the standard C++17 attribute, but not in C++17,
3148 // warn about using it as an extension. If there are attribute arguments,
3149 // then claim it's a C++2a extension instead.
3150 // FIXME: If WG14 does not seem likely to adopt the same feature, add an
3151 // extension warning for C2x mode.
3152 const LangOptions &LO = S.getLangOpts();
3153 if (AL.getNumArgs() == 1) {
3154 if (LO.CPlusPlus && !LO.CPlusPlus20)
3155 S.Diag(AL.getLoc(), diag::ext_cxx20_attr) << AL;
3156
3157 // Since this is spelled [[nodiscard]], get the optional string
3158 // literal. If in C++ mode, but not in C++2a mode, diagnose as an
3159 // extension.
3160 // FIXME: C2x should support this feature as well, even as an extension.
3161 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
3162 return;
3163 } else if (LO.CPlusPlus && !LO.CPlusPlus17)
3164 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
3165 }
3166
3167 if ((!AL.isGNUAttribute() &&
3168 !(AL.isStandardAttributeSyntax() && AL.isClangScope())) &&
3169 isa<TypedefNameDecl>(D)) {
3170 S.Diag(AL.getLoc(), diag::warn_unused_result_typedef_unsupported_spelling)
3171 << AL.isGNUScope();
3172 return;
3173 }
3174
3175 D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str));
3176}
3177
3178static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3179 // weak_import only applies to variable & function declarations.
3180 bool isDef = false;
3181 if (!D->canBeWeakImported(isDef)) {
3182 if (isDef)
3183 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
3184 << "weak_import";
3185 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
3186 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
3187 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
3188 // Nothing to warn about here.
3189 } else
3190 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3192
3193 return;
3194 }
3195
3196 D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL));
3197}
3198
3199// Handles reqd_work_group_size and work_group_size_hint.
3200template <typename WorkGroupAttr>
3201static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
3202 uint32_t WGSize[3];
3203 for (unsigned i = 0; i < 3; ++i) {
3204 const Expr *E = AL.getArgAsExpr(i);
3205 if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
3206 /*StrictlyUnsigned=*/true))
3207 return;
3208 if (WGSize[i] == 0) {
3209 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
3210 << AL << E->getSourceRange();
3211 return;
3212 }
3213 }
3214
3215 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
3216 if (Existing && !(Existing->getXDim() == WGSize[0] &&
3217 Existing->getYDim() == WGSize[1] &&
3218 Existing->getZDim() == WGSize[2]))
3219 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3220
3221 D->addAttr(::new (S.Context)
3222 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
3223}
3224
3225// Handles intel_reqd_sub_group_size.
3226static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
3227 uint32_t SGSize;
3228 const Expr *E = AL.getArgAsExpr(0);
3229 if (!checkUInt32Argument(S, AL, E, SGSize))
3230 return;
3231 if (SGSize == 0) {
3232 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
3233 << AL << E->getSourceRange();
3234 return;
3235 }
3236
3237 OpenCLIntelReqdSubGroupSizeAttr *Existing =
3238 D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
3239 if (Existing && Existing->getSubGroupSize() != SGSize)
3240 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3241
3242 D->addAttr(::new (S.Context)
3243 OpenCLIntelReqdSubGroupSizeAttr(S.Context, AL, SGSize));
3244}
3245
3246static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
3247 if (!AL.hasParsedType()) {
3248 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
3249 return;
3250 }
3251
3252 TypeSourceInfo *ParmTSI = nullptr;
3253 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
3254 assert(ParmTSI && "no type source info for attribute argument");
3255
3256 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
3257 (ParmType->isBooleanType() ||
3258 !ParmType->isIntegralType(S.getASTContext()))) {
3259 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 2 << AL;
3260 return;
3261 }
3262
3263 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
3264 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
3265 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3266 return;
3267 }
3268 }
3269
3270 D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
3271}
3272
3274 StringRef Name) {
3275 // Explicit or partial specializations do not inherit
3276 // the section attribute from the primary template.
3277 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3278 if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate &&
3280 return nullptr;
3281 }
3282 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
3283 if (ExistingAttr->getName() == Name)
3284 return nullptr;
3285 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3286 << 1 /*section*/;
3287 Diag(CI.getLoc(), diag::note_previous_attribute);
3288 return nullptr;
3289 }
3290 return ::new (Context) SectionAttr(Context, CI, Name);
3291}
3292
3293/// Used to implement to perform semantic checking on
3294/// attribute((section("foo"))) specifiers.
3295///
3296/// In this case, "foo" is passed in to be checked. If the section
3297/// specifier is invalid, return an Error that indicates the problem.
3298///
3299/// This is a simple quality of implementation feature to catch errors
3300/// and give good diagnostics in cases when the assembler or code generator
3301/// would otherwise reject the section specifier.
3302llvm::Error Sema::isValidSectionSpecifier(StringRef SecName) {
3303 if (!Context.getTargetInfo().getTriple().isOSDarwin())
3304 return llvm::Error::success();
3305
3306 // Let MCSectionMachO validate this.
3307 StringRef Segment, Section;
3308 unsigned TAA, StubSize;
3309 bool HasTAA;
3310 return llvm::MCSectionMachO::ParseSectionSpecifier(SecName, Segment, Section,
3311 TAA, HasTAA, StubSize);
3312}
3313
3314bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
3315 if (llvm::Error E = isValidSectionSpecifier(SecName)) {
3316 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3317 << toString(std::move(E)) << 1 /*'section'*/;
3318 return false;
3319 }
3320 return true;
3321}
3322
3323static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3324 // Make sure that there is a string literal as the sections's single
3325 // argument.
3326 StringRef Str;
3327 SourceLocation LiteralLoc;
3328 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3329 return;
3330
3331 if (!S.checkSectionName(LiteralLoc, Str))
3332 return;
3333
3334 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
3335 if (NewAttr) {
3336 D->addAttr(NewAttr);
3338 ObjCPropertyDecl>(D))
3339 S.UnifySection(NewAttr->getName(),
3341 cast<NamedDecl>(D));
3342 }
3343}
3344
3345// This is used for `__declspec(code_seg("segname"))` on a decl.
3346// `#pragma code_seg("segname")` uses checkSectionName() instead.
3347static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
3348 StringRef CodeSegName) {
3349 if (llvm::Error E = S.isValidSectionSpecifier(CodeSegName)) {
3350 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3351 << toString(std::move(E)) << 0 /*'code-seg'*/;
3352 return false;
3353 }
3354
3355 return true;
3356}
3357
3359 StringRef Name) {
3360 // Explicit or partial specializations do not inherit
3361 // the code_seg attribute from the primary template.
3362 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3364 return nullptr;
3365 }
3366 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3367 if (ExistingAttr->getName() == Name)
3368 return nullptr;
3369 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3370 << 0 /*codeseg*/;
3371 Diag(CI.getLoc(), diag::note_previous_attribute);
3372 return nullptr;
3373 }
3374 return ::new (Context) CodeSegAttr(Context, CI, Name);
3375}
3376
3377static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3378 StringRef Str;
3379 SourceLocation LiteralLoc;
3380 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3381 return;
3382 if (!checkCodeSegName(S, LiteralLoc, Str))
3383 return;
3384 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3385 if (!ExistingAttr->isImplicit()) {
3386 S.Diag(AL.getLoc(),
3387 ExistingAttr->getName() == Str
3388 ? diag::warn_duplicate_codeseg_attribute
3389 : diag::err_conflicting_codeseg_attribute);
3390 return;
3391 }
3392 D->dropAttr<CodeSegAttr>();
3393 }
3394 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
3395 D->addAttr(CSA);
3396}
3397
3398// Check for things we'd like to warn about. Multiversioning issues are
3399// handled later in the process, once we know how many exist.
3400bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3401 enum FirstParam { Unsupported, Duplicate, Unknown };
3402 enum SecondParam { None, CPU, Tune };
3403 enum ThirdParam { Target, TargetClones };
3404 if (AttrStr.contains("fpmath="))
3405 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3406 << Unsupported << None << "fpmath=" << Target;
3407
3408 // Diagnose use of tune if target doesn't support it.
3410 AttrStr.contains("tune="))
3411 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3412 << Unsupported << None << "tune=" << Target;
3413
3414 ParsedTargetAttr ParsedAttrs =
3416
3417 if (!ParsedAttrs.CPU.empty() &&
3418 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.CPU))
3419 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3420 << Unknown << CPU << ParsedAttrs.CPU << Target;
3421
3422 if (!ParsedAttrs.Tune.empty() &&
3423 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Tune))
3424 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3425 << Unknown << Tune << ParsedAttrs.Tune << Target;
3426
3427 if (ParsedAttrs.Duplicate != "")
3428 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3429 << Duplicate << None << ParsedAttrs.Duplicate << Target;
3430
3431 for (const auto &Feature : ParsedAttrs.Features) {
3432 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3433 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3434 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3435 << Unsupported << None << CurFeature << Target;
3436 }
3437
3439 StringRef DiagMsg;
3440 if (ParsedAttrs.BranchProtection.empty())
3441 return false;
3443 ParsedAttrs.BranchProtection, ParsedAttrs.CPU, BPI, DiagMsg)) {
3444 if (DiagMsg.empty())
3445 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3446 << Unsupported << None << "branch-protection" << Target;
3447 return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec)
3448 << DiagMsg;
3449 }
3450 if (!DiagMsg.empty())
3451 Diag(LiteralLoc, diag::warn_unsupported_branch_protection_spec) << DiagMsg;
3452
3453 return false;
3454}
3455
3456// Check Target Version attrs
3457bool Sema::checkTargetVersionAttr(SourceLocation LiteralLoc, StringRef &AttrStr,
3458 bool &isDefault) {
3459 enum FirstParam { Unsupported };
3460 enum SecondParam { None };
3461 enum ThirdParam { Target, TargetClones, TargetVersion };
3462 if (AttrStr.trim() == "default")
3463 isDefault = true;
3465 AttrStr.split(Features, "+");
3466 for (auto &CurFeature : Features) {
3467 CurFeature = CurFeature.trim();
3468 if (CurFeature == "default")
3469 continue;
3470 if (!Context.getTargetInfo().validateCpuSupports(CurFeature))
3471 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3472 << Unsupported << None << CurFeature << TargetVersion;
3473 }
3474 return false;
3475}
3476
3477static void handleTargetVersionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3478 StringRef Str;
3479 SourceLocation LiteralLoc;
3480 bool isDefault = false;
3481 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
3482 S.checkTargetVersionAttr(LiteralLoc, Str, isDefault))
3483 return;
3484 // Do not create default only target_version attribute
3485 if (!isDefault) {
3486 TargetVersionAttr *NewAttr =
3487 ::new (S.Context) TargetVersionAttr(S.Context, AL, Str);
3488 D->addAttr(NewAttr);
3489 }
3490}
3491
3492static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3493 StringRef Str;
3494 SourceLocation LiteralLoc;
3495 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
3496 S.checkTargetAttr(LiteralLoc, Str))
3497 return;
3498
3499 TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str);
3500 D->addAttr(NewAttr);
3501}
3502
3504 SourceLocation LiteralLoc, StringRef Str, const StringLiteral *Literal,
3505 bool &HasDefault, bool &HasCommas, bool &HasNotDefault,
3506 SmallVectorImpl<SmallString<64>> &StringsBuffer) {
3507 enum FirstParam { Unsupported, Duplicate, Unknown };
3508 enum SecondParam { None, CPU, Tune };
3509 enum ThirdParam { Target, TargetClones };
3510 HasCommas = HasCommas || Str.contains(',');
3511 const TargetInfo &TInfo = Context.getTargetInfo();
3512 // Warn on empty at the beginning of a string.
3513 if (Str.size() == 0)
3514 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3515 << Unsupported << None << "" << TargetClones;
3516
3517 std::pair<StringRef, StringRef> Parts = {{}, Str};
3518 while (!Parts.second.empty()) {
3519 Parts = Parts.second.split(',');
3520 StringRef Cur = Parts.first.trim();
3521 SourceLocation CurLoc =
3522 Literal->getLocationOfByte(Cur.data() - Literal->getString().data(),
3523 getSourceManager(), getLangOpts(), TInfo);
3524
3525 bool DefaultIsDupe = false;
3526 bool HasCodeGenImpact = false;
3527 if (Cur.empty())
3528 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
3529 << Unsupported << None << "" << TargetClones;
3530
3531 if (TInfo.getTriple().isAArch64()) {
3532 // AArch64 target clones specific
3533 if (Cur == "default") {
3534 DefaultIsDupe = HasDefault;
3535 HasDefault = true;
3536 if (llvm::is_contained(StringsBuffer, Cur) || DefaultIsDupe)
3537 Diag(CurLoc, diag::warn_target_clone_duplicate_options);
3538 else
3539 StringsBuffer.push_back(Cur);
3540 } else {
3541 std::pair<StringRef, StringRef> CurParts = {{}, Cur};
3543 while (!CurParts.second.empty()) {
3544 CurParts = CurParts.second.split('+');
3545 StringRef CurFeature = CurParts.first.trim();
3546 if (!TInfo.validateCpuSupports(CurFeature)) {
3547 Diag(CurLoc, diag::warn_unsupported_target_attribute)
3548 << Unsupported << None << CurFeature << TargetClones;
3549 continue;
3550 }
3551 if (TInfo.doesFeatureAffectCodeGen(CurFeature))
3552 HasCodeGenImpact = true;
3553 CurFeatures.push_back(CurFeature);
3554 }
3555 // Canonize TargetClones Attributes
3556 llvm::sort(CurFeatures);
3557 SmallString<64> Res;
3558 for (auto &CurFeat : CurFeatures) {
3559 if (!Res.equals(""))
3560 Res.append("+");
3561 Res.append(CurFeat);
3562 }
3563 if (llvm::is_contained(StringsBuffer, Res) || DefaultIsDupe)
3564 Diag(CurLoc, diag::warn_target_clone_duplicate_options);
3565 else if (!HasCodeGenImpact)
3566 // Ignore features in target_clone attribute that don't impact
3567 // code generation
3568 Diag(CurLoc, diag::warn_target_clone_no_impact_options);
3569 else if (!Res.empty()) {
3570 StringsBuffer.push_back(Res);
3571 HasNotDefault = true;
3572 }
3573 }
3574 } else {
3575 // Other targets ( currently X86 )
3576 if (Cur.startswith("arch=")) {
3578 Cur.drop_front(sizeof("arch=") - 1)))
3579 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
3580 << Unsupported << CPU << Cur.drop_front(sizeof("arch=") - 1)
3581 << TargetClones;
3582 } else if (Cur == "default") {
3583 DefaultIsDupe = HasDefault;
3584 HasDefault = true;
3585 } else if (!Context.getTargetInfo().isValidFeatureName(Cur))
3586 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
3587 << Unsupported << None << Cur << TargetClones;
3588 if (llvm::is_contained(StringsBuffer, Cur) || DefaultIsDupe)
3589 Diag(CurLoc, diag::warn_target_clone_duplicate_options);
3590 // Note: Add even if there are duplicates, since it changes name mangling.
3591 StringsBuffer.push_back(Cur);
3592 }
3593 }
3594 if (Str.rtrim().endswith(","))
3595 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3596 << Unsupported << None << "" << TargetClones;
3597 return false;
3598}
3599
3600static void handleTargetClonesAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3601 if (S.Context.getTargetInfo().getTriple().isAArch64() &&
3602 !S.Context.getTargetInfo().hasFeature("fmv"))
3603 return;
3604
3605 // Ensure we don't combine these with themselves, since that causes some
3606 // confusing behavior.
3607 if (const auto *Other = D->getAttr<TargetClonesAttr>()) {
3608 S.Diag(AL.getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
3609 S.Diag(Other->getLocation(), diag::note_conflicting_attribute);
3610 return;
3611 }
3612 if (checkAttrMutualExclusion<TargetClonesAttr>(S, D, AL))
3613 return;
3614
3616 SmallVector<SmallString<64>, 2> StringsBuffer;
3617 bool HasCommas = false, HasDefault = false, HasNotDefault = false;
3618
3619 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
3620 StringRef CurStr;
3621 SourceLocation LiteralLoc;
3622 if (!S.checkStringLiteralArgumentAttr(AL, I, CurStr, &LiteralLoc) ||
3624 LiteralLoc, CurStr,
3625 cast<StringLiteral>(AL.getArgAsExpr(I)->IgnoreParenCasts()),
3626 HasDefault, HasCommas, HasNotDefault, StringsBuffer))
3627 return;
3628 }
3629 for (auto &SmallStr : StringsBuffer)
3630 Strings.push_back(SmallStr.str());
3631
3632 if (HasCommas && AL.getNumArgs() > 1)
3633 S.Diag(AL.getLoc(), diag::warn_target_clone_mixed_values);
3634
3635 if (S.Context.getTargetInfo().getTriple().isAArch64() && !HasDefault) {
3636 // Add default attribute if there is no one
3637 HasDefault = true;
3638 Strings.push_back("default");
3639 }
3640
3641 if (!HasDefault) {
3642 S.Diag(AL.getLoc(), diag::err_target_clone_must_have_default);
3643 return;
3644 }
3645
3646 // FIXME: We could probably figure out how to get this to work for lambdas
3647 // someday.
3648 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
3649 if (MD->getParent()->isLambda()) {
3650 S.Diag(D->getLocation(), diag::err_multiversion_doesnt_support)
3651 << static_cast<unsigned>(MultiVersionKind::TargetClones)
3652 << /*Lambda*/ 9;
3653 return;
3654 }
3655 }
3656
3657 // No multiversion if we have default version only.
3658 if (S.Context.getTargetInfo().getTriple().isAArch64() && !HasNotDefault)
3659 return;
3660
3661 cast<FunctionDecl>(D)->setIsMultiVersion();
3662 TargetClonesAttr *NewAttr = ::new (S.Context)
3663 TargetClonesAttr(S.Context, AL, Strings.data(), Strings.size());
3664 D->addAttr(NewAttr);
3665}
3666
3667static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3668 Expr *E = AL.getArgAsExpr(0);
3669 uint32_t VecWidth;
3670 if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3671 AL.setInvalid();
3672 return;
3673 }
3674
3675 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3676 if (Existing && Existing->getVectorWidth() != VecWidth) {
3677 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3678 return;
3679 }
3680
3681 D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth));
3682}
3683
3684static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3685 Expr *E = AL.getArgAsExpr(0);
3686 SourceLocation Loc = E->getExprLoc();
3687 FunctionDecl *FD = nullptr;
3689
3690 // gcc only allows for simple identifiers. Since we support more than gcc, we
3691 // will warn the user.
3692 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3693 if (DRE->hasQualifier())
3694 S.Diag(Loc, diag::warn_cleanup_ext);
3695 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3696 NI = DRE->getNameInfo();
3697 if (!FD) {
3698 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3699 << NI.getName();
3700 return;
3701 }
3702 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
3703 if (ULE->hasExplicitTemplateArgs())
3704 S.Diag(Loc, diag::warn_cleanup_ext);
3706 NI = ULE->getNameInfo();
3707 if (!FD) {
3708 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3709 << NI.getName();
3710 if (ULE->getType() == S.Context.OverloadTy)
3712 return;
3713 }
3714 } else {
3715 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
3716 return;
3717 }
3718
3719 if (FD->getNumParams() != 1) {
3720 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3721 << NI.getName();
3722 return;
3723 }
3724
3725 // We're currently more strict than GCC about what function types we accept.
3726 // If this ever proves to be a problem it should be easy to fix.
3727 QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
3728 QualType ParamTy = FD->getParamDecl(0)->getType();
3730 ParamTy, Ty) != Sema::Compatible) {
3731 S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3732 << NI.getName() << ParamTy << Ty;
3733 return;
3734 }
3735
3736 D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
3737}
3738
3740 const ParsedAttr &AL) {
3741 if (!AL.isArgIdent(0)) {
3742 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3743 << AL << 0 << AANT_ArgumentIdentifier;
3744 return;
3745 }
3746
3747 EnumExtensibilityAttr::Kind ExtensibilityKind;
3748 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
3749 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3750 ExtensibilityKind)) {
3751 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
3752 return;
3753 }
3754
3755 D->addAttr(::new (S.Context)
3756 EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind));
3757}
3758
3759/// Handle __attribute__((format_arg((idx)))) attribute based on
3760/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
3761static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3762 Expr *IdxExpr = AL.getArgAsExpr(0);
3763 ParamIdx Idx;
3764 if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
3765 return;
3766
3767 // Make sure the format string is really a string.
3769
3770 bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3771 if (NotNSStringTy &&
3772 !isCFStringType(Ty, S.Context) &&
3773 (!Ty->isPointerType() ||
3775 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
3776 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
3777 return;
3778 }
3780 // replace instancetype with the class type
3781 auto Instancetype = S.Context.getObjCInstanceTypeDecl()->getTypeForDecl();
3782 if (Ty->getAs<TypedefType>() == Instancetype)
3783 if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
3784 if (auto *Interface = OMD->getClassInterface())
3786 QualType(Interface->getTypeForDecl(), 0));
3787 if (!isNSStringType(Ty, S.Context, /*AllowNSAttributedString=*/true) &&
3788 !isCFStringType(Ty, S.Context) &&
3789 (!Ty->isPointerType() ||
3791 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
3792 << (NotNSStringTy ? "string type" : "NSString")
3793 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
3794 return;
3795 }
3796
3797 D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
3798}
3799
3808
3809/// getFormatAttrKind - Map from format attribute names to supported format
3810/// types.
3811static FormatAttrKind getFormatAttrKind(StringRef Format) {
3812 return llvm::StringSwitch<FormatAttrKind>(Format)
3813 // Check for formats that get handled specially.
3814 .Case("NSString", NSStringFormat)
3815 .Case("CFString", CFStringFormat)
3816 .Case("strftime", StrftimeFormat)
3817
3818 // Otherwise, check for supported formats.
3819 .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3820 .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3821 .Case("kprintf", SupportedFormat) // OpenBSD.
3822 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3823 .Case("os_trace", SupportedFormat)
3824 .Case("os_log", SupportedFormat)
3825
3826 .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3827 .Default(InvalidFormat);
3828}
3829
3830/// Handle __attribute__((init_priority(priority))) attributes based on
3831/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
3832static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3833 if (!S.getLangOpts().CPlusPlus) {
3834 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
3835 return;
3836 }
3837
3838 if (S.getLangOpts().HLSL) {
3839 S.Diag(AL.getLoc(), diag::err_hlsl_init_priority_unsupported);
3840 return;
3841 }
3842
3844 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3845 AL.setInvalid();
3846 return;
3847 }
3848 QualType T = cast<VarDecl>(D)->getType();
3849 if (S.Context.getAsArrayType(T))
3850 T = S.Context.getBaseElementType(T);
3851 if (!T->getAs<RecordType>()) {
3852 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3853 AL.setInvalid();
3854 return;
3855 }
3856
3857 Expr *E = AL.getArgAsExpr(0);
3858 uint32_t prioritynum;
3859 if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3860 AL.setInvalid();
3861 return;
3862 }
3863
3864 // Only perform the priority check if the attribute is outside of a system
3865 // header. Values <= 100 are reserved for the implementation, and libc++
3866 // benefits from being able to specify values in that range.
3867 if ((prioritynum < 101 || prioritynum > 65535) &&
3869 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
3870 << E->getSourceRange() << AL << 101 << 65535;
3871 AL.setInvalid();
3872 return;
3873 }
3874 D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
3875}
3876
3878 StringRef NewUserDiagnostic) {
3879 if (const auto *EA = D->getAttr<ErrorAttr>()) {
3880 std::string NewAttr = CI.getNormalizedFullName();
3881 assert((NewAttr == "error" || NewAttr == "warning") &&
3882 "unexpected normalized full name");
3883 bool Match = (EA->isError() && NewAttr == "error") ||
3884 (EA->isWarning() && NewAttr == "warning");
3885 if (!Match) {
3886 Diag(EA->getLocation(), diag::err_attributes_are_not_compatible)
3887 << CI << EA;
3888 Diag(CI.getLoc(), diag::note_conflicting_attribute);
3889 return nullptr;
3890 }
3891 if (EA->getUserDiagnostic() != NewUserDiagnostic) {
3892 Diag(CI.getLoc(), diag::warn_duplicate_attribute) << EA;
3893 Diag(EA->getLoc(), diag::note_previous_attribute);
3894 }
3895 D->dropAttr<ErrorAttr>();
3896 }
3897 return ::new (Context) ErrorAttr(Context, CI, NewUserDiagnostic);
3898}
3899
3901 IdentifierInfo *Format, int FormatIdx,
3902 int FirstArg) {
3903 // Check whether we already have an equivalent format attribute.
3904 for (auto *F : D->specific_attrs<FormatAttr>()) {
3905 if (F->getType() == Format &&
3906 F->getFormatIdx() == FormatIdx &&
3907 F->getFirstArg() == FirstArg) {
3908 // If we don't have a valid location for this attribute, adopt the
3909 // location.
3910 if (F->getLocation().isInvalid())
3911 F->setRange(CI.getRange());
3912 return nullptr;
3913 }
3914 }
3915
3916 return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg);
3917}
3918
3919/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
3920/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
3921static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3922 if (!AL.isArgIdent(0)) {
3923 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3924 << AL << 1 << AANT_ArgumentIdentifier;
3925 return;
3926 }
3927
3928 // In C++ the implicit 'this' function parameter also counts, and they are
3929 // counted from one.
3930 bool HasImplicitThisParam = isInstanceMethod(D);
3931 unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
3932
3933 IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
3934 StringRef Format = II->getName();
3935
3936 if (normalizeName(Format)) {
3937 // If we've modified the string name, we need a new identifier for it.
3938 II = &S.Context.Idents.get(Format);
3939 }
3940
3941 // Check for supported formats.
3942 FormatAttrKind Kind = getFormatAttrKind(Format);
3943
3944 if (Kind == IgnoredFormat)
3945 return;
3946
3947 if (Kind == InvalidFormat) {
3948 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3949 << AL << II->getName();
3950 return;
3951 }
3952
3953 // checks for the 2nd argument
3954 Expr *IdxExpr = AL.getArgAsExpr(1);
3955 uint32_t Idx;
3956 if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
3957 return;
3958
3959 if (Idx < 1 || Idx > NumArgs) {
3960 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3961 << AL << 2 << IdxExpr->getSourceRange();
3962 return;
3963 }
3964
3965 // FIXME: Do we need to bounds check?
3966 unsigned ArgIdx = Idx - 1;
3967
3968 if (HasImplicitThisParam) {
3969 if (ArgIdx == 0) {
3970 S.Diag(AL.getLoc(),
3971 diag::err_format_attribute_implicit_this_format_string)
3972 << IdxExpr->getSourceRange();
3973 return;
3974 }
3975 ArgIdx--;
3976 }
3977
3978 // make sure the format string is really a string
3979 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
3980
3981 if (!isNSStringType(Ty, S.Context, true) &&
3982 !isCFStringType(Ty, S.Context) &&
3983 (!Ty->isPointerType() ||
3985 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
3986 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, ArgIdx);
3987 return;
3988 }
3989
3990 // check the 3rd argument
3991 Expr *FirstArgExpr = AL.getArgAsExpr(2);
3992 uint32_t FirstArg;
3993 if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
3994 return;
3995
3996 // FirstArg == 0 is is always valid.
3997 if (FirstArg != 0) {
3998 if (Kind == StrftimeFormat) {
3999 // If the kind is strftime, FirstArg must be 0 because strftime does not
4000 // use any variadic arguments.
4001 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
4002 << FirstArgExpr->getSourceRange()
4003 << FixItHint::CreateReplacement(FirstArgExpr->getSourceRange(), "0");
4004 return;
4005 } else if (isFunctionOrMethodVariadic(D)) {
4006 // Else, if the function is variadic, then FirstArg must be 0 or the
4007 // "position" of the ... parameter. It's unusual to use 0 with variadic
4008 // functions, so the fixit proposes the latter.
4009 if (FirstArg != NumArgs + 1) {
4010 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4011 << AL << 3 << FirstArgExpr->getSourceRange()
4013 std::to_string(NumArgs + 1));
4014 return;
4015 }
4016 } else {
4017 // Inescapable GCC compatibility diagnostic.
4018 S.Diag(D->getLocation(), diag::warn_gcc_requires_variadic_function) << AL;
4019 if (FirstArg <= Idx) {
4020 // Else, the function is not variadic, and FirstArg must be 0 or any
4021 // parameter after the format parameter. We don't offer a fixit because
4022 // there are too many possible good values.
4023 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4024 << AL << 3 << FirstArgExpr->getSourceRange();
4025 return;
4026 }
4027 }
4028 }
4029
4030 FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg);
4031 if (NewAttr)
4032 D->addAttr(NewAttr);
4033}
4034
4035/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
4036static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4037 // The index that identifies the callback callee is mandatory.
4038 if (AL.getNumArgs() == 0) {
4039 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
4040 << AL.getRange();
4041 return;
4042 }
4043
4044 bool HasImplicitThisParam = isInstanceMethod(D);
4045 int32_t NumArgs = getFunctionOrMethodNumParams(D);
4046
4047 FunctionDecl *FD = D->getAsFunction();
4048 assert(FD && "Expected a function declaration!");
4049
4050 llvm::StringMap<int> NameIdxMapping;
4051 NameIdxMapping["__"] = -1;
4052
4053 NameIdxMapping["this"] = 0;
4054
4055 int Idx = 1;
4056 for (const ParmVarDecl *PVD : FD->parameters())
4057 NameIdxMapping[PVD->getName()] = Idx++;
4058
4059 auto UnknownName = NameIdxMapping.end();
4060
4061 SmallVector<int, 8> EncodingIndices;
4062 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
4063 SourceRange SR;
4064 int32_t ArgIdx;
4065
4066 if (AL.isArgIdent(I)) {
4067 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
4068 auto It = NameIdxMapping.find(IdLoc->Ident->getName());
4069 if (It == UnknownName) {
4070 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
4071 << IdLoc->Ident << IdLoc->Loc;
4072 return;
4073 }
4074
4075 SR = SourceRange(IdLoc->Loc);
4076 ArgIdx = It->second;
4077 } else if (AL.isArgExpr(I)) {
4078 Expr *IdxExpr = AL.getArgAsExpr(I);
4079
4080 // If the expression is not parseable as an int32_t we have a problem.
4081 if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
4082 false)) {
4083 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4084 << AL << (I + 1) << IdxExpr->getSourceRange();
4085 return;
4086 }
4087
4088 // Check oob, excluding the special values, 0 and -1.
4089 if (ArgIdx < -1 || ArgIdx > NumArgs) {
4090 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4091 << AL << (I + 1) << IdxExpr->getSourceRange();
4092 return;
4093 }
4094
4095 SR = IdxExpr->getSourceRange();
4096 } else {
4097 llvm_unreachable("Unexpected ParsedAttr argument type!");
4098 }
4099
4100 if (ArgIdx == 0 && !HasImplicitThisParam) {
4101 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
4102 << (I + 1) << SR;
4103 return;
4104 }
4105
4106 // Adjust for the case we do not have an implicit "this" parameter. In this
4107 // case we decrease all positive values by 1 to get LLVM argument indices.
4108 if (!HasImplicitThisParam && ArgIdx > 0)
4109 ArgIdx -= 1;
4110
4111 EncodingIndices.push_back(ArgIdx);
4112 }
4113
4114 int CalleeIdx = EncodingIndices.front();
4115 // Check if the callee index is proper, thus not "this" and not "unknown".
4116 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
4117 // is false and positive if "HasImplicitThisParam" is true.
4118 if (CalleeIdx < (int)HasImplicitThisParam) {
4119 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
4120 << AL.getRange();
4121 return;
4122 }
4123
4124 // Get the callee type, note the index adjustment as the AST doesn't contain
4125 // the this type (which the callee cannot reference anyway!).
4126 const Type *CalleeType =
4127 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
4128 .getTypePtr();
4129 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
4130 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
4131 << AL.getRange();
4132 return;
4133 }
4134
4135 const Type *CalleeFnType =
4137
4138 // TODO: Check the type of the callee arguments.
4139
4140 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
4141 if (!CalleeFnProtoType) {
4142 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
4143 << AL.getRange();
4144 return;
4145 }
4146
4147 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
4148 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
4149 << AL << (unsigned)(EncodingIndices.size() - 1);
4150 return;
4151 }
4152
4153 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
4154 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
4155 << AL << (unsigned)(EncodingIndices.size() - 1);
4156 return;
4157 }
4158
4159 if (CalleeFnProtoType->isVariadic()) {
4160 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
4161 return;
4162 }
4163
4164 // Do not allow multiple callback attributes.
4165 if (D->hasAttr<CallbackAttr>()) {
4166 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
4167 return;
4168 }
4169
4170 D->addAttr(::new (S.Context) CallbackAttr(
4171 S.Context, AL, EncodingIndices.data(), EncodingIndices.size()));
4172}
4173
4174static bool isFunctionLike(const Type &T) {
4175 // Check for explicit function types.
4176 // 'called_once' is only supported in Objective-C and it has
4177 // function pointers and block pointers.
4178 return T.isFunctionPointerType() || T.isBlockPointerType();
4179}
4180
4181/// Handle 'called_once' attribute.
4182static void handleCalledOnceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4183 // 'called_once' only applies to parameters representing functions.
4184 QualType T = cast<ParmVarDecl>(D)->getType();
4185
4186 if (!isFunctionLike(*T)) {
4187 S.Diag(AL.getLoc(), diag::err_called_once_attribute_wrong_type);
4188 return;
4189 }
4190
4191 D->addAttr(::new (S.Context) CalledOnceAttr(S.Context, AL));
4192}
4193
4194static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4195 // Try to find the underlying union declaration.
4196 RecordDecl *RD = nullptr;
4197 const auto *TD = dyn_cast<TypedefNameDecl>(D);
4198 if (TD && TD->getUnderlyingType()->isUnionType())
4199 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
4200 else
4201 RD = dyn_cast<RecordDecl>(D);
4202
4203 if (!RD || !RD->isUnion()) {
4204 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
4205 << ExpectedUnion;
4206 return;
4207 }
4208
4209 if (!RD->isCompleteDefinition()) {
4210 if (!RD->isBeingDefined())
4211 S.Diag(AL.getLoc(),
4212 diag::warn_transparent_union_attribute_not_definition);
4213 return;
4214 }
4215
4217 FieldEnd = RD->field_end();
4218 if (Field == FieldEnd) {
4219 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
4220 return;
4221 }
4222
4223 FieldDecl *FirstField = *Field;
4224 QualType FirstType = FirstField->getType();
4225 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
4226 S.Diag(FirstField->getLocation(),
4227 diag::warn_transparent_union_attribute_floating)
4228 << FirstType->isVectorType() << FirstType;
4229 return;
4230 }
4231
4232 if (FirstType->isIncompleteType())
4233 return;
4234 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
4235 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
4236 for (; Field != FieldEnd; ++Field) {
4237 QualType FieldType = Field->getType();
4238 if (FieldType->isIncompleteType())
4239 return;
4240 // FIXME: this isn't fully correct; we also need to test whether the
4241 // members of the union would all have the same calling convention as the
4242 // first member of the union. Checking just the size and alignment isn't
4243 // sufficient (consider structs passed on the stack instead of in registers
4244 // as an example).
4245 if (S.Context.getTypeSize(FieldType) != FirstSize ||
4246 S.Context.getTypeAlign(FieldType) > FirstAlign) {
4247 // Warn if we drop the attribute.
4248 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
4249 unsigned FieldBits = isSize ? S.Context.getTypeSize(FieldType)
4250 : S.Context.getTypeAlign(FieldType);
4251 S.Diag(Field->getLocation(),
4252 diag::warn_transparent_union_attribute_field_size_align)
4253 << isSize << *Field << FieldBits;
4254 unsigned FirstBits = isSize ? FirstSize : FirstAlign;
4255 S.Diag(FirstField->getLocation(),
4256 diag::note_transparent_union_first_field_size_align)
4257 << isSize << FirstBits;
4258 return;
4259 }
4260 }
4261
4262 RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL));
4263}
4264
4266 StringRef Str, MutableArrayRef<Expr *> Args) {
4267 auto *Attr = AnnotateAttr::Create(Context, Str, Args.data(), Args.size(), CI);
4269 CI, MutableArrayRef<Expr *>(Attr->args_begin(), Attr->args_end()))) {
4270 D->addAttr(Attr);
4271 }
4272}
4273
4274static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4275 // Make sure that there is a string literal as the annotation's first
4276 // argument.
4277 StringRef Str;
4278 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
4279 return;
4280
4282 Args.reserve(AL.getNumArgs() - 1);
4283 for (unsigned Idx = 1; Idx < AL.getNumArgs(); Idx++) {
4284 assert(!AL.isArgIdent(Idx));
4285 Args.push_back(AL.getArgAsExpr(Idx));
4286 }
4287
4288 S.AddAnnotationAttr(D, AL, Str, Args);
4289}
4290
4291static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4292 S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0));
4293}
4294
4296 AlignValueAttr TmpAttr(Context, CI, E);
4297 SourceLocation AttrLoc = CI.getLoc();
4298
4299 QualType T;
4300 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
4301 T = TD->getUnderlyingType();
4302 else if (const auto *VD = dyn_cast<ValueDecl>(D))
4303 T = VD->getType();
4304 else
4305 llvm_unreachable("Unknown decl type for align_value");
4306
4307 if (!T->isDependentType() && !T->isAnyPointerType() &&
4308 !T->isReferenceType() && !T->isMemberPointerType()) {
4309 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
4310 << &TmpAttr << T << D->getSourceRange();
4311 return;
4312 }
4313
4314 if (!E->isValueDependent()) {
4315 llvm::APSInt Alignment;
4317 E, &Alignment, diag::err_align_value_attribute_argument_not_int);
4318 if (ICE.isInvalid())
4319 return;
4320
4321 if (!Alignment.isPowerOf2()) {
4322 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
4323 << E->getSourceRange();
4324 return;
4325 }
4326
4327 D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
4328 return;
4329 }
4330
4331 // Save dependent expressions in the AST to be instantiated.
4332 D->addAttr(::new (Context) AlignValueAttr(Context, CI, E));
4333}
4334
4335static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4336 // check the attribute arguments.
4337 if (AL.getNumArgs() > 1) {
4338 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
4339 return;
4340 }
4341
4342 if (AL.getNumArgs() == 0) {
4343 D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
4344 return;
4345 }
4346
4347 Expr *E = AL.getArgAsExpr(0);
4349 S.Diag(AL.getEllipsisLoc(),
4350 diag::err_pack_expansion_without_parameter_packs);
4351 return;
4352 }
4353
4355 return;
4356
4357 S.AddAlignedAttr(D, AL, E, AL.isPackExpansion());
4358}
4359
4361 bool IsPackExpansion) {
4362 AlignedAttr TmpAttr(Context, CI, true, E);
4363 SourceLocation AttrLoc = CI.getLoc();
4364
4365 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
4366 if (TmpAttr.isAlignas()) {
4367 // C++11 [dcl.align]p1:
4368 // An alignment-specifier may be applied to a variable or to a class
4369 // data member, but it shall not be applied to a bit-field, a function
4370 // parameter, the formal parameter of a catch clause, or a variable
4371 // declared with the register storage class specifier. An
4372 // alignment-specifier may also be applied to the declaration of a class
4373 // or enumeration type.
4374 // CWG 2354:
4375 // CWG agreed to remove permission for alignas to be applied to
4376 // enumerations.
4377 // C11 6.7.5/2:
4378 // An alignment attribute shall not be specified in a declaration of
4379 // a typedef, or a bit-field, or a function, or a parameter, or an
4380 // object declared with the register storage-class specifier.
4381 int DiagKind = -1;
4382 if (isa<ParmVarDecl>(D)) {
4383 DiagKind = 0;
4384 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
4385 if (VD->getStorageClass() == SC_Register)
4386 DiagKind = 1;
4387 if (VD->isExceptionVariable())
4388 DiagKind = 2;
4389 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
4390 if (FD->isBitField())
4391 DiagKind = 3;
4392 } else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
4393 if (ED->getLangOpts().CPlusPlus)
4394 DiagKind = 4;
4395 } else if (!isa<TagDecl>(D)) {
4396 Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
4397 << (TmpAttr.isC11() ? ExpectedVariableOrField
4399 return;
4400 }
4401 if (DiagKind != -1) {
4402 Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
4403 << &TmpAttr << DiagKind;
4404 return;
4405 }
4406 }
4407
4408 if (E->isValueDependent()) {
4409 // We can't support a dependent alignment on a non-dependent type,
4410 // because we have no way to model that a type is "alignment-dependent"
4411 // but not dependent in any other way.
4412 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
4413 if (!TND->getUnderlyingType()->isDependentType()) {
4414 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
4415 << E->getSourceRange();
4416 return;
4417 }
4418 }
4419
4420 // Save dependent expressions in the AST to be instantiated.
4421 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
4422 AA->setPackExpansion(IsPackExpansion);
4423 D->addAttr(AA);
4424 return;
4425 }
4426
4427 // FIXME: Cache the number on the AL object?
4428 llvm::APSInt Alignment;
4430 E, &Alignment, diag::err_aligned_attribute_argument_not_int);
4431 if (ICE.isInvalid())
4432 return;
4433
4434 uint64_t AlignVal = Alignment.getZExtValue();
4435 // C++11 [dcl.align]p2:
4436 // -- if the constant expression evaluates to zero, the alignment
4437 // specifier shall have no effect
4438 // C11 6.7.5p6:
4439 // An alignment specification of zero has no effect.
4440 if (!(TmpAttr.isAlignas() && !Alignment)) {
4441 if (!llvm::isPowerOf2_64(AlignVal)) {
4442 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
4443 << E->getSourceRange();
4444 return;
4445 }
4446 }
4447
4449 if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
4450 MaximumAlignment = std::min(MaximumAlignment, uint64_t(8192));
4451 if (AlignVal > MaximumAlignment) {
4452 Diag(AttrLoc, diag::err_attribute_aligned_too_great)
4454 return;
4455 }
4456
4457 const auto *VD = dyn_cast<VarDecl>(D);
4458 if (VD) {
4459 unsigned MaxTLSAlign =
4461 .getQuantity();
4462 if (MaxTLSAlign && AlignVal > MaxTLSAlign &&
4463 VD->getTLSKind() != VarDecl::TLS_None) {
4464 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
4465 << (unsigned)AlignVal << VD << MaxTLSAlign;
4466 return;
4467 }
4468 }
4469
4470 // On AIX, an aligned attribute can not decrease the alignment when applied
4471 // to a variable declaration with vector type.
4472 if (VD && Context.getTargetInfo().getTriple().isOSAIX()) {
4473 const Type *Ty = VD->getType().getTypePtr();
4474 if (Ty->isVectorType() && AlignVal < 16) {
4475 Diag(VD->getLocation(), diag::warn_aligned_attr_underaligned)
4476 << VD->getType() << 16;
4477 return;
4478 }
4479 }
4480
4481 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
4482 AA->setPackExpansion(IsPackExpansion);
4483 D->addAttr(AA);
4484}
4485
4487 TypeSourceInfo *TS, bool IsPackExpansion) {
4488 // FIXME: Cache the number on the AL object if non-dependent?
4489 // FIXME: Perform checking of type validity
4490 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
4491 AA->setPackExpansion(IsPackExpansion);
4492 D->addAttr(AA);
4493}
4494
4496 assert(D->hasAttrs() && "no attributes on decl");
4497
4498 QualType UnderlyingTy, DiagTy;
4499 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
4500 UnderlyingTy = DiagTy = VD->getType();
4501 } else {
4502 UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
4503 if (const auto *ED = dyn_cast<EnumDecl>(D))
4504 UnderlyingTy = ED->getIntegerType();
4505 }
4506 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
4507 return;
4508
4509 // C++11 [dcl.align]p5, C11 6.7.5/4:
4510 // The combined effect of all alignment attributes in a declaration shall
4511 // not specify an alignment that is less strict than the alignment that
4512 // would otherwise be required for the entity being declared.
4513 AlignedAttr *AlignasAttr = nullptr;
4514 AlignedAttr *LastAlignedAttr = nullptr;
4515 unsigned Align = 0;
4516 for (auto *I : D->specific_attrs<AlignedAttr>()) {
4517 if (I->isAlignmentDependent())
4518 return;
4519 if (I->isAlignas())
4520 AlignasAttr = I;
4521 Align = std::max(Align, I->getAlignment(Context));
4522 LastAlignedAttr = I;
4523 }
4524
4525 if (Align && DiagTy->isSizelessType()) {
4526 Diag(LastAlignedAttr->getLocation(), diag::err_attribute_sizeless_type)
4527 << LastAlignedAttr << DiagTy;
4528 } else if (AlignasAttr && Align) {
4529 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
4530 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
4531 if (NaturalAlign > RequestedAlign)
4532 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
4533 << DiagTy << (unsigned)NaturalAlign.getQuantity();
4534 }
4535}
4536
4538 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
4539 MSInheritanceModel ExplicitModel) {
4540 assert(RD->hasDefinition() && "RD has no definition!");
4541
4542 // We may not have seen base specifiers or any virtual methods yet. We will
4543 // have to wait until the record is defined to catch any mismatches.
4544 if (!RD->getDefinition()->isCompleteDefinition())
4545 return false;
4546
4547 // The unspecified model never matches what a definition could need.
4548 if (ExplicitModel == MSInheritanceModel::Unspecified)
4549 return false;
4550
4551 if (BestCase) {
4552 if (RD->calculateInheritanceModel() == ExplicitModel)
4553 return false;
4554 } else {
4555 if (RD->calculateInheritanceModel() <= ExplicitModel)
4556 return false;
4557 }
4558
4559 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
4560 << 0 /*definition*/;
4561 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) << RD;
4562 return true;
4563}
4564
4565/// parseModeAttrArg - Parses attribute mode string and returns parsed type
4566/// attribute.
4567static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
4568 bool &IntegerMode, bool &ComplexMode,
4569 FloatModeKind &ExplicitType) {
4570 IntegerMode = true;
4571 ComplexMode = false;
4572 ExplicitType = FloatModeKind::NoFloat;
4573 switch (Str.size()) {
4574 case 2:
4575 switch (Str[0]) {
4576 case 'Q':
4577 DestWidth = 8;
4578 break;
4579 case 'H':
4580 DestWidth = 16;
4581 break;
4582 case 'S':
4583 DestWidth = 32;
4584 break;
4585 case 'D':
4586 DestWidth = 64;
4587 break;
4588 case 'X':
4589 DestWidth = 96;
4590 break;
4591 case 'K': // KFmode - IEEE quad precision (__float128)
4592 ExplicitType = FloatModeKind::Float128;
4593 DestWidth = Str[1] == 'I' ? 0 : 128;
4594 break;
4595 case 'T':
4596 ExplicitType = FloatModeKind::LongDouble;
4597 DestWidth = 128;
4598 break;
4599 case 'I':
4600 ExplicitType = FloatModeKind::Ibm128;
4601 DestWidth = Str[1] == 'I' ? 0 : 128;
4602 break;
4603 }
4604 if (Str[1] == 'F') {
4605 IntegerMode = false;
4606 } else if (Str[1] == 'C') {
4607 IntegerMode = false;
4608 ComplexMode = true;
4609 } else if (Str[1] != 'I') {
4610 DestWidth = 0;
4611 }
4612 break;
4613 case 4:
4614 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
4615 // pointer on PIC16 and other embedded platforms.
4616 if (Str == "word")
4617 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
4618 else if (Str == "byte")
4619 DestWidth = S.Context.getTargetInfo().getCharWidth();
4620 break;
4621 case 7:
4622 if (Str == "pointer")
4624 break;
4625 case 11:
4626 if (Str == "unwind_word")
4627 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
4628 break;
4629 }
4630}
4631
4632/// handleModeAttr - This attribute modifies the width of a decl with primitive
4633/// type.
4634///
4635/// Despite what would be logical, the mode attribute is a decl attribute, not a
4636/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
4637/// HImode, not an intermediate pointer.
4638static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4639 // This attribute isn't documented, but glibc uses it. It changes
4640 // the width of an int or unsigned int to the specified size.
4641 if (!AL.isArgIdent(0)) {
4642 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
4643 << AL << AANT_ArgumentIdentifier;
4644 return;
4645 }
4646
4647 IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident;
4648
4649 S.AddModeAttr(D, AL, Name);
4650}
4651
4653 IdentifierInfo *Name, bool InInstantiation) {
4654 StringRef Str = Name->getName();
4655 normalizeName(Str);
4656 SourceLocation AttrLoc = CI.getLoc();
4657
4658 unsigned DestWidth = 0;
4659 bool IntegerMode = true;
4660 bool ComplexMode = false;
4662 llvm::APInt VectorSize(64, 0);
4663 if (Str.size() >= 4 && Str[0] == 'V') {
4664 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
4665 size_t StrSize = Str.size();
4666 size_t VectorStringLength = 0;
4667 while ((VectorStringLength + 1) < StrSize &&
4668 isdigit(Str[VectorStringLength + 1]))
4669 ++VectorStringLength;
4670 if (VectorStringLength &&
4671 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
4672 VectorSize.isPowerOf2()) {
4673 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
4674 IntegerMode, ComplexMode, ExplicitType);
4675 // Avoid duplicate warning from template instantiation.
4676 if (!InInstantiation)
4677 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
4678 } else {
4679 VectorSize = 0;
4680 }
4681 }
4682
4683 if (!VectorSize)
4684 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode,
4685 ExplicitType);
4686
4687 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
4688 // and friends, at least with glibc.
4689 // FIXME: Make sure floating-point mappings are accurate
4690 // FIXME: Support XF and TF types
4691 if (!DestWidth) {
4692 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
4693 return;
4694 }
4695
4696 QualType OldTy;
4697 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
4698 OldTy = TD->getUnderlyingType();
4699 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
4700 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
4701 // Try to get type from enum declaration, default to int.
4702 OldTy = ED->getIntegerType();
4703 if (OldTy.isNull())
4704 OldTy = Context.IntTy;
4705 } else
4706 OldTy = cast<ValueDecl>(D)->getType();
4707
4708 if (OldTy->isDependentType()) {
4709 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
4710 return;
4711 }
4712
4713 // Base type can also be a vector type (see PR17453).
4714 // Distinguish between base type and base element type.
4715 QualType OldElemTy = OldTy;
4716 if (const auto *VT = OldTy->getAs<VectorType>())
4717 OldElemTy = VT->getElementType();
4718
4719 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
4720 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
4721 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
4722 if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) &&
4723 VectorSize.getBoolValue()) {
4724 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.getRange();
4725 return;
4726 }
4727 bool IntegralOrAnyEnumType = (OldElemTy->isIntegralOrEnumerationType() &&
4728 !OldElemTy->isBitIntType()) ||
4729 OldElemTy->getAs<EnumType>();
4730
4731 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
4732 !IntegralOrAnyEnumType)
4733 Diag(AttrLoc, diag::err_mode_not_primitive);
4734 else if (IntegerMode) {
4735 if (!IntegralOrAnyEnumType)
4736 Diag(AttrLoc, diag::err_mode_wrong_type);
4737 } else if (ComplexMode) {
4738 if (!OldElemTy->isComplexType())
4739 Diag(AttrLoc, diag::err_mode_wrong_type);
4740 } else {
4741 if (!OldElemTy->isFloatingType())
4742 Diag(AttrLoc, diag::err_mode_wrong_type);
4743 }
4744
4745 QualType NewElemTy;
4746
4747 if (IntegerMode)
4748 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
4749 OldElemTy->isSignedIntegerType());
4750 else
4751 NewElemTy = Context.getRealTypeForBitwidth(DestWidth, ExplicitType);
4752
4753 if (NewElemTy.isNull()) {
4754 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
4755 return;
4756 }
4757
4758 if (ComplexMode) {
4759 NewElemTy = Context.getComplexType(NewElemTy);
4760 }
4761
4762 QualType NewTy = NewElemTy;
4763 if (VectorSize.getBoolValue()) {
4764 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
4766 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
4767 // Complex machine mode does not support base vector types.
4768 if (ComplexMode) {
4769 Diag(AttrLoc, diag::err_complex_mode_vector_type);
4770 return;
4771 }
4772 unsigned NumElements = Context.getTypeSize(OldElemTy) *
4773 OldVT->getNumElements() /
4774 Context.getTypeSize(NewElemTy);
4775 NewTy =
4776 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
4777 }
4778
4779 if (NewTy.isNull()) {
4780 Diag(AttrLoc, diag::err_mode_wrong_type);
4781 return;
4782 }
4783
4784 // Install the new type.
4785 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
4786 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
4787 else if (auto *ED = dyn_cast<EnumDecl>(D))
4788 ED->setIntegerType(NewTy);
4789 else
4790 cast<ValueDecl>(D)->setType(NewTy);
4791
4792 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
4793}
4794
4795static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4796 D->addAttr(::new (S.Context) NoDebugAttr(S.Context, AL));
4797}
4798
4800 const AttributeCommonInfo &CI,
4801 const IdentifierInfo *Ident) {
4802 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4803 Diag(CI.getLoc(), diag::warn_attribute_ignored) << Ident;
4804 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4805 return nullptr;
4806 }
4807
4808 if (D->hasAttr<AlwaysInlineAttr>())
4809 return nullptr;
4810
4811 return ::new (Context) AlwaysInlineAttr(Context, CI);
4812}
4813
4814InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
4815 const ParsedAttr &AL) {
4816 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4817 // Attribute applies to Var but not any subclass of it (like ParmVar,
4818 // ImplicitParm or VarTemplateSpecialization).
4819 if (VD->getKind() != Decl::Var) {
4820 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4821 << AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4823 return nullptr;
4824 }
4825 // Attribute does not apply to non-static local variables.
4826 if (VD->hasLocalStorage()) {
4827 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4828 return nullptr;
4829 }
4830 }
4831
4832 return ::new (Context) InternalLinkageAttr(Context, AL);
4833}
4834InternalLinkageAttr *
4835Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
4836 if (const auto *VD = dyn_cast<VarDecl>(D)) {
4837 // Attribute applies to Var but not any subclass of it (like ParmVar,
4838 // ImplicitParm or VarTemplateSpecialization).
4839 if (VD->getKind() != Decl::Var) {
4840 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4841 << &AL << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass
4843 return nullptr;
4844 }
4845 // Attribute does not apply to non-static local variables.
4846 if (VD->hasLocalStorage()) {
4847 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4848 return nullptr;
4849 }
4850 }
4851
4852 return ::new (Context) InternalLinkageAttr(Context, AL);
4853}
4854
4856 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
4857 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'minsize'";
4858 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4859 return nullptr;
4860 }
4861
4862 if (D->hasAttr<MinSizeAttr>())
4863 return nullptr;
4864
4865 return ::new (Context) MinSizeAttr(Context, CI);
4866}
4867
4868SwiftNameAttr *Sema::mergeSwiftNameAttr(Decl *D, const SwiftNameAttr &SNA,
4869 StringRef Name) {
4870 if (const auto *PrevSNA = D->getAttr<SwiftNameAttr>()) {
4871 if (PrevSNA->getName() != Name && !PrevSNA->isImplicit()) {
4872 Diag(PrevSNA->getLocation(), diag::err_attributes_are_not_compatible)
4873 << PrevSNA << &SNA;
4874 Diag(SNA.getLoc(), diag::note_conflicting_attribute);
4875 }
4876
4877 D->dropAttr<SwiftNameAttr>();
4878 }
4879 return ::new (Context) SwiftNameAttr(Context, SNA, Name);
4880}
4881
4883 const AttributeCommonInfo &CI) {
4884 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
4885 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
4886 Diag(CI.getLoc(), diag::note_conflicting_attribute);
4887 D->dropAttr<AlwaysInlineAttr>();
4888 }
4889 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
4890 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
4891 Diag(CI.getLoc(), diag::note_conflicting_attribute);
4892 D->dropAttr<MinSizeAttr>();
4893 }
4894
4895 if (D->hasAttr<OptimizeNoneAttr>())
4896 return nullptr;
4897
4898 return ::new (Context) OptimizeNoneAttr(Context, CI);
4899}
4900
4901static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4902 if (AlwaysInlineAttr *Inline =
4903 S.mergeAlwaysInlineAttr(D, AL, AL.getAttrName()))
4904 D->addAttr(Inline);
4905}
4906
4907static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4908 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(D, AL))
4909 D->addAttr(MinSize);
4910}
4911
4912static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4913 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(D, AL))
4914 D->addAttr(Optnone);
4915}
4916
4917static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4918 const auto *VD = cast<VarDecl>(D);
4919 if (VD->hasLocalStorage()) {
4920 S.Diag(AL.getLoc(), diag::err_cuda_nonstatic_constdev);
4921 return;
4922 }
4923 // constexpr variable may already get an implicit constant attr, which should
4924 // be replaced by the explicit constant attr.
4925 if (auto *A = D->getAttr<CUDAConstantAttr>()) {
4926 if (!A->isImplicit())
4927 return;
4928 D->dropAttr<CUDAConstantAttr>();
4929 }
4930 D->addAttr(::new (S.Context) CUDAConstantAttr(S.Context, AL));
4931}
4932
4933static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4934 const auto *VD = cast<VarDecl>(D);
4935 // extern __shared__ is only allowed on arrays with no length (e.g.
4936 // "int x[]").
4937 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
4938 !isa<IncompleteArrayType>(VD->getType())) {
4939 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
4940 return;
4941 }
4942 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
4943 S.CUDADiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
4944 << S.CurrentCUDATarget())
4945 return;
4946 D->addAttr(::new (S.Context) CUDASharedAttr(S.Context, AL));
4947}
4948
4949static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4950 const auto *FD = cast<FunctionDecl>(D);
4951 if (!FD->getReturnType()->isVoidType() &&
4952 !FD->getReturnType()->getAs<AutoType>() &&
4954 SourceRange RTRange = FD->getReturnTypeSourceRange();
4955 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
4956 << FD->getType()
4957 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
4958 : FixItHint());
4959 return;
4960 }
4961 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
4962 if (Method->isInstance()) {
4963 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
4964 << Method;
4965 return;
4966 }
4967 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
4968 }
4969 // Only warn for "inline" when compiling for host, to cut down on noise.
4970 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
4971 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
4972
4973 D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL));
4974 // In host compilation the kernel is emitted as a stub function, which is
4975 // a helper function for launching the kernel. The instructions in the helper