43#include "llvm/ADT/STLExtras.h"
44#include "llvm/ADT/StringExtras.h"
45#include "llvm/IR/Assumptions.h"
46#include "llvm/MC/MCSectionMachO.h"
47#include "llvm/Support/Error.h"
48#include "llvm/Support/MathExtras.h"
49#include "llvm/Support/raw_ostream.h"
84 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
85 isa<ObjCPropertyDecl>(D);
93 return isa<FunctionProtoType>(FnTy);
94 return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D);
102 return cast<FunctionProtoType>(FnTy)->getNumParams();
103 if (
const auto *BD = dyn_cast<BlockDecl>(D))
104 return BD->getNumParams();
105 return cast<ObjCMethodDecl>(D)->param_size();
110 if (
const auto *FD = dyn_cast<FunctionDecl>(D))
111 return FD->getParamDecl(Idx);
112 if (
const auto *MD = dyn_cast<ObjCMethodDecl>(D))
113 return MD->getParamDecl(Idx);
114 if (
const auto *BD = dyn_cast<BlockDecl>(D))
115 return BD->getParamDecl(Idx);
121 return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
122 if (
const auto *BD = dyn_cast<BlockDecl>(D))
123 return BD->getParamDecl(Idx)->getType();
125 return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
130 return PVD->getSourceRange();
136 return FnTy->getReturnType();
137 return cast<ObjCMethodDecl>(D)->getReturnType();
141 if (
const auto *FD = dyn_cast<FunctionDecl>(D))
142 return FD->getReturnTypeSourceRange();
143 if (
const auto *MD = dyn_cast<ObjCMethodDecl>(D))
144 return MD->getReturnTypeSourceRange();
150 return cast<FunctionProtoType>(FnTy)->isVariadic();
151 if (
const auto *BD = dyn_cast<BlockDecl>(D))
152 return BD->isVariadic();
153 return cast<ObjCMethodDecl>(D)->isVariadic();
157 if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
158 return MethodDecl->isInstance();
163 bool AllowNSAttributedString =
false) {
174 if (AllowNSAttributedString &&
175 ClsName == &Ctx.
Idents.
get(
"NSAttributedString"))
178 return ClsName == &Ctx.
Idents.
get(
"NSString") ||
179 ClsName == &Ctx.
Idents.
get(
"NSMutableString");
205template <
typename AttrInfo>
206static std::enable_if_t<std::is_base_of_v<Attr, AttrInfo>,
SourceLocation>
208 return AL.getLocation();
217template <
typename AttrInfo>
219 uint32_t &Val,
unsigned Idx =
UINT_MAX,
220 bool StrictlyUnsigned =
false) {
221 std::optional<llvm::APSInt> I = llvm::APSInt(32);
234 if (!I->isIntN(32)) {
236 <<
toString(*I, 10,
false) << 32 << 1;
240 if (StrictlyUnsigned && I->isSigned() && I->isNegative()) {
241 S.
Diag(
getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
246 Val = (uint32_t)I->getZExtValue();
253template <
typename AttrInfo>
255 int &Val,
unsigned Idx =
UINT_MAX) {
260 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
264 <<
toString(I, 10,
false) << 32 << 0;
274template <
typename AttrTy>
276 if (
const auto *A = D->
getAttr<AttrTy>()) {
277 S.
Diag(AL.
getLoc(), diag::err_attributes_are_not_compatible)
280 S.
Diag(A->getLocation(), diag::note_conflicting_attribute);
286template <
typename AttrTy>
288 if (
const auto *A = D->
getAttr<AttrTy>()) {
292 S.
Diag(A->getLocation(), diag::note_conflicting_attribute);
302template <
typename AttrInfo>
304 Sema &S,
const Decl *D,
const AttrInfo &AI,
unsigned AttrArgNum,
305 const Expr *IdxExpr,
ParamIdx &Idx,
bool CanIndexImplicitThis =
false) {
316 std::optional<llvm::APSInt> IdxInt;
325 unsigned IdxSource = IdxInt->getLimitedValue(
UINT_MAX);
326 if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
327 S.
Diag(
getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
331 if (HasImplicitThisParam && !CanIndexImplicitThis) {
332 if (IdxSource == 1) {
333 S.
Diag(
getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
347 const Expr *E, StringRef &Str,
353 if (!Literal || (!Literal->isUnevaluated() && !Literal->isOrdinary())) {
359 Str = Literal->getString();
373 Diag(Loc->
Loc, diag::err_attribute_argument_type)
379 *ArgLocation = Loc->
Loc;
389 if (!Literal || (!Literal->isUnevaluated() && !Literal->isOrdinary())) {
394 Str = Literal->getString();
400template <
typename AttrType>
406template <
typename... DiagnosticArgs>
412template <
typename T,
typename... DiagnosticArgs>
415 DiagnosticArgs &&... ExtraArgs) {
417 std::forward<DiagnosticArgs>(ExtraArgs)...);
424template <
typename AttrType,
typename... DiagnosticArgs>
427 bool PassesCheck,
unsigned DiagID,
428 DiagnosticArgs &&... ExtraArgs) {
434 handleSimpleAttribute<AttrType>(S, D, CI);
447 auto IsOverloadedOperatorPresent = [&S](
const RecordDecl *Record,
455 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
456 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
457 if (foundStarOperator && foundArrowOperator)
460 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
464 for (
const auto &BaseSpecifier : CXXRecord->bases()) {
465 if (!foundStarOperator)
466 foundStarOperator = IsOverloadedOperatorPresent(
467 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
468 if (!foundArrowOperator)
469 foundArrowOperator = IsOverloadedOperatorPresent(
470 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
473 if (foundStarOperator && foundArrowOperator)
484 const auto *VD = cast<ValueDecl>(D);
493 if (RT->isIncompleteType())
500 S.
Diag(AL.
getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
517template <
typename AttrType>
524 if (
const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
526 return !Base->hasAttr<AttrType>();
548 return checkRecordDeclForAttr<CapabilityAttr>(RT->
getDecl());
560 return TN->
hasAttr<CapabilityAttr>();
579 if (
const auto *E = dyn_cast<CastExpr>(Ex))
581 else if (
const auto *E = dyn_cast<ParenExpr>(Ex))
583 else if (
const auto *E = dyn_cast<UnaryOperator>(Ex)) {
584 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
585 E->getOpcode() == UO_Deref)
588 }
else if (
const auto *E = dyn_cast<BinaryOperator>(Ex)) {
589 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
607 bool ParamIdxOk =
false) {
612 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
613 if (MD && !MD->isStatic()) {
616 if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
617 !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
619 diag::warn_thread_attribute_not_on_capability_member)
620 << AL << MD->getParent();
622 S.
Diag(AL.
getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
627 for (
unsigned Idx = Sidx; Idx < AL.
getNumArgs(); ++Idx) {
632 Args.push_back(ArgExp);
636 if (
const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
637 if (StrLit->getLength() == 0 ||
638 (StrLit->isOrdinary() && StrLit->getString() == StringRef(
"*"))) {
641 Args.push_back(ArgExp);
647 S.
Diag(AL.
getLoc(), diag::warn_thread_attribute_ignored) << AL;
648 Args.push_back(ArgExp);
656 if (
const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
657 if (UOp->getOpcode() == UO_AddrOf)
658 if (
const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
659 if (DRE->getDecl()->isCXXInstanceMember())
660 ArgTy = DRE->getDecl()->getType();
666 if(!RT && ParamIdxOk) {
667 const auto *FD = dyn_cast<FunctionDecl>(D);
668 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
670 unsigned int NumParams = FD->getNumParams();
671 llvm::APInt ArgValue = IL->getValue();
672 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
673 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
674 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
676 diag::err_attribute_argument_out_of_bounds_extra_info)
677 << AL << Idx + 1 << NumParams;
680 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
689 S.
Diag(AL.
getLoc(), diag::warn_thread_attribute_argument_not_lockable)
692 Args.push_back(ArgExp);
712 unsigned Size = Args.size();
746 QualType QT = cast<ValueDecl>(D)->getType();
748 S.
Diag(AL.
getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
765 Expr **StartArg = &Args[0];
767 AcquiredAfterAttr(S.
Context, AL, StartArg, Args.size()));
775 Expr **StartArg = &Args[0];
777 AcquiredBeforeAttr(S.
Context, AL, StartArg, Args.size()));
794 unsigned Size = Args.size();
795 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
797 AssertSharedLockAttr(S.
Context, AL, StartArg, Size));
806 unsigned Size = Args.size();
807 Expr **StartArg = Size == 0 ? nullptr : &Args[0];
809 AssertExclusiveLockAttr(S.
Context, AL, StartArg, Size));
817template <
typename AttrInfo>
819 unsigned AttrArgNo) {
820 assert(AI.isArgExpr(AttrArgNo) &&
"Expected expression argument");
821 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
830 S.
Diag(SrcLoc, diag::err_attribute_integers_only)
845 S.
Diag(AL.
getLoc(), diag::warn_attribute_return_pointers_only) << AL;
856 ParamIdx SizeArgNo(SizeArgNoVal, D);
871 AllocSizeAttr(S.
Context, AL, SizeArgNo, NumberArgNo));
880 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
915 unsigned Size = Args.size();
929 unsigned Size = Args.size();
932 Expr **StartArg = &Args[0];
935 LocksExcludedAttr(S.
Context, AL, StartArg, Size));
939 Expr *&Cond, StringRef &Msg) {
945 Cond = Converted.
get();
952 Msg =
"<no message provided>";
958 S.
Diag(AL.
getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
960 S.
Diag(PDiag.first, PDiag.second);
967 S.
Diag(AL.
getLoc(), diag::ext_clang_enable_if);
976 StringRef NewUserDiagnostic;
986class ArgumentDependenceChecker
997 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1000 ClassType =
nullptr;
1005 bool referencesArgs(
Expr *E) {
1013 "`this` doesn't refer to the enclosing class?");
1019 if (
const auto *PVD = dyn_cast<ParmVarDecl>(DRE->
getDecl()))
1020 if (Parms.count(PVD)) {
1031 const auto *DeclFD = cast<FunctionDecl>(D);
1033 if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(DeclFD))
1034 if (!MethodDecl->isStatic()) {
1035 S.
Diag(AL.
getLoc(), diag::err_attribute_no_member_function) << AL;
1047 S.
Diag(Loc, diag::err_attribute_argument_n_type) << AL << Index << T;
1053 auto *F = dyn_cast_if_present<DeclRefExpr>(AL.
getArgAsExpr(0));
1056 return dyn_cast_if_present<FunctionDecl>(F->getFoundDecl());
1065 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_number_arguments_for)
1072 for (
unsigned I = 1; I < AL.
getNumArgs(); ++I) {
1084 if (Index > DeclFD->getNumParams()) {
1085 S.
Diag(AL.
getLoc(), diag::err_attribute_bounds_for_function)
1086 << AL << Index << DeclFD << DeclFD->getNumParams();
1091 QualType T2 = DeclFD->getParamDecl(Index - 1)->getType();
1096 << AL << Index << DeclFD << T2 << I << AttrFD << T1;
1100 Indices.push_back(Index - 1);
1104 S.
Context, AL, AttrFD, Indices.data(), Indices.size()));
1108 S.
Diag(AL.
getLoc(), diag::ext_clang_diagnose_if);
1115 StringRef DiagTypeStr;
1119 DiagnoseIfAttr::DiagnosticType DiagType;
1120 if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
1122 diag::err_diagnose_if_invalid_diagnostic_type);
1126 bool ArgDependent =
false;
1127 if (
const auto *FD = dyn_cast<FunctionDecl>(D))
1128 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
1130 S.
Context, AL, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D)));
1134 static constexpr const StringRef kWildcard =
"*";
1137 bool HasWildcard =
false;
1139 const auto AddBuiltinName = [&Names, &HasWildcard](StringRef Name) {
1140 if (Name == kWildcard)
1142 Names.push_back(Name);
1146 if (
const auto *NBA = D->
getAttr<NoBuiltinAttr>())
1147 for (StringRef BuiltinName : NBA->builtinNames())
1148 AddBuiltinName(BuiltinName);
1152 AddBuiltinName(kWildcard);
1154 for (
unsigned I = 0, E = AL.
getNumArgs(); I != E; ++I) {
1155 StringRef BuiltinName;
1161 AddBuiltinName(BuiltinName);
1163 S.
Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name)
1164 << BuiltinName << AL;
1169 Names.erase(std::unique(Names.begin(), Names.end()), Names.end());
1172 if (HasWildcard && Names.size() > 1)
1174 diag::err_attribute_no_builtin_wildcard_or_builtin_name)
1177 if (D->
hasAttr<NoBuiltinAttr>())
1180 NoBuiltinAttr(S.
Context, AL, Names.data(), Names.size()));
1184 if (D->
hasAttr<PassObjectSizeAttr>()) {
1185 S.
Diag(D->
getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
1207 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
1208 S.
Diag(D->
getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
1216 ConsumableAttr::ConsumedState DefaultState;
1220 if (!ConsumableAttr::ConvertStrToConsumedState(IL->
Ident->
getName(),
1222 S.
Diag(IL->
Loc, diag::warn_attribute_type_not_supported) << AL
1227 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_type)
1240 if (!RD->hasAttr<ConsumableAttr>()) {
1241 S.
Diag(AL.
getLoc(), diag::warn_attr_on_unconsumable_class) << RD;
1258 for (
unsigned ArgIndex = 0; ArgIndex < AL.
getNumArgs(); ++ArgIndex) {
1259 CallableWhenAttr::ConsumedState CallableState;
1261 StringRef StateString;
1272 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
1274 S.
Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
1278 States.push_back(CallableState);
1282 CallableWhenAttr(S.
Context, AL, States.data(), States.size()));
1286 ParamTypestateAttr::ConsumedState ParamState;
1292 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1294 S.
Diag(Ident->
Loc, diag::warn_attribute_type_not_supported)
1295 << AL << StateString;
1299 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_type)
1320 ReturnTypestateAttr::ConsumedState ReturnState;
1324 if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->
Ident->
getName(),
1326 S.
Diag(IL->
Loc, diag::warn_attribute_type_not_supported) << AL
1331 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_type)
1368 SetTypestateAttr::ConsumedState NewState;
1372 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1373 S.
Diag(Ident->
Loc, diag::warn_attribute_type_not_supported) << AL
1378 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_type)
1390 TestTypestateAttr::ConsumedState TestState;
1394 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
1395 S.
Diag(Ident->
Loc, diag::warn_attribute_type_not_supported) << AL
1400 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_type)
1414 if (
auto *TD = dyn_cast<TagDecl>(D))
1416 else if (
auto *FD = dyn_cast<FieldDecl>(D)) {
1423 if (BitfieldByteAligned)
1425 S.
Diag(AL.
getLoc(), diag::warn_attribute_ignored_for_field_of_type)
1431 if (BitfieldByteAligned)
1432 S.
Diag(AL.
getLoc(), diag::warn_attribute_packed_for_bitfield);
1438 S.
Diag(AL.
getLoc(), diag::warn_attribute_ignored) << AL;
1442 auto *RD = cast<CXXRecordDecl>(D);
1444 assert(CTD &&
"attribute does not appertain to this declaration");
1455 if (
const auto *CTSD = dyn_cast_if_present<ClassTemplateSpecializationDecl>(
1457 Template = CTSD->getSpecializedTemplate();
1459 while (TST && TST->isTypeAlias())
1462 Template = TST->getTemplateName().getAsTemplateDecl();
1471 S.
Diag(AL.
getLoc(), diag::err_attribute_preferred_name_arg_invalid)
1474 S.
Diag(TT->getDecl()->getLocation(), diag::note_entity_declared_at)
1482 if (
const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
1484 S.
Diag(AL.
getLoc(), diag::warn_iboutlet_object_type)
1485 << AL << VD->getType() << 0;
1489 else if (
const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1491 S.
Diag(AL.
getLoc(), diag::warn_iboutlet_object_type)
1492 << AL << PD->getType() << 1;
1497 S.
Diag(AL.
getLoc(), diag::warn_attribute_iboutlet) << AL;
1515 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1530 S.
Diag(AL.
getLoc(), diag::err_iboutletcollection_type) <<
"NSObject";
1546 QT->
isBuiltinType() ? diag::err_iboutletcollection_builtintype
1547 : diag::err_iboutletcollection_type) << QT;
1565 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1567 for (
const auto *I : UD->
fields()) {
1581 bool isReturnValue =
false) {
1584 S.
Diag(AL.
getLoc(), diag::warn_attribute_return_pointers_only)
1585 << AL << AttrParmRange << TypeRange;
1587 S.
Diag(AL.
getLoc(), diag::warn_attribute_pointers_only)
1588 << AL << AttrParmRange << TypeRange << 0;
1596 for (
unsigned I = 0; I < AL.
getNumArgs(); ++I) {
1610 NonNullArgs.push_back(Idx);
1621 I != E && !AnyPointers; ++I) {
1628 S.
Diag(AL.
getLoc(), diag::warn_attribute_nonnull_no_pointers);
1631 ParamIdx *Start = NonNullArgs.data();
1632 unsigned Size = NonNullArgs.size();
1633 llvm::array_pod_sort(Start, Start + Size);
1643 S.
Diag(AL.
getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1672 QualType T = cast<ParmVarDecl>(D)->getType();
1674 S.
Diag(AL.
getLoc(), diag::warn_attribute_pointers_only)
1697 AssumeAlignedAttr TmpAttr(
Context, CI, E, OE);
1701 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1702 << &TmpAttr << TmpAttr.getRange() << SR;
1707 std::optional<llvm::APSInt> I = llvm::APSInt(64);
1710 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1714 Diag(AttrLoc, diag::err_attribute_argument_type)
1720 if (!I->isPowerOf2()) {
1721 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1727 Diag(CI.
getLoc(), diag::warn_assume_aligned_too_great)
1732 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1750 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1756 const auto *FuncDecl = cast<FunctionDecl>(D);
1766 << FuncDecl->getParamDecl(Idx.
getASTIndex())->getSourceRange();
1775 StringRef AssumptionStr) {
1776 if (llvm::KnownAssumptionStrings.count(AssumptionStr))
1779 unsigned BestEditDistance = 3;
1780 StringRef Suggestion;
1781 for (
const auto &KnownAssumptionIt : llvm::KnownAssumptionStrings) {
1782 unsigned EditDistance =
1783 AssumptionStr.edit_distance(KnownAssumptionIt.getKey());
1784 if (EditDistance < BestEditDistance) {
1785 Suggestion = KnownAssumptionIt.getKey();
1786 BestEditDistance = EditDistance;
1790 if (!Suggestion.empty())
1791 S.
Diag(Loc, diag::warn_assume_attribute_string_unknown_suggested)
1792 << AssumptionStr << Suggestion;
1794 S.
Diag(Loc, diag::warn_assume_attribute_string_unknown) << AssumptionStr;
1812 if (AttrName.size() > 4 && AttrName.startswith(
"__") &&
1813 AttrName.endswith(
"__")) {
1814 AttrName = AttrName.drop_front(2).drop_back(2);
1830 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
1836 OwnershipAttr::OwnershipKind K =
1837 OwnershipAttr(S.
Context, AL,
nullptr,
nullptr, 0).getOwnKind();
1841 case OwnershipAttr::Takes:
1842 case OwnershipAttr::Holds:
1844 S.
Diag(AL.
getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
1848 case OwnershipAttr::Returns:
1850 S.
Diag(AL.
getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
1858 StringRef ModuleName =
Module->getName();
1864 for (
unsigned i = 1; i < AL.
getNumArgs(); ++i) {
1874 case OwnershipAttr::Takes:
1875 case OwnershipAttr::Holds:
1879 case OwnershipAttr::Returns:
1885 S.
Diag(AL.
getLoc(), diag::err_ownership_type) << AL << Err
1894 if (I->getOwnKind() != K && llvm::is_contained(I->args(), Idx)) {
1895 S.
Diag(AL.
getLoc(), diag::err_attributes_are_not_compatible)
1898 I->isRegularKeywordAttribute());
1900 }
else if (K == OwnershipAttr::Returns &&
1901 I->getOwnKind() == OwnershipAttr::Returns) {
1904 if (!llvm::is_contained(I->args(), Idx)) {
1905 S.
Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1906 << I->args_begin()->getSourceIndex();
1908 S.
Diag(AL.
getLoc(), diag::note_ownership_returns_index_mismatch)
1914 OwnershipArgs.push_back(Idx);
1917 ParamIdx *Start = OwnershipArgs.data();
1918 unsigned Size = OwnershipArgs.size();
1919 llvm::array_pod_sort(Start, Start + Size);
1927 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1943 S.
Diag(AL.
getLoc(), diag::err_attribute_weakref_not_global_context)
1944 << cast<NamedDecl>(D);
1988 const auto *FD = cast<FunctionDecl>(D);
1990 S.
Diag(AL.
getLoc(), diag::err_alias_is_definition) << FD << 1;
2003 S.
Diag(AL.
getLoc(), diag::err_alias_not_supported_on_darwin);
2011 S.
Diag(AL.
getLoc(), diag::err_alias_not_supported_on_nvptx);
2015 if (
const auto *FD = dyn_cast<FunctionDecl>(D)) {
2017 S.
Diag(AL.
getLoc(), diag::err_alias_is_definition) << FD << 0;
2021 const auto *VD = cast<VarDecl>(D);
2022 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
2023 S.
Diag(AL.
getLoc(), diag::err_alias_is_definition) << VD << 0;
2050 if (Model !=
"global-dynamic" && Model !=
"local-dynamic"
2051 && Model !=
"initial-exec" && Model !=
"local-exec") {
2052 S.
Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
2057 Model ==
"local-dynamic") {
2058 S.
Diag(LiteralLoc, diag::err_aix_attr_unsupported_tls_model) << Model;
2072 S.
Diag(AL.
getLoc(), diag::warn_attribute_return_pointers_only)
2080 if (checkAttrMutualExclusion<CPUSpecificAttr>(S, D, AL))
2083 if (
const auto *Other = D->
getAttr<CPUDispatchAttr>()) {
2084 S.
Diag(AL.
getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
2085 S.
Diag(Other->getLocation(), diag::note_conflicting_attribute);
2088 }
else if (AL.
getParsedKind() == ParsedAttr::AT_CPUSpecific) {
2089 if (checkAttrMutualExclusion<CPUDispatchAttr>(S, D, AL))
2092 if (
const auto *Other = D->
getAttr<CPUSpecificAttr>()) {
2093 S.
Diag(AL.
getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
2094 S.
Diag(Other->getLocation(), diag::note_conflicting_attribute);
2101 if (
const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2102 if (MD->getParent()->isLambda()) {
2103 S.
Diag(AL.
getLoc(), diag::err_attribute_dll_lambda) << AL;
2114 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_type)
2123 S.
Diag(CPUArg->
Loc, diag::err_invalid_cpu_specific_dispatch_value)
2124 << CPUName << (AL.
getKind() == ParsedAttr::AT_CPUDispatch);
2130 return Target.CPUSpecificManglingCharacter(CPUName) ==
2133 S.
Diag(AL.
getLoc(), diag::warn_multiversion_duplicate_entries);
2136 CPUs.push_back(CPUArg->
Ident);
2140 if (AL.
getKind() == ParsedAttr::AT_CPUSpecific)
2142 CPUSpecificAttr(S.
Context, AL, CPUs.data(), CPUs.size()));
2145 CPUDispatchAttr(S.
Context, AL, CPUs.data(), CPUs.size()));
2150 S.
Diag(AL.
getLoc(), diag::err_attribute_not_supported_in_lang)
2160 S.
Diag(AL.
getLoc(), diag::err_attribute_not_clinkage) << AL;
2164 const auto *FD = cast<FunctionDecl>(D);
2166 S.
Diag(AL.
getLoc(), diag::warn_attribute_cmse_entry_static);
2176 const auto &Arch = Triple.getArch();
2177 if (Arch != llvm::Triple::x86 &&
2178 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
2179 S.
Diag(AL.
getLoc(), diag::err_attribute_not_supported_on_arch)
2180 << AL << Triple.getArchName();
2186 if (S.
getLangOpts().MSVCCompat && isa<CXXMethodDecl>(D)) {
2187 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_decl_type_str)
2199 if (!isa<ObjCMethodDecl>(D)) {
2200 S.
Diag(Attrs.
getLoc(), diag::warn_attribute_wrong_decl_type)
2226 S.
Diag(Attrs.
getLoc(), diag::warn_nocf_check_attribute_ignored);
2228 handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
2244 ? diag::err_keyword_not_supported_on_target
2245 : diag::warn_unknown_attribute_ignored)
2263 ? diag::err_attribute_wrong_decl_type
2264 : diag::warn_attribute_wrong_decl_type)
2300 if (VecReturnAttr *A = D->
getAttr<VecReturnAttr>()) {
2301 S.
Diag(AL.
getLoc(), diag::err_repeat_attribute) << A;
2305 const auto *R = cast<RecordDecl>(D);
2308 if (!isa<CXXRecordDecl>(R)) {
2309 S.
Diag(AL.
getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2313 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2314 S.
Diag(AL.
getLoc(), diag::err_attribute_vecreturn_only_pod_record);
2318 for (
const auto *I : R->fields()) {
2319 if ((count == 1) || !I->getType()->isVectorType()) {
2320 S.
Diag(AL.
getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2331 if (isa<ParmVarDecl>(D)) {
2336 diag::err_carries_dependency_param_not_function_decl);
2350 S.
Diag(AL.
getLoc(), diag::ext_cxx17_attr) << AL;
2356 uint32_t priority = ConstructorAttr::DefaultPriority;
2358 S.
Diag(AL.
getLoc(), diag::err_hlsl_init_priority_unsupported);
2369 uint32_t priority = DestructorAttr::DefaultPriority;
2377template <
typename AttrTy>
2389 if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
2390 S.
Diag(AL.
getLoc(), diag::err_objc_attr_protocol_requires_definition)
2400 VersionTuple Introduced,
2401 VersionTuple Deprecated,
2402 VersionTuple Obsoleted) {
2403 StringRef PlatformName
2404 = AvailabilityAttr::getPrettyPlatformName(Platform->
getName());
2405 if (PlatformName.empty())
2406 PlatformName = Platform->
getName();
2410 if (!Introduced.empty() && !Deprecated.empty() &&
2411 !(Introduced <= Deprecated)) {
2412 S.
Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2413 << 1 << PlatformName << Deprecated.getAsString()
2414 << 0 << Introduced.getAsString();
2418 if (!Introduced.empty() && !Obsoleted.empty() &&
2419 !(Introduced <= Obsoleted)) {
2420 S.
Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2421 << 2 << PlatformName << Obsoleted.getAsString()
2422 << 0 << Introduced.getAsString();
2426 if (!Deprecated.empty() && !Obsoleted.empty() &&
2427 !(Deprecated <= Obsoleted)) {
2428 S.
Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2429 << 2 << PlatformName << Obsoleted.getAsString()
2430 << 1 << Deprecated.getAsString();
2442 bool BeforeIsOkay) {
2443 if (
X.empty() || Y.empty())
2449 if (BeforeIsOkay &&
X < Y)
2457 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2458 VersionTuple Obsoleted,
bool IsUnavailable, StringRef Message,
2461 VersionTuple MergedIntroduced = Introduced;
2462 VersionTuple MergedDeprecated = Deprecated;
2463 VersionTuple MergedObsoleted = Obsoleted;
2464 bool FoundAny =
false;
2465 bool OverrideOrImpl =
false;
2469 OverrideOrImpl =
false;
2475 OverrideOrImpl =
true;
2481 for (
unsigned i = 0, e = Attrs.size(); i != e;) {
2482 const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2489 if (OldPlatform != Platform) {
2497 if (OldAA->getPriority() <
Priority)
2503 if (OldAA->getPriority() >
Priority) {
2504 Attrs.erase(Attrs.begin() + i);
2510 VersionTuple OldIntroduced = OldAA->getIntroduced();
2511 VersionTuple OldDeprecated = OldAA->getDeprecated();
2512 VersionTuple OldObsoleted = OldAA->getObsoleted();
2513 bool OldIsUnavailable = OldAA->getUnavailable();
2515 if (!
versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2516 !
versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2518 !(OldIsUnavailable == IsUnavailable ||
2519 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2520 if (OverrideOrImpl) {
2522 VersionTuple FirstVersion;
2523 VersionTuple SecondVersion;
2524 if (!
versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
2526 FirstVersion = OldIntroduced;
2527 SecondVersion = Introduced;
2528 }
else if (!
versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
2530 FirstVersion = Deprecated;
2531 SecondVersion = OldDeprecated;
2532 }
else if (!
versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
2534 FirstVersion = Obsoleted;
2535 SecondVersion = OldObsoleted;
2539 Diag(OldAA->getLocation(),
2540 diag::warn_mismatched_availability_override_unavail)
2541 << AvailabilityAttr::getPrettyPlatformName(Platform->
getName())
2552 Diag(OldAA->getLocation(),
2553 diag::warn_mismatched_availability_override)
2555 << AvailabilityAttr::getPrettyPlatformName(Platform->
getName())
2556 << FirstVersion.getAsString() << SecondVersion.getAsString()
2560 Diag(CI.
getLoc(), diag::note_overridden_method);
2562 Diag(CI.
getLoc(), diag::note_protocol_method);
2564 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2565 Diag(CI.
getLoc(), diag::note_previous_attribute);
2568 Attrs.erase(Attrs.begin() + i);
2573 VersionTuple MergedIntroduced2 = MergedIntroduced;
2574 VersionTuple MergedDeprecated2 = MergedDeprecated;
2575 VersionTuple MergedObsoleted2 = MergedObsoleted;
2577 if (MergedIntroduced2.empty())
2578 MergedIntroduced2 = OldIntroduced;
2579 if (MergedDeprecated2.empty())
2580 MergedDeprecated2 = OldDeprecated;
2581 if (MergedObsoleted2.empty())
2582 MergedObsoleted2 = OldObsoleted;
2585 MergedIntroduced2, MergedDeprecated2,
2586 MergedObsoleted2)) {
2587 Attrs.erase(Attrs.begin() + i);
2592 MergedIntroduced = MergedIntroduced2;
2593 MergedDeprecated = MergedDeprecated2;
2594 MergedObsoleted = MergedObsoleted2;
2600 MergedIntroduced == Introduced &&
2601 MergedDeprecated == Deprecated &&
2602 MergedObsoleted == Obsoleted)
2608 MergedDeprecated, MergedObsoleted) &&
2610 auto *Avail = ::new (
Context) AvailabilityAttr(
2611 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2612 Message, IsStrict, Replacement,
Priority);
2613 Avail->setImplicit(Implicit);
2620 if (isa<UsingDecl, UnresolvedUsingTypenameDecl, UnresolvedUsingValueDecl>(
2632 if (AvailabilityAttr::getPrettyPlatformName(II->
getName()).empty())
2633 S.
Diag(Platform->
Loc, diag::warn_availability_unknown_platform)
2636 auto *ND = dyn_cast<NamedDecl>(D);
2646 if (
const auto *SE = dyn_cast_if_present<StringLiteral>(AL.
getMessageExpr()))
2647 Str = SE->getString();
2648 StringRef Replacement;
2649 if (
const auto *SE =
2651 Replacement = SE->getString();
2653 if (II->
isStr(
"swift")) {
2655 (!IsUnavailable && !Deprecated.
isValid())) {
2657 diag::warn_availability_swift_unavailable_deprecated_only);
2662 if (II->
isStr(
"fuchsia")) {
2663 std::optional<unsigned> Min, Sub;
2664 if ((Min = Introduced.
Version.getMinor()) ||
2665 (Sub = Introduced.
Version.getSubminor())) {
2666 S.
Diag(AL.
getLoc(), diag::warn_availability_fuchsia_unavailable_minor);
2676 Obsoleted.
Version, IsUnavailable, Str, IsStrict, Replacement,
2687 else if (II->
getName() ==
"ios_app_extension")
2692 const auto *IOSToWatchOSMapping =
2697 auto adjustWatchOSVersion =
2698 [IOSToWatchOSMapping](VersionTuple Version) -> VersionTuple {
2699 if (Version.empty())
2701 auto MinimumWatchOSVersion = VersionTuple(2, 0);
2703 if (IOSToWatchOSMapping) {
2704 if (
auto MappedVersion = IOSToWatchOSMapping->map(
2705 Version, MinimumWatchOSVersion, std::nullopt)) {
2706 return *MappedVersion;
2710 auto Major = Version.getMajor();
2711 auto NewMajor = Major >= 9 ? Major - 7 : 0;
2712 if (NewMajor >= 2) {
2713 if (Version.getMinor()) {
2714 if (Version.getSubminor())
2715 return VersionTuple(NewMajor, *Version.getMinor(),
2716 *Version.getSubminor());
2718 return VersionTuple(NewMajor, *Version.getMinor());
2720 return VersionTuple(NewMajor);
2723 return MinimumWatchOSVersion;
2726 auto NewIntroduced = adjustWatchOSVersion(Introduced.
Version);
2727 auto NewDeprecated = adjustWatchOSVersion(Deprecated.
Version);
2728 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.
Version);
2731 ND, AL, NewII,
true , NewIntroduced, NewDeprecated,
2732 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2744 else if (II->
getName() ==
"ios_app_extension")
2749 const auto *IOSToTvOSMapping =
2754 auto AdjustTvOSVersion =
2755 [IOSToTvOSMapping](VersionTuple Version) -> VersionTuple {
2756 if (Version.empty())
2759 if (IOSToTvOSMapping) {
2760 if (
auto MappedVersion = IOSToTvOSMapping->map(
2761 Version, VersionTuple(0, 0), std::nullopt)) {
2762 return *MappedVersion;
2768 auto NewIntroduced = AdjustTvOSVersion(Introduced.
Version);
2769 auto NewDeprecated = AdjustTvOSVersion(Deprecated.
Version);
2770 auto NewObsoleted = AdjustTvOSVersion(Obsoleted.
Version);
2773 ND, AL, NewII,
true , NewIntroduced, NewDeprecated,
2774 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2781 llvm::Triple::IOS &&
2783 auto GetSDKInfo = [&]() {
2792 else if (II->
getName() ==
"ios_app_extension")
2795 auto MinMacCatalystVersion = [](
const VersionTuple &
V) {
2798 if (
V.getMajor() < 13 ||
2799 (
V.getMajor() == 13 &&
V.getMinor() && *
V.getMinor() < 1))
2800 return VersionTuple(13, 1);
2804 ND, AL, NewII,
true ,
2805 MinMacCatalystVersion(Introduced.
Version),
2806 MinMacCatalystVersion(Deprecated.
Version),
2807 MinMacCatalystVersion(Obsoleted.
Version), IsUnavailable, Str,
2812 }
else if (II->
getName() ==
"macos" && GetSDKInfo() &&
2814 !Obsoleted.
Version.empty())) {
2815 if (
const auto *MacOStoMacCatalystMapping =
2816 GetSDKInfo()->getVersionMapping(
2823 auto RemapMacOSVersion =
2824 [&](
const VersionTuple &
V) -> std::optional<VersionTuple> {
2826 return std::nullopt;
2828 if (
V.getMajor() == 100000)
2829 return VersionTuple(100000);
2831 return MacOStoMacCatalystMapping->map(
V, VersionTuple(13, 1),
2834 std::optional<VersionTuple> NewIntroduced =
2835 RemapMacOSVersion(Introduced.
Version),
2837 RemapMacOSVersion(Deprecated.
Version),
2839 RemapMacOSVersion(Obsoleted.
Version);
2840 if (NewIntroduced || NewDeprecated || NewObsoleted) {
2841 auto VersionOrEmptyVersion =
2842 [](
const std::optional<VersionTuple> &
V) -> VersionTuple {
2843 return V ? *
V : VersionTuple();
2846 ND, AL, NewII,
true ,
2847 VersionOrEmptyVersion(NewIntroduced),
2848 VersionOrEmptyVersion(NewDeprecated),
2849 VersionOrEmptyVersion(NewObsoleted),
false, Str,
2867 if (
const auto *SE = dyn_cast_if_present<StringLiteral>(AL.
getArgAsExpr(0)))
2869 StringRef DefinedIn;
2870 if (
const auto *SE = dyn_cast_if_present<StringLiteral>(AL.
getArgAsExpr(1)))
2871 DefinedIn = SE->getString();
2872 bool IsGeneratedDeclaration = AL.
getArgAsIdent(2) !=
nullptr;
2874 if (
const auto *SE = dyn_cast_if_present<StringLiteral>(AL.
getArgAsExpr(3)))
2875 USR = SE->getString();
2883 typename T::VisibilityType value) {
2884 T *existingAttr = D->
getAttr<T>();
2886 typename T::VisibilityType existingValue = existingAttr->getVisibility();
2887 if (existingValue == value)
2889 S.
Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2890 S.
Diag(CI.
getLoc(), diag::note_previous_attribute);
2898 VisibilityAttr::VisibilityType Vis) {
2899 return ::mergeVisibilityAttr<VisibilityAttr>(*
this, D, CI, Vis);
2904 TypeVisibilityAttr::VisibilityType Vis) {
2905 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*
this, D, CI, Vis);
2909 bool isTypeVisibility) {
2911 if (isa<TypedefNameDecl>(D)) {
2917 if (isTypeVisibility && !(isa<TagDecl>(D) || isa<ObjCInterfaceDecl>(D) ||
2918 isa<NamespaceDecl>(D))) {
2930 VisibilityAttr::VisibilityType
type;
2931 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr,
type)) {
2932 S.
Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2939 if (
type == VisibilityAttr::Protected &&
2941 S.
Diag(AL.
getLoc(), diag::warn_attribute_protected_visibility);
2942 type = VisibilityAttr::Default;
2946 if (isTypeVisibility) {
2948 D, AL, (TypeVisibilityAttr::VisibilityType)
type);
2959 S.
Diag(AL.
getLoc(), diag::err_objc_direct_on_protocol) <<
false;
2964 handleSimpleAttribute<ObjCDirectAttr>(S, D, AL);
2966 S.
Diag(AL.
getLoc(), diag::warn_objc_direct_ignored) << AL;
2973 handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
2975 S.
Diag(AL.
getLoc(), diag::warn_objc_direct_ignored) << AL;
2980 const auto *M = cast<ObjCMethodDecl>(D);
2982 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
2988 ObjCMethodFamilyAttr::FamilyKind F;
2989 if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->
Ident->
getName(), F)) {
2990 S.
Diag(IL->
Loc, diag::warn_attribute_type_not_supported) << AL << IL->
Ident;
2994 if (F == ObjCMethodFamilyAttr::OMF_init &&
2995 !M->getReturnType()->isObjCObjectPointerType()) {
2996 S.
Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2997 << M->getReturnType();
3006 if (
const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
3007 QualType T = TD->getUnderlyingType();
3009 S.
Diag(TD->getLocation(), diag::err_nsobject_attribute);
3013 else if (
const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
3016 S.
Diag(PD->getLocation(), diag::err_nsobject_attribute);
3033 if (
const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
3034 QualType T = TD->getUnderlyingType();
3036 S.
Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
3048 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
3054 BlocksAttr::BlockType
type;
3055 if (!BlocksAttr::ConvertStrToBlockType(II->
getName(),
type)) {
3056 S.
Diag(AL.
getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
3064 unsigned sentinel = (
unsigned)SentinelAttr::DefaultSentinel;
3067 std::optional<llvm::APSInt> Idx = llvm::APSInt(32);
3069 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
3074 if (Idx->isSigned() && Idx->isNegative()) {
3075 S.
Diag(AL.
getLoc(), diag::err_attribute_sentinel_less_than_zero)
3080 sentinel = Idx->getZExtValue();
3083 unsigned nullPos = (
unsigned)SentinelAttr::DefaultNullPos;
3086 std::optional<llvm::APSInt> Idx = llvm::APSInt(32);
3088 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
3092 nullPos = Idx->getZExtValue();
3094 if ((Idx->isSigned() && Idx->isNegative()) || nullPos > 1) {
3097 S.
Diag(AL.
getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
3103 if (
const auto *FD = dyn_cast<FunctionDecl>(D)) {
3105 if (isa<FunctionNoProtoType>(FT)) {
3106 S.
Diag(AL.
getLoc(), diag::warn_attribute_sentinel_named_arguments);
3110 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
3111 S.
Diag(AL.
getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
3114 }
else if (
const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
3115 if (!MD->isVariadic()) {
3116 S.
Diag(AL.
getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
3119 }
else if (
const auto *BD = dyn_cast<BlockDecl>(D)) {
3120 if (!BD->isVariadic()) {
3121 S.
Diag(AL.
getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
3124 }
else if (
const auto *
V = dyn_cast<VarDecl>(D)) {
3132 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
3134 S.
Diag(AL.
getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
3138 S.
Diag(AL.
getLoc(), diag::warn_attribute_wrong_decl_type)
3144 S.
Diag(AL.
getLoc(), diag::warn_attribute_wrong_decl_type)
3155 !isa<CXXConstructorDecl>(D)) {
3156 S.
Diag(AL.
getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
3159 if (
const auto *MD = dyn_cast<ObjCMethodDecl>(D))
3160 if (MD->getReturnType()->isVoidType()) {
3161 S.
Diag(AL.
getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
3169 if (isa<VarDecl>(D))
3170 S.
Diag(AL.
getLoc(), diag::warn_attribute_wrong_decl_type_str)
3172 <<
"functions, classes, or enumerations";
3181 if (LO.CPlusPlus && !LO.CPlusPlus20)
3182 S.
Diag(AL.
getLoc(), diag::ext_cxx20_attr) << AL;
3190 }
else if (LO.CPlusPlus && !LO.CPlusPlus17)
3191 S.
Diag(AL.
getLoc(), diag::ext_cxx17_attr) << AL;
3196 isa<TypedefNameDecl>(D)) {
3197 S.
Diag(AL.
getLoc(), diag::warn_unused_result_typedef_unsupported_spelling)
3210 S.
Diag(AL.
getLoc(), diag::warn_attribute_invalid_on_definition)
3212 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
3214 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
3217 S.
Diag(AL.
getLoc(), diag::warn_attribute_wrong_decl_type)
3227template <
typename WorkGroupAttr>
3230 for (
unsigned i = 0; i < 3; ++i) {
3235 if (WGSize[i] == 0) {
3236 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_is_zero)
3242 WorkGroupAttr *Existing = D->
getAttr<WorkGroupAttr>();
3243 if (Existing && !(Existing->getXDim() == WGSize[0] &&
3244 Existing->getYDim() == WGSize[1] &&
3245 Existing->getZDim() == WGSize[2]))
3246 S.
Diag(AL.
getLoc(), diag::warn_duplicate_attribute) << AL;
3249 WorkGroupAttr(S.
Context, AL, WGSize[0], WGSize[1], WGSize[2]));
3259 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_is_zero)
3264 OpenCLIntelReqdSubGroupSizeAttr *Existing =
3265 D->
getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
3266 if (Existing && Existing->getSubGroupSize() != SGSize)
3267 S.
Diag(AL.
getLoc(), diag::warn_duplicate_attribute) << AL;
3270 OpenCLIntelReqdSubGroupSizeAttr(S.
Context, AL, SGSize));
3275 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
3281 assert(ParmTSI &&
"no type source info for attribute argument");
3286 S.
Diag(AL.
getLoc(), diag::err_attribute_invalid_argument) << 2 << AL;
3290 if (VecTypeHintAttr *A = D->
getAttr<VecTypeHintAttr>()) {
3292 S.
Diag(AL.
getLoc(), diag::warn_duplicate_attribute) << AL;
3304 if (
const auto *FD = dyn_cast<FunctionDecl>(D)) {
3309 if (SectionAttr *ExistingAttr = D->
getAttr<SectionAttr>()) {
3310 if (ExistingAttr->getName() == Name)
3312 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3314 Diag(CI.
getLoc(), diag::note_previous_attribute);
3331 return llvm::Error::success();
3334 StringRef Segment, Section;
3335 unsigned TAA, StubSize;
3337 return llvm::MCSectionMachO::ParseSectionSpecifier(SecName, Segment, Section,
3338 TAA, HasTAA, StubSize);
3343 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3368 cast<NamedDecl>(D));
3375 StringRef CodeSegName) {
3377 S.
Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3389 if (
const auto *FD = dyn_cast<FunctionDecl>(D)) {
3393 if (
const auto *ExistingAttr = D->
getAttr<CodeSegAttr>()) {
3394 if (ExistingAttr->getName() == Name)
3396 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3398 Diag(CI.
getLoc(), diag::note_previous_attribute);
3411 if (
const auto *ExistingAttr = D->
getAttr<CodeSegAttr>()) {
3412 if (!ExistingAttr->isImplicit()) {
3414 ExistingAttr->getName() == Str
3415 ? diag::warn_duplicate_codeseg_attribute
3416 : diag::err_conflicting_codeseg_attribute);
3428 enum FirstParam { Unsupported, Duplicate,
Unknown };
3429 enum SecondParam {
None, CPU, Tune };
3431 if (AttrStr.contains(
"fpmath="))
3432 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3433 << Unsupported <<
None <<
"fpmath=" <<
Target;
3437 AttrStr.contains(
"tune="))
3438 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3444 if (!ParsedAttrs.
CPU.empty() &&
3446 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3449 if (!ParsedAttrs.
Tune.empty() &&
3451 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3455 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3458 for (
const auto &Feature : ParsedAttrs.
Features) {
3459 auto CurFeature = StringRef(Feature).drop_front();
3461 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3462 << Unsupported <<
None << CurFeature <<
Target;
3471 if (DiagMsg.empty())
3472 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3473 << Unsupported <<
None <<
"branch-protection" <<
Target;
3474 return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec)
3477 if (!DiagMsg.empty())
3478 Diag(LiteralLoc, diag::warn_unsupported_branch_protection_spec) << DiagMsg;
3486 enum FirstParam { Unsupported };
3487 enum SecondParam {
None };
3489 if (AttrStr.trim() ==
"default")
3492 AttrStr.split(Features,
"+");
3493 for (
auto &CurFeature : Features) {
3494 CurFeature = CurFeature.trim();
3495 if (CurFeature ==
"default")
3498 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3507 bool isDefault =
false;
3513 TargetVersionAttr *NewAttr =
3526 TargetAttr *NewAttr = ::new (S.
Context) TargetAttr(S.
Context, AL, Str);
3532 bool &HasDefault,
bool &HasCommas,
bool &HasNotDefault,
3534 enum FirstParam { Unsupported, Duplicate,
Unknown };
3535 enum SecondParam {
None, CPU, Tune };
3537 HasCommas = HasCommas || Str.contains(
',');
3540 if (Str.size() == 0)
3541 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3544 std::pair<StringRef, StringRef> Parts = {{}, Str};
3545 while (!Parts.second.empty()) {
3546 Parts = Parts.second.split(
',');
3547 StringRef Cur = Parts.first.trim();
3549 Literal->getLocationOfByte(Cur.data() - Literal->getString().data(),
3552 bool DefaultIsDupe =
false;
3553 bool HasCodeGenImpact =
false;
3555 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
3560 if (Cur ==
"default") {
3561 DefaultIsDupe = HasDefault;
3563 if (llvm::is_contained(StringsBuffer, Cur) || DefaultIsDupe)
3564 Diag(CurLoc, diag::warn_target_clone_duplicate_options);
3566 StringsBuffer.push_back(Cur);
3568 std::pair<StringRef, StringRef> CurParts = {{}, Cur};
3570 while (!CurParts.second.empty()) {
3571 CurParts = CurParts.second.split(
'+');
3572 StringRef CurFeature = CurParts.first.trim();
3574 Diag(CurLoc, diag::warn_unsupported_target_attribute)
3579 HasCodeGenImpact =
true;
3580 CurFeatures.push_back(CurFeature);
3583 llvm::sort(CurFeatures);
3585 for (
auto &CurFeat : CurFeatures) {
3586 if (!Res.equals(
""))
3588 Res.append(CurFeat);
3590 if (llvm::is_contained(StringsBuffer, Res) || DefaultIsDupe)
3591 Diag(CurLoc, diag::warn_target_clone_duplicate_options);
3592 else if (!HasCodeGenImpact)
3595 Diag(CurLoc, diag::warn_target_clone_no_impact_options);
3596 else if (!Res.empty()) {
3597 StringsBuffer.push_back(Res);
3598 HasNotDefault =
true;
3603 if (Cur.startswith(
"arch=")) {
3605 Cur.drop_front(
sizeof(
"arch=") - 1)))
3606 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
3607 << Unsupported << CPU << Cur.drop_front(
sizeof(
"arch=") - 1)
3609 }
else if (Cur ==
"default") {
3610 DefaultIsDupe = HasDefault;
3613 return Diag(CurLoc, diag::warn_unsupported_target_attribute)
3615 if (llvm::is_contained(StringsBuffer, Cur) || DefaultIsDupe)
3616 Diag(CurLoc, diag::warn_target_clone_duplicate_options);
3618 StringsBuffer.push_back(Cur);
3621 if (Str.rtrim().endswith(
","))
3622 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3634 if (
const auto *Other = D->
getAttr<TargetClonesAttr>()) {
3635 S.
Diag(AL.
getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
3636 S.
Diag(Other->getLocation(), diag::note_conflicting_attribute);
3639 if (checkAttrMutualExclusion<TargetClonesAttr>(S, D, AL))
3644 bool HasCommas =
false, HasDefault =
false, HasNotDefault =
false;
3646 for (
unsigned I = 0, E = AL.
getNumArgs(); I != E; ++I) {
3653 HasDefault, HasCommas, HasNotDefault, StringsBuffer))
3656 for (
auto &SmallStr : StringsBuffer)
3657 Strings.push_back(SmallStr.str());
3660 S.
Diag(AL.
getLoc(), diag::warn_target_clone_mixed_values);
3665 Strings.push_back(
"default");
3669 S.
Diag(AL.
getLoc(), diag::err_target_clone_must_have_default);
3675 if (
const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
3676 if (MD->getParent()->isLambda()) {
3688 cast<FunctionDecl>(D)->setIsMultiVersion();
3689 TargetClonesAttr *NewAttr = ::new (S.
Context)
3690 TargetClonesAttr(S.
Context, AL, Strings.data(), Strings.size());
3702 MinVectorWidthAttr *Existing = D->
getAttr<MinVectorWidthAttr>();
3703 if (Existing && Existing->getVectorWidth() != VecWidth) {
3704 S.
Diag(AL.
getLoc(), diag::warn_duplicate_attribute) << AL;
3719 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3721 S.
Diag(Loc, diag::warn_cleanup_ext);
3722 FD = dyn_cast<FunctionDecl>(DRE->
getDecl());
3725 S.
Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3729 }
else if (
auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
3730 if (ULE->hasExplicitTemplateArgs())
3731 S.
Diag(Loc, diag::warn_cleanup_ext);
3733 NI = ULE->getNameInfo();
3735 S.
Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3742 S.
Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
3747 S.
Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3758 S.
Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3759 << NI.
getName() << ParamTy << Ty;
3769 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
3774 EnumExtensibilityAttr::Kind ExtensibilityKind;
3776 if (!EnumExtensibilityAttr::ConvertStrToKind(II->
getName(),
3777 ExtensibilityKind)) {
3778 S.
Diag(AL.
getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
3783 EnumExtensibilityAttr(S.
Context, AL, ExtensibilityKind));
3798 if (NotNSStringTy &&
3802 S.
Diag(AL.
getLoc(), diag::err_format_attribute_not)
3810 if (
auto *OMD = dyn_cast<ObjCMethodDecl>(D))
3811 if (
auto *Interface = OMD->getClassInterface())
3813 QualType(Interface->getTypeForDecl(), 0));
3818 S.
Diag(AL.
getLoc(), diag::err_format_attribute_result_not)
3819 << (NotNSStringTy ?
"string type" :
"NSString")
3839 return llvm::StringSwitch<FormatAttrKind>(Format)
3853 .Cases(
"gcc_diag",
"gcc_cdiag",
"gcc_cxxdiag",
"gcc_tdiag",
IgnoredFormat)
3861 S.
Diag(AL.
getLoc(), diag::warn_attribute_ignored) << AL;
3866 S.
Diag(AL.
getLoc(), diag::err_hlsl_init_priority_unsupported);
3871 S.
Diag(AL.
getLoc(), diag::err_init_priority_object_attr);
3875 QualType T = cast<VarDecl>(D)->getType();
3879 S.
Diag(AL.
getLoc(), diag::err_init_priority_object_attr);
3885 uint32_t prioritynum;
3894 if ((prioritynum < 101 || prioritynum > 65535) &&
3896 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_out_of_range)
3905 StringRef NewUserDiagnostic) {
3906 if (
const auto *EA = D->
getAttr<ErrorAttr>()) {
3908 assert((NewAttr ==
"error" || NewAttr ==
"warning") &&
3909 "unexpected normalized full name");
3910 bool Match = (EA->isError() && NewAttr ==
"error") ||
3911 (EA->isWarning() && NewAttr ==
"warning");
3913 Diag(EA->getLocation(), diag::err_attributes_are_not_compatible)
3916 EA->isRegularKeywordAttribute());
3917 Diag(CI.
getLoc(), diag::note_conflicting_attribute);
3920 if (EA->getUserDiagnostic() != NewUserDiagnostic) {
3921 Diag(CI.
getLoc(), diag::warn_duplicate_attribute) << EA;
3922 Diag(EA->getLoc(), diag::note_previous_attribute);
3926 return ::new (
Context) ErrorAttr(
Context, CI, NewUserDiagnostic);
3934 if (F->getType() == Format &&
3935 F->getFormatIdx() == FormatIdx &&
3936 F->getFirstArg() == FirstArg) {
3939 if (F->getLocation().isInvalid())
3945 return ::new (
Context) FormatAttr(
Context, CI, Format, FormatIdx, FirstArg);
3952 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_n_type)
3963 StringRef Format = II->
getName();
3977 S.
Diag(AL.
getLoc(), diag::warn_attribute_type_not_supported)
3988 if (Idx < 1 || Idx > NumArgs) {
3989 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_out_of_bounds)
3995 unsigned ArgIdx = Idx - 1;
3997 if (HasImplicitThisParam) {
4000 diag::err_format_attribute_implicit_this_format_string)
4014 S.
Diag(AL.
getLoc(), diag::err_format_attribute_not)
4026 if (FirstArg != 0) {
4030 S.
Diag(AL.
getLoc(), diag::err_format_strftime_third_parameter)
4038 if (FirstArg != NumArgs + 1) {
4039 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_out_of_bounds)
4042 std::to_string(NumArgs + 1));
4047 S.
Diag(D->
getLocation(), diag::warn_gcc_requires_variadic_function) << AL;
4048 if (FirstArg <= Idx) {
4052 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_out_of_bounds)
4068 S.
Diag(AL.
getLoc(), diag::err_callback_attribute_no_callee)
4077 assert(FD &&
"Expected a function declaration!");
4079 llvm::StringMap<int> NameIdxMapping;
4080 NameIdxMapping[
"__"] = -1;
4082 NameIdxMapping[
"this"] = 0;
4086 NameIdxMapping[PVD->getName()] = Idx++;
4088 auto UnknownName = NameIdxMapping.end();
4091 for (
unsigned I = 0, E = AL.
getNumArgs(); I < E; ++I) {
4097 auto It = NameIdxMapping.find(IdLoc->
Ident->
getName());
4098 if (It == UnknownName) {
4099 S.
Diag(AL.
getLoc(), diag::err_callback_attribute_argument_unknown)
4105 ArgIdx = It->second;
4112 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_out_of_bounds)
4118 if (ArgIdx < -1 || ArgIdx > NumArgs) {
4119 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_out_of_bounds)
4126 llvm_unreachable(
"Unexpected ParsedAttr argument type!");
4129 if (ArgIdx == 0 && !HasImplicitThisParam) {
4130 S.
Diag(AL.
getLoc(), diag::err_callback_implicit_this_not_available)
4137 if (!HasImplicitThisParam && ArgIdx > 0)
4140 EncodingIndices.push_back(ArgIdx);
4143 int CalleeIdx = EncodingIndices.front();
4147 if (CalleeIdx < (
int)HasImplicitThisParam) {
4148 S.
Diag(AL.
getLoc(), diag::err_callback_attribute_invalid_callee)
4155 const Type *CalleeType =
4159 S.
Diag(AL.
getLoc(), diag::err_callback_callee_no_function_type)
4164 const Type *CalleeFnType =
4169 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
4170 if (!CalleeFnProtoType) {
4171 S.
Diag(AL.
getLoc(), diag::err_callback_callee_no_function_type)
4176 if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
4177 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_number_arguments)
4178 << AL << (
unsigned)(EncodingIndices.size() - 1);
4182 if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
4183 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_number_arguments)
4184 << AL << (
unsigned)(EncodingIndices.size() - 1);
4188 if (CalleeFnProtoType->isVariadic()) {
4194 if (D->
hasAttr<CallbackAttr>()) {
4200 S.
Context, AL, EncodingIndices.data(), EncodingIndices.size()));
4213 QualType T = cast<ParmVarDecl>(D)->getType();
4216 S.
Diag(AL.
getLoc(), diag::err_called_once_attribute_wrong_type);
4226 const auto *TD = dyn_cast<TypedefNameDecl>(D);
4227 if (TD && TD->getUnderlyingType()->isUnionType())
4228 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
4230 RD = dyn_cast<RecordDecl>(D);
4233 S.
Diag(AL.
getLoc(), diag::warn_attribute_wrong_decl_type)
4241 diag::warn_transparent_union_attribute_not_definition);
4247 if (Field == FieldEnd) {
4248 S.
Diag(AL.
getLoc(), diag::warn_transparent_union_attribute_zero_fields);
4256 diag::warn_transparent_union_attribute_floating)
4265 for (; Field != FieldEnd; ++Field) {
4266 QualType FieldType = Field->getType();
4280 S.
Diag(Field->getLocation(),
4281 diag::warn_transparent_union_attribute_field_size_align)
4282 << isSize << *Field << FieldBits;
4283 unsigned FirstBits = isSize ? FirstSize : FirstAlign;
4285 diag::note_transparent_union_first_field_size_align)
4286 << isSize << FirstBits;
4296 auto *
Attr = AnnotateAttr::Create(
Context, Str, Args.data(), Args.size(), CI);
4312 for (
unsigned Idx = 1; Idx < AL.
getNumArgs(); Idx++) {
4325 AlignValueAttr TmpAttr(
Context, CI, E);
4329 if (
const auto *TD = dyn_cast<TypedefNameDecl>(D))
4330 T = TD->getUnderlyingType();
4331 else if (
const auto *VD = dyn_cast<ValueDecl>(D))
4334 llvm_unreachable(
"Unknown decl type for align_value");
4338 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
4344 llvm::APSInt Alignment;
4346 E, &Alignment, diag::err_align_value_attribute_argument_not_int);
4350 if (!Alignment.isPowerOf2()) {
4351 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
4373 diag::err_pack_expansion_without_parameter_packs);
4388 S.
Diag(AL.
getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
4400 diag::err_pack_expansion_without_parameter_packs);
4427 const AlignedAttr &
Attr,
4430 if (isa<ParmVarDecl>(D)) {
4432 }
else if (
const auto *VD = dyn_cast<VarDecl>(D)) {
4435 if (VD->isExceptionVariable())
4437 }
else if (
const auto *FD = dyn_cast<FieldDecl>(D)) {
4438 if (FD->isBitField())
4440 }
else if (
const auto *ED = dyn_cast<EnumDecl>(D)) {
4441 if (ED->getLangOpts().CPlusPlus)
4443 }
else if (!isa<TagDecl>(D)) {
4444 return S.
Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
4449 if (DiagKind != -1) {
4450 return S.
Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
4451 << &
Attr << DiagKind;
4457 bool IsPackExpansion) {
4458 AlignedAttr TmpAttr(
Context, CI,
true, E);
4462 if (TmpAttr.isAlignas() &&
4470 if (
const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
4471 if (!TND->getUnderlyingType()->isDependentType()) {
4472 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
4479 AlignedAttr *AA = ::new (
Context) AlignedAttr(
Context, CI,
true, E);
4480 AA->setPackExpansion(IsPackExpansion);
4486 llvm::APSInt Alignment;
4488 E, &Alignment, diag::err_aligned_attribute_argument_not_int);
4496 Diag(AttrLoc, diag::err_attribute_aligned_too_great)
4501 uint64_t AlignVal = Alignment.getZExtValue();
4507 if (!(TmpAttr.isAlignas() && !Alignment)) {
4508 if (!llvm::isPowerOf2_64(AlignVal)) {
4509 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
4515 const auto *VD = dyn_cast<VarDecl>(D);
4517 unsigned MaxTLSAlign =
4520 if (MaxTLSAlign && AlignVal > MaxTLSAlign &&
4522 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
4523 << (
unsigned)AlignVal << VD << MaxTLSAlign;
4531 const Type *Ty = VD->getType().getTypePtr();
4533 Diag(VD->getLocation(), diag::warn_aligned_attr_underaligned)
4534 << VD->getType() << 16;
4540 AA->setPackExpansion(IsPackExpansion);
4541 AA->setCachedAlignmentValue(
4548 AlignedAttr TmpAttr(
Context, CI,
false, TS);
4552 if (TmpAttr.isAlignas() &&
4560 if (
const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
4561 if (!TND->getUnderlyingType()->isDependentType()) {
4562 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
4568 AlignedAttr *AA = ::new (
Context) AlignedAttr(
Context, CI,
false, TS);
4569 AA->setPackExpansion(IsPackExpansion);
4574 const auto *VD = dyn_cast<VarDecl>(D);
4575 unsigned AlignVal = TmpAttr.getAlignment(
Context);
4579 const Type *Ty = VD->getType().getTypePtr();
4582 Diag(VD->getLocation(), diag::warn_aligned_attr_underaligned)
4583 << VD->getType() << 16;
4588 AlignedAttr *AA = ::new (
Context) AlignedAttr(
Context, CI,
false, TS);
4589 AA->setPackExpansion(IsPackExpansion);
4590 AA->setCachedAlignmentValue(AlignVal);
4595 assert(D->
hasAttrs() &&
"no attributes on decl");
4598 if (
const auto *VD = dyn_cast<ValueDecl>(D)) {
4599 UnderlyingTy = DiagTy = VD->getType();
4602 if (
const auto *ED = dyn_cast<EnumDecl>(D))
4603 UnderlyingTy = ED->getIntegerType();
4612 AlignedAttr *AlignasAttr =
nullptr;
4613 AlignedAttr *LastAlignedAttr =
nullptr;
4616 if (I->isAlignmentDependent())
4620 Align = std::max(Align, I->getAlignment(
Context));
4621 LastAlignedAttr = I;
4625 Diag(LastAlignedAttr->getLocation(), diag::err_attribute_sizeless_type)
4626 << LastAlignedAttr << DiagTy;
4627 }
else if (AlignasAttr && Align) {
4630 if (NaturalAlign > RequestedAlign)
4631 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
4658 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
4667 bool &IntegerMode,
bool &ComplexMode,
4670 ComplexMode =
false;
4672 switch (Str.size()) {
4692 DestWidth = Str[1] ==
'I' ? 0 : 128;
4700 DestWidth = Str[1] ==
'I' ? 0 : 128;
4703 if (Str[1] ==
'F') {
4704 IntegerMode =
false;
4705 }
else if (Str[1] ==
'C') {
4706 IntegerMode =
false;
4708 }
else if (Str[1] !=
'I') {
4717 else if (Str ==
"byte")
4721 if (Str ==
"pointer")
4725 if (Str ==
"unwind_word")
4741 S.
Diag(AL.
getLoc(), diag::err_attribute_argument_type)
4753 StringRef Str = Name->getName();
4757 unsigned DestWidth = 0;
4758 bool IntegerMode =
true;
4759 bool ComplexMode =
false;
4761 llvm::APInt VectorSize(64, 0);
4762 if (Str.size() >= 4 && Str[0] ==
'V') {
4764 size_t StrSize = Str.size();
4765 size_t VectorStringLength = 0;
4766 while ((VectorStringLength + 1) < StrSize &&
4767 isdigit(Str[VectorStringLength + 1]))
4768 ++VectorStringLength;
4769 if (VectorStringLength &&
4770 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
4771 VectorSize.isPowerOf2()) {
4773 IntegerMode, ComplexMode, ExplicitType);
4775 if (!InInstantiation)
4776 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
4791 Diag(AttrLoc, diag::err_machine_mode) << 0 << Name;
4796 if (
const auto *TD = dyn_cast<TypedefNameDecl>(D))
4797 OldTy = TD->getUnderlyingType();
4798 else if (
const auto *ED = dyn_cast<EnumDecl>(D)) {
4801 OldTy = ED->getIntegerType();
4805 OldTy = cast<ValueDecl>(D)->getType();
4816 OldElemTy = VT->getElementType();
4822 VectorSize.getBoolValue()) {
4823 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.
getRange();
4831 !IntegralOrAnyEnumType)
4832 Diag(AttrLoc, diag::err_mode_not_primitive);
4833 else if (IntegerMode) {
4834 if (!IntegralOrAnyEnumType)
4835 Diag(AttrLoc, diag::err_mode_wrong_type);
4836 }
else if (ComplexMode) {
4838 Diag(AttrLoc, diag::err_mode_wrong_type);
4841 Diag(AttrLoc, diag::err_mode_wrong_type);
4852 if (NewElemTy.
isNull()) {
4853 Diag(AttrLoc, diag::err_machine_mode) << 1 << Name;
4862 if (VectorSize.getBoolValue()) {
4868 Diag(AttrLoc, diag::err_complex_mode_vector_type);
4872 OldVT->getNumElements() /
4879 Diag(AttrLoc, diag::err_mode_wrong_type);
4884 if (
auto *TD = dyn_cast<TypedefNameDecl>(D))
4885 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
4886 else if (
auto *ED = dyn_cast<EnumDecl>(D))
4887 ED->setIntegerType(NewTy);
4889 cast<ValueDecl>(D)->setType(NewTy);
4901 if (OptimizeNoneAttr *Optnone = D->
getAttr<OptimizeNoneAttr>()) {
4902 Diag(CI.
getLoc(), diag::warn_attribute_ignored) << Ident;
4903 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4907 if (D->
hasAttr<AlwaysInlineAttr>())
4915 if (
const auto *VD = dyn_cast<VarDecl>(D)) {
4918 if (VD->getKind() != Decl::Var) {
4919 Diag(AL.
getLoc(), diag::warn_attribute_wrong_decl_type)
4926 if (VD->hasLocalStorage()) {
4927 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4934InternalLinkageAttr *
4936 if (
const auto *VD = dyn_cast<VarDecl>(D)) {
4939 if (VD->getKind() != Decl::Var) {
4940 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
4941 << &AL << AL.isRegularKeywordAttribute()
4947 if (VD->hasLocalStorage()) {
4948 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
4957 if (OptimizeNoneAttr *Optnone = D->
getAttr<OptimizeNoneAttr>()) {
4958 Diag(CI.
getLoc(), diag::warn_attribute_ignored) <<
"'minsize'";
4959 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
4963 if (D->
hasAttr<MinSizeAttr>())
4971 if (
const auto *PrevSNA = D->
getAttr<SwiftNameAttr>()) {
4972 if (PrevSNA->getName() != Name && !PrevSNA->isImplicit()) {
4973 Diag(PrevSNA->getLocation(), diag::err_attributes_are_not_compatible)
4975 << (PrevSNA->isRegularKeywordAttribute() ||
4976 SNA.isRegularKeywordAttribute());
4977 Diag(SNA.getLoc(), diag::note_conflicting_attribute);