clang 24.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
13#include "clang/AST/APValue.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/ExprCXX.h"
26#include "clang/AST/Mangle.h"
27#include "clang/AST/Type.h"
29#include "clang/Basic/Cuda.h"
37#include "clang/Sema/Attr.h"
38#include "clang/Sema/DeclSpec.h"
41#include "clang/Sema/Lookup.h"
43#include "clang/Sema/Scope.h"
45#include "clang/Sema/Sema.h"
47#include "clang/Sema/SemaARM.h"
48#include "clang/Sema/SemaAVR.h"
49#include "clang/Sema/SemaBPF.h"
50#include "clang/Sema/SemaCUDA.h"
51#include "clang/Sema/SemaHLSL.h"
53#include "clang/Sema/SemaM68k.h"
54#include "clang/Sema/SemaMIPS.h"
56#include "clang/Sema/SemaObjC.h"
59#include "clang/Sema/SemaPPC.h"
61#include "clang/Sema/SemaSYCL.h"
63#include "clang/Sema/SemaWasm.h"
64#include "clang/Sema/SemaX86.h"
65#include "llvm/ADT/APSInt.h"
66#include "llvm/ADT/STLExtras.h"
67#include "llvm/ADT/StringExtras.h"
68#include "llvm/Demangle/Demangle.h"
69#include "llvm/IR/DerivedTypes.h"
70#include "llvm/MC/MCSectionMachO.h"
71#include "llvm/Support/Error.h"
72#include "llvm/Support/ErrorHandling.h"
73#include "llvm/Support/MathExtras.h"
74#include "llvm/Support/raw_ostream.h"
75#include "llvm/TargetParser/Triple.h"
76#include <optional>
77
78using namespace clang;
79using namespace sema;
80
82 enum LANG {
86 };
87} // end namespace AttributeLangSupport
88
89static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
90 // FIXME: Include the type in the argument list.
91 return AL.getNumArgs() + AL.hasParsedType();
92}
93
97
98/// Wrapper around checkUInt32Argument, with an extra check to be sure
99/// that the result will fit into a regular (signed) int. All args have the same
100/// purpose as they do in checkUInt32Argument.
101template <typename AttrInfo>
102static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
103 int &Val, unsigned Idx = UINT_MAX) {
104 uint32_t UVal;
105 if (!S.checkUInt32Argument(AI, Expr, UVal, Idx))
106 return false;
107
108 if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
109 llvm::APSInt I(32); // for toString
110 I = UVal;
111 S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
112 << toString(I, 10, false) << 32 << /* Unsigned */ 0;
113 return false;
114 }
115
116 Val = UVal;
117 return true;
118}
119
121 const Expr *E, StringRef &Str,
122 SourceLocation *ArgLocation) {
123 const auto *Literal = dyn_cast<StringLiteral>(E->IgnoreParenCasts());
124 if (ArgLocation)
125 *ArgLocation = E->getBeginLoc();
126
127 if (!Literal || (!Literal->isUnevaluated() && !Literal->isOrdinary())) {
128 Diag(E->getBeginLoc(), diag::err_attribute_argument_type)
129 << CI << AANT_ArgumentString;
130 return false;
131 }
132
133 Str = Literal->getString();
134 return true;
135}
136
137bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
138 StringRef &Str,
139 SourceLocation *ArgLocation) {
140 // Look for identifiers. If we have one emit a hint to fix it to a literal.
141 if (AL.isArgIdent(ArgNum)) {
142 IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
143 Diag(Loc->getLoc(), diag::err_attribute_argument_type)
144 << AL << AANT_ArgumentString
145 << FixItHint::CreateInsertion(Loc->getLoc(), "\"")
147 Str = Loc->getIdentifierInfo()->getName();
148 if (ArgLocation)
149 *ArgLocation = Loc->getLoc();
150 return true;
151 }
152
153 // Now check for an actual string literal.
154 Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
155 const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
156 if (ArgLocation)
157 *ArgLocation = ArgExpr->getBeginLoc();
158
159 if (!Literal || (!Literal->isUnevaluated() && !Literal->isOrdinary())) {
160 Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
161 << AL << AANT_ArgumentString;
162 return false;
163 }
164 Str = Literal->getString();
165 return checkStringLiteralArgumentAttr(AL, ArgExpr, Str, ArgLocation);
166}
167
168/// Check if the passed-in expression is of type int or bool.
169static bool isIntOrBool(Expr *Exp) {
170 QualType QT = Exp->getType();
171 return QT->isBooleanType() || QT->isIntegerType();
172}
173
174
175// Check to see if the type is a smart pointer of some kind. We assume
176// it's a smart pointer if it defines both operator-> and operator*.
178 auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
182 return !Result.empty();
183 };
184
185 bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
186 bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
187 if (foundStarOperator && foundArrowOperator)
188 return true;
189
190 const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
191 if (!CXXRecord)
192 return false;
193
194 for (const auto &BaseSpecifier : CXXRecord->bases()) {
195 if (!foundStarOperator)
196 foundStarOperator = IsOverloadedOperatorPresent(
197 BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
198 if (!foundArrowOperator)
199 foundArrowOperator = IsOverloadedOperatorPresent(
200 BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
201 }
202
203 if (foundStarOperator && foundArrowOperator)
204 return true;
205
206 return false;
207}
208
209/// Check if passed in Decl is a pointer type.
210/// Note that this function may produce an error message.
211/// \return true if the Decl is a pointer type; false otherwise
212static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
213 const ParsedAttr &AL) {
214 const auto *VD = cast<ValueDecl>(D);
215 QualType QT = VD->getType();
216 if (QT->isAnyPointerType())
217 return true;
218
219 if (const auto *RD = QT->getAsRecordDecl()) {
220 // If it's an incomplete type, it could be a smart pointer; skip it.
221 // (We don't want to force template instantiation if we can avoid it,
222 // since that would alter the order in which templates are instantiated.)
223 if (!RD->isCompleteDefinition())
224 return true;
225
227 return true;
228 }
229
230 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
231 return false;
232}
233
234/// Checks that the passed in QualType either is of RecordType or points
235/// to RecordType. Returns the relevant RecordType, null if it does not exit.
237 if (const auto *RD = QT->getAsRecordDecl())
238 return RD;
239
240 // Now check if we point to a record.
241 if (const auto *PT = QT->getAsCanonical<PointerType>())
242 return PT->getPointeeType()->getAsRecordDecl();
243
244 return nullptr;
245}
246
247template <typename AttrType>
248static bool checkRecordDeclForAttr(const RecordDecl *RD) {
249 // Check if the record itself has the attribute.
250 if (RD->hasAttr<AttrType>())
251 return true;
252
253 // Else check if any base classes have the attribute.
254 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
255 if (!CRD->forallBases([](const CXXRecordDecl *Base) {
256 return !Base->hasAttr<AttrType>();
257 }))
258 return true;
259 }
260 return false;
261}
262
264 const auto *RD = getRecordDecl(Ty);
265
266 if (!RD)
267 return false;
268
269 // Don't check for the capability if the class hasn't been defined yet.
270 if (!RD->isCompleteDefinition())
271 return true;
272
273 // Allow smart pointers to be used as capability objects.
274 // FIXME -- Check the type that the smart pointer points to.
276 return true;
277
279}
280
282 const auto *RD = getRecordDecl(Ty);
283
284 if (!RD)
285 return false;
286
287 // Don't check for the capability if the class hasn't been defined yet.
288 if (!RD->isCompleteDefinition())
289 return true;
290
292}
293
295 const auto *TD = Ty->getAs<TypedefType>();
296 if (!TD)
297 return false;
298
299 TypedefNameDecl *TN = TD->getDecl();
300 if (!TN)
301 return false;
302
303 return TN->hasAttr<CapabilityAttr>();
304}
305
306static bool typeHasCapability(Sema &S, QualType Ty) {
308 return true;
309
311 return true;
312
313 return false;
314}
315
316static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
317 // Capability expressions are simple expressions involving the boolean logic
318 // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
319 // a DeclRefExpr is found, its type should be checked to determine whether it
320 // is a capability or not.
321
322 if (const auto *E = dyn_cast<CastExpr>(Ex))
323 return isCapabilityExpr(S, E->getSubExpr());
324 else if (const auto *E = dyn_cast<ParenExpr>(Ex))
325 return isCapabilityExpr(S, E->getSubExpr());
326 else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
327 if (E->getOpcode() == UO_LNot || E->getOpcode() == UO_AddrOf ||
328 E->getOpcode() == UO_Deref)
329 return isCapabilityExpr(S, E->getSubExpr());
330 return false;
331 } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
332 if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr)
333 return isCapabilityExpr(S, E->getLHS()) &&
334 isCapabilityExpr(S, E->getRHS());
335 return false;
336 }
337
338 return typeHasCapability(S, Ex->getType());
339}
340
341/// Checks that all attribute arguments, starting from Sidx, resolve to
342/// a capability object.
343/// \param Sidx The attribute argument index to start checking with.
344/// \param ParamIdxOk Whether an argument can be indexing into a function
345/// parameter list.
347 const ParsedAttr &AL,
349 unsigned Sidx = 0,
350 bool ParamIdxOk = false) {
351 if (Sidx == AL.getNumArgs()) {
352 // If we don't have any capability arguments, the attribute implicitly
353 // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
354 // a non-static method, and that the class is a (scoped) capability.
355 const auto *MD = dyn_cast<const CXXMethodDecl>(D);
356 if (MD && !MD->isStatic()) {
357 const CXXRecordDecl *RD = MD->getParent();
358 // FIXME -- need to check this again on template instantiation
361 S.Diag(AL.getLoc(),
362 diag::warn_thread_attribute_not_on_capability_member)
363 << AL << MD->getParent();
364 } else {
365 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
366 << AL;
367 }
368 }
369
370 for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); ++Idx) {
371 Expr *ArgExp = AL.getArgAsExpr(Idx);
372
373 if (ArgExp->isTypeDependent()) {
374 // FIXME -- need to check this again on template instantiation
375 Args.push_back(ArgExp);
376 continue;
377 }
378
379 if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
380 if (StrLit->getLength() == 0 ||
381 (StrLit->isOrdinary() && StrLit->getString() == "*")) {
382 // Pass empty strings to the analyzer without warnings.
383 // Treat "*" as the universal lock.
384 Args.push_back(ArgExp);
385 continue;
386 }
387
388 // We allow constant strings to be used as a placeholder for expressions
389 // that are not valid C++ syntax, but warn that they are ignored.
390 S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
391 Args.push_back(ArgExp);
392 continue;
393 }
394
395 QualType ArgTy = ArgExp->getType();
396
397 // A pointer to member expression of the form &MyClass::mu is treated
398 // specially -- we need to look at the type of the member.
399 if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
400 if (UOp->getOpcode() == UO_AddrOf)
401 if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
402 if (DRE->getDecl()->isCXXInstanceMember())
403 ArgTy = DRE->getDecl()->getType();
404
405 // First see if we can just cast to record type, or pointer to record type.
406 const auto *RD = getRecordDecl(ArgTy);
407
408 // Now check if we index into a record type function param.
409 if (!RD && ParamIdxOk) {
410 const auto *FD = dyn_cast<FunctionDecl>(D);
411 const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
412 if(FD && IL) {
413 unsigned int NumParams = FD->getNumParams();
414 llvm::APInt ArgValue = IL->getValue();
415 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
416 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
417 if (!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
418 S.Diag(AL.getLoc(),
419 diag::err_attribute_argument_out_of_bounds_extra_info)
420 << AL << Idx + 1 << NumParams;
421 continue;
422 }
423 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
424 }
425 }
426
427 // If the type does not have a capability, see if the components of the
428 // expression have capabilities. This allows for writing C code where the
429 // capability may be on the type, and the expression is a capability
430 // boolean logic expression. Eg) requires_capability(A || B && !C)
431 if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp))
432 S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
433 << AL << ArgTy;
434
435 Args.push_back(ArgExp);
436 }
437}
438
439/// True if T names a function to call: a function pointer, a function
440/// reference, or a reference to a function pointer. Dependent types are also
441/// accepted, and re-checked after instantiation.
443 T = T.getNonReferenceType();
444 return T->isDependentType() || T->isFunctionPointerType() ||
445 T->isFunctionType();
446}
447
448/// Checks that thread-safety attributes on variables or fields apply only to
449/// function pointer or function reference types.
451 const AttributeCommonInfo &A) {
453 return true;
454 S.Diag(A.getLoc(), diag::warn_thread_attribute_not_on_fun_ptr)
455 << A << (isa<FieldDecl>(VD) ? 1 : 0);
456 return false;
457}
458
460 const ParmVarDecl *ParamDecl,
461 const AttributeCommonInfo &AL) {
462 QualType ParamType = ParamDecl->getType();
463 if (ParamType->isDependentType())
464 return true;
465 if (const auto *RefType = ParamType->getAs<ReferenceType>();
466 RefType &&
467 checkRecordTypeForScopedCapability(S, RefType->getPointeeType()))
468 return true;
469 S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_scoped_lockable_param)
470 << AL;
471 return false;
472}
473
474static bool checkThreadSafetyAttrSubject(Sema &S, Decl *D, const ParsedAttr &AL,
475 bool CheckParmVar = false) {
476 const auto *VD = dyn_cast<ValueDecl>(D);
477 if (!VD || isa<FunctionDecl>(VD))
478 return true;
479
480 if (CheckParmVar) {
481 if (const auto *PVD = dyn_cast<ParmVarDecl>(VD)) {
482 // A function-pointer or function-reference parameter is also valid here.
483 if (isCallbackOrDependent(PVD->getType()))
484 return true;
485 return checkFunParamsAreScopedLockable(S, PVD, AL);
486 }
487 }
488
489 return checkThreadSafetyValueDeclIsFunPtr(S, VD, AL);
490}
491
493 if (!isa<AssertCapabilityAttr, AcquireCapabilityAttr,
494 TryAcquireCapabilityAttr, ReleaseCapabilityAttr,
495 RequiresCapabilityAttr, LocksExcludedAttr>(A))
496 return true;
497
498 const auto *VD = dyn_cast<ValueDecl>(D);
499 if (!VD)
500 return true;
501
502 // Parameters of template functions need to be re-checked during
503 // instantiation because their types might have been dependent.
504 if (const auto *PVD = dyn_cast<ParmVarDecl>(VD)) {
505 if (isCallbackOrDependent(PVD->getType()))
506 return true;
507 return checkFunParamsAreScopedLockable(*this, PVD, *A);
508 }
509
510 if (isa<FunctionDecl>(VD))
511 return true;
512
513 return checkThreadSafetyValueDeclIsFunPtr(*this, VD, *A);
514}
515
516//===----------------------------------------------------------------------===//
517// Attribute Implementations
518//===----------------------------------------------------------------------===//
519
520static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
521 if (!threadSafetyCheckIsPointer(S, D, AL))
522 return;
523
524 D->addAttr(::new (S.Context) PtGuardedVarAttr(S.Context, AL));
525}
526
527static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
529 if (!AL.checkAtLeastNumArgs(S, 1))
530 return false;
531
532 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
533 return !Args.empty();
534}
535
536static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
538 if (!checkGuardedByAttrCommon(S, D, AL, Args))
539 return;
540
541 D->addAttr(::new (S.Context)
542 GuardedByAttr(S.Context, AL, Args.data(), Args.size()));
543}
544
545static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
547 if (!checkGuardedByAttrCommon(S, D, AL, Args))
548 return;
549
550 if (!threadSafetyCheckIsPointer(S, D, AL))
551 return;
552
553 D->addAttr(::new (S.Context)
554 PtGuardedByAttr(S.Context, AL, Args.data(), Args.size()));
555}
556
557static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
559 if (!AL.checkAtLeastNumArgs(S, 1))
560 return false;
561
562 // Check that this attribute only applies to lockable types.
563 QualType QT = cast<ValueDecl>(D)->getType();
564 if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
565 S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
566 return false;
567 }
568
569 // Check that all arguments are lockable objects.
570 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
571 if (Args.empty())
572 return false;
573
574 return true;
575}
576
577static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
579 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
580 return;
581
582 Expr **StartArg = &Args[0];
583 D->addAttr(::new (S.Context)
584 AcquiredAfterAttr(S.Context, AL, StartArg, Args.size()));
585}
586
587static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
589 if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
590 return;
591
592 Expr **StartArg = &Args[0];
593 D->addAttr(::new (S.Context)
594 AcquiredBeforeAttr(S.Context, AL, StartArg, Args.size()));
595}
596
597static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
599 // zero or more arguments ok
600 // check that all arguments are lockable objects
601 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
602
603 return true;
604}
605
606/// Checks to be sure that the given parameter number is in bounds, and
607/// is an integral type. Will emit appropriate diagnostics if this returns
608/// false.
609///
610/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
611template <typename AttrInfo>
612static bool checkParamIsIntegerType(Sema &S, const Decl *D, const AttrInfo &AI,
613 unsigned AttrArgNo) {
614 assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
615 Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
616 ParamIdx Idx;
617 if (!S.checkFunctionOrMethodParameterIndex(D, AI, AttrArgNo + 1, AttrArg,
618 Idx))
619 return false;
620
622 if (!ParamTy->isIntegerType() && !ParamTy->isCharType()) {
623 SourceLocation SrcLoc = AttrArg->getBeginLoc();
624 S.Diag(SrcLoc, diag::err_attribute_integers_only)
626 return false;
627 }
628 return true;
629}
630
631static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
632 if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 2))
633 return;
634
636
638 if (!RetTy->isPointerType()) {
639 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
640 return;
641 }
642
643 const Expr *SizeExpr = AL.getArgAsExpr(0);
644 int SizeArgNoVal;
645 // Parameter indices are 1-indexed, hence Index=1
646 if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1))
647 return;
648 if (!checkParamIsIntegerType(S, D, AL, /*AttrArgNo=*/0))
649 return;
650 ParamIdx SizeArgNo(SizeArgNoVal, D);
651
652 ParamIdx NumberArgNo;
653 if (AL.getNumArgs() == 2) {
654 const Expr *NumberExpr = AL.getArgAsExpr(1);
655 int Val;
656 // Parameter indices are 1-based, hence Index=2
657 if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2))
658 return;
659 if (!checkParamIsIntegerType(S, D, AL, /*AttrArgNo=*/1))
660 return;
661 NumberArgNo = ParamIdx(Val, D);
662 }
663
664 D->addAttr(::new (S.Context)
665 AllocSizeAttr(S.Context, AL, SizeArgNo, NumberArgNo));
666}
667
668static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
670 if (!AL.checkAtLeastNumArgs(S, 1))
671 return false;
672
673 if (!isIntOrBool(AL.getArgAsExpr(0))) {
674 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
675 << AL << 1 << AANT_ArgumentIntOrBool;
676 return false;
677 }
678
679 // check that all arguments are lockable objects
680 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
681
682 return true;
683}
684
685static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
686 // check that the argument is lockable object
688 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
689 unsigned Size = Args.size();
690 if (Size == 0)
691 return;
692
693 D->addAttr(::new (S.Context) LockReturnedAttr(S.Context, AL, Args[0]));
694}
695
696static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
697 if (!checkThreadSafetyAttrSubject(S, D, AL, /*CheckParmVar=*/true))
698 return;
699
700 if (!AL.checkAtLeastNumArgs(S, 1))
701 return;
702
703 // check that all arguments are lockable objects
705 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
706 unsigned Size = Args.size();
707 if (Size == 0)
708 return;
709 Expr **StartArg = &Args[0];
710
711 D->addAttr(::new (S.Context)
712 LocksExcludedAttr(S.Context, AL, StartArg, Size));
713}
714
715static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
716 Expr *&Cond, StringRef &Msg) {
717 Cond = AL.getArgAsExpr(0);
718 if (!Cond->isTypeDependent()) {
720 if (Converted.isInvalid())
721 return false;
722 Cond = Converted.get();
723 }
724
725 if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
726 return false;
727
728 if (Msg.empty())
729 Msg = "<no message provided>";
730
732 if (isa<FunctionDecl>(D) && !Cond->isValueDependent() &&
734 Diags)) {
735 S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
736 for (const PartialDiagnosticAt &PDiag : Diags)
737 S.Diag(PDiag.first, PDiag.second);
738 return false;
739 }
740 return true;
741}
742
743static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
744 S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
745
746 Expr *Cond;
747 StringRef Msg;
748 if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
749 D->addAttr(::new (S.Context) EnableIfAttr(S.Context, AL, Cond, Msg));
750}
751
752static void handleErrorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
753 StringRef NewUserDiagnostic;
754 if (!S.checkStringLiteralArgumentAttr(AL, 0, NewUserDiagnostic))
755 return;
756 if (ErrorAttr *EA = S.mergeErrorAttr(D, AL, NewUserDiagnostic))
757 D->addAttr(EA);
758}
759
761 const ParsedAttr &AL) {
762 const auto *PD = isa<CXXRecordDecl>(D)
765 if (const auto *RD = dyn_cast<CXXRecordDecl>(PD); RD && RD->isLocalClass()) {
766 S.Diag(AL.getLoc(),
767 diag::warn_attribute_exclude_from_explicit_instantiation_local_class)
768 << AL << /*IsMember=*/!isa<CXXRecordDecl>(D);
769 return;
770 }
771
772 if (auto *DA = getDLLAttr(D); DA && !DA->isInherited()) {
773 S.Diag(DA->getLoc(), diag::warn_dllattr_ignored_exclusion_takes_precedence)
774 << DA << AL;
775 D->dropAttrs<DLLExportAttr, DLLImportAttr>();
776 }
777
778 D->addAttr(::new (S.Context)
779 ExcludeFromExplicitInstantiationAttr(S.Context, AL));
780}
781
782namespace {
783/// Determines if a given Expr references any of the given function's
784/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
785class ArgumentDependenceChecker : public DynamicRecursiveASTVisitor {
786#ifndef NDEBUG
787 const CXXRecordDecl *ClassType;
788#endif
789 llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
790 bool Result;
791
792public:
793 ArgumentDependenceChecker(const FunctionDecl *FD) {
794#ifndef NDEBUG
795 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
796 ClassType = MD->getParent();
797 else
798 ClassType = nullptr;
799#endif
800 Parms.insert(FD->param_begin(), FD->param_end());
801 }
802
803 bool referencesArgs(Expr *E) {
804 Result = false;
805 TraverseStmt(E);
806 return Result;
807 }
808
809 bool VisitCXXThisExpr(CXXThisExpr *E) override {
810 assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
811 "`this` doesn't refer to the enclosing class?");
812 Result = true;
813 return false;
814 }
815
816 bool VisitDeclRefExpr(DeclRefExpr *DRE) override {
817 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
818 if (Parms.count(PVD)) {
819 Result = true;
820 return false;
821 }
822 return true;
823 }
824};
825}
826
828 const ParsedAttr &AL) {
829 const auto *DeclFD = cast<FunctionDecl>(D);
830
831 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(DeclFD))
832 if (!MethodDecl->isStatic()) {
833 S.Diag(AL.getLoc(), diag::err_attribute_no_member_function) << AL;
834 return;
835 }
836
837 auto DiagnoseType = [&](unsigned Index, AttributeArgumentNType T) {
838 SourceLocation Loc = [&]() {
839 auto Union = AL.getArg(Index - 1);
840 if (auto *E = dyn_cast<Expr *>(Union))
841 return E->getBeginLoc();
842 return cast<IdentifierLoc *>(Union)->getLoc();
843 }();
844
845 S.Diag(Loc, diag::err_attribute_argument_n_type) << AL << Index << T;
846 };
847
848 FunctionDecl *AttrFD = [&]() -> FunctionDecl * {
849 if (!AL.isArgExpr(0))
850 return nullptr;
851 auto *F = dyn_cast_if_present<DeclRefExpr>(AL.getArgAsExpr(0));
852 if (!F)
853 return nullptr;
854 return dyn_cast_if_present<FunctionDecl>(F->getFoundDecl());
855 }();
856
857 if (!AttrFD || !AttrFD->getBuiltinID(true)) {
858 DiagnoseType(1, AANT_ArgumentBuiltinFunction);
859 return;
860 }
861
862 if (AttrFD->getNumParams() != AL.getNumArgs() - 1) {
863 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments_for)
864 << AL << AttrFD << AttrFD->getNumParams();
865 return;
866 }
867
869
870 for (unsigned I = 1; I < AL.getNumArgs(); ++I) {
871 if (!AL.isArgExpr(I)) {
872 DiagnoseType(I + 1, AANT_ArgumentIntegerConstant);
873 return;
874 }
875
876 const Expr *IndexExpr = AL.getArgAsExpr(I);
877 uint32_t Index;
878
879 if (!S.checkUInt32Argument(AL, IndexExpr, Index, I + 1, false))
880 return;
881
882 if (Index > DeclFD->getNumParams()) {
883 S.Diag(AL.getLoc(), diag::err_attribute_bounds_for_function)
884 << AL << Index << DeclFD << DeclFD->getNumParams();
885 return;
886 }
887
888 QualType T1 = AttrFD->getParamDecl(I - 1)->getType();
889 QualType T2 = DeclFD->getParamDecl(Index - 1)->getType();
890
893 S.Diag(IndexExpr->getBeginLoc(), diag::err_attribute_parameter_types)
894 << AL << Index << DeclFD << T2 << I << AttrFD << T1;
895 return;
896 }
897
898 Indices.push_back(Index - 1);
899 }
900
901 D->addAttr(::new (S.Context) DiagnoseAsBuiltinAttr(
902 S.Context, AL, AttrFD, Indices.data(), Indices.size()));
903}
904
905static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
906 S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
907
908 Expr *Cond;
909 StringRef Msg;
910 if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
911 return;
912
913 StringRef DefaultSevStr;
914 if (!S.checkStringLiteralArgumentAttr(AL, 2, DefaultSevStr))
915 return;
916
917 DiagnoseIfAttr::DefaultSeverity DefaultSev;
918 if (!DiagnoseIfAttr::ConvertStrToDefaultSeverity(DefaultSevStr, DefaultSev)) {
919 S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
920 diag::err_diagnose_if_invalid_diagnostic_type);
921 return;
922 }
923
924 StringRef WarningGroup;
925 if (AL.getNumArgs() > 3) {
926 if (!S.checkStringLiteralArgumentAttr(AL, 3, WarningGroup))
927 return;
928 if (WarningGroup.empty() ||
929 !S.getDiagnostics().getDiagnosticIDs()->getGroupForWarningOption(
930 WarningGroup)) {
931 S.Diag(AL.getArgAsExpr(3)->getBeginLoc(),
932 diag::err_diagnose_if_unknown_warning)
933 << WarningGroup;
934 return;
935 }
936 }
937
938 bool ArgDependent = false;
939 if (const auto *FD = dyn_cast<FunctionDecl>(D))
940 ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
941 D->addAttr(::new (S.Context) DiagnoseIfAttr(
942 S.Context, AL, Cond, Msg, DefaultSev, WarningGroup, ArgDependent,
943 cast<NamedDecl>(D)));
944}
945
947 const ParsedAttr &Attrs) {
948 if (hasDeclarator(D))
949 return;
950
951 if (!isa<ObjCMethodDecl>(D)) {
952 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
953 << Attrs << Attrs.isRegularKeywordAttribute()
955 return;
956 }
957
958 D->addAttr(::new (S.Context) CFIUncheckedCalleeAttr(S.Context, Attrs));
959}
960
961static void handleNoBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
962 static constexpr const StringRef kWildcard = "*";
963
965 bool HasWildcard = false;
966
967 const auto AddBuiltinName = [&Names, &HasWildcard](StringRef Name) {
968 if (Name == kWildcard)
969 HasWildcard = true;
970 Names.push_back(Name);
971 };
972
973 // Add previously defined attributes.
974 if (const auto *NBA = D->getAttr<NoBuiltinAttr>())
975 for (StringRef BuiltinName : NBA->builtinNames())
976 AddBuiltinName(BuiltinName);
977
978 // Add current attributes.
979 if (AL.getNumArgs() == 0)
980 AddBuiltinName(kWildcard);
981 else
982 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
983 StringRef BuiltinName;
984 SourceLocation LiteralLoc;
985 if (!S.checkStringLiteralArgumentAttr(AL, I, BuiltinName, &LiteralLoc))
986 return;
987
988 if (Builtin::Context::isBuiltinFunc(BuiltinName))
989 AddBuiltinName(BuiltinName);
990 else
991 S.Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name)
992 << BuiltinName << AL;
993 }
994
995 // Repeating the same attribute is fine.
996 llvm::sort(Names);
997 Names.erase(llvm::unique(Names), Names.end());
998
999 // Empty no_builtin must be on its own.
1000 if (HasWildcard && Names.size() > 1)
1001 S.Diag(D->getLocation(),
1002 diag::err_attribute_no_builtin_wildcard_or_builtin_name)
1003 << AL;
1004
1005 if (D->hasAttr<NoBuiltinAttr>())
1006 D->dropAttr<NoBuiltinAttr>();
1007 D->addAttr(::new (S.Context)
1008 NoBuiltinAttr(S.Context, AL, Names.data(), Names.size()));
1009}
1010
1011static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1012 if (D->hasAttr<PassObjectSizeAttr>()) {
1013 S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
1014 return;
1015 }
1016
1017 Expr *E = AL.getArgAsExpr(0);
1018 uint32_t Type;
1019 if (!S.checkUInt32Argument(AL, E, Type, /*Idx=*/1))
1020 return;
1021
1022 // pass_object_size's argument is passed in as the second argument of
1023 // __builtin_object_size. So, it has the same constraints as that second
1024 // argument; namely, it must be in the range [0, 3].
1025 if (Type > 3) {
1026 S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
1027 << AL << 0 << 3 << E->getSourceRange();
1028 return;
1029 }
1030
1031 // pass_object_size is only supported on constant pointer parameters; as a
1032 // kindness to users, we allow the parameter to be non-const for declarations.
1033 // At this point, we have no clue if `D` belongs to a function declaration or
1034 // definition, so we defer the constness check until later.
1035 if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
1036 S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
1037 return;
1038 }
1039
1040 D->addAttr(::new (S.Context) PassObjectSizeAttr(S.Context, AL, (int)Type));
1041}
1042
1043static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1044 ConsumableAttr::ConsumedState DefaultState;
1045
1046 if (AL.isArgIdent(0)) {
1047 IdentifierLoc *IL = AL.getArgAsIdent(0);
1048 if (!ConsumableAttr::ConvertStrToConsumedState(
1049 IL->getIdentifierInfo()->getName(), DefaultState)) {
1050 S.Diag(IL->getLoc(), diag::warn_attribute_type_not_supported)
1051 << AL << IL->getIdentifierInfo();
1052 return;
1053 }
1054 } else {
1055 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1056 << AL << AANT_ArgumentIdentifier;
1057 return;
1058 }
1059
1060 D->addAttr(::new (S.Context) ConsumableAttr(S.Context, AL, DefaultState));
1061}
1062
1064 const ParsedAttr &AL) {
1066
1067 if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1068 if (!RD->hasAttr<ConsumableAttr>()) {
1069 S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << RD;
1070
1071 return false;
1072 }
1073 }
1074
1075 return true;
1076}
1077
1078static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1079 if (!AL.checkAtLeastNumArgs(S, 1))
1080 return;
1081
1083 return;
1084
1086 for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); ++ArgIndex) {
1087 CallableWhenAttr::ConsumedState CallableState;
1088
1089 StringRef StateString;
1090 SourceLocation Loc;
1091 if (AL.isArgIdent(ArgIndex)) {
1092 IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
1093 StateString = Ident->getIdentifierInfo()->getName();
1094 Loc = Ident->getLoc();
1095 } else {
1096 if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
1097 return;
1098 }
1099
1100 if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
1101 CallableState)) {
1102 S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
1103 return;
1104 }
1105
1106 States.push_back(CallableState);
1107 }
1108
1109 D->addAttr(::new (S.Context)
1110 CallableWhenAttr(S.Context, AL, States.data(), States.size()));
1111}
1112
1113static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1114 ParamTypestateAttr::ConsumedState ParamState;
1115
1116 if (AL.isArgIdent(0)) {
1117 IdentifierLoc *Ident = AL.getArgAsIdent(0);
1118 StringRef StateString = Ident->getIdentifierInfo()->getName();
1119
1120 if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1121 ParamState)) {
1122 S.Diag(Ident->getLoc(), diag::warn_attribute_type_not_supported)
1123 << AL << StateString;
1124 return;
1125 }
1126 } else {
1127 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1128 << AL << AANT_ArgumentIdentifier;
1129 return;
1130 }
1131
1132 // FIXME: This check is currently being done in the analysis. It can be
1133 // enabled here only after the parser propagates attributes at
1134 // template specialization definition, not declaration.
1135 //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1136 //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1137 //
1138 //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1139 // S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1140 // ReturnType.getAsString();
1141 // return;
1142 //}
1143
1144 D->addAttr(::new (S.Context) ParamTypestateAttr(S.Context, AL, ParamState));
1145}
1146
1147static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1148 ReturnTypestateAttr::ConsumedState ReturnState;
1149
1150 if (AL.isArgIdent(0)) {
1151 IdentifierLoc *IL = AL.getArgAsIdent(0);
1152 if (!ReturnTypestateAttr::ConvertStrToConsumedState(
1153 IL->getIdentifierInfo()->getName(), ReturnState)) {
1154 S.Diag(IL->getLoc(), diag::warn_attribute_type_not_supported)
1155 << AL << IL->getIdentifierInfo();
1156 return;
1157 }
1158 } else {
1159 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1160 << AL << AANT_ArgumentIdentifier;
1161 return;
1162 }
1163
1164 // FIXME: This check is currently being done in the analysis. It can be
1165 // enabled here only after the parser propagates attributes at
1166 // template specialization definition, not declaration.
1167 // QualType ReturnType;
1168 //
1169 // if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1170 // ReturnType = Param->getType();
1171 //
1172 //} else if (const CXXConstructorDecl *Constructor =
1173 // dyn_cast<CXXConstructorDecl>(D)) {
1174 // ReturnType = Constructor->getFunctionObjectParameterType();
1175 //
1176 //} else {
1177 //
1178 // ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1179 //}
1180 //
1181 // const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1182 //
1183 // if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1184 // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1185 // ReturnType.getAsString();
1186 // return;
1187 //}
1188
1189 D->addAttr(::new (S.Context) ReturnTypestateAttr(S.Context, AL, ReturnState));
1190}
1191
1192static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1194 return;
1195
1196 SetTypestateAttr::ConsumedState NewState;
1197 if (AL.isArgIdent(0)) {
1198 IdentifierLoc *Ident = AL.getArgAsIdent(0);
1199 StringRef Param = Ident->getIdentifierInfo()->getName();
1200 if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1201 S.Diag(Ident->getLoc(), diag::warn_attribute_type_not_supported)
1202 << AL << Param;
1203 return;
1204 }
1205 } else {
1206 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1207 << AL << AANT_ArgumentIdentifier;
1208 return;
1209 }
1210
1211 D->addAttr(::new (S.Context) SetTypestateAttr(S.Context, AL, NewState));
1212}
1213
1214static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1216 return;
1217
1218 TestTypestateAttr::ConsumedState TestState;
1219 if (AL.isArgIdent(0)) {
1220 IdentifierLoc *Ident = AL.getArgAsIdent(0);
1221 StringRef Param = Ident->getIdentifierInfo()->getName();
1222 if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
1223 S.Diag(Ident->getLoc(), diag::warn_attribute_type_not_supported)
1224 << AL << Param;
1225 return;
1226 }
1227 } else {
1228 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1229 << AL << AANT_ArgumentIdentifier;
1230 return;
1231 }
1232
1233 D->addAttr(::new (S.Context) TestTypestateAttr(S.Context, AL, TestState));
1234}
1235
1236static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1237 // Remember this typedef decl, we will need it later for diagnostics.
1238 if (isa<TypedefNameDecl>(D))
1240}
1241
1242static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1243 if (auto *TD = dyn_cast<TagDecl>(D))
1244 TD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1245 else if (auto *FD = dyn_cast<FieldDecl>(D)) {
1246 bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1247 !FD->getType()->isIncompleteType() &&
1248 FD->isBitField() &&
1249 S.Context.getTypeAlign(FD->getType()) <= 8);
1250
1251 if (S.getASTContext().getTargetInfo().getTriple().isPS()) {
1252 if (BitfieldByteAligned)
1253 // The PS4/PS5 targets need to maintain ABI backwards compatibility.
1254 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
1255 << AL << FD->getType();
1256 else
1257 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1258 } else {
1259 // Report warning about changed offset in the newer compiler versions.
1260 if (BitfieldByteAligned)
1261 S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
1262
1263 FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1264 }
1265
1266 } else
1267 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
1268}
1269
1270static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
1271 auto *RD = cast<CXXRecordDecl>(D);
1272 ClassTemplateDecl *CTD = RD->getDescribedClassTemplate();
1273 assert(CTD && "attribute does not appertain to this declaration");
1274
1275 ParsedType PT = AL.getTypeArg();
1276 TypeSourceInfo *TSI = nullptr;
1277 QualType T = S.GetTypeFromParser(PT, &TSI);
1278 if (!TSI)
1280
1281 if (!T.hasQualifiers() && T->isTypedefNameType()) {
1282 // Find the template name, if this type names a template specialization.
1283 const TemplateDecl *Template = nullptr;
1284 if (const auto *CTSD = dyn_cast_if_present<ClassTemplateSpecializationDecl>(
1285 T->getAsCXXRecordDecl())) {
1286 Template = CTSD->getSpecializedTemplate();
1287 } else if (const auto *TST = T->getAs<TemplateSpecializationType>()) {
1288 while (TST && TST->isTypeAlias())
1289 TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
1290 if (TST)
1291 Template = TST->getTemplateName().getAsTemplateDecl();
1292 }
1293
1294 if (Template && declaresSameEntity(Template, CTD)) {
1295 D->addAttr(::new (S.Context) PreferredNameAttr(S.Context, AL, TSI));
1296 return;
1297 }
1298 }
1299
1300 S.Diag(AL.getLoc(), diag::err_attribute_not_typedef_for_specialization)
1301 << T << AL << CTD;
1302 if (const auto *TT = T->getAs<TypedefType>())
1303 S.Diag(TT->getDecl()->getLocation(), diag::note_entity_declared_at)
1304 << TT->getDecl();
1305}
1306
1307static void handleNoSpecializations(Sema &S, Decl *D, const ParsedAttr &AL) {
1308 StringRef Message;
1309 if (AL.getNumArgs() != 0)
1310 S.checkStringLiteralArgumentAttr(AL, 0, Message);
1312 NoSpecializationsAttr::Create(S.Context, Message, AL));
1313}
1314
1316 if (T->isDependentType())
1317 return true;
1318 if (RefOkay) {
1319 if (T->isReferenceType())
1320 return true;
1321 } else {
1322 T = T.getNonReferenceType();
1323 }
1324
1325 // The nonnull attribute, and other similar attributes, can be applied to a
1326 // transparent union that contains a pointer type.
1327 if (const RecordType *UT = T->getAsUnionType()) {
1328 RecordDecl *UD = UT->getDecl()->getDefinitionOrSelf();
1329 if (UD->hasAttr<TransparentUnionAttr>()) {
1330 for (const auto *I : UD->fields()) {
1331 QualType QT = I->getType();
1332 if (QT->isAnyPointerType() || QT->isBlockPointerType())
1333 return true;
1334 }
1335 }
1336 }
1337
1338 return T->isAnyPointerType() || T->isBlockPointerType();
1339}
1340
1341static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
1342 SourceRange AttrParmRange,
1343 SourceRange TypeRange,
1344 bool isReturnValue = false) {
1345 if (!S.isValidPointerAttrType(T)) {
1346 if (isReturnValue)
1347 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1348 << AL << AttrParmRange << TypeRange;
1349 else
1350 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1351 << AL << AttrParmRange << TypeRange << 0;
1352 return false;
1353 }
1354 return true;
1355}
1356
1357static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1358 SmallVector<ParamIdx, 8> NonNullArgs;
1359 for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
1360 Expr *Ex = AL.getArgAsExpr(I);
1361 ParamIdx Idx;
1363 D, AL, I + 1, Ex, Idx,
1364 /*CanIndexImplicitThis=*/false,
1365 /*CanIndexVariadicArguments=*/true))
1366 return;
1367
1368 // Is the function argument a pointer type?
1372 Ex->getSourceRange(),
1374 continue;
1375
1376 NonNullArgs.push_back(Idx);
1377 }
1378
1379 // If no arguments were specified to __attribute__((nonnull)) then all pointer
1380 // arguments have a nonnull attribute; warn if there aren't any. Skip this
1381 // check if the attribute came from a macro expansion or a template
1382 // instantiation.
1383 if (NonNullArgs.empty() && AL.getLoc().isFileID() &&
1385 bool AnyPointers = isFunctionOrMethodVariadic(D);
1386 for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1387 I != E && !AnyPointers; ++I) {
1390 AnyPointers = true;
1391 }
1392
1393 if (!AnyPointers)
1394 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
1395 }
1396
1397 ParamIdx *Start = NonNullArgs.data();
1398 unsigned Size = NonNullArgs.size();
1399 llvm::array_pod_sort(Start, Start + Size);
1400 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, Start, Size));
1401}
1402
1404 const ParsedAttr &AL) {
1405 if (AL.getNumArgs() > 0) {
1406 if (D->getFunctionType()) {
1407 handleNonNullAttr(S, D, AL);
1408 } else {
1409 S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1410 << D->getSourceRange();
1411 }
1412 return;
1413 }
1414
1415 // Is the argument a pointer type?
1416 if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
1417 D->getSourceRange()))
1418 return;
1419
1420 D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, nullptr, 0));
1421}
1422
1423static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1426 if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
1427 /* isReturnValue */ true))
1428 return;
1429
1430 D->addAttr(::new (S.Context) ReturnsNonNullAttr(S.Context, AL));
1431}
1432
1433static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1434 if (D->isInvalidDecl())
1435 return;
1436
1437 // noescape only applies to pointer types.
1438 QualType T = cast<ParmVarDecl>(D)->getType();
1439 if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
1440 S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1441 << AL << AL.getRange() << 0;
1442 return;
1443 }
1444
1445 D->addAttr(::new (S.Context) NoEscapeAttr(S.Context, AL));
1446}
1447
1448static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1449 Expr *E = AL.getArgAsExpr(0),
1450 *OE = AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr;
1451 S.AddAssumeAlignedAttr(D, AL, E, OE);
1452}
1453
1454static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1455 S.AddAllocAlignAttr(D, AL, AL.getArgAsExpr(0));
1456}
1457
1459 Expr *OE) {
1462 SourceLocation AttrLoc = CI.getLoc();
1463
1464 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1465 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1466 << CI << CI.getRange() << SR;
1467 return;
1468 }
1469
1470 if (!E->isValueDependent()) {
1471 std::optional<llvm::APSInt> I = llvm::APSInt(64);
1472 if (!(I = E->getIntegerConstantExpr(Context))) {
1473 if (OE)
1474 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1475 << CI << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
1476 else
1477 Diag(AttrLoc, diag::err_attribute_argument_type)
1479 return;
1480 }
1481
1482 if (!I->isPowerOf2()) {
1483 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1484 << E->getSourceRange();
1485 return;
1486 }
1487
1488 if (*I > Sema::MaximumAlignment)
1489 Diag(CI.getLoc(), diag::warn_assume_aligned_too_great)
1491 }
1492
1493 if (OE && !OE->isValueDependent() && !OE->isIntegerConstantExpr(Context)) {
1494 Diag(AttrLoc, diag::err_attribute_argument_n_type)
1495 << CI << 2 << AANT_ArgumentIntegerConstant << OE->getSourceRange();
1496 return;
1497 }
1498
1499 D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE));
1500}
1501
1503 Expr *ParamExpr) {
1505 SourceLocation AttrLoc = CI.getLoc();
1506
1507 if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1508 Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1510 return;
1511 }
1512
1513 ParamIdx Idx;
1514 const auto *FuncDecl = cast<FunctionDecl>(D);
1515 if (!checkFunctionOrMethodParameterIndex(FuncDecl, CI,
1516 /*AttrArgNum=*/1, ParamExpr, Idx))
1517 return;
1518
1520 if (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1521 !Ty->isAlignValT()) {
1522 Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
1523 << CI << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
1524 return;
1525 }
1526
1527 D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx));
1528}
1529
1530/// Normalize the attribute, __foo__ becomes foo.
1531/// Returns true if normalization was applied.
1532static bool normalizeName(StringRef &AttrName) {
1533 if (AttrName.size() > 4 && AttrName.starts_with("__") &&
1534 AttrName.ends_with("__")) {
1535 AttrName = AttrName.drop_front(2).drop_back(2);
1536 return true;
1537 }
1538 return false;
1539}
1540
1541static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1542 // This attribute must be applied to a function declaration. The first
1543 // argument to the attribute must be an identifier, the name of the resource,
1544 // for example: malloc. The following arguments must be argument indexes, the
1545 // arguments must be of integer type for Returns, otherwise of pointer type.
1546 // The difference between Holds and Takes is that a pointer may still be used
1547 // after being held. free() should be __attribute((ownership_takes)), whereas
1548 // a list append function may well be __attribute((ownership_holds)).
1549
1550 if (!AL.isArgIdent(0)) {
1551 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
1552 << AL << 1 << AANT_ArgumentIdentifier;
1553 return;
1554 }
1555
1556 // Figure out our Kind.
1557 OwnershipAttr::OwnershipKind K =
1558 OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind();
1559
1560 // Check arguments.
1561 switch (K) {
1562 case OwnershipAttr::Takes:
1563 case OwnershipAttr::Holds:
1564 if (AL.getNumArgs() < 2) {
1565 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
1566 return;
1567 }
1568 break;
1569 case OwnershipAttr::Returns:
1570 if (AL.getNumArgs() > 2) {
1571 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 2;
1572 return;
1573 }
1574 break;
1575 }
1576
1577 // Allow only pointers to be return type for functions with ownership_returns
1578 // attribute. This matches with current OwnershipAttr::Takes semantics
1579 if (K == OwnershipAttr::Returns &&
1580 !getFunctionOrMethodResultType(D)->isPointerType()) {
1581 S.Diag(AL.getLoc(), diag::err_ownership_takes_return_type) << AL;
1582 return;
1583 }
1584
1586
1587 StringRef ModuleName = Module->getName();
1588 if (normalizeName(ModuleName)) {
1589 Module = &S.PP.getIdentifierTable().get(ModuleName);
1590 }
1591
1592 // Check if the new ownership_returns attribute does not contain
1593 // an index, but previous attributes do.
1594 if (K == OwnershipAttr::Returns && AL.getNumArgs() == 1) {
1595 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
1596 if (I->getOwnKind() == OwnershipAttr::Returns && I->args_size() > 0) {
1597 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1598 << I->args_begin()->getSourceIndex() << 0;
1599 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1600 << 0 << 1;
1601 return;
1602 }
1603 }
1604 }
1605
1606 SmallVector<ParamIdx, 8> OwnershipArgs;
1607 for (unsigned i = 1; i < AL.getNumArgs(); ++i) {
1608 Expr *Ex = AL.getArgAsExpr(i);
1609 ParamIdx Idx;
1610 if (!S.checkFunctionOrMethodParameterIndex(D, AL, i, Ex, Idx))
1611 return;
1612
1613 // Is the function argument a pointer type?
1615 int Err = -1; // No error
1616 switch (K) {
1617 case OwnershipAttr::Takes:
1618 case OwnershipAttr::Holds:
1619 if (!T->isAnyPointerType() && !T->isBlockPointerType())
1620 Err = 0;
1621 break;
1622 case OwnershipAttr::Returns:
1623 if (!T->isIntegerType())
1624 Err = 1;
1625 break;
1626 }
1627 if (-1 != Err) {
1628 S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1629 << Ex->getSourceRange();
1630 return;
1631 }
1632
1633 // Check we don't have a conflict with another ownership attribute.
1634 for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
1635 // Cannot have two ownership attributes of different kinds for the same
1636 // index.
1637 if (I->getOwnKind() != K && llvm::is_contained(I->args(), Idx)) {
1638 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1639 << AL << I
1640 << (AL.isRegularKeywordAttribute() ||
1641 I->isRegularKeywordAttribute());
1642 return;
1643 }
1644
1645 if (K == OwnershipAttr::Returns &&
1646 I->getOwnKind() == OwnershipAttr::Returns) {
1647 bool IHasArgs = I->args_size() > 0;
1648
1649 if (!IHasArgs || !llvm::is_contained(I->args(), Idx)) {
1650 unsigned IIdx = IHasArgs ? I->args_begin()->getSourceIndex() : 0;
1651
1652 S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1653 << IIdx << (IHasArgs ? 0 : 1);
1654
1655 S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1656 << Idx.getSourceIndex() << 0 << Ex->getSourceRange();
1657 return;
1658 }
1659 } else if (K == OwnershipAttr::Takes &&
1660 I->getOwnKind() == OwnershipAttr::Takes) {
1661 if (I->getModule()->getName() != ModuleName) {
1662 S.Diag(I->getLocation(), diag::err_ownership_takes_class_mismatch)
1663 << I->getModule()->getName();
1664 S.Diag(AL.getLoc(), diag::note_ownership_takes_class_mismatch)
1665 << ModuleName << Ex->getSourceRange();
1666
1667 return;
1668 }
1669 }
1670 }
1671 OwnershipArgs.push_back(Idx);
1672 }
1673
1674 ParamIdx *Start = OwnershipArgs.data();
1675 unsigned Size = OwnershipArgs.size();
1676 llvm::array_pod_sort(Start, Start + Size);
1677 D->addAttr(::new (S.Context)
1678 OwnershipAttr(S.Context, AL, Module, Start, Size));
1679}
1680
1681static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1682 // Check the attribute arguments.
1683 if (AL.getNumArgs() > 1) {
1684 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1685 return;
1686 }
1687
1688 // gcc rejects
1689 // class c {
1690 // static int a __attribute__((weakref ("v2")));
1691 // static int b() __attribute__((weakref ("f3")));
1692 // };
1693 // and ignores the attributes of
1694 // void f(void) {
1695 // static int a __attribute__((weakref ("v2")));
1696 // }
1697 // we reject them
1698 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
1699 if (!Ctx->isFileContext()) {
1700 S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1701 << cast<NamedDecl>(D);
1702 return;
1703 }
1704
1705 // The GCC manual says
1706 //
1707 // At present, a declaration to which `weakref' is attached can only
1708 // be `static'.
1709 //
1710 // It also says
1711 //
1712 // Without a TARGET,
1713 // given as an argument to `weakref' or to `alias', `weakref' is
1714 // equivalent to `weak'.
1715 //
1716 // gcc 4.4.1 will accept
1717 // int a7 __attribute__((weakref));
1718 // as
1719 // int a7 __attribute__((weak));
1720 // This looks like a bug in gcc. We reject that for now. We should revisit
1721 // it if this behaviour is actually used.
1722
1723 // GCC rejects
1724 // static ((alias ("y"), weakref)).
1725 // Should we? How to check that weakref is before or after alias?
1726
1727 // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1728 // of transforming it into an AliasAttr. The WeakRefAttr never uses the
1729 // StringRef parameter it was given anyway.
1730 StringRef Str;
1731 if (AL.getNumArgs() && S.checkStringLiteralArgumentAttr(AL, 0, Str))
1732 // GCC will accept anything as the argument of weakref. Should we
1733 // check for an existing decl?
1734 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
1735
1736 D->addAttr(::new (S.Context) WeakRefAttr(S.Context, AL));
1737}
1738
1739// Mark alias/ifunc target as used. Due to name mangling, we look up the
1740// demangled name ignoring parameters (not supported by microsoftDemangle
1741// https://github.com/llvm/llvm-project/issues/88825). This should handle the
1742// majority of use cases while leaving namespace scope names unmarked.
1743static void markUsedForAliasOrIfunc(Sema &S, Decl *D, const ParsedAttr &AL,
1744 StringRef Str) {
1745 std::unique_ptr<char, llvm::FreeDeleter> Demangled;
1746 if (S.getASTContext().getCXXABIKind() != TargetCXXABI::Microsoft)
1747 Demangled.reset(llvm::itaniumDemangle(Str, /*ParseParams=*/false));
1748 std::unique_ptr<MangleContext> MC(S.Context.createMangleContext());
1749 SmallString<256> Name;
1750
1752 &S.Context.Idents.get(Demangled ? Demangled.get() : Str), AL.getLoc());
1754 if (S.LookupName(LR, S.TUScope)) {
1755 for (NamedDecl *ND : LR) {
1756 if (!isa<FunctionDecl>(ND) && !isa<VarDecl>(ND))
1757 continue;
1758 if (MC->shouldMangleDeclName(ND)) {
1759 llvm::raw_svector_ostream Out(Name);
1760 Name.clear();
1761 MC->mangleName(GlobalDecl(ND), Out);
1762 } else {
1763 Name = ND->getIdentifier()->getName();
1764 }
1765 if (Name == Str)
1766 ND->markUsed(S.Context);
1767 }
1768 }
1769}
1770
1771static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1772 StringRef Str;
1773 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1774 return;
1775
1776 // Aliases should be on declarations, not definitions.
1777 const auto *FD = cast<FunctionDecl>(D);
1778 if (FD->isThisDeclarationADefinition()) {
1779 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
1780 return;
1781 }
1782
1783 markUsedForAliasOrIfunc(S, D, AL, Str);
1784 D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str));
1785}
1786
1787static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1788 StringRef Str;
1789 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1790 return;
1791
1792 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
1793 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
1794 return;
1795 }
1796
1797 if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
1798 CudaVersion Version =
1800 if (Version != CudaVersion::UNKNOWN && Version < CudaVersion::CUDA_100)
1801 S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
1802 }
1803
1804 // Aliases should be on declarations, not definitions.
1805 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1806 if (FD->isThisDeclarationADefinition()) {
1807 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
1808 return;
1809 }
1810 } else {
1811 const auto *VD = cast<VarDecl>(D);
1812 if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {
1813 S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
1814 return;
1815 }
1816 }
1817
1818 markUsedForAliasOrIfunc(S, D, AL, Str);
1819 D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
1820}
1821
1822static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1823 StringRef Model;
1824 SourceLocation LiteralLoc;
1825 // Check that it is a string.
1826 if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
1827 return;
1828
1829 // Check that the value.
1830 if (Model != "global-dynamic" && Model != "local-dynamic"
1831 && Model != "initial-exec" && Model != "local-exec") {
1832 S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
1833 return;
1834 }
1835
1836 D->addAttr(::new (S.Context) TLSModelAttr(S.Context, AL, Model));
1837}
1838
1839static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1841 if (!ResultType->isAnyPointerType() && !ResultType->isBlockPointerType()) {
1842 S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1844 return;
1845 }
1846
1847 if (AL.getNumArgs() == 0) {
1848 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
1849 return;
1850 }
1851
1852 if (AL.getAttributeSpellingListIndex() == RestrictAttr::Declspec_restrict) {
1853 // __declspec(restrict) accepts no arguments
1854 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 0;
1855 return;
1856 }
1857
1858 // [[gnu::malloc(deallocator)]] with args specifies a deallocator function
1859 Expr *DeallocE = AL.getArgAsExpr(0);
1860 SourceLocation DeallocLoc = DeallocE->getExprLoc();
1861 FunctionDecl *DeallocFD = nullptr;
1862 DeclarationNameInfo DeallocNI;
1863
1864 if (auto *DRE = dyn_cast<DeclRefExpr>(DeallocE)) {
1865 DeallocFD = dyn_cast<FunctionDecl>(DRE->getDecl());
1866 DeallocNI = DRE->getNameInfo();
1867 if (!DeallocFD) {
1868 S.Diag(DeallocLoc, diag::err_attribute_malloc_arg_not_function)
1869 << 1 << DeallocNI.getName();
1870 return;
1871 }
1872 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(DeallocE)) {
1873 DeallocFD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
1874 DeallocNI = ULE->getNameInfo();
1875 if (!DeallocFD) {
1876 S.Diag(DeallocLoc, diag::err_attribute_malloc_arg_not_function)
1877 << 2 << DeallocNI.getName();
1878 if (ULE->getType() == S.Context.OverloadTy)
1880 return;
1881 }
1882 } else {
1883 S.Diag(DeallocLoc, diag::err_attribute_malloc_arg_not_function) << 0;
1884 return;
1885 }
1886
1887 // 2nd arg of [[gnu::malloc(deallocator, 2)]] with args specifies the param
1888 // of deallocator that deallocates the pointer (defaults to 1)
1889 ParamIdx DeallocPtrIdx;
1890 if (AL.getNumArgs() == 1) {
1891 DeallocPtrIdx = ParamIdx(1, DeallocFD);
1892
1893 // FIXME: We could probably be better about diagnosing that there IS no
1894 // argument, or that the function doesn't have a prototype, but this is how
1895 // GCC diagnoses this, and is reasonably clear.
1896 if (!DeallocPtrIdx.isValid() || !hasFunctionProto(DeallocFD) ||
1897 getFunctionOrMethodNumParams(DeallocFD) < 1 ||
1898 !getFunctionOrMethodParamType(DeallocFD, DeallocPtrIdx.getASTIndex())
1900 ->isPointerType()) {
1901 S.Diag(DeallocLoc,
1902 diag::err_attribute_malloc_arg_not_function_with_pointer_arg)
1903 << DeallocNI.getName();
1904 return;
1905 }
1906 } else {
1908 DeallocFD, AL, 2, AL.getArgAsExpr(1), DeallocPtrIdx,
1909 /* CanIndexImplicitThis=*/false))
1910 return;
1911
1912 QualType DeallocPtrArgType =
1913 getFunctionOrMethodParamType(DeallocFD, DeallocPtrIdx.getASTIndex());
1914 if (!DeallocPtrArgType.getCanonicalType()->isPointerType()) {
1915 S.Diag(DeallocLoc,
1916 diag::err_attribute_malloc_arg_refers_to_non_pointer_type)
1917 << DeallocPtrIdx.getSourceIndex() << DeallocPtrArgType
1918 << DeallocNI.getName();
1919 return;
1920 }
1921 }
1922
1923 // FIXME: we should add this attribute to Clang's AST, so that clang-analyzer
1924 // can use it, see -Wmismatched-dealloc in GCC for what we can do with this.
1925 S.Diag(AL.getLoc(), diag::warn_attribute_form_ignored) << AL;
1926 D->addAttr(::new (S.Context)
1927 RestrictAttr(S.Context, AL, DeallocE, DeallocPtrIdx));
1928}
1929
1931 const QualType &Ty) {
1932 // Note that there may also be numerous cases of pointer + integer /
1933 // pointer + pointer / integer + pointer structures not actually exhibiting
1934 // a span-like semantics, so sometimes these heuristics expectedly
1935 // lead to false positive results.
1936 auto emitWarning = [this, &CI](unsigned NoteDiagID) {
1937 Diag(CI.getLoc(), diag::warn_attribute_return_span_only) << CI;
1938 return Diag(CI.getLoc(), NoteDiagID);
1939 };
1940 if (Ty->isDependentType())
1941 return false;
1942 // isCompleteType is used to force template class instantiation.
1943 if (!isCompleteType(CI.getLoc(), Ty))
1944 return emitWarning(diag::note_returned_incomplete_type);
1945 const RecordDecl *RD = Ty->getAsRecordDecl();
1946 if (!RD || RD->isUnion())
1947 return emitWarning(diag::note_returned_not_struct);
1948 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1949 if (CXXRD->getNumBases() > 0) {
1950 return emitWarning(diag::note_type_inherits_from_base);
1951 }
1952 }
1953 auto FieldsBegin = RD->field_begin();
1954 auto FieldsCount = std::distance(FieldsBegin, RD->field_end());
1955 if (FieldsCount != 2)
1956 return emitWarning(diag::note_returned_not_two_field_struct) << FieldsCount;
1957 QualType FirstFieldType = FieldsBegin->getType();
1958 QualType SecondFieldType = std::next(FieldsBegin)->getType();
1959 auto validatePointerType = [](const QualType &T) {
1960 // It must not point to functions.
1961 return T->isPointerType() && !T->isFunctionPointerType();
1962 };
1963 auto checkIntegerType = [this, emitWarning](const QualType &T,
1964 const int FieldNo) -> bool {
1965 const auto *BT = dyn_cast<BuiltinType>(T.getCanonicalType());
1966 if (!BT || !BT->isInteger())
1967 return emitWarning(diag::note_returned_not_integer_field) << FieldNo;
1968 auto IntSize = Context.getTypeSize(Context.IntTy);
1969 if (Context.getTypeSize(BT) < IntSize)
1970 return emitWarning(diag::note_returned_not_wide_enough_field)
1971 << FieldNo << IntSize;
1972 return false;
1973 };
1974 if (validatePointerType(FirstFieldType) &&
1975 validatePointerType(SecondFieldType)) {
1976 // Pointer + pointer.
1977 return false;
1978 } else if (validatePointerType(FirstFieldType)) {
1979 // Pointer + integer?
1980 return checkIntegerType(SecondFieldType, 2);
1981 } else if (validatePointerType(SecondFieldType)) {
1982 // Integer + pointer?
1983 return checkIntegerType(FirstFieldType, 1);
1984 }
1985 return emitWarning(diag::note_returned_not_span_struct);
1986}
1987
1988static void handleMallocSpanAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1990 if (!S.CheckSpanLikeType(AL, ResultType))
1991 D->addAttr(::new (S.Context) MallocSpanAttr(S.Context, AL));
1992}
1993
1994static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1995 // Ensure we don't combine these with themselves, since that causes some
1996 // confusing behavior.
1997 if (AL.getParsedKind() == ParsedAttr::AT_CPUDispatch) {
1999 return;
2000
2001 if (const auto *Other = D->getAttr<CPUDispatchAttr>()) {
2002 S.Diag(AL.getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
2003 S.Diag(Other->getLocation(), diag::note_conflicting_attribute);
2004 return;
2005 }
2006 } else if (AL.getParsedKind() == ParsedAttr::AT_CPUSpecific) {
2008 return;
2009
2010 if (const auto *Other = D->getAttr<CPUSpecificAttr>()) {
2011 S.Diag(AL.getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
2012 S.Diag(Other->getLocation(), diag::note_conflicting_attribute);
2013 return;
2014 }
2015 }
2016
2018
2019 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
2020 if (MD->getParent()->isLambda()) {
2021 S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
2022 return;
2023 }
2024 }
2025
2026 if (!AL.checkAtLeastNumArgs(S, 1))
2027 return;
2028
2030 for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); ++ArgNo) {
2031 if (!AL.isArgIdent(ArgNo)) {
2032 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
2033 << AL << AANT_ArgumentIdentifier;
2034 return;
2035 }
2036
2037 IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
2038 StringRef CPUName = CPUArg->getIdentifierInfo()->getName().trim();
2039
2041 S.Diag(CPUArg->getLoc(), diag::err_invalid_cpu_specific_dispatch_value)
2042 << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
2043 return;
2044 }
2045
2047 if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
2048 return Target.CPUSpecificManglingCharacter(CPUName) ==
2049 Target.CPUSpecificManglingCharacter(Cur->getName());
2050 })) {
2051 S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
2052 return;
2053 }
2054 CPUs.push_back(CPUArg->getIdentifierInfo());
2055 }
2056
2057 FD->setIsMultiVersion(true);
2058 if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
2059 D->addAttr(::new (S.Context)
2060 CPUSpecificAttr(S.Context, AL, CPUs.data(), CPUs.size()));
2061 else
2062 D->addAttr(::new (S.Context)
2063 CPUDispatchAttr(S.Context, AL, CPUs.data(), CPUs.size()));
2064}
2065
2066static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2067 if (S.LangOpts.CPlusPlus) {
2068 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
2070 return;
2071 }
2072
2073 D->addAttr(::new (S.Context) CommonAttr(S.Context, AL));
2074}
2075
2076static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2077 if (AL.isDeclspecAttribute()) {
2078 const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2079 const auto &Arch = Triple.getArch();
2080 if (Arch != llvm::Triple::x86 &&
2081 (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
2082 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
2083 << AL << Triple.getArchName();
2084 return;
2085 }
2086
2087 // This form is not allowed to be written on a member function (static or
2088 // nonstatic) when in Microsoft compatibility mode.
2089 if (S.getLangOpts().MSVCCompat && isa<CXXMethodDecl>(D)) {
2090 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
2092 return;
2093 }
2094 }
2095
2096 D->addAttr(::new (S.Context) NakedAttr(S.Context, AL));
2097}
2098
2099// FIXME: This is a best-effort heuristic.
2100// Currently only handles single throw expressions (optionally with
2101// ExprWithCleanups). We could expand this to perform control-flow analysis for
2102// more complex patterns.
2103static bool isKnownToAlwaysThrow(const FunctionDecl *FD) {
2104 if (!FD->hasBody())
2105 return false;
2106 const Stmt *Body = FD->getBody();
2107 const Stmt *OnlyStmt = nullptr;
2108
2109 if (const auto *Compound = dyn_cast<CompoundStmt>(Body)) {
2110 if (Compound->size() != 1)
2111 return false; // More than one statement, can't be known to always throw.
2112 OnlyStmt = *Compound->body_begin();
2113 } else {
2114 OnlyStmt = Body;
2115 }
2116
2117 // Unwrap ExprWithCleanups if necessary.
2118 if (const auto *EWC = dyn_cast<ExprWithCleanups>(OnlyStmt)) {
2119 OnlyStmt = EWC->getSubExpr();
2120 }
2121
2122 if (isa<CXXThrowExpr>(OnlyStmt)) {
2123 const auto *MD = dyn_cast<CXXMethodDecl>(FD);
2124 if (MD && MD->isVirtual()) {
2125 const auto *RD = MD->getParent();
2126 return MD->hasAttr<FinalAttr>() || (RD && RD->isEffectivelyFinal());
2127 }
2128 return true;
2129 }
2130 return false;
2131}
2132
2134 auto *FD = dyn_cast<FunctionDecl>(D);
2135 if (!FD)
2136 return;
2137
2138 // Skip explicit specializations here as they may have
2139 // a user-provided definition that may deliberately differ from the primary
2140 // template. If an explicit specialization truly never returns, the user
2141 // should explicitly mark it with [[noreturn]].
2143 return;
2144
2145 DiagnosticsEngine &Diags = S.getDiagnostics();
2146 if (Diags.isIgnored(diag::warn_falloff_nonvoid, FD->getLocation()) &&
2147 Diags.isIgnored(diag::warn_suggest_noreturn_function, FD->getLocation()))
2148 return;
2149
2150 if (!FD->isNoReturn() && !FD->hasAttr<InferredNoReturnAttr>() &&
2152 FD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
2153
2154 // [[noreturn]] can only be added to lambdas since C++23
2155 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD);
2157 return;
2158
2159 // Emit a diagnostic suggesting the function being marked [[noreturn]].
2160 S.Diag(FD->getLocation(), diag::warn_suggest_noreturn_function)
2161 << /*isFunction=*/0 << FD;
2162 }
2163}
2164
2165static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
2166 if (hasDeclarator(D)) return;
2167
2168 if (!isa<ObjCMethodDecl>(D)) {
2169 S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
2170 << Attrs << Attrs.isRegularKeywordAttribute()
2172 return;
2173 }
2174
2175 D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs));
2176}
2177
2178static void handleStandardNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &A) {
2179 // The [[_Noreturn]] spelling is deprecated in C23, so if that was used,
2180 // issue an appropriate diagnostic. However, don't issue a diagnostic if the
2181 // attribute name comes from a macro expansion. We don't want to punish users
2182 // who write [[noreturn]] after including <stdnoreturn.h> (where 'noreturn'
2183 // is defined as a macro which expands to '_Noreturn').
2184 if (!S.getLangOpts().CPlusPlus &&
2185 A.getSemanticSpelling() == CXX11NoReturnAttr::C23_Noreturn &&
2186 !(A.getLoc().isMacroID() &&
2188 S.Diag(A.getLoc(), diag::warn_deprecated_noreturn_spelling) << A.getRange();
2189
2190 D->addAttr(::new (S.Context) CXX11NoReturnAttr(S.Context, A));
2191}
2192
2193static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
2194 if (!S.getLangOpts().CFProtectionBranch)
2195 S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2196 else
2198}
2199
2201 if (!Attrs.checkExactlyNumArgs(*this, 0)) {
2202 Attrs.setInvalid();
2203 return true;
2204 }
2205
2206 return false;
2207}
2208
2210 // Check whether the attribute is valid on the current target.
2211 if (!AL.existsInTarget(Context.getTargetInfo())) {
2213 Diag(AL.getLoc(), diag::err_keyword_not_supported_on_target)
2214 << AL << AL.getRange();
2215 else
2217 AL.setInvalid();
2218 return true;
2219 }
2220 return false;
2221}
2222
2223static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2224
2225 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2226 // because 'analyzer_noreturn' does not impact the type.
2228 ValueDecl *VD = dyn_cast<ValueDecl>(D);
2229 if (!VD || (!VD->getType()->isBlockPointerType() &&
2230 !VD->getType()->isFunctionPointerType())) {
2232 ? diag::err_attribute_wrong_decl_type
2233 : diag::warn_attribute_wrong_decl_type)
2234 << AL << AL.isRegularKeywordAttribute()
2236 return;
2237 }
2238 }
2239
2240 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL));
2241}
2242
2243// PS3 PPU-specific.
2244static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2245 /*
2246 Returning a Vector Class in Registers
2247
2248 According to the PPU ABI specifications, a class with a single member of
2249 vector type is returned in memory when used as the return value of a
2250 function.
2251 This results in inefficient code when implementing vector classes. To return
2252 the value in a single vector register, add the vecreturn attribute to the
2253 class definition. This attribute is also applicable to struct types.
2254
2255 Example:
2256
2257 struct Vector
2258 {
2259 __vector float xyzw;
2260 } __attribute__((vecreturn));
2261
2262 Vector Add(Vector lhs, Vector rhs)
2263 {
2264 Vector result;
2265 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2266 return result; // This will be returned in a register
2267 }
2268 */
2269 if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
2270 S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
2271 return;
2272 }
2273
2274 const auto *R = cast<RecordDecl>(D);
2275 int count = 0;
2276
2277 if (!isa<CXXRecordDecl>(R)) {
2278 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2279 return;
2280 }
2281
2282 if (!cast<CXXRecordDecl>(R)->isPOD()) {
2283 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
2284 return;
2285 }
2286
2287 for (const auto *I : R->fields()) {
2288 if ((count == 1) || !I->getType()->isVectorType()) {
2289 S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2290 return;
2291 }
2292 count++;
2293 }
2294
2295 D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL));
2296}
2297
2299 const ParsedAttr &AL) {
2300 if (isa<ParmVarDecl>(D)) {
2301 // [[carries_dependency]] can only be applied to a parameter if it is a
2302 // parameter of a function declaration or lambda.
2304 S.Diag(AL.getLoc(),
2305 diag::err_carries_dependency_param_not_function_decl);
2306 return;
2307 }
2308 }
2309
2310 D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
2311}
2312
2313static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2314 bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
2315
2316 // If this is spelled as the standard C++17 attribute, but not in C++17, warn
2317 // about using it as an extension.
2318 if (!S.getLangOpts().CPlusPlus17 && IsCXX17Attr)
2319 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2320
2321 D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
2322}
2323
2325 const ParsedAttr &AL) {
2326 // If no Expr node exists on the attribute, return a nullptr result (default
2327 // priority to be used). If Expr node exists but is not valid, return an
2328 // invalid result. Otherwise, return the Expr.
2329 Expr *E = nullptr;
2330 if (AL.getNumArgs() == 1) {
2331 E = AL.getArgAsExpr(0);
2332 if (E->isValueDependent()) {
2333 if (!E->isTypeDependent() && !E->getType()->isIntegerType()) {
2334 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
2336 return ExprError();
2337 }
2338 } else {
2339 uint32_t priority;
2340 if (!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), priority)) {
2341 return ExprError();
2342 }
2343 return ConstantExpr::Create(S.Context, E,
2344 APValue(llvm::APSInt::getUnsigned(priority)));
2345 }
2346 }
2347 return E;
2348}
2349
2350static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2351 if (S.getLangOpts().HLSL && AL.getNumArgs()) {
2352 S.Diag(AL.getLoc(), diag::err_hlsl_init_priority_unsupported);
2353 return;
2354 }
2356 if (E.isInvalid())
2357 return;
2358 S.Diag(D->getLocation(), diag::warn_global_constructor)
2359 << D->getSourceRange();
2360 D->addAttr(ConstructorAttr::Create(S.Context, E.get(), AL));
2361}
2362
2363static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2365 if (E.isInvalid())
2366 return;
2367 S.Diag(D->getLocation(), diag::warn_global_destructor) << D->getSourceRange();
2368 D->addAttr(DestructorAttr::Create(S.Context, E.get(), AL));
2369}
2370
2371template <typename AttrTy>
2372static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
2373 // Handle the case where the attribute has a text message.
2374 StringRef Str;
2375 if (AL.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(AL, 0, Str))
2376 return;
2377
2378 D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str));
2379}
2380
2382 const IdentifierInfo *Platform,
2383 VersionTuple Introduced,
2384 VersionTuple Deprecated,
2385 VersionTuple Obsoleted) {
2386 StringRef PlatformName
2387 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2388 if (PlatformName.empty())
2389 PlatformName = Platform->getName();
2390
2391 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2392 // of these steps are needed).
2393 if (!Introduced.empty() && !Deprecated.empty() &&
2394 !(Introduced <= Deprecated)) {
2395 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2396 << 1 << PlatformName << Deprecated.getAsString()
2397 << 0 << Introduced.getAsString();
2398 return true;
2399 }
2400
2401 if (!Introduced.empty() && !Obsoleted.empty() &&
2402 !(Introduced <= Obsoleted)) {
2403 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2404 << 2 << PlatformName << Obsoleted.getAsString()
2405 << 0 << Introduced.getAsString();
2406 return true;
2407 }
2408
2409 if (!Deprecated.empty() && !Obsoleted.empty() &&
2410 !(Deprecated <= Obsoleted)) {
2411 S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2412 << 2 << PlatformName << Obsoleted.getAsString()
2413 << 1 << Deprecated.getAsString();
2414 return true;
2415 }
2416
2417 return false;
2418}
2419
2420/// Check whether the two versions match.
2421///
2422/// If either version tuple is empty, then they are assumed to match. If
2423/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2424static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2425 bool BeforeIsOkay) {
2426 if (X.empty() || Y.empty())
2427 return true;
2428
2429 if (X == Y)
2430 return true;
2431
2432 if (BeforeIsOkay && X < Y)
2433 return true;
2434
2435 return false;
2436}
2437
2439 NamedDecl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Platform,
2440 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2441 VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2442 bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2443 int Priority, const IdentifierInfo *Environment,
2444 const IdentifierInfo *InferredPlatformII) {
2445 VersionTuple MergedIntroduced = Introduced;
2446 VersionTuple MergedDeprecated = Deprecated;
2447 VersionTuple MergedObsoleted = Obsoleted;
2448 bool FoundAny = false;
2449 bool OverrideOrImpl = false;
2450 switch (AMK) {
2453 OverrideOrImpl = false;
2454 break;
2455
2459 OverrideOrImpl = true;
2460 break;
2461 }
2462
2463 if (D->hasAttrs()) {
2464 AttrVec &Attrs = D->getAttrs();
2465 for (unsigned i = 0, e = Attrs.size(); i != e;) {
2466 auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2467 if (!OldAA) {
2468 ++i;
2469 continue;
2470 }
2471
2472 const IdentifierInfo *OldEnvironment = OldAA->getEnvironment();
2473 if (OldEnvironment != Environment) {
2474 ++i;
2475 continue;
2476 }
2477
2478 if (OldAA->getPlatform() != Platform) {
2479 // If this new attr is for anyappleos and the old attr is for the
2480 // inferred platform, the existing explicit platform attr wins.
2481 if (InferredPlatformII) {
2482 if (OldAA->getPlatform() == InferredPlatformII)
2483 return nullptr;
2484 } else {
2485 // If this new attr is an explicit platform attr, check if the old
2486 // attr is an existing anyAppleOS attr whose inferred attr is for this
2487 // platform. If so, the explicit attr wins: erase the old attr.
2488 if (AvailabilityAttr *Inf = OldAA->getInferredAttrAs();
2489 Inf && Inf->getPlatform() == Platform) {
2490 Attrs.erase(Attrs.begin() + i);
2491 --e;
2492 continue;
2493 }
2494 }
2495 ++i;
2496 continue;
2497 }
2498
2499 // If there is an existing availability attribute for this platform that
2500 // has a lower priority use the existing one and discard the new
2501 // attribute.
2502 if (OldAA->getPriority() < Priority)
2503 return nullptr;
2504
2505 // If there is an existing attribute for this platform that has a higher
2506 // priority than the new attribute then erase the old one and continue
2507 // processing the attributes.
2508 if (OldAA->getPriority() > Priority) {
2509 Attrs.erase(Attrs.begin() + i);
2510 --e;
2511 continue;
2512 }
2513
2514 FoundAny = true;
2515 VersionTuple OldIntroduced = OldAA->getIntroduced();
2516 VersionTuple OldDeprecated = OldAA->getDeprecated();
2517 VersionTuple OldObsoleted = OldAA->getObsoleted();
2518 bool OldIsUnavailable = OldAA->getUnavailable();
2519
2520 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2521 !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2522 !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
2523 !(OldIsUnavailable == IsUnavailable ||
2524 (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) {
2525 if (OverrideOrImpl) {
2526 int Which = -1;
2527 VersionTuple FirstVersion;
2528 VersionTuple SecondVersion;
2529 if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
2530 Which = 0;
2531 FirstVersion = OldIntroduced;
2532 SecondVersion = Introduced;
2533 } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
2534 Which = 1;
2535 FirstVersion = Deprecated;
2536 SecondVersion = OldDeprecated;
2537 } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
2538 Which = 2;
2539 FirstVersion = Obsoleted;
2540 SecondVersion = OldObsoleted;
2541 }
2542
2543 if (Which == -1) {
2544 Diag(OldAA->getLocation(),
2545 diag::warn_mismatched_availability_override_unavail)
2546 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2548 } else if (Which != 1 && AMK == AvailabilityMergeKind::
2550 // Allow different 'introduced' / 'obsoleted' availability versions
2551 // on a method that implements an optional protocol requirement. It
2552 // makes less sense to allow this for 'deprecated' as the user can't
2553 // see if the method is 'deprecated' as 'respondsToSelector' will
2554 // still return true when the method is deprecated.
2555 ++i;
2556 continue;
2557 } else {
2558 Diag(OldAA->getLocation(),
2559 diag::warn_mismatched_availability_override)
2560 << Which
2561 << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2562 << FirstVersion.getAsString() << SecondVersion.getAsString()
2564 }
2566 Diag(CI.getLoc(), diag::note_overridden_method);
2567 else
2568 Diag(CI.getLoc(), diag::note_protocol_method);
2569 } else {
2570 Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2571 Diag(CI.getLoc(), diag::note_previous_attribute);
2572 }
2573
2574 Attrs.erase(Attrs.begin() + i);
2575 --e;
2576 continue;
2577 }
2578
2579 VersionTuple MergedIntroduced2 = MergedIntroduced;
2580 VersionTuple MergedDeprecated2 = MergedDeprecated;
2581 VersionTuple MergedObsoleted2 = MergedObsoleted;
2582
2583 if (MergedIntroduced2.empty())
2584 MergedIntroduced2 = OldIntroduced;
2585 if (MergedDeprecated2.empty())
2586 MergedDeprecated2 = OldDeprecated;
2587 if (MergedObsoleted2.empty())
2588 MergedObsoleted2 = OldObsoleted;
2589
2590 if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2591 MergedIntroduced2, MergedDeprecated2,
2592 MergedObsoleted2)) {
2593 Attrs.erase(Attrs.begin() + i);
2594 --e;
2595 continue;
2596 }
2597
2598 MergedIntroduced = MergedIntroduced2;
2599 MergedDeprecated = MergedDeprecated2;
2600 MergedObsoleted = MergedObsoleted2;
2601 ++i;
2602 }
2603 }
2604
2605 if (FoundAny &&
2606 MergedIntroduced == Introduced &&
2607 MergedDeprecated == Deprecated &&
2608 MergedObsoleted == Obsoleted)
2609 return nullptr;
2610
2611 // Only create a new attribute if !OverrideOrImpl, but we want to do
2612 // the checking.
2613 if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
2614 MergedDeprecated, MergedObsoleted) &&
2615 !OverrideOrImpl) {
2616 auto *Avail = ::new (Context) AvailabilityAttr(
2617 Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2618 Message, IsStrict, Replacement, Priority, Environment,
2619 /*InferredAttr=*/nullptr);
2620 Avail->setImplicit(Implicit);
2621 return Avail;
2622 }
2623 return nullptr;
2624}
2625
2627 NamedDecl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Platform,
2628 bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2629 VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2630 bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2631 int Priority, const IdentifierInfo *IIEnvironment,
2632 const IdentifierInfo *InferredPlatformII) {
2633 AvailabilityAttr *OrigAttr = mergeAvailabilityAttr(
2634 D, CI, Platform, Implicit, Introduced, Deprecated, Obsoleted,
2635 IsUnavailable, Message, IsStrict, Replacement, AMK, Priority,
2636 IIEnvironment, InferredPlatformII);
2637 if (!OrigAttr || !InferredPlatformII)
2638 return OrigAttr;
2639
2640 auto *InferredAttr = ::new (Context) AvailabilityAttr(
2641 Context, CI, InferredPlatformII, OrigAttr->getIntroduced(),
2642 OrigAttr->getDeprecated(), OrigAttr->getObsoleted(),
2643 OrigAttr->getUnavailable(), OrigAttr->getMessage(), OrigAttr->getStrict(),
2644 OrigAttr->getReplacement(),
2645 Priority == AP_PragmaClangAttribute
2648 IIEnvironment, /*InferredAttr=*/nullptr);
2649 InferredAttr->setImplicit(true);
2650 OrigAttr->setInferredAttr(InferredAttr);
2651 return OrigAttr;
2652}
2653
2654/// Returns true if the given availability attribute should be inferred, and
2655/// adjusts the value of the attribute as necessary to facilitate that.
2657 IdentifierInfo *&II,
2658 bool &IsUnavailable,
2659 VersionTuple &Introduced,
2660 VersionTuple &Deprecated,
2661 VersionTuple &Obsolete, Sema &S) {
2662 const llvm::Triple &TT = S.Context.getTargetInfo().getTriple();
2663 const ASTContext &Context = S.Context;
2664 if (TT.getOS() != llvm::Triple::XROS)
2665 return false;
2666 IdentifierInfo *NewII = nullptr;
2667 if (II->getName() == "ios")
2668 NewII = &Context.Idents.get("xros");
2669 else if (II->getName() == "ios_app_extension")
2670 NewII = &Context.Idents.get("xros_app_extension");
2671 if (!NewII)
2672 return false;
2673 II = NewII;
2674
2675 auto MakeUnavailable = [&]() {
2676 IsUnavailable = true;
2677 // Reset introduced, deprecated, obsoleted.
2678 Introduced = VersionTuple();
2679 Deprecated = VersionTuple();
2680 Obsolete = VersionTuple();
2681 };
2682
2684 AL.getRange().getBegin(), "ios");
2685
2686 if (!SDKInfo) {
2687 MakeUnavailable();
2688 return true;
2689 }
2690 // Map from the fallback platform availability to the current platform
2691 // availability.
2692 const auto *Mapping = SDKInfo->getVersionMapping(DarwinSDKInfo::OSEnvPair(
2693 llvm::Triple::IOS, llvm::Triple::UnknownEnvironment, llvm::Triple::XROS,
2694 llvm::Triple::UnknownEnvironment));
2695 if (!Mapping) {
2696 MakeUnavailable();
2697 return true;
2698 }
2699
2700 if (!Introduced.empty()) {
2701 auto NewIntroduced = Mapping->mapIntroducedAvailabilityVersion(Introduced);
2702 if (!NewIntroduced) {
2703 MakeUnavailable();
2704 return true;
2705 }
2706 Introduced = *NewIntroduced;
2707 }
2708
2709 if (!Obsolete.empty()) {
2710 auto NewObsolete =
2711 Mapping->mapDeprecatedObsoletedAvailabilityVersion(Obsolete);
2712 if (!NewObsolete) {
2713 MakeUnavailable();
2714 return true;
2715 }
2716 Obsolete = *NewObsolete;
2717 }
2718
2719 if (!Deprecated.empty()) {
2720 auto NewDeprecated =
2721 Mapping->mapDeprecatedObsoletedAvailabilityVersion(Deprecated);
2722 Deprecated = NewDeprecated ? *NewDeprecated : VersionTuple();
2723 }
2724
2725 return true;
2726}
2727
2728static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2730 D)) {
2731 S.Diag(AL.getRange().getBegin(), diag::warn_deprecated_ignored_on_using)
2732 << AL;
2733 return;
2734 }
2735
2736 if (!AL.checkExactlyNumArgs(S, 1))
2737 return;
2738 IdentifierLoc *Platform = AL.getArgAsIdent(0);
2739
2740 IdentifierInfo *II = Platform->getIdentifierInfo();
2741 StringRef PrettyName = AvailabilityAttr::getPrettyPlatformName(II->getName());
2742 if (PrettyName.empty())
2743 S.Diag(Platform->getLoc(), diag::warn_availability_unknown_platform)
2744 << Platform->getIdentifierInfo();
2745
2746 auto *ND = dyn_cast<NamedDecl>(D);
2747 if (!ND) // We warned about this already, so just return.
2748 return;
2749
2753
2754 const llvm::Triple::OSType PlatformOS = AvailabilityAttr::getOSType(
2755 AvailabilityAttr::canonicalizePlatformName(II->getName()));
2756
2757 auto reportAndUpdateIfInvalidOS = [&](auto &InputVersion) -> void {
2758 const bool IsInValidRange =
2759 llvm::Triple::isValidVersionForOS(PlatformOS, InputVersion);
2760 // Canonicalize availability versions.
2761 auto CanonicalVersion = llvm::Triple::getCanonicalVersionForOS(
2762 PlatformOS, InputVersion, IsInValidRange);
2763 if (!IsInValidRange) {
2764 S.Diag(Platform->getLoc(), diag::warn_availability_invalid_os_version)
2765 << InputVersion.getAsString() << PrettyName;
2766 S.Diag(Platform->getLoc(),
2767 diag::note_availability_invalid_os_version_adjusted)
2768 << CanonicalVersion.getAsString();
2769 }
2770 InputVersion = CanonicalVersion;
2771 };
2772
2773 if (PlatformOS != llvm::Triple::OSType::UnknownOS) {
2774 reportAndUpdateIfInvalidOS(Introduced.Version);
2775 reportAndUpdateIfInvalidOS(Deprecated.Version);
2776 reportAndUpdateIfInvalidOS(Obsoleted.Version);
2777 }
2778
2779 bool IsUnavailable = AL.getUnavailableLoc().isValid();
2780 bool IsStrict = AL.getStrictLoc().isValid();
2781 StringRef Str;
2782 if (const auto *SE = dyn_cast_if_present<StringLiteral>(AL.getMessageExpr()))
2783 Str = SE->getString();
2784 StringRef Replacement;
2785 if (const auto *SE =
2786 dyn_cast_if_present<StringLiteral>(AL.getReplacementExpr()))
2787 Replacement = SE->getString();
2788
2789 if (II->isStr("swift")) {
2790 if (Introduced.isValid() || Obsoleted.isValid() ||
2791 (!IsUnavailable && !Deprecated.isValid())) {
2792 S.Diag(AL.getLoc(),
2793 diag::warn_availability_swift_unavailable_deprecated_only);
2794 return;
2795 }
2796 }
2797
2798 if (II->isStr("fuchsia")) {
2799 std::optional<unsigned> Min, Sub;
2800 if ((Min = Introduced.Version.getMinor()) ||
2801 (Sub = Introduced.Version.getSubminor())) {
2802 S.Diag(AL.getLoc(), diag::warn_availability_fuchsia_unavailable_minor);
2803 return;
2804 }
2805 }
2806
2807 if (S.getLangOpts().HLSL && IsStrict)
2808 S.Diag(AL.getStrictLoc(), diag::err_availability_unexpected_parameter)
2809 << "strict" << /* HLSL */ 0;
2810
2811 int PriorityModifier = AL.isPragmaClangAttribute()
2814
2815 const IdentifierLoc *EnvironmentLoc = AL.getEnvironment();
2816 IdentifierInfo *IIEnvironment = nullptr;
2817 if (EnvironmentLoc) {
2818 if (S.getLangOpts().HLSL) {
2819 IIEnvironment = EnvironmentLoc->getIdentifierInfo();
2820 if (AvailabilityAttr::getEnvironmentType(
2821 EnvironmentLoc->getIdentifierInfo()->getName()) ==
2822 llvm::Triple::EnvironmentType::UnknownEnvironment)
2823 S.Diag(EnvironmentLoc->getLoc(),
2824 diag::warn_availability_unknown_environment)
2825 << EnvironmentLoc->getIdentifierInfo();
2826 } else {
2827 S.Diag(EnvironmentLoc->getLoc(),
2828 diag::err_availability_unexpected_parameter)
2829 << "environment" << /* C/C++ */ 1;
2830 }
2831 }
2832
2833 // Handle anyAppleOS: preserve the original anyappleos attr on the decl and
2834 // store the inferred platform-specific attr as a field on it.
2835 if (II->getName() == "anyappleos") {
2836 // Validate anyAppleOS versions; reject versions older than 26.0.
2837 auto ValidateVersion = [&](const llvm::VersionTuple &Version,
2838 SourceLocation Loc) -> bool {
2840 return true;
2841 S.Diag(Loc, diag::err_availability_invalid_anyappleos_version)
2842 << Version.getAsString();
2843 return false;
2844 };
2845
2846 // Validate the versions; bail out if any are invalid.
2847 bool Valid = ValidateVersion(Introduced.Version, Introduced.KeywordLoc);
2848 Valid &= ValidateVersion(Deprecated.Version, Deprecated.KeywordLoc);
2849 Valid &= ValidateVersion(Obsoleted.Version, Obsoleted.KeywordLoc);
2850 if (!Valid)
2851 return;
2852
2853 llvm::Triple T = S.Context.getTargetInfo().getTriple();
2854
2855 // Only create implicit attributes for Darwin OSes.
2856 if (!T.isOSDarwin())
2857 return;
2858
2859 StringRef PlatformName;
2860
2861 // Determine the platform name based on the target triple.
2862 if (T.isMacOSX())
2863 PlatformName = "macos";
2864 else if (T.getOS() == llvm::Triple::IOS && T.isMacCatalystEnvironment())
2865 PlatformName = "maccatalyst";
2866 else // For iOS, tvOS, watchOS, visionOS, bridgeOS, etc.
2867 PlatformName = llvm::Triple::getOSTypeName(T.getOS());
2868
2869 IdentifierInfo *InferredPlatformII = &S.Context.Idents.get(PlatformName);
2870
2871 // Call mergeAvailabilityAttr for the original anyappleos attr. Pass
2872 // InferredPlatformII so the dedup loop can detect a conflicting explicit
2873 // platform attr (in which case mergeAvailabilityAttr returns null and we
2874 // add neither attr).
2875 AvailabilityAttr *OrigAttr = S.mergeAndInferAvailabilityAttr(
2876 ND, AL, II, /*Implicit=*/false, Introduced.Version, Deprecated.Version,
2877 Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2878 AvailabilityMergeKind::None, PriorityModifier, IIEnvironment,
2879 InferredPlatformII);
2880 if (!OrigAttr)
2881 return;
2882 D->addAttr(OrigAttr);
2883 return;
2884 }
2885
2886 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2887 ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version,
2888 Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2889 AvailabilityMergeKind::None, PriorityModifier, IIEnvironment);
2890 if (NewAttr)
2891 D->addAttr(NewAttr);
2892
2893 if (S.Context.getTargetInfo().getTriple().getOS() == llvm::Triple::XROS) {
2894 IdentifierInfo *NewII = II;
2895 bool NewIsUnavailable = IsUnavailable;
2896 VersionTuple NewIntroduced = Introduced.Version;
2897 VersionTuple NewDeprecated = Deprecated.Version;
2898 VersionTuple NewObsoleted = Obsoleted.Version;
2899 if (shouldInferAvailabilityAttribute(AL, NewII, NewIsUnavailable,
2900 NewIntroduced, NewDeprecated,
2901 NewObsoleted, S)) {
2902 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2903 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2904 NewObsoleted, NewIsUnavailable, Str, IsStrict, Replacement,
2906 PriorityModifier + Sema::AP_InferredFromOtherPlatform, IIEnvironment);
2907 if (NewAttr)
2908 D->addAttr(NewAttr);
2909 }
2910 }
2911
2912 // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2913 // matches before the start of the watchOS platform.
2914 if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2915 IdentifierInfo *NewII = nullptr;
2916 if (II->getName() == "ios")
2917 NewII = &S.Context.Idents.get("watchos");
2918 else if (II->getName() == "ios_app_extension")
2919 NewII = &S.Context.Idents.get("watchos_app_extension");
2920
2921 if (NewII) {
2922 const auto *SDKInfo = S.getDarwinSDKInfoForAvailabilityChecking();
2923 const auto *IOSToWatchOSMapping =
2924 SDKInfo ? SDKInfo->getVersionMapping(
2926 : nullptr;
2927
2928 auto adjustWatchOSVersion =
2929 [IOSToWatchOSMapping](VersionTuple Version) -> VersionTuple {
2930 if (Version.empty())
2931 return Version;
2932 auto MinimumWatchOSVersion = VersionTuple(2, 0);
2933
2934 if (IOSToWatchOSMapping) {
2935 if (auto MappedVersion = IOSToWatchOSMapping->map(
2936 Version, MinimumWatchOSVersion, std::nullopt)) {
2937 return *MappedVersion;
2938 }
2939 }
2940
2941 auto Major = Version.getMajor();
2942 auto NewMajor = Major;
2943 if (Major < 9)
2944 NewMajor = 0;
2945 else if (Major < 12)
2946 NewMajor = Major - 7;
2947 if (NewMajor >= 2) {
2948 if (Version.getMinor()) {
2949 if (Version.getSubminor())
2950 return VersionTuple(NewMajor, *Version.getMinor(),
2951 *Version.getSubminor());
2952 else
2953 return VersionTuple(NewMajor, *Version.getMinor());
2954 }
2955 return VersionTuple(NewMajor);
2956 }
2957
2958 return MinimumWatchOSVersion;
2959 };
2960
2961 auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2962 auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2963 auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2964
2965 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2966 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2967 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2969 PriorityModifier + Sema::AP_InferredFromOtherPlatform, IIEnvironment);
2970 if (NewAttr)
2971 D->addAttr(NewAttr);
2972 }
2973 } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2974 // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2975 // matches before the start of the tvOS platform.
2976 IdentifierInfo *NewII = nullptr;
2977 if (II->getName() == "ios")
2978 NewII = &S.Context.Idents.get("tvos");
2979 else if (II->getName() == "ios_app_extension")
2980 NewII = &S.Context.Idents.get("tvos_app_extension");
2981
2982 if (NewII) {
2983 const auto *SDKInfo = S.getDarwinSDKInfoForAvailabilityChecking();
2984 const auto *IOSToTvOSMapping =
2985 SDKInfo ? SDKInfo->getVersionMapping(
2987 : nullptr;
2988
2989 auto AdjustTvOSVersion =
2990 [IOSToTvOSMapping](VersionTuple Version) -> VersionTuple {
2991 if (Version.empty())
2992 return Version;
2993
2994 if (IOSToTvOSMapping) {
2995 if (auto MappedVersion = IOSToTvOSMapping->map(
2996 Version, VersionTuple(0, 0), std::nullopt)) {
2997 return *MappedVersion;
2998 }
2999 }
3000 return Version;
3001 };
3002
3003 auto NewIntroduced = AdjustTvOSVersion(Introduced.Version);
3004 auto NewDeprecated = AdjustTvOSVersion(Deprecated.Version);
3005 auto NewObsoleted = AdjustTvOSVersion(Obsoleted.Version);
3006
3007 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
3008 ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
3009 NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
3011 PriorityModifier + Sema::AP_InferredFromOtherPlatform, IIEnvironment);
3012 if (NewAttr)
3013 D->addAttr(NewAttr);
3014 }
3015 } else if (S.Context.getTargetInfo().getTriple().getOS() ==
3016 llvm::Triple::IOS &&
3017 S.Context.getTargetInfo().getTriple().isMacCatalystEnvironment()) {
3018 auto GetSDKInfo = [&]() {
3020 "macOS");
3021 };
3022
3023 // Transcribe "ios" to "maccatalyst" (and add a new attribute).
3024 IdentifierInfo *NewII = nullptr;
3025 if (II->getName() == "ios")
3026 NewII = &S.Context.Idents.get("maccatalyst");
3027 else if (II->getName() == "ios_app_extension")
3028 NewII = &S.Context.Idents.get("maccatalyst_app_extension");
3029 if (NewII) {
3030 auto MinMacCatalystVersion = [](const VersionTuple &V) {
3031 if (V.empty())
3032 return V;
3033 if (V.getMajor() < 13 ||
3034 (V.getMajor() == 13 && V.getMinor() && *V.getMinor() < 1))
3035 return VersionTuple(13, 1); // The min Mac Catalyst version is 13.1.
3036 return V;
3037 };
3038 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
3039 ND, AL, NewII, true /*Implicit*/,
3040 MinMacCatalystVersion(Introduced.Version),
3041 MinMacCatalystVersion(Deprecated.Version),
3042 MinMacCatalystVersion(Obsoleted.Version), IsUnavailable, Str,
3043 IsStrict, Replacement, AvailabilityMergeKind::None,
3044 PriorityModifier + Sema::AP_InferredFromOtherPlatform, IIEnvironment);
3045 if (NewAttr)
3046 D->addAttr(NewAttr);
3047 } else if (II->getName() == "macos" && GetSDKInfo() &&
3048 (!Introduced.Version.empty() || !Deprecated.Version.empty() ||
3049 !Obsoleted.Version.empty())) {
3050 if (const auto *MacOStoMacCatalystMapping =
3051 GetSDKInfo()->getVersionMapping(
3053 // Infer Mac Catalyst availability from the macOS availability attribute
3054 // if it has versioned availability. Don't infer 'unavailable'. This
3055 // inferred availability has lower priority than the other availability
3056 // attributes that are inferred from 'ios'.
3057 NewII = &S.Context.Idents.get("maccatalyst");
3058 auto RemapMacOSVersion =
3059 [&](const VersionTuple &V) -> std::optional<VersionTuple> {
3060 if (V.empty())
3061 return std::nullopt;
3062 // API_TO_BE_DEPRECATED is 100000.
3063 if (V.getMajor() == 100000)
3064 return VersionTuple(100000);
3065 // The minimum iosmac version is 13.1
3066 return MacOStoMacCatalystMapping->map(V, VersionTuple(13, 1),
3067 std::nullopt);
3068 };
3069 std::optional<VersionTuple> NewIntroduced =
3070 RemapMacOSVersion(Introduced.Version),
3071 NewDeprecated =
3072 RemapMacOSVersion(Deprecated.Version),
3073 NewObsoleted =
3074 RemapMacOSVersion(Obsoleted.Version);
3075 if (NewIntroduced || NewDeprecated || NewObsoleted) {
3076 auto VersionOrEmptyVersion =
3077 [](const std::optional<VersionTuple> &V) -> VersionTuple {
3078 return V ? *V : VersionTuple();
3079 };
3080 AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
3081 ND, AL, NewII, true /*Implicit*/,
3082 VersionOrEmptyVersion(NewIntroduced),
3083 VersionOrEmptyVersion(NewDeprecated),
3084 VersionOrEmptyVersion(NewObsoleted), /*IsUnavailable=*/false, Str,
3085 IsStrict, Replacement, AvailabilityMergeKind::None,
3086 PriorityModifier + Sema::AP_InferredFromOtherPlatform +
3088 IIEnvironment);
3089 if (NewAttr)
3090 D->addAttr(NewAttr);
3091 }
3092 }
3093 }
3094 }
3095}
3096
3098 const ParsedAttr &AL) {
3099 if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 4))
3100 return;
3101
3102 StringRef Language;
3103 if (const auto *SE = dyn_cast_if_present<StringLiteral>(AL.getArgAsExpr(0)))
3104 Language = SE->getString();
3105 StringRef DefinedIn;
3106 if (const auto *SE = dyn_cast_if_present<StringLiteral>(AL.getArgAsExpr(1)))
3107 DefinedIn = SE->getString();
3108 bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
3109 StringRef USR;
3110 if (const auto *SE = dyn_cast_if_present<StringLiteral>(AL.getArgAsExpr(3)))
3111 USR = SE->getString();
3112
3113 D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
3114 S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration, USR));
3115}
3116
3118 VisibilityAttr::VisibilityType Value) {
3119 if (VisibilityAttr *Attr = D->getAttr<VisibilityAttr>()) {
3120 if (Attr->getVisibility() != Value)
3121 Diag(Loc, diag::err_mismatched_visibility);
3122 } else
3123 D->addAttr(VisibilityAttr::CreateImplicit(Context, Value));
3124}
3125
3126template <class T>
3128 typename T::VisibilityType value) {
3129 T *existingAttr = D->getAttr<T>();
3130 if (existingAttr) {
3131 typename T::VisibilityType existingValue = existingAttr->getVisibility();
3132 if (existingValue == value)
3133 return nullptr;
3134 S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
3135 S.Diag(CI.getLoc(), diag::note_previous_attribute);
3136 D->dropAttr<T>();
3137 }
3138 return ::new (S.Context) T(S.Context, CI, value);
3139}
3140
3142 const AttributeCommonInfo &CI,
3143 VisibilityAttr::VisibilityType Vis) {
3144 return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis);
3145}
3146
3147TypeVisibilityAttr *
3149 TypeVisibilityAttr::VisibilityType Vis) {
3150 return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis);
3151}
3152
3153static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
3154 bool isTypeVisibility) {
3155 // Visibility attributes don't mean anything on a typedef.
3156 if (isa<TypedefNameDecl>(D)) {
3157 S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
3158 return;
3159 }
3160
3161 // 'type_visibility' can only go on a type or namespace.
3162 if (isTypeVisibility && !(isa<TagDecl>(D) || isa<ObjCInterfaceDecl>(D) ||
3163 isa<NamespaceDecl>(D))) {
3164 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
3166 return;
3167 }
3168
3169 // Check that the argument is a string literal.
3170 StringRef TypeStr;
3171 SourceLocation LiteralLoc;
3172 if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
3173 return;
3174
3175 VisibilityAttr::VisibilityType type;
3176 if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
3177 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
3178 << TypeStr;
3179 return;
3180 }
3181
3182 // Complain about attempts to use protected visibility on targets
3183 // (like Darwin) that don't support it.
3184 if (type == VisibilityAttr::Protected &&
3186 S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
3187 type = VisibilityAttr::Default;
3188 }
3189
3190 Attr *newAttr;
3191 if (isTypeVisibility) {
3192 newAttr = S.mergeTypeVisibilityAttr(
3193 D, AL, (TypeVisibilityAttr::VisibilityType)type);
3194 } else {
3195 newAttr = S.mergeVisibilityAttr(D, AL, type);
3196 }
3197 if (newAttr)
3198 D->addAttr(newAttr);
3199}
3200
3201static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3202 unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
3203 if (AL.getNumArgs() > 0) {
3204 Expr *E = AL.getArgAsExpr(0);
3205 std::optional<llvm::APSInt> Idx = llvm::APSInt(32);
3206 if (E->isTypeDependent() || !(Idx = E->getIntegerConstantExpr(S.Context))) {
3207 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3208 << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
3209 return;
3210 }
3211
3212 if (Idx->isSigned() && Idx->isNegative()) {
3213 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
3214 << E->getSourceRange();
3215 return;
3216 }
3217
3218 sentinel = Idx->getZExtValue();
3219 }
3220
3221 unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
3222 if (AL.getNumArgs() > 1) {
3223 Expr *E = AL.getArgAsExpr(1);
3224 std::optional<llvm::APSInt> Idx = llvm::APSInt(32);
3225 if (E->isTypeDependent() || !(Idx = E->getIntegerConstantExpr(S.Context))) {
3226 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3227 << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
3228 return;
3229 }
3230 nullPos = Idx->getZExtValue();
3231
3232 if ((Idx->isSigned() && Idx->isNegative()) || nullPos > 1) {
3233 // FIXME: This error message could be improved, it would be nice
3234 // to say what the bounds actually are.
3235 S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
3236 << E->getSourceRange();
3237 return;
3238 }
3239 }
3240
3241 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3242 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
3243 if (isa<FunctionNoProtoType>(FT)) {
3244 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
3245 return;
3246 }
3247
3248 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
3249 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
3250 return;
3251 }
3252 } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
3253 if (!MD->isVariadic()) {
3254 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
3255 return;
3256 }
3257 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
3258 if (!BD->isVariadic()) {
3259 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
3260 return;
3261 }
3262 } else if (const auto *V = dyn_cast<VarDecl>(D)) {
3263 QualType Ty = V->getType();
3264 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
3265 const FunctionType *FT = Ty->isFunctionPointerType()
3266 ? D->getFunctionType()
3267 : Ty->castAs<BlockPointerType>()
3268 ->getPointeeType()
3269 ->castAs<FunctionType>();
3270 if (isa<FunctionNoProtoType>(FT)) {
3271 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
3272 return;
3273 }
3274 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
3275 int m = Ty->isFunctionPointerType() ? 0 : 1;
3276 S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
3277 return;
3278 }
3279 } else {
3280 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3281 << AL << AL.isRegularKeywordAttribute()
3283 return;
3284 }
3285 } else {
3286 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3287 << AL << AL.isRegularKeywordAttribute()
3289 return;
3290 }
3291 D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos));
3292}
3293
3294static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
3295 if (D->getFunctionType() &&
3298 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
3299 return;
3300 }
3301 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
3302 if (MD->getReturnType()->isVoidType()) {
3303 S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
3304 return;
3305 }
3306
3307 StringRef Str;
3308 if (AL.isStandardAttributeSyntax()) {
3309 // If this is spelled [[clang::warn_unused_result]] we look for an optional
3310 // string literal. This is not gated behind any specific version of the
3311 // standard.
3312 if (AL.isClangScope()) {
3313 if (AL.getNumArgs() == 1 &&
3314 !S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
3315 return;
3316 } else if (!AL.getScopeName()) {
3317 // The standard attribute cannot be applied to variable declarations such
3318 // as a function pointer.
3319 if (isa<VarDecl>(D))
3320 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3321 << AL << AL.isRegularKeywordAttribute()
3323
3324 // If this is spelled as the standard C++17 attribute, but not in C++17,
3325 // warn about using it as an extension. If there are attribute arguments,
3326 // then claim it's a C++20 extension instead. C23 supports this attribute
3327 // with the message; no extension warning is needed there beyond the one
3328 // already issued for accepting attributes in older modes.
3329 const LangOptions &LO = S.getLangOpts();
3330 if (AL.getNumArgs() == 1) {
3331 if (LO.CPlusPlus && !LO.CPlusPlus20)
3332 S.Diag(AL.getLoc(), diag::ext_cxx20_attr) << AL;
3333
3334 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
3335 return;
3336 } else if (LO.CPlusPlus && !LO.CPlusPlus17)
3337 S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
3338 }
3339 }
3340
3341 if ((!AL.isGNUAttribute() &&
3342 !(AL.isStandardAttributeSyntax() && AL.isClangScope())) &&
3344 S.Diag(AL.getLoc(), diag::warn_unused_result_typedef_unsupported_spelling)
3345 << AL.isGNUScope();
3346 return;
3347 }
3348
3349 D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str));
3350}
3351
3352static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3353 // weak_import only applies to variable & function declarations.
3354 bool isDef = false;
3355 if (!D->canBeWeakImported(isDef)) {
3356 if (isDef)
3357 S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
3358 << "weak_import";
3359 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
3360 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
3362 // Nothing to warn about here.
3363 } else
3364 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
3366
3367 return;
3368 }
3369
3370 D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL));
3371}
3372
3373// Checks whether an argument of launch_bounds-like attribute is
3374// acceptable, performs implicit conversion to Rvalue, and returns
3375// non-nullptr Expr result on success. Otherwise, it returns nullptr
3376// and may output an error.
3377template <class Attribute>
3378static Expr *makeAttributeArgExpr(Sema &S, Expr *E, const Attribute &Attr,
3379 const unsigned Idx) {
3381 return nullptr;
3382
3383 // Accept template arguments for now as they depend on something else.
3384 // We'll get to check them when they eventually get instantiated.
3385 if (E->isValueDependent())
3386 return E;
3387
3388 std::optional<llvm::APSInt> I = llvm::APSInt(64);
3389 if (!(I = E->getIntegerConstantExpr(S.Context))) {
3390 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
3391 << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
3392 return nullptr;
3393 }
3394 // Make sure we can fit it in 32 bits.
3395 if (!I->isIntN(32)) {
3396 S.Diag(E->getExprLoc(), diag::err_ice_too_large)
3397 << toString(*I, 10, false) << 32 << /* Unsigned */ 1;
3398 return nullptr;
3399 }
3400 if (*I < 0)
3401 S.Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
3402 << &Attr << /*non-negative*/ 1 << E->getSourceRange();
3403
3404 // We may need to perform implicit conversion of the argument.
3406 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
3407 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
3408 assert(!ValArg.isInvalid() &&
3409 "Unexpected PerformCopyInitialization() failure.");
3410
3411 return ValArg.getAs<Expr>();
3412}
3413
3414// Handles reqd_work_group_size and work_group_size_hint.
3415template <typename WorkGroupAttr>
3416static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
3417 Expr *WGSize[3];
3418 for (unsigned i = 0; i < 3; ++i) {
3419 if (Expr *E = makeAttributeArgExpr(S, AL.getArgAsExpr(i), AL, i))
3420 WGSize[i] = E;
3421 else
3422 return;
3423 }
3424
3425 auto IsZero = [&](Expr *E) {
3426 if (E->isValueDependent())
3427 return false;
3428 std::optional<llvm::APSInt> I = E->getIntegerConstantExpr(S.Context);
3429 assert(I && "Non-integer constant expr");
3430 return I->isZero();
3431 };
3432
3433 if (!llvm::all_of(WGSize, IsZero)) {
3434 for (unsigned i = 0; i < 3; ++i) {
3435 const Expr *E = AL.getArgAsExpr(i);
3436 if (IsZero(WGSize[i])) {
3437 S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
3438 << AL << E->getSourceRange();
3439 return;
3440 }
3441 }
3442 }
3443
3444 auto Equal = [&](Expr *LHS, Expr *RHS) {
3445 if (LHS->isValueDependent() || RHS->isValueDependent())
3446 return true;
3447 std::optional<llvm::APSInt> L = LHS->getIntegerConstantExpr(S.Context);
3448 assert(L && "Non-integer constant expr");
3449 std::optional<llvm::APSInt> R = RHS->getIntegerConstantExpr(S.Context);
3450 assert(L && "Non-integer constant expr");
3451 return L == R;
3452 };
3453
3454 WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
3455 if (Existing &&
3456 !llvm::equal(std::initializer_list<Expr *>{Existing->getXDim(),
3457 Existing->getYDim(),
3458 Existing->getZDim()},
3459 WGSize, Equal))
3460 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3461
3462 D->addAttr(::new (S.Context)
3463 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
3464}
3465
3466static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
3467 if (!AL.hasParsedType()) {
3468 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
3469 return;
3470 }
3471
3472 TypeSourceInfo *ParmTSI = nullptr;
3473 QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
3474 assert(ParmTSI && "no type source info for attribute argument");
3475
3476 if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() &&
3477 (ParmType->isBooleanType() ||
3478 !ParmType->isIntegralType(S.getASTContext()))) {
3479 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 2 << AL;
3480 return;
3481 }
3482
3483 if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
3484 if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
3485 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3486 return;
3487 }
3488 }
3489
3490 D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
3491}
3492
3494 StringRef Name) {
3495 // Explicit or partial specializations do not inherit
3496 // the section attribute from the primary template.
3497 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3498 if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate &&
3500 return nullptr;
3501 }
3502 if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
3503 if (ExistingAttr->getName() == Name)
3504 return nullptr;
3505 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3506 << 1 /*section*/;
3507 Diag(CI.getLoc(), diag::note_previous_attribute);
3508 return nullptr;
3509 }
3510 return ::new (Context) SectionAttr(Context, CI, Name);
3511}
3512
3513llvm::Error Sema::isValidSectionSpecifier(StringRef SecName) {
3514 if (!Context.getTargetInfo().getTriple().isOSDarwin())
3515 return llvm::Error::success();
3516
3517 // Let MCSectionMachO validate this.
3518 StringRef Segment, Section;
3519 unsigned TAA, StubSize;
3520 bool HasTAA;
3521 return llvm::MCSectionMachO::ParseSectionSpecifier(SecName, Segment, Section,
3522 TAA, HasTAA, StubSize);
3523}
3524
3525bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
3526 if (llvm::Error E = isValidSectionSpecifier(SecName)) {
3527 Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3528 << toString(std::move(E)) << 1 /*'section'*/;
3529 return false;
3530 }
3531 return true;
3532}
3533
3534static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3535 // Make sure that there is a string literal as the sections's single
3536 // argument.
3537 StringRef Str;
3538 SourceLocation LiteralLoc;
3539 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3540 return;
3541
3542 if (!S.checkSectionName(LiteralLoc, Str))
3543 return;
3544
3545 SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
3546 if (NewAttr) {
3547 D->addAttr(NewAttr);
3549 ObjCPropertyDecl>(D))
3550 S.UnifySection(NewAttr->getName(),
3552 cast<NamedDecl>(D));
3553 }
3554}
3555
3556static bool isValidCodeModelAttr(llvm::Triple &Triple, StringRef Str) {
3557 if (Triple.isLoongArch()) {
3558 return Str == "normal" || Str == "medium" || Str == "extreme";
3559 } else {
3560 assert(Triple.getArch() == llvm::Triple::x86_64 &&
3561 "only loongarch/x86-64 supported");
3562 return Str == "small" || Str == "large";
3563 }
3564}
3565
3566static void handleCodeModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3567 StringRef Str;
3568 SourceLocation LiteralLoc;
3569 auto IsTripleSupported = [](llvm::Triple &Triple) {
3570 return Triple.getArch() == llvm::Triple::ArchType::x86_64 ||
3571 Triple.isLoongArch();
3572 };
3573
3574 // Check that it is a string.
3575 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3576 return;
3577
3580 if (auto *aux = S.Context.getAuxTargetInfo()) {
3581 Triples.push_back(aux->getTriple());
3582 } else if (S.Context.getTargetInfo().getTriple().isNVPTX() ||
3583 S.Context.getTargetInfo().getTriple().isAMDGPU() ||
3584 S.Context.getTargetInfo().getTriple().isSPIRV()) {
3585 // Ignore the attribute for pure GPU device compiles since it only applies
3586 // to host globals.
3587 return;
3588 }
3589
3590 auto SupportedTripleIt = llvm::find_if(Triples, IsTripleSupported);
3591 if (SupportedTripleIt == Triples.end()) {
3592 S.Diag(LiteralLoc, diag::warn_unknown_attribute_ignored) << AL;
3593 return;
3594 }
3595
3596 llvm::CodeModel::Model CM;
3597 if (!CodeModelAttr::ConvertStrToModel(Str, CM) ||
3598 !isValidCodeModelAttr(*SupportedTripleIt, Str)) {
3599 S.Diag(LiteralLoc, diag::err_attr_codemodel_arg) << Str;
3600 return;
3601 }
3602
3603 D->addAttr(::new (S.Context) CodeModelAttr(S.Context, AL, CM));
3604}
3605
3606// This is used for `__declspec(code_seg("segname"))` on a decl.
3607// `#pragma code_seg("segname")` uses checkSectionName() instead.
3608static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
3609 StringRef CodeSegName) {
3610 if (llvm::Error E = S.isValidSectionSpecifier(CodeSegName)) {
3611 S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3612 << toString(std::move(E)) << 0 /*'code-seg'*/;
3613 return false;
3614 }
3615
3616 return true;
3617}
3618
3620 StringRef Name) {
3621 // Explicit or partial specializations do not inherit
3622 // the code_seg attribute from the primary template.
3623 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3625 return nullptr;
3626 }
3627 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3628 if (ExistingAttr->getName() == Name)
3629 return nullptr;
3630 Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3631 << 0 /*codeseg*/;
3632 Diag(CI.getLoc(), diag::note_previous_attribute);
3633 return nullptr;
3634 }
3635 return ::new (Context) CodeSegAttr(Context, CI, Name);
3636}
3637
3638static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3639 StringRef Str;
3640 SourceLocation LiteralLoc;
3641 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3642 return;
3643 if (!checkCodeSegName(S, LiteralLoc, Str))
3644 return;
3645 if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3646 if (!ExistingAttr->isImplicit()) {
3647 S.Diag(AL.getLoc(),
3648 ExistingAttr->getName() == Str
3649 ? diag::warn_duplicate_codeseg_attribute
3650 : diag::err_conflicting_codeseg_attribute);
3651 return;
3652 }
3653 D->dropAttr<CodeSegAttr>();
3654 }
3655 if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
3656 D->addAttr(CSA);
3657}
3658
3659bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3660 using namespace DiagAttrParams;
3661
3662 if (AttrStr.contains("fpmath="))
3663 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3664 << Unsupported << None << "fpmath=" << Target;
3665
3666 // Diagnose use of tune if target doesn't support it.
3667 if (!Context.getTargetInfo().supportsTargetAttributeTune() &&
3668 AttrStr.contains("tune="))
3669 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3670 << Unsupported << None << "tune=" << Target;
3671
3672 ParsedTargetAttr ParsedAttrs =
3673 Context.getTargetInfo().parseTargetAttr(AttrStr);
3674
3675 if (!ParsedAttrs.CPU.empty() &&
3676 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.CPU))
3677 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3678 << Unknown << CPU << ParsedAttrs.CPU << Target;
3679
3680 if (!ParsedAttrs.Tune.empty() &&
3681 !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Tune))
3682 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3683 << Unknown << Tune << ParsedAttrs.Tune << Target;
3684
3685 if (Context.getTargetInfo().getTriple().isRISCV()) {
3686 if (ParsedAttrs.Duplicate != "")
3687 return Diag(LiteralLoc, diag::err_duplicate_target_attribute)
3688 << Duplicate << None << ParsedAttrs.Duplicate << Target;
3689 for (StringRef CurFeature : ParsedAttrs.Features) {
3690 if (!CurFeature.starts_with('+') && !CurFeature.starts_with('-'))
3691 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3692 << Unsupported << None << AttrStr << Target;
3693 }
3694 }
3695
3696 if (Context.getTargetInfo().getTriple().isLoongArch()) {
3697 for (StringRef CurFeature : ParsedAttrs.Features) {
3698 if (CurFeature.starts_with("!arch=")) {
3699 StringRef ArchValue = CurFeature.split("=").second.trim();
3700 return Diag(LiteralLoc, diag::err_attribute_unsupported)
3701 << "target(arch=..)" << ArchValue;
3702 }
3703 }
3704 }
3705
3706 if (ParsedAttrs.Duplicate != "")
3707 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3708 << Duplicate << None << ParsedAttrs.Duplicate << Target;
3709
3710 for (const auto &Feature : ParsedAttrs.Features) {
3711 auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3712 if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3713 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3714 << Unsupported << None << CurFeature << Target;
3715 }
3716
3718 StringRef DiagMsg;
3719 if (ParsedAttrs.BranchProtection.empty())
3720 return false;
3721 if (!Context.getTargetInfo().validateBranchProtection(
3722 ParsedAttrs.BranchProtection, ParsedAttrs.CPU, BPI,
3723 Context.getLangOpts(), DiagMsg)) {
3724 if (DiagMsg.empty())
3725 return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3726 << Unsupported << None << "branch-protection" << Target;
3727 return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec)
3728 << DiagMsg;
3729 }
3730 if (!DiagMsg.empty())
3731 Diag(LiteralLoc, diag::warn_unsupported_branch_protection_spec) << DiagMsg;
3732
3733 return false;
3734}
3735
3736static void handleTargetVersionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3737 StringRef Param;
3738 SourceLocation Loc;
3739 SmallString<64> NewParam;
3740 if (!S.checkStringLiteralArgumentAttr(AL, 0, Param, &Loc))
3741 return;
3742
3743 if (S.Context.getTargetInfo().getTriple().isAArch64()) {
3744 if (S.ARM().checkTargetVersionAttr(Param, Loc, NewParam))
3745 return;
3746 } else if (S.Context.getTargetInfo().getTriple().isRISCV()) {
3747 if (S.RISCV().checkTargetVersionAttr(Param, Loc, NewParam))
3748 return;
3749 }
3750
3751 TargetVersionAttr *NewAttr =
3752 ::new (S.Context) TargetVersionAttr(S.Context, AL, NewParam);
3753 D->addAttr(NewAttr);
3754}
3755
3756static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3757 StringRef Str;
3758 SourceLocation LiteralLoc;
3759 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
3760 S.checkTargetAttr(LiteralLoc, Str))
3761 return;
3762
3763 TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str);
3764 D->addAttr(NewAttr);
3765}
3766
3767static void handleTargetClonesAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3768 // Ensure we don't combine these with themselves, since that causes some
3769 // confusing behavior.
3770 if (const auto *Other = D->getAttr<TargetClonesAttr>()) {
3771 S.Diag(AL.getLoc(), diag::err_disallowed_duplicate_attribute) << AL;
3772 S.Diag(Other->getLocation(), diag::note_conflicting_attribute);
3773 return;
3774 }
3776 return;
3777
3778 // FIXME: We could probably figure out how to get this to work for lambdas
3779 // someday.
3780 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
3781 if (MD->getParent()->isLambda()) {
3782 S.Diag(D->getLocation(), diag::err_multiversion_doesnt_support)
3783 << static_cast<unsigned>(MultiVersionKind::TargetClones)
3784 << /*Lambda*/ 9;
3785 return;
3786 }
3787 }
3788
3791 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
3792 StringRef Param;
3793 SourceLocation Loc;
3794 if (!S.checkStringLiteralArgumentAttr(AL, I, Param, &Loc))
3795 return;
3796 Params.push_back(Param);
3797 Locations.push_back(Loc);
3798 }
3799
3800 SmallVector<SmallString<64>, 2> NewParams;
3801 if (S.Context.getTargetInfo().getTriple().isAArch64()) {
3802 if (S.ARM().checkTargetClonesAttr(Params, Locations, NewParams))
3803 return;
3804 } else if (S.Context.getTargetInfo().getTriple().isRISCV()) {
3805 if (S.RISCV().checkTargetClonesAttr(Params, Locations, NewParams,
3806 AL.getLoc()))
3807 return;
3808 } else if (S.Context.getTargetInfo().getTriple().isX86()) {
3809 if (S.X86().checkTargetClonesAttr(Params, Locations, NewParams,
3810 AL.getLoc()))
3811 return;
3812 } else if (S.Context.getTargetInfo().getTriple().isOSAIX()) {
3813 if (S.PPC().checkTargetClonesAttr(Params, Locations, NewParams,
3814 AL.getLoc()))
3815 return;
3816 }
3817 Params.clear();
3818 for (auto &SmallStr : NewParams)
3819 Params.push_back(SmallStr.str());
3820
3821 TargetClonesAttr *NewAttr = ::new (S.Context)
3822 TargetClonesAttr(S.Context, AL, Params.data(), Params.size());
3823 D->addAttr(NewAttr);
3824}
3825
3826static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3827 Expr *E = AL.getArgAsExpr(0);
3828 uint32_t VecWidth;
3829 if (!S.checkUInt32Argument(AL, E, VecWidth)) {
3830 AL.setInvalid();
3831 return;
3832 }
3833
3834 MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3835 if (Existing && Existing->getVectorWidth() != VecWidth) {
3836 S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3837 return;
3838 }
3839
3840 D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth));
3841}
3842
3843static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3844 Expr *E = AL.getArgAsExpr(0);
3845 SourceLocation Loc = E->getExprLoc();
3846 FunctionDecl *FD = nullptr;
3848
3849 // gcc only allows for simple identifiers. Since we support more than gcc, we
3850 // will warn the user.
3851 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3852 if (DRE->hasQualifier())
3853 S.Diag(Loc, diag::warn_cleanup_ext);
3854 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3855 NI = DRE->getNameInfo();
3856 if (!FD) {
3857 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3858 << NI.getName();
3859 return;
3860 }
3861 } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
3862 if (ULE->hasExplicitTemplateArgs())
3863 S.Diag(Loc, diag::warn_cleanup_ext);
3865 NI = ULE->getNameInfo();
3866 if (!FD) {
3867 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3868 << NI.getName();
3869 if (ULE->getType() == S.Context.OverloadTy)
3871 return;
3872 }
3873 } else {
3874 S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
3875 return;
3876 }
3877
3878 if (FD->getNumParams() != 1) {
3879 S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3880 << NI.getName();
3881 return;
3882 }
3883
3884 VarDecl *VD = cast<VarDecl>(D);
3885 // Create a reference to the variable declaration. This is a fake/dummy
3886 // reference.
3887 DeclRefExpr *VariableReference = DeclRefExpr::Create(
3888 S.Context, NestedNameSpecifierLoc{}, FD->getLocation(), VD, false,
3889 DeclarationNameInfo{VD->getDeclName(), VD->getLocation()}, VD->getType(),
3890 VK_LValue);
3891
3892 // Create a unary operator expression that represents taking the address of
3893 // the variable. This is a fake/dummy expression.
3894 Expr *AddressOfVariable = UnaryOperator::Create(
3895 S.Context, VariableReference, UnaryOperatorKind::UO_AddrOf,
3897 +false, FPOptionsOverride{});
3898
3899 // Create a function call expression. This is a fake/dummy call expression.
3900 CallExpr *FunctionCallExpression =
3901 CallExpr::Create(S.Context, E, ArrayRef{AddressOfVariable},
3903
3904 if (S.CheckFunctionCall(FD, FunctionCallExpression,
3905 FD->getType()->getAs<FunctionProtoType>())) {
3906 return;
3907 }
3908
3909 // If a declaration contains multiple cleanup attributes, GCC only uses
3910 // the last one.
3911 if (const auto *A = D->getAttr<CleanupAttr>()) {
3912 S.Diag(A->getLoc(), diag::warn_duplicate_cleanup_attr) << A->getRange();
3913 D->dropAttr<CleanupAttr>();
3914 }
3915
3916 auto *attr = ::new (S.Context) CleanupAttr(S.Context, AL, FD);
3917 attr->setArgLoc(E->getExprLoc());
3918 D->addAttr(attr);
3919}
3920
3922 const ParsedAttr &AL) {
3923 if (!AL.isArgIdent(0)) {
3924 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3925 << AL << 0 << AANT_ArgumentIdentifier;
3926 return;
3927 }
3928
3929 EnumExtensibilityAttr::Kind ExtensibilityKind;
3931 if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3932 ExtensibilityKind)) {
3933 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
3934 return;
3935 }
3936
3937 D->addAttr(::new (S.Context)
3938 EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind));
3939}
3940
3941/// Handle __attribute__((format_arg((idx)))) attribute based on
3942/// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
3943static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3944 const Expr *IdxExpr = AL.getArgAsExpr(0);
3945 ParamIdx Idx;
3946 if (!S.checkFunctionOrMethodParameterIndex(D, AL, 1, IdxExpr, Idx))
3947 return;
3948
3949 // Make sure the format string is really a string.
3951
3952 bool NotNSStringTy = !S.ObjC().isNSStringType(Ty);
3953 if (NotNSStringTy && !S.ObjC().isCFStringType(Ty) &&
3954 (!Ty->isPointerType() ||
3956 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
3957 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
3958 return;
3959 }
3961 // replace instancetype with the class type
3962 auto *Instancetype = cast<TypedefType>(S.Context.getTypedefType(
3963 ElaboratedTypeKeyword::None, /*Qualifier=*/std::nullopt,
3965 if (Ty->getAs<TypedefType>() == Instancetype)
3966 if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
3967 if (auto *Interface = OMD->getClassInterface())
3969 QualType(Interface->getTypeForDecl(), 0));
3970 if (!S.ObjC().isNSStringType(Ty, /*AllowNSAttributedString=*/true) &&
3971 !S.ObjC().isCFStringType(Ty) &&
3972 (!Ty->isPointerType() ||
3974 S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
3975 << (NotNSStringTy ? "string type" : "NSString")
3976 << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
3977 return;
3978 }
3979
3980 D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
3981}
3982
3991
3992/// getFormatAttrKind - Map from format attribute names to supported format
3993/// types.
3994static FormatAttrKind getFormatAttrKind(StringRef Format) {
3995 return llvm::StringSwitch<FormatAttrKind>(Format)
3996 // Check for formats that get handled specially.
3997 .Case("NSString", NSStringFormat)
3998 .Case("CFString", CFStringFormat)
3999 .Cases({"gnu_strftime", "strftime"}, StrftimeFormat)
4000
4001 // Otherwise, check for supported formats.
4002 .Cases({"gnu_scanf", "scanf", "gnu_printf", "printf", "printf0",
4003 "gnu_strfmon", "strfmon"},
4005 .Cases({"cmn_err", "vcmn_err", "zcmn_err"}, SupportedFormat)
4006 .Cases({"kprintf", "syslog"}, SupportedFormat) // OpenBSD.
4007 .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
4008 .Case("os_trace", SupportedFormat)
4009 .Case("os_log", SupportedFormat)
4010
4011 .Cases({"gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag"},
4014}
4015
4016/// Handle __attribute__((init_priority(priority))) attributes based on
4017/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
4018static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4019 if (!S.getLangOpts().CPlusPlus) {
4020 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
4021 return;
4022 }
4023
4024 if (S.getLangOpts().HLSL) {
4025 S.Diag(AL.getLoc(), diag::err_hlsl_init_priority_unsupported);
4026 return;
4027 }
4028
4030 S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
4031 AL.setInvalid();
4032 return;
4033 }
4034
4035 Expr *E = AL.getArgAsExpr(0);
4036 uint32_t prioritynum;
4037 if (!S.checkUInt32Argument(AL, E, prioritynum)) {
4038 AL.setInvalid();
4039 return;
4040 }
4041
4042 if (prioritynum > 65535) {
4043 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
4044 << E->getSourceRange() << AL << 0 << 65535;
4045 AL.setInvalid();
4046 return;
4047 }
4048
4049 // Values <= 100 are reserved for the implementation, and libc++
4050 // benefits from being able to specify values in that range.
4051 if (prioritynum < 101)
4052 S.Diag(AL.getLoc(), diag::warn_init_priority_reserved)
4053 << E->getSourceRange() << prioritynum;
4054 D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
4055}
4056
4058 StringRef NewUserDiagnostic) {
4059 if (const auto *EA = D->getAttr<ErrorAttr>()) {
4060 std::string NewAttr = CI.getNormalizedFullName();
4061 assert((NewAttr == "error" || NewAttr == "warning") &&
4062 "unexpected normalized full name");
4063 bool Match = (EA->isError() && NewAttr == "error") ||
4064 (EA->isWarning() && NewAttr == "warning");
4065 if (!Match) {
4066 Diag(EA->getLocation(), diag::err_attributes_are_not_compatible)
4067 << CI << EA
4068 << (CI.isRegularKeywordAttribute() ||
4069 EA->isRegularKeywordAttribute());
4070 Diag(CI.getLoc(), diag::note_conflicting_attribute);
4071 return nullptr;
4072 }
4073 if (EA->getUserDiagnostic() != NewUserDiagnostic) {
4074 Diag(CI.getLoc(), diag::warn_duplicate_attribute) << EA;
4075 Diag(EA->getLoc(), diag::note_previous_attribute);
4076 }
4077 D->dropAttr<ErrorAttr>();
4078 }
4079 return ::new (Context) ErrorAttr(Context, CI, NewUserDiagnostic);
4080}
4081
4083 const IdentifierInfo *Format, int FormatIdx,
4084 int FirstArg) {
4085 // Check whether we already have an equivalent format attribute.
4086 for (auto *F : D->specific_attrs<FormatAttr>()) {
4087 if (F->getType() == Format &&
4088 F->getFormatIdx() == FormatIdx &&
4089 F->getFirstArg() == FirstArg) {
4090 // If we don't have a valid location for this attribute, adopt the
4091 // location.
4092 if (F->getLocation().isInvalid())
4093 F->setRange(CI.getRange());
4094 return nullptr;
4095 }
4096 }
4097
4098 return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg);
4099}
4100
4102 const AttributeCommonInfo &CI,
4103 const IdentifierInfo *Format,
4104 int FormatIdx,
4105 StringLiteral *FormatStr) {
4106 // Check whether we already have an equivalent FormatMatches attribute.
4107 for (auto *F : D->specific_attrs<FormatMatchesAttr>()) {
4108 if (F->getType() == Format && F->getFormatIdx() == FormatIdx) {
4109 if (!CheckFormatStringsCompatible(GetFormatStringType(Format->getName()),
4110 F->getFormatString(), FormatStr))
4111 return nullptr;
4112
4113 // If we don't have a valid location for this attribute, adopt the
4114 // location.
4115 if (F->getLocation().isInvalid())
4116 F->setRange(CI.getRange());
4117 return nullptr;
4118 }
4119 }
4120
4121 return ::new (Context)
4122 FormatMatchesAttr(Context, CI, Format, FormatIdx, FormatStr);
4123}
4124
4131
4132/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
4133/// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
4134static bool handleFormatAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
4135 FormatAttrCommon *Info) {
4136 // Checks the first two arguments of the attribute; this is shared between
4137 // Format and FormatMatches attributes.
4138
4139 if (!AL.isArgIdent(0)) {
4140 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
4141 << AL << 1 << AANT_ArgumentIdentifier;
4142 return false;
4143 }
4144
4145 // In C++ the implicit 'this' function parameter also counts, and they are
4146 // counted from one.
4147 bool HasImplicitThisParam = hasImplicitObjectParameter(D);
4148 Info->NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
4149
4151 StringRef Format = Info->Identifier->getName();
4152
4153 if (normalizeName(Format)) {
4154 // If we've modified the string name, we need a new identifier for it.
4155 Info->Identifier = &S.Context.Idents.get(Format);
4156 }
4157
4158 // Check for supported formats.
4159 Info->Kind = getFormatAttrKind(Format);
4160
4161 if (Info->Kind == IgnoredFormat)
4162 return false;
4163
4164 if (Info->Kind == InvalidFormat) {
4165 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
4166 << AL << Info->Identifier->getName();
4167 return false;
4168 }
4169
4170 // checks for the 2nd argument
4171 Expr *IdxExpr = AL.getArgAsExpr(1);
4172 if (!S.checkUInt32Argument(AL, IdxExpr, Info->FormatStringIdx, 2))
4173 return false;
4174
4175 if (Info->FormatStringIdx < 1 || Info->FormatStringIdx > Info->NumArgs) {
4176 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4177 << AL << 2 << IdxExpr->getSourceRange();
4178 return false;
4179 }
4180
4181 // FIXME: Do we need to bounds check?
4182 unsigned ArgIdx = Info->FormatStringIdx - 1;
4183
4184 if (HasImplicitThisParam) {
4185 if (ArgIdx == 0) {
4186 S.Diag(AL.getLoc(),
4187 diag::err_format_attribute_implicit_this_format_string)
4188 << IdxExpr->getSourceRange();
4189 return false;
4190 }
4191 ArgIdx--;
4192 }
4193
4194 // make sure the format string is really a string
4195 QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
4196
4197 if (!S.ObjC().isNSStringType(Ty, true) && !S.ObjC().isCFStringType(Ty) &&
4198 (!Ty->isPointerType() ||
4200 S.Diag(AL.getLoc(), diag::err_format_attribute_not)
4201 << IdxExpr->getSourceRange()
4202 << getFunctionOrMethodParamRange(D, ArgIdx);
4203 return false;
4204 }
4205
4206 return true;
4207}
4208
4209static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4210 FormatAttrCommon Info;
4211 if (!handleFormatAttrCommon(S, D, AL, &Info))
4212 return;
4213
4214 // check the 3rd argument
4215 Expr *FirstArgExpr = AL.getArgAsExpr(2);
4216 uint32_t FirstArg;
4217 if (!S.checkUInt32Argument(AL, FirstArgExpr, FirstArg, 3))
4218 return;
4219
4220 // FirstArg == 0 is always valid.
4221 if (FirstArg != 0) {
4222 if (Info.Kind == StrftimeFormat) {
4223 // If the kind is strftime, FirstArg must be 0 because strftime does not
4224 // use any variadic arguments.
4225 S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
4226 << FirstArgExpr->getSourceRange()
4227 << FixItHint::CreateReplacement(FirstArgExpr->getSourceRange(), "0");
4228 return;
4229 } else if (isFunctionOrMethodVariadic(D)) {
4230 // Else, if the function is variadic, then FirstArg must be 0 or the
4231 // "position" of the ... parameter. It's unusual to use 0 with variadic
4232 // functions, so the fixit proposes the latter.
4233 if (FirstArg != Info.NumArgs + 1) {
4234 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4235 << AL << 3 << FirstArgExpr->getSourceRange()
4237 std::to_string(Info.NumArgs + 1));
4238 return;
4239 }
4240 } else {
4241 // Inescapable GCC compatibility diagnostic.
4242 S.Diag(D->getLocation(), diag::warn_gcc_requires_variadic_function) << AL;
4243 if (FirstArg <= Info.FormatStringIdx) {
4244 // Else, the function is not variadic, and FirstArg must be 0 or any
4245 // parameter after the format parameter. We don't offer a fixit because
4246 // there are too many possible good values.
4247 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4248 << AL << 3 << FirstArgExpr->getSourceRange();
4249 return;
4250 }
4251 }
4252 }
4253
4254 FormatAttr *NewAttr =
4255 S.mergeFormatAttr(D, AL, Info.Identifier, Info.FormatStringIdx, FirstArg);
4256 if (NewAttr)
4257 D->addAttr(NewAttr);
4258}
4259
4260static void handleFormatMatchesAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4261 FormatAttrCommon Info;
4262 if (!handleFormatAttrCommon(S, D, AL, &Info))
4263 return;
4264
4265 Expr *FormatStrExpr = AL.getArgAsExpr(2)->IgnoreParenImpCasts();
4266 if (auto *SL = dyn_cast<StringLiteral>(FormatStrExpr)) {
4268 if (S.ValidateFormatString(FST, SL))
4269 if (auto *NewAttr = S.mergeFormatMatchesAttr(D, AL, Info.Identifier,
4270 Info.FormatStringIdx, SL))
4271 D->addAttr(NewAttr);
4272 return;
4273 }
4274
4275 S.Diag(AL.getLoc(), diag::err_format_nonliteral)
4276 << FormatStrExpr->getSourceRange();
4277}
4278
4279/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
4280static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4281 // The index that identifies the callback callee is mandatory.
4282 if (AL.getNumArgs() == 0) {
4283 S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
4284 << AL.getRange();
4285 return;
4286 }
4287
4288 bool HasImplicitThisParam = hasImplicitObjectParameter(D);
4290
4291 FunctionDecl *FD = D->getAsFunction();
4292 assert(FD && "Expected a function declaration!");
4293
4294 llvm::StringMap<int> NameIdxMapping;
4295 NameIdxMapping["__"] = -1;
4296
4297 NameIdxMapping["this"] = 0;
4298
4299 int Idx = 1;
4300 for (const ParmVarDecl *PVD : FD->parameters())
4301 NameIdxMapping[PVD->getName()] = Idx++;
4302
4303 auto UnknownName = NameIdxMapping.end();
4304
4305 SmallVector<int, 8> EncodingIndices;
4306 for (unsigned I = 0, E = AL.getNumArgs(); I < E; ++I) {
4307 SourceRange SR;
4308 int32_t ArgIdx;
4309
4310 if (AL.isArgIdent(I)) {
4311 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
4312 auto It = NameIdxMapping.find(IdLoc->getIdentifierInfo()->getName());
4313 if (It == UnknownName) {
4314 S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
4315 << IdLoc->getIdentifierInfo() << IdLoc->getLoc();
4316 return;
4317 }
4318
4319 SR = SourceRange(IdLoc->getLoc());
4320 ArgIdx = It->second;
4321 } else if (AL.isArgExpr(I)) {
4322 Expr *IdxExpr = AL.getArgAsExpr(I);
4323
4324 // If the expression is not parseable as an int32_t we have a problem.
4325 if (!S.checkUInt32Argument(AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
4326 false)) {
4327 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4328 << AL << (I + 1) << IdxExpr->getSourceRange();
4329 return;
4330 }
4331
4332 // Check oob, excluding the special values, 0 and -1.
4333 if (ArgIdx < -1 || ArgIdx > NumArgs) {
4334 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
4335 << AL << (I + 1) << IdxExpr->getSourceRange();
4336 return;
4337 }
4338
4339 SR = IdxExpr->getSourceRange();
4340 } else {
4341 llvm_unreachable("Unexpected ParsedAttr argument type!");
4342 }
4343
4344 if (ArgIdx == 0 && !HasImplicitThisParam) {
4345 S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
4346 << (I + 1) << SR;
4347 return;
4348 }
4349
4350 // Adjust for the case we do not have an implicit "this" parameter. In this
4351 // case we decrease all positive values by 1 to get LLVM argument indices.
4352 if (!HasImplicitThisParam && ArgIdx > 0)
4353 ArgIdx -= 1;
4354
4355 EncodingIndices.push_back(ArgIdx);
4356 }
4357
4358 int CalleeIdx = EncodingIndices.front();
4359 // Check if the callee index is proper, thus not "this" and not "unknown".
4360 // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
4361 // is false and positive if "HasImplicitThisParam" is true.
4362 if (CalleeIdx < (int)HasImplicitThisParam) {
4363 S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
4364 << AL.getRange();
4365 return;
4366 }
4367
4368 // Get the callee type, note the index adjustment as the AST doesn't contain
4369 // the this type (which the callee cannot reference anyway!).
4370 const Type *CalleeType =
4371 getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
4372 .getTypePtr();
4373 if (!CalleeType || !CalleeType->isFunctionPointerType()) {
4374 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
4375 << AL.getRange();
4376 return;
4377 }
4378
4379 const Type *CalleeFnType =
4381
4382 // TODO: Check the type of the callee arguments.
4383
4384 const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
4385 if (!CalleeFnProtoType) {
4386 S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
4387 << AL.getRange();
4388 return;
4389 }
4390
4391 if (CalleeFnProtoType->getNumParams() != EncodingIndices.size() - 1) {
4392 S.Diag(AL.getLoc(), diag::err_attribute_wrong_arg_count_for_func)
4393 << AL << QualType{CalleeFnProtoType, 0}
4394 << CalleeFnProtoType->getNumParams()
4395 << (unsigned)(EncodingIndices.size() - 1);
4396 return;
4397 }
4398
4399 if (CalleeFnProtoType->isVariadic()) {
4400 S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
4401 return;
4402 }
4403
4404 // Do not allow multiple callback attributes.
4405 if (D->hasAttr<CallbackAttr>()) {
4406 S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
4407 return;
4408 }
4409
4410 D->addAttr(::new (S.Context) CallbackAttr(
4411 S.Context, AL, EncodingIndices.data(), EncodingIndices.size()));
4412}
4413
4414LifetimeCaptureByAttr *Sema::ParseLifetimeCaptureByAttr(const ParsedAttr &AL,
4415 StringRef ParamName) {
4416 StringRef AttrName = AL.getAttrName()->getName();
4417 StringRef SpecialEntity;
4418 if (AttrName == "lifetime_capture_by_this")
4419 SpecialEntity = "this";
4420 else if (AttrName == "lifetime_capture_by_global")
4421 SpecialEntity = "global";
4422 else if (AttrName == "lifetime_capture_by_unknown")
4423 SpecialEntity = "unknown";
4424
4425 if (!SpecialEntity.empty() && AL.getNumArgs() != 0) {
4426 Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 0;
4427 return nullptr;
4428 }
4429
4430 // Atleast one capture by is required.
4431 if (SpecialEntity.empty() && AL.getNumArgs() == 0) {
4432 Diag(AL.getLoc(), diag::err_capture_by_attribute_no_entity)
4433 << AL.getRange();
4434 return nullptr;
4435 }
4436 unsigned N = SpecialEntity.empty() ? AL.getNumArgs() : 1;
4437 auto ParamIdents =
4439 auto ParamLocs =
4441 if (!SpecialEntity.empty()) {
4442 ParamIdents[0] = &Context.Idents.get(SpecialEntity);
4443 ParamLocs[0] = AL.getRange().getEnd();
4444 int FakeParamIndices[] = {LifetimeCaptureByAttr::Invalid};
4445 auto *CapturedBy =
4446 LifetimeCaptureByAttr::Create(Context, FakeParamIndices, 1, AL);
4447 CapturedBy->setArgs(ParamIdents, ParamLocs);
4448 return CapturedBy;
4449 }
4450
4451 bool IsValid = true;
4452 for (unsigned I = 0; I < N; ++I) {
4453 if (AL.isArgExpr(I)) {
4454 Expr *E = AL.getArgAsExpr(I);
4455 Diag(E->getExprLoc(), diag::err_capture_by_attribute_argument_unknown)
4456 << E << E->getExprLoc();
4457 IsValid = false;
4458 continue;
4459 }
4460 assert(AL.isArgIdent(I));
4461 IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
4462 StringRef Name = IdLoc->getIdentifierInfo()->getName();
4463 StringRef Replacement;
4464 if (Name == "this")
4465 Replacement = "lifetime_capture_by_this";
4466 else if (Name == "global")
4467 Replacement = "lifetime_capture_by_global";
4468 else if (Name == "unknown")
4469 Replacement = "lifetime_capture_by_unknown";
4470 if (!Replacement.empty())
4471 Diag(IdLoc->getLoc(), diag::warn_deprecated_capture_by_special_entity)
4472 << Name << Replacement << IdLoc->getLoc();
4473 if (IdLoc->getIdentifierInfo()->getName() == ParamName) {
4474 Diag(IdLoc->getLoc(), diag::err_capture_by_references_itself)
4475 << IdLoc->getLoc();
4476 IsValid = false;
4477 continue;
4478 }
4479 ParamIdents[I] = IdLoc->getIdentifierInfo();
4480 ParamLocs[I] = IdLoc->getLoc();
4481 }
4482 if (!IsValid)
4483 return nullptr;
4484 SmallVector<int> FakeParamIndices(N, LifetimeCaptureByAttr::Invalid);
4485 auto *CapturedBy =
4486 LifetimeCaptureByAttr::Create(Context, FakeParamIndices.data(), N, AL);
4487 CapturedBy->setArgs(ParamIdents, ParamLocs);
4488 return CapturedBy;
4489}
4490
4492 const ParsedAttr &AL) {
4493 auto *PVD = dyn_cast<ParmVarDecl>(D);
4494 assert(PVD);
4495 auto *CaptureByAttr = S.ParseLifetimeCaptureByAttr(AL, PVD->getName());
4496 if (!CaptureByAttr)
4497 return;
4498
4499 enum class SpellingKind { ParameterList, This, Global, Unknown };
4500 auto GetSpellingKind = [](const LifetimeCaptureByAttr *A) {
4501 if (A->isThis())
4502 return SpellingKind::This;
4503 if (A->isGlobal())
4504 return SpellingKind::Global;
4505 if (A->isUnknown())
4506 return SpellingKind::Unknown;
4507 return SpellingKind::ParameterList;
4508 };
4509 auto GetSpellingName = [](SpellingKind Kind) -> StringRef {
4510 switch (Kind) {
4511 case SpellingKind::ParameterList:
4512 return "lifetime_capture_by";
4513 case SpellingKind::This:
4514 return "lifetime_capture_by_this";
4515 case SpellingKind::Global:
4516 return "lifetime_capture_by_global";
4517 case SpellingKind::Unknown:
4518 return "lifetime_capture_by_unknown";
4519 }
4520 llvm_unreachable("unknown lifetime_capture_by spelling kind");
4521 };
4522
4523 SpellingKind NewKind = GetSpellingKind(CaptureByAttr);
4524 for (const auto *Existing : D->specific_attrs<LifetimeCaptureByAttr>()) {
4525 if (GetSpellingKind(Existing) == NewKind) {
4526 S.Diag(AL.getLoc(), diag::err_capture_by_attribute_multiple)
4527 << GetSpellingName(NewKind) << AL.getRange();
4528 return;
4529 }
4530 }
4531
4532 D->addAttr(CaptureByAttr);
4533}
4534
4536 bool HasImplicitThisParam = hasImplicitObjectParameter(FD);
4538 for (ParmVarDecl *PVD : FD->parameters())
4539 for (auto *A : PVD->specific_attrs<LifetimeCaptureByAttr>())
4540 Attrs.push_back(A);
4541 if (HasImplicitThisParam) {
4542 TypeSourceInfo *TSI = FD->getTypeSourceInfo();
4543 if (!TSI)
4544 return;
4546 for (TypeLoc TL = TSI->getTypeLoc();
4547 (ATL = TL.getAsAdjusted<AttributedTypeLoc>());
4548 TL = ATL.getModifiedLoc()) {
4549 if (auto *A = ATL.getAttrAs<LifetimeCaptureByAttr>())
4550 Attrs.push_back(const_cast<LifetimeCaptureByAttr *>(A));
4551 }
4552 }
4553 if (Attrs.empty())
4554 return;
4555 llvm::StringMap<int> NameIdxMapping = {
4556 {"global", LifetimeCaptureByAttr::Global},
4557 {"unknown", LifetimeCaptureByAttr::Unknown}};
4558 int Idx = 0;
4559 if (HasImplicitThisParam) {
4560 NameIdxMapping["this"] = 0;
4561 Idx++;
4562 }
4563 for (const ParmVarDecl *PVD : FD->parameters())
4564 NameIdxMapping[PVD->getName()] = Idx++;
4565 auto DisallowReservedParams = [&](StringRef Reserved) {
4566 for (const ParmVarDecl *PVD : FD->parameters())
4567 if (PVD->getName() == Reserved)
4568 Diag(PVD->getLocation(), diag::err_capture_by_param_uses_reserved_name)
4569 << PVD->getName();
4570 };
4571 for (auto *CapturedBy : Attrs) {
4572 const auto &Entities = CapturedBy->getArgIdents();
4573 for (size_t I = 0; I < Entities.size(); ++I) {
4574 StringRef Name = Entities[I]->getName();
4575 auto It = NameIdxMapping.find(Name);
4576 if (It == NameIdxMapping.end()) {
4577 auto Loc = CapturedBy->getArgLocs()[I];
4578 if (!HasImplicitThisParam && Name == "this") {
4579 unsigned DiagID =
4580 CapturedBy->isStandaloneSpecial()
4581 ? diag::err_capture_by_this_attr_without_implicit_this
4582 : diag::err_capture_by_implicit_this_not_available;
4583 Diag(Loc, DiagID) << Loc;
4584 } else
4585 Diag(Loc, diag::err_capture_by_attribute_argument_unknown)
4586 << Entities[I] << Loc;
4587 continue;
4588 }
4589 if ((Name == "unknown" || Name == "global") &&
4590 !CapturedBy->isStandaloneSpecial())
4591 DisallowReservedParams(Name);
4592 CapturedBy->setParamIdx(I, It->second);
4593 }
4594 }
4595}
4596
4597static bool isFunctionLike(const Type &T) {
4598 // Check for explicit function types.
4599 // 'called_once' is only supported in Objective-C and it has
4600 // function pointers and block pointers.
4601 return T.isFunctionPointerType() || T.isBlockPointerType();
4602}
4603
4604/// Handle 'called_once' attribute.
4605static void handleCalledOnceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4606 // 'called_once' only applies to parameters representing functions.
4607 QualType T = cast<ParmVarDecl>(D)->getType();
4608
4609 if (!isFunctionLike(*T)) {
4610 S.Diag(AL.getLoc(), diag::err_called_once_attribute_wrong_type);
4611 return;
4612 }
4613
4614 D->addAttr(::new (S.Context) CalledOnceAttr(S.Context, AL));
4615}
4616
4617static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4618 // Try to find the underlying union declaration.
4619 RecordDecl *RD = nullptr;
4620 const auto *TD = dyn_cast<TypedefNameDecl>(D);
4621 if (TD && TD->getUnderlyingType()->isUnionType())
4622 RD = TD->getUnderlyingType()->getAsRecordDecl();
4623 else
4624 RD = dyn_cast<RecordDecl>(D);
4625
4626 if (!RD || !RD->isUnion()) {
4627 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
4629 return;
4630 }
4631
4632 if (!RD->isCompleteDefinition()) {
4633 if (!RD->isBeingDefined())
4634 S.Diag(AL.getLoc(),
4635 diag::warn_transparent_union_attribute_not_definition);
4636 return;
4637 }
4638
4640 FieldEnd = RD->field_end();
4641 if (Field == FieldEnd) {
4642 S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
4643 return;
4644 }
4645
4646 FieldDecl *FirstField = *Field;
4647 QualType FirstType = FirstField->getType();
4648 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
4649 S.Diag(FirstField->getLocation(),
4650 diag::warn_transparent_union_attribute_floating)
4651 << FirstType->isVectorType() << FirstType;
4652 return;
4653 }
4654
4655 if (FirstType->isIncompleteType())
4656 return;
4657 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
4658 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
4659 for (; Field != FieldEnd; ++Field) {
4660 QualType FieldType = Field->getType();
4661 if (FieldType->isIncompleteType())
4662 return;
4663 // FIXME: this isn't fully correct; we also need to test whether the
4664 // members of the union would all have the same calling convention as the
4665 // first member of the union. Checking just the size and alignment isn't
4666 // sufficient (consider structs passed on the stack instead of in registers
4667 // as an example).
4668 if (S.Context.getTypeSize(FieldType) != FirstSize ||
4669 S.Context.getTypeAlign(FieldType) > FirstAlign) {
4670 // Warn if we drop the attribute.
4671 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
4672 unsigned FieldBits = isSize ? S.Context.getTypeSize(FieldType)
4673 : S.Context.getTypeAlign(FieldType);
4674 S.Diag(Field->getLocation(),
4675 diag::warn_transparent_union_attribute_field_size_align)
4676 << isSize << *Field << FieldBits;
4677 unsigned FirstBits = isSize ? FirstSize : FirstAlign;
4678 S.Diag(FirstField->getLocation(),
4679 diag::note_transparent_union_first_field_size_align)
4680 << isSize << FirstBits;
4681 return;
4682 }
4683 }
4684
4685 RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL));
4686}
4687
4688static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4689 auto *Attr = S.CreateAnnotationAttr(AL);
4690 if (Attr) {
4691 D->addAttr(Attr);
4692 }
4693}
4694
4695static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4696 S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0));
4697}
4698
4700 SourceLocation AttrLoc = CI.getLoc();
4701
4702 QualType T;
4703 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
4704 T = TD->getUnderlyingType();
4705 else if (const auto *VD = dyn_cast<ValueDecl>(D))
4706 T = VD->getType();
4707 else
4708 llvm_unreachable("Unknown decl type for align_value");
4709
4710 if (!T->isDependentType() && !T->isAnyPointerType() &&
4711 !T->isReferenceType() && !T->isMemberPointerType()) {
4712 Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
4713 << CI << T << D->getSourceRange();
4714 return;
4715 }
4716
4717 if (!E->isValueDependent()) {
4718 llvm::APSInt Alignment;
4720 E, &Alignment, diag::err_align_value_attribute_argument_not_int);
4721 if (ICE.isInvalid())
4722 return;
4723
4724 if (!Alignment.isPowerOf2()) {
4725 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
4726 << E->getSourceRange();
4727 return;
4728 }
4729
4730 D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
4731 return;
4732 }
4733
4734 // Save dependent expressions in the AST to be instantiated.
4735 D->addAttr(::new (Context) AlignValueAttr(Context, CI, E));
4736}
4737
4738static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4739 if (AL.hasParsedType()) {
4740 const ParsedType &TypeArg = AL.getTypeArg();
4741 TypeSourceInfo *TInfo;
4742 (void)S.GetTypeFromParser(
4743 ParsedType::getFromOpaquePtr(TypeArg.getAsOpaquePtr()), &TInfo);
4744 if (AL.isPackExpansion() &&
4746 S.Diag(AL.getEllipsisLoc(),
4747 diag::err_pack_expansion_without_parameter_packs);
4748 return;
4749 }
4750
4751 if (!AL.isPackExpansion() &&
4753 TInfo, Sema::UPPC_Expression))
4754 return;
4755
4756 S.AddAlignedAttr(D, AL, TInfo, AL.isPackExpansion());
4757 return;
4758 }
4759
4760 // check the attribute arguments.
4761 if (AL.getNumArgs() > 1) {
4762 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
4763 return;
4764 }
4765
4766 if (AL.getNumArgs() == 0) {
4767 D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
4768 return;
4769 }
4770
4771 Expr *E = AL.getArgAsExpr(0);
4773 S.Diag(AL.getEllipsisLoc(),
4774 diag::err_pack_expansion_without_parameter_packs);
4775 return;
4776 }
4777
4779 return;
4780
4781 S.AddAlignedAttr(D, AL, E, AL.isPackExpansion());
4782}
4783
4784/// Perform checking of type validity
4785///
4786/// C++11 [dcl.align]p1:
4787/// An alignment-specifier may be applied to a variable or to a class
4788/// data member, but it shall not be applied to a bit-field, a function
4789/// parameter, the formal parameter of a catch clause, or a variable
4790/// declared with the register storage class specifier. An
4791/// alignment-specifier may also be applied to the declaration of a class
4792/// or enumeration type.
4793/// CWG 2354:
4794/// CWG agreed to remove permission for alignas to be applied to
4795/// enumerations.
4796/// C11 6.7.5/2:
4797/// An alignment attribute shall not be specified in a declaration of
4798/// a typedef, or a bit-field, or a function, or a parameter, or an
4799/// object declared with the register storage-class specifier.
4801 const AlignedAttr &Attr,
4802 SourceLocation AttrLoc) {
4803 int DiagKind = -1;
4804 if (isa<ParmVarDecl>(D)) {
4805 DiagKind = 0;
4806 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
4807 if (VD->getStorageClass() == SC_Register)
4808 DiagKind = 1;
4809 if (VD->isExceptionVariable())
4810 DiagKind = 2;
4811 } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
4812 if (FD->isBitField())
4813 DiagKind = 3;
4814 } else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
4815 if (ED->getLangOpts().CPlusPlus)
4816 DiagKind = 4;
4817 } else if (!isa<TagDecl>(D)) {
4818 return S.Diag(AttrLoc, diag::err_attribute_wrong_decl_type)
4820 << (Attr.isC11() ? ExpectedVariableOrField
4822 }
4823 if (DiagKind != -1) {
4824 return S.Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
4825 << &Attr << DiagKind;
4826 }
4827 return false;
4828}
4829
4831 bool IsPackExpansion) {
4832 AlignedAttr TmpAttr(Context, CI, true, E);
4833 SourceLocation AttrLoc = CI.getLoc();
4834
4835 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
4836 if (TmpAttr.isAlignas() &&
4837 validateAlignasAppliedType(*this, D, TmpAttr, AttrLoc))
4838 return;
4839
4840 if (E->isValueDependent()) {
4841 // We can't support a dependent alignment on a non-dependent type,
4842 // because we have no way to model that a type is "alignment-dependent"
4843 // but not dependent in any other way.
4844 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
4845 if (!TND->getUnderlyingType()->isDependentType()) {
4846 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
4847 << E->getSourceRange();
4848 return;
4849 }
4850 }
4851
4852 // Save dependent expressions in the AST to be instantiated.
4853 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
4854 AA->setPackExpansion(IsPackExpansion);
4855 D->addAttr(AA);
4856 return;
4857 }
4858
4859 // FIXME: Cache the number on the AL object?
4860 llvm::APSInt Alignment;
4862 E, &Alignment, diag::err_aligned_attribute_argument_not_int);
4863 if (ICE.isInvalid())
4864 return;
4865
4867 if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
4868 MaximumAlignment = std::min(MaximumAlignment, uint64_t(8192));
4869 if (Alignment > MaximumAlignment) {
4870 Diag(AttrLoc, diag::err_attribute_aligned_too_great)
4872 return;
4873 }
4874
4875 uint64_t AlignVal = Alignment.getZExtValue();
4876 // C++11 [dcl.align]p2:
4877 // -- if the constant expression evaluates to zero, the alignment
4878 // specifier shall have no effect
4879 // C11 6.7.5p6:
4880 // An alignment specification of zero has no effect.
4881 if (!(TmpAttr.isAlignas() && !Alignment)) {
4882 if (!llvm::isPowerOf2_64(AlignVal)) {
4883 Diag(AttrLoc, diag::err_alignment_not_power_of_two)
4884 << E->getSourceRange();
4885 return;
4886 }
4887 }
4888
4889 const auto *VD = dyn_cast<VarDecl>(D);
4890 if (VD) {
4891 unsigned MaxTLSAlign =
4892 Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
4893 .getQuantity();
4894 if (MaxTLSAlign && AlignVal > MaxTLSAlign &&
4895 VD->getTLSKind() != VarDecl::TLS_None) {
4896 Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
4897 << (unsigned)AlignVal << VD << MaxTLSAlign;
4898 return;
4899 }
4900 }
4901
4902 // On AIX, an aligned attribute can not decrease the alignment when applied
4903 // to a variable declaration with vector type.
4904 if (VD && Context.getTargetInfo().getTriple().isOSAIX()) {
4905 const Type *Ty = VD->getType().getTypePtr();
4906 if (Ty->isVectorType() && AlignVal < 16) {
4907 Diag(VD->getLocation(), diag::warn_aligned_attr_underaligned)
4908 << VD->getType() << 16;
4909 return;
4910 }
4911 }
4912
4913 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
4914 AA->setPackExpansion(IsPackExpansion);
4915 AA->setCachedAlignmentValue(
4916 static_cast<unsigned>(AlignVal * Context.getCharWidth()));
4917 D->addAttr(AA);
4918}
4919
4921 TypeSourceInfo *TS, bool IsPackExpansion) {
4922 AlignedAttr TmpAttr(Context, CI, false, TS);
4923 SourceLocation AttrLoc = CI.getLoc();
4924
4925 // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
4926 if (TmpAttr.isAlignas() &&
4927 validateAlignasAppliedType(*this, D, TmpAttr, AttrLoc))
4928 return;
4929
4930 if (TS->getType()->isDependentType()) {
4931 // We can't support a dependent alignment on a non-dependent type,
4932 // because we have no way to model that a type is "type-dependent"
4933 // but not dependent in any other way.
4934 if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
4935 if (!TND->getUnderlyingType()->isDependentType()) {
4936 Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
4937 << TS->getTypeLoc().getSourceRange();
4938 return;
4939 }
4940 }
4941
4942 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
4943 AA->setPackExpansion(IsPackExpansion);
4944 D->addAttr(AA);
4945 return;
4946 }
4947
4948 const auto *VD = dyn_cast<VarDecl>(D);
4949 unsigned AlignVal = TmpAttr.getAlignment(Context);
4950 // On AIX, an aligned attribute can not decrease the alignment when applied
4951 // to a variable declaration with vector type.
4952 if (VD && Context.getTargetInfo().getTriple().isOSAIX()) {
4953 const Type *Ty = VD->getType().getTypePtr();
4954 if (Ty->isVectorType() &&
4955 Context.toCharUnitsFromBits(AlignVal).getQuantity() < 16) {
4956 Diag(VD->getLocation(), diag::warn_aligned_attr_underaligned)
4957 << VD->getType() << 16;
4958 return;
4959 }
4960 }
4961
4962 AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
4963 AA->setPackExpansion(IsPackExpansion);
4964 AA->setCachedAlignmentValue(AlignVal);
4965 D->addAttr(AA);
4966}
4967
4969 assert(D->hasAttrs() && "no attributes on decl");
4970
4971 QualType UnderlyingTy, DiagTy;
4972 if (const auto *VD = dyn_cast<ValueDecl>(D)) {
4973 UnderlyingTy = DiagTy = VD->getType();
4974 } else {
4975 UnderlyingTy = DiagTy = Context.getCanonicalTagType(cast<TagDecl>(D));
4976 if (const auto *ED = dyn_cast<EnumDecl>(D))
4977 UnderlyingTy = ED->getIntegerType();
4978 }
4979 if (DiagTy->isDependentType() || DiagTy->isIncompleteType())
4980 return;
4981
4982 // C++11 [dcl.align]p5, C11 6.7.5/4:
4983 // The combined effect of all alignment attributes in a declaration shall
4984 // not specify an alignment that is less strict than the alignment that
4985 // would otherwise be required for the entity being declared.
4986 AlignedAttr *AlignasAttr = nullptr;
4987 AlignedAttr *LastAlignedAttr = nullptr;
4988 unsigned Align = 0;
4989 for (auto *I : D->specific_attrs<AlignedAttr>()) {
4990 if (I->isAlignmentDependent())
4991 return;
4992 if (I->isAlignas())
4993 AlignasAttr = I;
4994 Align = std::max(Align, I->getAlignment(Context));
4995 LastAlignedAttr = I;
4996 }
4997
4998 if (Align && DiagTy->isSizelessType()) {
4999 Diag(LastAlignedAttr->getLocation(), diag::err_attribute_sizeless_type)
5000 << LastAlignedAttr << DiagTy;
5001 } else if (AlignasAttr && Align) {
5002 CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
5003 CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
5004 if (NaturalAlign > RequestedAlign)
5005 Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
5006 << DiagTy << (unsigned)NaturalAlign.getQuantity();
5007 }
5008}
5009
5011 CXXRecordDecl *RD, SourceRange Range, bool BestCase,
5012 MSInheritanceModel ExplicitModel) {
5013 assert(RD->hasDefinition() && "RD has no definition!");
5014
5015 // We may not have seen base specifiers or any virtual methods yet. We will
5016 // have to wait until the record is defined to catch any mismatches.
5017 if (!RD->getDefinition()->isCompleteDefinition())
5018 return false;
5019
5020 // The unspecified model never matches what a definition could need.
5021 if (ExplicitModel == MSInheritanceModel::Unspecified)
5022 return false;
5023
5024 if (BestCase) {
5025 if (RD->calculateInheritanceModel() == ExplicitModel)
5026 return false;
5027 } else {
5028 if (RD->calculateInheritanceModel() <= ExplicitModel)
5029 return false;
5030 }
5031
5032 Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
5033 << 0 /*definition*/;
5034 Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) << RD;
5035 return true;
5036}
5037
5038/// parseModeAttrArg - Parses attribute mode string and returns parsed type
5039/// attribute.
5040static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
5041 bool &IntegerMode, bool &ComplexMode,
5042 FloatModeKind &ExplicitType) {
5043 IntegerMode = true;
5044 ComplexMode = false;
5045 ExplicitType = FloatModeKind::NoFloat;
5046 switch (Str.size()) {
5047 case 2:
5048 switch (Str[0]) {
5049 case 'Q':
5050 DestWidth = 8;
5051 break;
5052 case 'H':
5053 DestWidth = 16;
5054 break;
5055 case 'S':
5056 DestWidth = 32;
5057 break;
5058 case 'D':
5059 DestWidth = 64;
5060 break;
5061 case 'X':
5062 DestWidth = 96;
5063 break;
5064 case 'K': // KFmode - IEEE quad precision (__float128)
5065 ExplicitType = FloatModeKind::Float128;
5066 DestWidth = Str[1] == 'I' ? 0 : 128;
5067 break;
5068 case 'T':
5069 ExplicitType = FloatModeKind::LongDouble;
5070 DestWidth = 128;
5071 break;
5072 case 'I':
5073 ExplicitType = FloatModeKind::Ibm128;
5074 DestWidth = Str[1] == 'I' ? 0 : 128;
5075 break;
5076 }
5077 if (Str[1] == 'F') {
5078 IntegerMode = false;
5079 } else if (Str[1] == 'C') {
5080 IntegerMode = false;
5081 ComplexMode = true;
5082 } else if (Str[1] != 'I') {
5083 DestWidth = 0;
5084 }
5085 break;
5086 case 4:
5087 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
5088 // pointer on PIC16 and other embedded platforms.
5089 if (Str == "word")
5090 DestWidth = S.Context.getTargetInfo().getRegisterWidth();
5091 else if (Str == "byte")
5092 DestWidth = S.Context.getTargetInfo().getCharWidth();
5093 break;
5094 case 7:
5095 if (Str == "pointer")
5097 break;
5098 case 11:
5099 if (Str == "unwind_word")
5100 DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
5101 break;
5102 }
5103}
5104
5105/// handleModeAttr - This attribute modifies the width of a decl with primitive
5106/// type.
5107///
5108/// Despite what would be logical, the mode attribute is a decl attribute, not a
5109/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
5110/// HImode, not an intermediate pointer.
5111static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5112 // This attribute isn't documented, but glibc uses it. It changes
5113 // the width of an int or unsigned int to the specified size.
5114 if (!AL.isArgIdent(0)) {
5115 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
5116 << AL << AANT_ArgumentIdentifier;
5117 return;
5118 }
5119
5121
5122 S.AddModeAttr(D, AL, Name);
5123}
5124
5126 const IdentifierInfo *Name, bool InInstantiation) {
5127 StringRef Str = Name->getName();
5128 normalizeName(Str);
5129 SourceLocation AttrLoc = CI.getLoc();
5130
5131 unsigned DestWidth = 0;
5132 bool IntegerMode = true;
5133 bool ComplexMode = false;
5135 llvm::APInt VectorSize(64, 0);
5136 if (Str.size() >= 4 && Str[0] == 'V') {
5137 // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2).
5138 size_t StrSize = Str.size();
5139 size_t VectorStringLength = 0;
5140 while ((VectorStringLength + 1) < StrSize &&
5141 isdigit(Str[VectorStringLength + 1]))
5142 ++VectorStringLength;
5143 if (VectorStringLength &&
5144 !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) &&
5145 VectorSize.isPowerOf2()) {
5146 parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth,
5147 IntegerMode, ComplexMode, ExplicitType);
5148 // Avoid duplicate warning from template instantiation.
5149 if (!InInstantiation)
5150 Diag(AttrLoc, diag::warn_vector_mode_deprecated);
5151 } else {
5152 VectorSize = 0;
5153 }
5154 }
5155
5156 if (!VectorSize)
5157 parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode,
5158 ExplicitType);
5159
5160 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
5161 // and friends, at least with glibc.
5162 // FIXME: Make sure floating-point mappings are accurate
5163 // FIXME: Support XF and TF types
5164 if (!DestWidth) {
5165 Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name;
5166 return;
5167 }
5168
5169 QualType OldTy;
5170 if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
5171 OldTy = TD->getUnderlyingType();
5172 else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
5173 // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'.
5174 // Try to get type from enum declaration, default to int.
5175 OldTy = ED->getIntegerType();
5176 if (OldTy.isNull())
5177 OldTy = Context.IntTy;
5178 } else
5179 OldTy = cast<ValueDecl>(D)->getType();
5180
5181 if (OldTy->isDependentType()) {
5182 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
5183 return;
5184 }
5185
5186 // Base type can also be a vector type (see PR17453).
5187 // Distinguish between base type and base element type.
5188 QualType OldElemTy = OldTy;
5189 if (const auto *VT = OldTy->getAs<VectorType>())
5190 OldElemTy = VT->getElementType();
5191
5192 // GCC allows 'mode' attribute on enumeration types (even incomplete), except
5193 // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete
5194 // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected.
5195 if ((isa<EnumDecl>(D) || OldElemTy->isEnumeralType()) &&
5196 VectorSize.getBoolValue()) {
5197 Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << CI.getRange();
5198 return;
5199 }
5200 bool IntegralOrAnyEnumType = (OldElemTy->isIntegralOrEnumerationType() &&
5201 !OldElemTy->isBitIntType()) ||
5202 OldElemTy->isEnumeralType();
5203
5204 if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() &&
5205 !IntegralOrAnyEnumType)
5206 Diag(AttrLoc, diag::err_mode_not_primitive);
5207 else if (IntegerMode) {
5208 if (!IntegralOrAnyEnumType)
5209 Diag(AttrLoc, diag::err_mode_wrong_type);
5210 } else if (ComplexMode) {
5211 if (!OldElemTy->isComplexType())
5212 Diag(AttrLoc, diag::err_mode_wrong_type);
5213 } else {
5214 if (!OldElemTy->isFloatingType())
5215 Diag(AttrLoc, diag::err_mode_wrong_type);
5216 }
5217
5218 QualType NewElemTy;
5219
5220 if (IntegerMode)
5221 NewElemTy = Context.getIntTypeForBitwidth(DestWidth,
5222 OldElemTy->isSignedIntegerType());
5223 else
5224 NewElemTy = Context.getRealTypeForBitwidth(DestWidth, ExplicitType);
5225
5226 if (NewElemTy.isNull()) {
5227 // Only emit diagnostic on host for 128-bit mode attribute
5228 if (!(DestWidth == 128 &&
5229 (getLangOpts().CUDAIsDevice || getLangOpts().SYCLIsDevice)))
5230 Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name;
5231 return;
5232 }
5233
5234 if (ComplexMode) {
5235 NewElemTy = Context.getComplexType(NewElemTy);
5236 }
5237
5238 QualType NewTy = NewElemTy;
5239 if (VectorSize.getBoolValue()) {
5240 NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(),
5242 } else if (const auto *OldVT = OldTy->getAs<VectorType>()) {
5243 // Complex machine mode does not support base vector types.
5244 if (ComplexMode) {
5245 Diag(AttrLoc, diag::err_complex_mode_vector_type);
5246 return;
5247 }
5248 unsigned NumElements = Context.getTypeSize(OldElemTy) *
5249 OldVT->getNumElements() /
5250 Context.getTypeSize(NewElemTy);
5251 NewTy =
5252 Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind());
5253 }
5254
5255 if (NewTy.isNull()) {
5256 Diag(AttrLoc, diag::err_mode_wrong_type);
5257 return;
5258 }
5259
5260 // Install the new type.
5261 if (auto *TD = dyn_cast<TypedefNameDecl>(D))
5262 TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy);
5263 else if (auto *ED = dyn_cast<EnumDecl>(D))
5264 ED->setIntegerType(NewTy);
5265 else
5266 cast<ValueDecl>(D)->setType(NewTy);
5267
5268 D->addAttr(::new (Context) ModeAttr(Context, CI, Name));
5269}
5270
5271static void handleNonStringAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5272 // This only applies to fields and variable declarations which have an array
5273 // type or pointer type, with character elements.
5274 QualType QT = cast<ValueDecl>(D)->getType();
5275 if ((!QT->isArrayType() && !QT->isPointerType()) ||
5277 S.Diag(D->getBeginLoc(), diag::warn_attribute_non_character_array)
5278 << AL << AL.isRegularKeywordAttribute() << QT << AL.getRange();
5279 return;
5280 }
5281
5282 D->addAttr(::new (S.Context) NonStringAttr(S.Context, AL));
5283}
5284
5285static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5286 D->addAttr(::new (S.Context) NoDebugAttr(S.Context, AL));
5287}
5288
5290 const AttributeCommonInfo &CI,
5291 const IdentifierInfo *Ident) {
5292 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
5293 Diag(CI.getLoc(), diag::warn_attribute_ignored) << Ident;
5294 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
5295 return nullptr;
5296 }
5297
5298 if (D->hasAttr<AlwaysInlineAttr>())
5299 return nullptr;
5300
5301 return ::new (Context) AlwaysInlineAttr(Context, CI);
5302}
5303
5304InternalLinkageAttr *Sema::mergeInternalLinkageAttr(Decl *D,
5305 const ParsedAttr &AL) {
5306 if (const auto *VD = dyn_cast<VarDecl>(D)) {
5307 // Attribute applies to Var but not any subclass of it (like ParmVar,
5308 // ImplicitParm or VarTemplateSpecialization).
5309 if (VD->getKind() != Decl::Var) {
5310 Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5311 << AL << AL.isRegularKeywordAttribute()
5314 return nullptr;
5315 }
5316 // Attribute does not apply to non-static local variables.
5317 if (VD->hasLocalStorage()) {
5318 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
5319 return nullptr;
5320 }
5321 }
5322
5323 return ::new (Context) InternalLinkageAttr(Context, AL);
5324}
5325InternalLinkageAttr *
5326Sema::mergeInternalLinkageAttr(Decl *D, const InternalLinkageAttr &AL) {
5327 if (const auto *VD = dyn_cast<VarDecl>(D)) {
5328 // Attribute applies to Var but not any subclass of it (like ParmVar,
5329 // ImplicitParm or VarTemplateSpecialization).
5330 if (VD->getKind() != Decl::Var) {
5331 Diag(AL.getLocation(), diag::warn_attribute_wrong_decl_type)
5332 << &AL << AL.isRegularKeywordAttribute()
5335 return nullptr;
5336 }
5337 // Attribute does not apply to non-static local variables.
5338 if (VD->hasLocalStorage()) {
5339 Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage);
5340 return nullptr;
5341 }
5342 }
5343
5344 return ::new (Context) InternalLinkageAttr(Context, AL);
5345}
5346
5348 if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) {
5349 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'minsize'";
5350 Diag(Optnone->getLocation(), diag::note_conflicting_attribute);
5351 return nullptr;
5352 }
5353
5354 if (D->hasAttr<MinSizeAttr>())
5355 return nullptr;
5356
5357 return ::new (Context) MinSizeAttr(Context, CI);
5358}
5359
5361 const AttributeCommonInfo &CI) {
5362 if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) {
5363 Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline;
5364 Diag(CI.getLoc(), diag::note_conflicting_attribute);
5365 D->dropAttr<AlwaysInlineAttr>();
5366 }
5367 if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) {
5368 Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize;
5369 Diag(CI.getLoc(), diag::note_conflicting_attribute);
5370 D->dropAttr<MinSizeAttr>();
5371 }
5372
5373 if (D->hasAttr<OptimizeNoneAttr>())
5374 return nullptr;
5375
5376 return ::new (Context) OptimizeNoneAttr(Context, CI);
5377}
5378
5379static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5380 AlwaysInlineAttr AIA(S.Context, AL);
5381 if (!S.getLangOpts().MicrosoftExt &&
5382 (AIA.isMSVCForceInline() || AIA.isMSVCForceInlineCalls())) {
5383 S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
5384 return;
5385 }
5386 if (AIA.isMSVCForceInlineCalls()) {
5387 S.Diag(AL.getLoc(), diag::warn_stmt_attribute_ignored_in_function)
5388 << "[[msvc::forceinline]]";
5389 return;
5390 }
5391
5392 if (AlwaysInlineAttr *Inline =
5393 S.mergeAlwaysInlineAttr(D, AL, AL.getAttrName()))
5394 D->addAttr(Inline);
5395}
5396
5397static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5398 if (MinSizeAttr *MinSize = S.mergeMinSizeAttr(D, AL))
5399 D->addAttr(MinSize);
5400}
5401
5402static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5403 if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr(D, AL))
5404 D->addAttr(Optnone);
5405}
5406
5407static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5408 const auto *VD = cast<VarDecl>(D);
5409 if (VD->hasLocalStorage()) {
5410 S.Diag(AL.getLoc(), diag::err_cuda_nonstatic_constdev);
5411 return;
5412 }
5414 return;
5415 // constexpr variable may already get an implicit constant attr, which should
5416 // be replaced by the explicit constant attr.
5417 if (auto *A = D->getAttr<CUDAConstantAttr>()) {
5418 if (!A->isImplicit())
5419 return;
5420 D->dropAttr<CUDAConstantAttr>();
5421 }
5422 D->addAttr(::new (S.Context) CUDAConstantAttr(S.Context, AL));
5423}
5424
5425static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5426 const auto *VD = cast<VarDecl>(D);
5427 // extern __shared__ is only allowed on arrays with no length (e.g.
5428 // "int x[]").
5429 if (!S.getLangOpts().GPURelocatableDeviceCode && VD->hasExternalStorage() &&
5430 !isa<IncompleteArrayType>(VD->getType())) {
5431 S.Diag(AL.getLoc(), diag::err_cuda_extern_shared) << VD;
5432 return;
5433 }
5435 return;
5436 if (S.getLangOpts().CUDA && VD->hasLocalStorage() &&
5437 S.CUDA().DiagIfHostCode(AL.getLoc(), diag::err_cuda_host_shared)
5438 << S.CUDA().CurrentTarget())
5439 return;
5440 D->addAttr(::new (S.Context) CUDASharedAttr(S.Context, AL));
5441}
5442
5443static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5444 const auto *FD = cast<FunctionDecl>(D);
5445 if (!FD->getReturnType()->isVoidType() &&
5446 !FD->getReturnType()->getAs<AutoType>() &&
5448 SourceRange RTRange = FD->getReturnTypeSourceRange();
5449 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
5450 << FD->getType()
5451 << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void")
5452 : FixItHint());
5453 return;
5454 }
5455 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) {
5456 if (Method->isInstance()) {
5457 S.Diag(Method->getBeginLoc(), diag::err_kern_is_nonstatic_method)
5458 << Method;
5459 return;
5460 }
5461 S.Diag(Method->getBeginLoc(), diag::warn_kern_is_method) << Method;
5462 }
5463 // Only warn for "inline" when compiling for host, to cut down on noise.
5464 if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice)
5465 S.Diag(FD->getBeginLoc(), diag::warn_kern_is_inline) << FD;
5466
5467 if (AL.getKind() == ParsedAttr::AT_DeviceKernel)
5468 D->addAttr(::new (S.Context) DeviceKernelAttr(S.Context, AL));
5469 else
5470 D->addAttr(::new (S.Context) CUDAGlobalAttr(S.Context, AL));
5471 // In host compilation the kernel is emitted as a stub function, which is
5472 // a helper function for launching the kernel. The instructions in the helper
5473 // function has nothing to do with the source code of the kernel. Do not emit
5474 // debug info for the stub function to avoid confusing the debugger.
5475 if (S.LangOpts.HIP && !S.LangOpts.CUDAIsDevice)
5476 D->addAttr(NoDebugAttr::CreateImplicit(S.Context));
5477}
5478
5479static void handleDeviceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5480 if (const auto *VD = dyn_cast<VarDecl>(D)) {
5481 if (VD->hasLocalStorage()) {
5482 S.Diag(AL.getLoc(), diag::err_cuda_nonstatic_constdev);
5483 return;
5484 }
5486 return;
5487 }
5488
5489 if (auto *A = D->getAttr<CUDADeviceAttr>()) {
5490 if (!A->isImplicit())
5491 return;
5492 D->dropAttr<CUDADeviceAttr>();
5493 }
5494 D->addAttr(::new (S.Context) CUDADeviceAttr(S.Context, AL));
5495}
5496
5497static void handleManagedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5498 if (const auto *VD = dyn_cast<VarDecl>(D)) {
5499 if (VD->hasLocalStorage()) {
5500 S.Diag(AL.getLoc(), diag::err_cuda_nonstatic_constdev);
5501 return;
5502 }
5504 return;
5505 }
5506 if (!D->hasAttr<HIPManagedAttr>())
5507 D->addAttr(::new (S.Context) HIPManagedAttr(S.Context, AL));
5508 if (!D->hasAttr<CUDADeviceAttr>())
5509 D->addAttr(CUDADeviceAttr::CreateImplicit(S.Context));
5510}
5511
5512static void handleGridConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5513 if (D->isInvalidDecl())
5514 return;
5515 // Whether __grid_constant__ is allowed to be used will be checked in
5516 // Sema::CheckFunctionDeclaration as we need complete function decl to make
5517 // the call.
5518 D->addAttr(::new (S.Context) CUDAGridConstantAttr(S.Context, AL));
5519}
5520
5521static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5522 const auto *Fn = cast<FunctionDecl>(D);
5523 if (!Fn->isInlineSpecified()) {
5524 S.Diag(AL.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
5525 return;
5526 }
5527
5528 if (S.LangOpts.CPlusPlus && Fn->getStorageClass() != SC_Extern)
5529 S.Diag(AL.getLoc(), diag::warn_gnu_inline_cplusplus_without_extern);
5530
5531 D->addAttr(::new (S.Context) GNUInlineAttr(S.Context, AL));
5532}
5533
5534static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5535 if (hasDeclarator(D)) return;
5536
5537 // Diagnostic is emitted elsewhere: here we store the (valid) AL
5538 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
5539 CallingConv CC;
5541 AL, CC, /*FD*/ nullptr,
5542 S.CUDA().IdentifyTarget(dyn_cast<FunctionDecl>(D))))
5543 return;
5544
5545 if (!isa<ObjCMethodDecl>(D)) {
5546 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
5548 return;
5549 }
5550
5551 switch (AL.getKind()) {
5552 case ParsedAttr::AT_FastCall:
5553 D->addAttr(::new (S.Context) FastCallAttr(S.Context, AL));
5554 return;
5555 case ParsedAttr::AT_StdCall:
5556 D->addAttr(::new (S.Context) StdCallAttr(S.Context, AL));
5557 return;
5558 case ParsedAttr::AT_ThisCall:
5559 D->addAttr(::new (S.Context) ThisCallAttr(S.Context, AL));
5560 return;
5561 case ParsedAttr::AT_CDecl:
5562 D->addAttr(::new (S.Context) CDeclAttr(S.Context, AL));
5563 return;
5564 case ParsedAttr::AT_Pascal:
5565 D->addAttr(::new (S.Context) PascalAttr(S.Context, AL));
5566 return;
5567 case ParsedAttr::AT_SwiftCall:
5568 D->addAttr(::new (S.Context) SwiftCallAttr(S.Context, AL));
5569 return;
5570 case ParsedAttr::AT_SwiftAsyncCall:
5571 D->addAttr(::new (S.Context) SwiftAsyncCallAttr(S.Context, AL));
5572 return;
5573 case ParsedAttr::AT_VectorCall:
5574 D->addAttr(::new (S.Context) VectorCallAttr(S.Context, AL));
5575 return;
5576 case ParsedAttr::AT_MSABI:
5577 D->addAttr(::new (S.Context) MSABIAttr(S.Context, AL));
5578 return;
5579 case ParsedAttr::AT_SysVABI:
5580 D->addAttr(::new (S.Context) SysVABIAttr(S.Context, AL));
5581 return;
5582 case ParsedAttr::AT_RegCall:
5583 D->addAttr(::new (S.Context) RegCallAttr(S.Context, AL));
5584 return;
5585 case ParsedAttr::AT_Pcs: {
5586 PcsAttr::PCSType PCS;
5587 switch (CC) {
5588 case CC_AAPCS:
5589 PCS = PcsAttr::AAPCS;
5590 break;
5591 case CC_AAPCS_VFP:
5592 PCS = PcsAttr::AAPCS_VFP;
5593 break;
5594 default:
5595 llvm_unreachable("unexpected calling convention in pcs attribute");
5596 }
5597
5598 D->addAttr(::new (S.Context) PcsAttr(S.Context, AL, PCS));
5599 return;
5600 }
5601 case ParsedAttr::AT_AArch64VectorPcs:
5602 D->addAttr(::new (S.Context) AArch64VectorPcsAttr(S.Context, AL));
5603 return;
5604 case ParsedAttr::AT_AArch64SVEPcs:
5605 D->addAttr(::new (S.Context) AArch64SVEPcsAttr(S.Context, AL));
5606 return;
5607 case ParsedAttr::AT_DeviceKernel: {
5608 // The attribute should already be applied.
5609 assert(D->hasAttr<DeviceKernelAttr>() && "Expected attribute");
5610 return;
5611 }
5612 case ParsedAttr::AT_IntelOclBicc:
5613 D->addAttr(::new (S.Context) IntelOclBiccAttr(S.Context, AL));
5614 return;
5615 case ParsedAttr::AT_PreserveMost:
5616 D->addAttr(::new (S.Context) PreserveMostAttr(S.Context, AL));
5617 return;
5618 case ParsedAttr::AT_PreserveAll:
5619 D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL));
5620 return;
5621 case ParsedAttr::AT_M68kRTD:
5622 D->addAttr(::new (S.Context) M68kRTDAttr(S.Context, AL));
5623 return;
5624 case ParsedAttr::AT_PreserveNone:
5625 D->addAttr(::new (S.Context) PreserveNoneAttr(S.Context, AL));
5626 return;
5627 case ParsedAttr::AT_RISCVVectorCC:
5628 D->addAttr(::new (S.Context) RISCVVectorCCAttr(S.Context, AL));
5629 return;
5630 case ParsedAttr::AT_RISCVVLSCC: {
5631 // If the riscv_abi_vlen doesn't have any argument, default ABI_VLEN is 128.
5632 unsigned VectorLength = 128;
5633 if (AL.getNumArgs() &&
5635 return;
5637 S.Diag(AL.getLoc(), diag::err_argument_invalid_range)
5638 << VectorLength << 32 << 65536;
5639 return;
5640 }
5641 if (!llvm::isPowerOf2_64(VectorLength)) {
5642 S.Diag(AL.getLoc(), diag::err_argument_not_power_of_2);
5643 return;
5644 }
5645
5646 D->addAttr(::new (S.Context) RISCVVLSCCAttr(S.Context, AL, VectorLength));
5647 return;
5648 }
5649 default:
5650 llvm_unreachable("unexpected attribute kind");
5651 }
5652}
5653
5654static void handleDeviceKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5655 const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
5656 bool IsFunctionTemplate = FD && FD->getDescribedFunctionTemplate();
5657 llvm::Triple Triple = S.getASTContext().getTargetInfo().getTriple();
5658 const LangOptions &LangOpts = S.getLangOpts();
5659 // OpenCL has its own error messages.
5660 if (!LangOpts.OpenCL && FD && !FD->isExternallyVisible()) {
5661 S.Diag(AL.getLoc(), diag::err_hidden_device_kernel) << FD;
5662 AL.setInvalid();
5663 return;
5664 }
5665 if (Triple.isNVPTX()) {
5666 handleGlobalAttr(S, D, AL);
5667 } else {
5668 // OpenCL C++ will throw a more specific error.
5669 if (!LangOpts.OpenCLCPlusPlus && (!FD || IsFunctionTemplate)) {
5670 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type_str)
5671 << AL << AL.isRegularKeywordAttribute() << "functions";
5672 AL.setInvalid();
5673 return;
5674 }
5676 }
5677 // TODO: isGPU() should probably return true for SPIR.
5678 bool TargetDeviceEnvironment = Triple.isGPU() || Triple.isSPIR() ||
5679 LangOpts.isTargetDevice() || LangOpts.OpenCL;
5680 if (!TargetDeviceEnvironment) {
5681 S.Diag(AL.getLoc(), diag::warn_cconv_unsupported)
5683 AL.setInvalid();
5684 return;
5685 }
5686
5687 // Make sure we validate the CC with the target
5688 // and warn/error if necessary.
5689 handleCallConvAttr(S, D, AL);
5690}
5691
5692static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5693 if (AL.getAttributeSpellingListIndex() == SuppressAttr::CXX11_gsl_suppress) {
5694 // Suppression attribute with GSL spelling requires at least 1 argument.
5695 if (!AL.checkAtLeastNumArgs(S, 1))
5696 return;
5697 }
5698
5699 std::vector<StringRef> DiagnosticIdentifiers;
5700 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
5701 StringRef RuleName;
5702
5703 if (!S.checkStringLiteralArgumentAttr(AL, I, RuleName, nullptr))
5704 return;
5705
5706 DiagnosticIdentifiers.push_back(RuleName);
5707 }
5708 D->addAttr(::new (S.Context)
5709 SuppressAttr(S.Context, AL, DiagnosticIdentifiers.data(),
5710 DiagnosticIdentifiers.size()));
5711}
5712
5713static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5714 TypeSourceInfo *DerefTypeLoc = nullptr;
5715 QualType ParmType;
5716 if (AL.hasParsedType()) {
5717 ParmType = S.GetTypeFromParser(AL.getTypeArg(), &DerefTypeLoc);
5718
5719 unsigned SelectIdx = ~0U;
5720 if (ParmType->isReferenceType())
5721 SelectIdx = 0;
5722 else if (ParmType->isArrayType())
5723 SelectIdx = 1;
5724
5725 if (SelectIdx != ~0U) {
5726 S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument)
5727 << SelectIdx << AL;
5728 return;
5729 }
5730 }
5731
5732 // To check if earlier decl attributes do not conflict the newly parsed ones
5733 // we always add (and check) the attribute to the canonical decl. We need
5734 // to repeat the check for attribute mutual exclusion because we're attaching
5735 // all of the attributes to the canonical declaration rather than the current
5736 // declaration.
5737 D = D->getCanonicalDecl();
5738 if (AL.getKind() == ParsedAttr::AT_Owner) {
5740 return;
5741 if (const auto *OAttr = D->getAttr<OwnerAttr>()) {
5742 const Type *ExistingDerefType = OAttr->getDerefTypeLoc()
5743 ? OAttr->getDerefType().getTypePtr()
5744 : nullptr;
5745 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
5746 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
5747 << AL << OAttr
5748 << (AL.isRegularKeywordAttribute() ||
5749 OAttr->isRegularKeywordAttribute());
5750 S.Diag(OAttr->getLocation(), diag::note_conflicting_attribute);
5751 }
5752 return;
5753 }
5754 for (Decl *Redecl : D->redecls()) {
5755 Redecl->addAttr(::new (S.Context) OwnerAttr(S.Context, AL, DerefTypeLoc));
5756 }
5757 } else {
5759 return;
5760 if (const auto *PAttr = D->getAttr<PointerAttr>()) {
5761 const Type *ExistingDerefType = PAttr->getDerefTypeLoc()
5762 ? PAttr->getDerefType().getTypePtr()
5763 : nullptr;
5764 if (ExistingDerefType != ParmType.getTypePtrOrNull()) {
5765 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
5766 << AL << PAttr
5767 << (AL.isRegularKeywordAttribute() ||
5768 PAttr->isRegularKeywordAttribute());
5769 S.Diag(PAttr->getLocation(), diag::note_conflicting_attribute);
5770 }
5771 return;
5772 }
5773 for (Decl *Redecl : D->redecls()) {
5774 Redecl->addAttr(::new (S.Context)
5775 PointerAttr(S.Context, AL, DerefTypeLoc));
5776 }
5777 }
5778}
5779
5780static void handleRandomizeLayoutAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5782 return;
5783 if (!D->hasAttr<RandomizeLayoutAttr>())
5784 D->addAttr(::new (S.Context) RandomizeLayoutAttr(S.Context, AL));
5785}
5786
5788 const ParsedAttr &AL) {
5790 return;
5791 if (!D->hasAttr<NoRandomizeLayoutAttr>())
5792 D->addAttr(::new (S.Context) NoRandomizeLayoutAttr(S.Context, AL));
5793}
5794
5796 const FunctionDecl *FD,
5797 CUDAFunctionTarget CFT) {
5798 if (Attrs.isInvalid())
5799 return true;
5800
5801 if (Attrs.hasProcessingCache()) {
5802 CC = (CallingConv) Attrs.getProcessingCache();
5803 return false;
5804 }
5805
5806 if (Attrs.getKind() == ParsedAttr::AT_RISCVVLSCC) {
5807 // riscv_vls_cc only accepts 0 or 1 argument.
5808 if (!Attrs.checkAtLeastNumArgs(*this, 0) ||
5809 !Attrs.checkAtMostNumArgs(*this, 1)) {
5810 Attrs.setInvalid();
5811 return true;
5812 }
5813 } else {
5814 unsigned ReqArgs = Attrs.getKind() == ParsedAttr::AT_Pcs ? 1 : 0;
5815 if (!Attrs.checkExactlyNumArgs(*this, ReqArgs)) {
5816 Attrs.setInvalid();
5817 return true;
5818 }
5819 }
5820
5821 bool IsTargetDefaultMSABI =
5822 Context.getTargetInfo().getTriple().isOSWindows() ||
5823 Context.getTargetInfo().getTriple().isUEFI();
5824 // TODO: diagnose uses of these conventions on the wrong target.
5825 switch (Attrs.getKind()) {
5826 case ParsedAttr::AT_CDecl:
5827 CC = CC_C;
5828 break;
5829 case ParsedAttr::AT_FastCall:
5830 CC = CC_X86FastCall;
5831 break;
5832 case ParsedAttr::AT_StdCall:
5833 CC = CC_X86StdCall;
5834 break;
5835 case ParsedAttr::AT_ThisCall:
5836 CC = CC_X86ThisCall;
5837 break;
5838 case ParsedAttr::AT_Pascal:
5839 CC = CC_X86Pascal;
5840 break;
5841 case ParsedAttr::AT_SwiftCall:
5842 CC = CC_Swift;
5843 break;
5844 case ParsedAttr::AT_SwiftAsyncCall:
5845 CC = CC_SwiftAsync;
5846 break;
5847 case ParsedAttr::AT_VectorCall:
5848 CC = CC_X86VectorCall;
5849 break;
5850 case ParsedAttr::AT_AArch64VectorPcs:
5852 break;
5853 case ParsedAttr::AT_AArch64SVEPcs:
5854 CC = CC_AArch64SVEPCS;
5855 break;
5856 case ParsedAttr::AT_RegCall:
5857 CC = CC_X86RegCall;
5858 break;
5859 case ParsedAttr::AT_MSABI:
5860 CC = IsTargetDefaultMSABI ? CC_C : CC_Win64;
5861 break;
5862 case ParsedAttr::AT_SysVABI:
5863 CC = IsTargetDefaultMSABI ? CC_X86_64SysV : CC_C;
5864 break;
5865 case ParsedAttr::AT_Pcs: {
5866 StringRef StrRef;
5867 if (!checkStringLiteralArgumentAttr(Attrs, 0, StrRef)) {
5868 Attrs.setInvalid();
5869 return true;
5870 }
5871 if (StrRef == "aapcs") {
5872 CC = CC_AAPCS;
5873 break;
5874 } else if (StrRef == "aapcs-vfp") {
5875 CC = CC_AAPCS_VFP;
5876 break;
5877 }
5878
5879 Attrs.setInvalid();
5880 Diag(Attrs.getLoc(), diag::err_invalid_pcs);
5881 return true;
5882 }
5883 case ParsedAttr::AT_IntelOclBicc:
5884 CC = CC_IntelOclBicc;
5885 break;
5886 case ParsedAttr::AT_PreserveMost:
5887 CC = CC_PreserveMost;
5888 break;
5889 case ParsedAttr::AT_PreserveAll:
5890 CC = CC_PreserveAll;
5891 break;
5892 case ParsedAttr::AT_M68kRTD:
5893 CC = CC_M68kRTD;
5894 break;
5895 case ParsedAttr::AT_PreserveNone:
5896 CC = CC_PreserveNone;
5897 break;
5898 case ParsedAttr::AT_RISCVVectorCC:
5899 CC = CC_RISCVVectorCall;
5900 break;
5901 case ParsedAttr::AT_RISCVVLSCC: {
5902 // If the riscv_abi_vlen doesn't have any argument, we set set it to default
5903 // value 128.
5904 unsigned ABIVLen = 128;
5905 if (Attrs.getNumArgs() &&
5906 !checkUInt32Argument(Attrs, Attrs.getArgAsExpr(0), ABIVLen)) {
5907 Attrs.setInvalid();
5908 return true;
5909 }
5910 if (Attrs.getNumArgs() && (ABIVLen < 32 || ABIVLen > 65536)) {
5911 Attrs.setInvalid();
5912 Diag(Attrs.getLoc(), diag::err_argument_invalid_range)
5913 << ABIVLen << 32 << 65536;
5914 return true;
5915 }
5916 if (!llvm::isPowerOf2_64(ABIVLen)) {
5917 Attrs.setInvalid();
5918 Diag(Attrs.getLoc(), diag::err_argument_not_power_of_2);
5919 return true;
5920 }
5922 llvm::Log2_64(ABIVLen) - 5);
5923 break;
5924 }
5925 case ParsedAttr::AT_DeviceKernel: {
5926 // Validation was handled in handleDeviceKernelAttr.
5927 CC = CC_DeviceKernel;
5928 break;
5929 }
5930 default: llvm_unreachable("unexpected attribute kind");
5931 }
5932
5934 const TargetInfo &TI = Context.getTargetInfo();
5935 auto *Aux = Context.getAuxTargetInfo();
5936 // CUDA functions may have host and/or device attributes which indicate
5937 // their targeted execution environment, therefore the calling convention
5938 // of functions in CUDA should be checked against the target deduced based
5939 // on their host/device attributes.
5940 if (LangOpts.CUDA) {
5941 assert(FD || CFT != CUDAFunctionTarget::InvalidTarget);
5942 auto CudaTarget = FD ? CUDA().IdentifyTarget(FD) : CFT;
5943 bool CheckHost = false, CheckDevice = false;
5944 switch (CudaTarget) {
5946 CheckHost = true;
5947 CheckDevice = true;
5948 break;
5950 CheckHost = true;
5951 break;
5954 CheckDevice = true;
5955 break;
5957 llvm_unreachable("unexpected cuda target");
5958 }
5959 auto *HostTI = LangOpts.CUDAIsDevice ? Aux : &TI;
5960 auto *DeviceTI = LangOpts.CUDAIsDevice ? &TI : Aux;
5961 if (CheckHost && HostTI)
5962 A = HostTI->checkCallingConvention(CC);
5963 if (A == TargetInfo::CCCR_OK && CheckDevice && DeviceTI)
5964 A = DeviceTI->checkCallingConvention(CC);
5965 } else if (LangOpts.SYCLIsDevice) {
5966 // During device compilation, calling conventions that are valid for the
5967 // host, for the device, and for both the host and the device may be
5968 // encountered. Diagnostics are desired for cases where the calling
5969 // convention is not supported by either the host or the device. If Aux is
5970 // null (which should rarely be the case), it isn't possible to check
5971 // whether the calling convention is supported by the host, so just assume
5972 // that it is. If the calling convention is supported for the device, there
5973 // is no need to check the host; the device target gets priority since this
5974 // check is only performed during device compilation.
5975 A = TI.checkCallingConvention(CC);
5976 if (Aux && A == TargetInfo::CCCR_Warning) {
5977 // If the calling convention would provoke a warning for the device, check
5978 // the host and preserve the warning only if the calling convention would
5979 // provoke an error for the host. Otherwise, assume this calling
5980 // convention is only used for host only functions.
5981 A = Aux->checkCallingConvention(CC);
5982 if (A == TargetInfo::CCCR_Error)
5984 } else if (Aux && A == TargetInfo::CCCR_Error) {
5985 // Assume this calling convention is only used for host only functions.
5986 A = Aux->checkCallingConvention(CC);
5987 }
5988 } else {
5989 A = TI.checkCallingConvention(CC);
5990 }
5991
5992 switch (A) {
5994 break;
5995
5997 // Treat an ignored convention as if it was an explicit C calling convention
5998 // attribute. For example, __stdcall on Win x64 functions as __cdecl, so
5999 // that command line flags that change the default convention to
6000 // __vectorcall don't affect declarations marked __stdcall.
6001 CC = CC_C;
6002 break;
6003
6005 Diag(Attrs.getLoc(), diag::error_cconv_unsupported)
6007 break;
6008
6010 Diag(Attrs.getLoc(), diag::warn_cconv_unsupported)
6012
6013 // This convention is not valid for the target. Use the default function or
6014 // method calling convention.
6015 bool IsCXXMethod = false, IsVariadic = false;
6016 if (FD) {
6017 IsCXXMethod = FD->isCXXInstanceMember();
6018 IsVariadic = FD->isVariadic();
6019 }
6020 CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod);
6021 break;
6022 }
6023 }
6024
6025 Attrs.setProcessingCache((unsigned) CC);
6026 return false;
6027}
6028
6029bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
6030 if (AL.isInvalid())
6031 return true;
6032
6033 if (!AL.checkExactlyNumArgs(*this, 1)) {
6034 AL.setInvalid();
6035 return true;
6036 }
6037
6038 uint32_t NP;
6039 Expr *NumParamsExpr = AL.getArgAsExpr(0);
6040 if (!checkUInt32Argument(AL, NumParamsExpr, NP)) {
6041 AL.setInvalid();
6042 return true;
6043 }
6044
6045 if (Context.getTargetInfo().getRegParmMax() == 0) {
6046 Diag(AL.getLoc(), diag::err_attribute_regparm_wrong_platform)
6047 << NumParamsExpr->getSourceRange();
6048 AL.setInvalid();
6049 return true;
6050 }
6051
6052 numParams = NP;
6053 if (numParams > Context.getTargetInfo().getRegParmMax()) {
6054 Diag(AL.getLoc(), diag::err_attribute_regparm_invalid_number)
6055 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
6056 AL.setInvalid();
6057 return true;
6058 }
6059
6060 return false;
6061}
6062
6063// Helper to get OffloadArch.
6065 if (!TI.getTriple().isNVPTX())
6066 llvm_unreachable("getOffloadArch is only valid for NVPTX triple");
6067 auto &TO = TI.getTargetOpts();
6068 return StringToOffloadArch(TO.CPU);
6069}
6070
6071// Checks whether an argument of launch_bounds attribute is
6072// acceptable, performs implicit conversion to Rvalue, and returns
6073// non-nullptr Expr result on success. Otherwise, it returns nullptr
6074// and may output an error.
6076 const CUDALaunchBoundsAttr &AL,
6077 const unsigned Idx) {
6079 return nullptr;
6080
6081 // Accept template arguments for now as they depend on something else.
6082 // We'll get to check them when they eventually get instantiated.
6083 if (E->isValueDependent())
6084 return E;
6085
6086 std::optional<llvm::APSInt> I = llvm::APSInt(64);
6087 if (!(I = E->getIntegerConstantExpr(S.Context))) {
6088 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
6089 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
6090 return nullptr;
6091 }
6092 // Make sure we can fit it in 32 bits.
6093 if (!I->isIntN(32)) {
6094 S.Diag(E->getExprLoc(), diag::err_ice_too_large)
6095 << toString(*I, 10, false) << 32 << /* Unsigned */ 1;
6096 return nullptr;
6097 }
6098 if (*I < 0)
6099 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
6100 << &AL << Idx << E->getSourceRange();
6101
6102 // We may need to perform implicit conversion of the argument.
6104 S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false);
6105 ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E);
6106 assert(!ValArg.isInvalid() &&
6107 "Unexpected PerformCopyInitialization() failure.");
6108
6109 return ValArg.getAs<Expr>();
6110}
6111
6112CUDALaunchBoundsAttr *
6114 Expr *MinBlocks, Expr *MaxBlocks,
6115 bool IgnoreArch) {
6116 CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks, MaxBlocks);
6117 MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
6118 if (!MaxThreads)
6119 return nullptr;
6120
6121 if (MinBlocks) {
6122 MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
6123 if (!MinBlocks)
6124 return nullptr;
6125 }
6126
6127 if (MaxBlocks) {
6128 // We might want to ignore the nvptx arch check, e.g., when processing the
6129 // launch bounds attribute within ompx_attribute to support other archs.
6130 if (!IgnoreArch) {
6131 // '.maxclusterrank' ptx directive requires .target sm_90 or higher.
6132 auto SM = getOffloadArch(Context.getTargetInfo());
6134 Diag(MaxBlocks->getBeginLoc(), diag::warn_cuda_maxclusterrank_sm_90)
6135 << OffloadArchToString(SM) << CI << MaxBlocks->getSourceRange();
6136 // Ignore it by setting MaxBlocks to null;
6137 MaxBlocks = nullptr;
6138 }
6139 }
6140
6141 if (MaxBlocks) {
6142 MaxBlocks = makeLaunchBoundsArgExpr(*this, MaxBlocks, TmpAttr, 2);
6143 if (!MaxBlocks)
6144 return nullptr;
6145 }
6146 }
6147
6148 return ::new (Context)
6149 CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks, MaxBlocks);
6150}
6151
6153 Expr *MaxThreads, Expr *MinBlocks,
6154 Expr *MaxBlocks) {
6155 if (auto *Attr = CreateLaunchBoundsAttr(CI, MaxThreads, MinBlocks, MaxBlocks))
6156 D->addAttr(Attr);
6157}
6158
6159static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6160 if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 3))
6161 return;
6162
6163 S.AddLaunchBoundsAttr(D, AL, AL.getArgAsExpr(0),
6164 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
6165 AL.getNumArgs() > 2 ? AL.getArgAsExpr(2) : nullptr);
6166}
6167
6168static std::pair<Expr *, int>
6169makeClusterDimsArgExpr(Sema &S, Expr *E, const CUDAClusterDimsAttr &AL,
6170 const unsigned Idx) {
6171 if (!E || S.DiagnoseUnexpandedParameterPack(E))
6172 return {};
6173
6174 // Accept template arguments for now as they depend on something else.
6175 // We'll get to check them when they eventually get instantiated.
6176 if (E->isInstantiationDependent())
6177 return {E, 1};
6178
6179 std::optional<llvm::APSInt> I = E->getIntegerConstantExpr(S.Context);
6180 if (!I) {
6181 S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type)
6182 << &AL << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange();
6183 return {};
6184 }
6185 // Make sure we can fit it in 4 bits.
6186 if (!I->isIntN(4)) {
6187 S.Diag(E->getExprLoc(), diag::err_ice_too_large)
6188 << toString(*I, 10, false) << 4 << /*Unsigned=*/1;
6189 return {};
6190 }
6191 if (*I < 0) {
6192 S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative)
6193 << &AL << Idx << E->getSourceRange();
6194 }
6195
6196 return {ConstantExpr::Create(S.getASTContext(), E, APValue(*I)),
6197 I->getZExtValue()};
6198}
6199
6201 Expr *X, Expr *Y, Expr *Z) {
6202 CUDAClusterDimsAttr TmpAttr(Context, CI, X, Y, Z);
6203
6204 auto [NewX, ValX] = makeClusterDimsArgExpr(*this, X, TmpAttr, /*Idx=*/0);
6205 auto [NewY, ValY] = makeClusterDimsArgExpr(*this, Y, TmpAttr, /*Idx=*/1);
6206 auto [NewZ, ValZ] = makeClusterDimsArgExpr(*this, Z, TmpAttr, /*Idx=*/2);
6207
6208 if (!NewX || (Y && !NewY) || (Z && !NewZ))
6209 return nullptr;
6210
6211 int FlatDim = ValX * ValY * ValZ;
6212 const llvm::Triple TT =
6213 (!Context.getLangOpts().CUDAIsDevice && Context.getAuxTargetInfo())
6214 ? Context.getAuxTargetInfo()->getTriple()
6215 : Context.getTargetInfo().getTriple();
6216 int MaxDim = 1;
6217 if (TT.isNVPTX())
6218 MaxDim = 8;
6219 else if (TT.isAMDGPU())
6220 MaxDim = 16;
6221 else
6222 return nullptr;
6223
6224 // A maximum of 8 thread blocks in a cluster is supported as a portable
6225 // cluster size in CUDA. The number is 16 for AMDGPU.
6226 if (FlatDim > MaxDim) {
6227 Diag(CI.getLoc(), diag::err_cluster_dims_too_large) << MaxDim << FlatDim;
6228 return nullptr;
6229 }
6230
6231 return CUDAClusterDimsAttr::Create(Context, NewX, NewY, NewZ, CI);
6232}
6233
6235 Expr *Y, Expr *Z) {
6236 if (auto *Attr = createClusterDimsAttr(CI, X, Y, Z))
6237 D->addAttr(Attr);
6238}
6239
6241 D->addAttr(CUDANoClusterAttr::Create(Context, CI));
6242}
6243
6244static void handleClusterDimsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6245 const TargetInfo &TTI = S.Context.getTargetInfo();
6247 if ((TTI.getTriple().isNVPTX() && Arch < clang::OffloadArch::SM_90) ||
6248 (TTI.getTriple().isAMDGPU() &&
6249 !TTI.hasFeatureEnabled(TTI.getTargetOpts().FeatureMap, "clusters"))) {
6250 S.Diag(AL.getLoc(), diag::err_cluster_attr_not_supported) << AL;
6251 return;
6252 }
6253
6254 if (!AL.checkAtLeastNumArgs(S, /*Num=*/1) ||
6255 !AL.checkAtMostNumArgs(S, /*Num=*/3))
6256 return;
6257
6258 S.addClusterDimsAttr(D, AL, AL.getArgAsExpr(0),
6259 AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
6260 AL.getNumArgs() > 2 ? AL.getArgAsExpr(2) : nullptr);
6261}
6262
6263static void handleNoClusterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6264 const TargetInfo &TTI = S.Context.getTargetInfo();
6266 if ((TTI.getTriple().isNVPTX() && Arch < clang::OffloadArch::SM_90) ||
6267 (TTI.getTriple().isAMDGPU() &&
6268 !TTI.hasFeatureEnabled(TTI.getTargetOpts().FeatureMap, "clusters"))) {
6269 S.Diag(AL.getLoc(), diag::err_cluster_attr_not_supported) << AL;
6270 return;
6271 }
6272
6273 S.addNoClusterAttr(D, AL);
6274}
6275
6277 const ParsedAttr &AL) {
6278 if (!AL.isArgIdent(0)) {
6279 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
6280 << AL << /* arg num = */ 1 << AANT_ArgumentIdentifier;
6281 return;
6282 }
6283
6284 ParamIdx ArgumentIdx;
6286 D, AL, 2, AL.getArgAsExpr(1), ArgumentIdx,
6287 /*CanIndexImplicitThis=*/false,
6288 /*CanIndexVariadicArguments=*/true))
6289 return;
6290
6291 ParamIdx TypeTagIdx;
6293 D, AL, 3, AL.getArgAsExpr(2), TypeTagIdx,
6294 /*CanIndexImplicitThis=*/false,
6295 /*CanIndexVariadicArguments=*/true))
6296 return;
6297
6298 bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag";
6299 if (IsPointer) {
6300 // Ensure that buffer has a pointer type.
6301 unsigned ArgumentIdxAST = ArgumentIdx.getASTIndex();
6302 if (ArgumentIdxAST >= getFunctionOrMethodNumParams(D) ||
6303 !getFunctionOrMethodParamType(D, ArgumentIdxAST)->isPointerType())
6304 S.Diag(AL.getLoc(), diag::err_attribute_pointers_only) << AL << 0;
6305 }
6306
6307 D->addAttr(::new (S.Context) ArgumentWithTypeTagAttr(
6308 S.Context, AL, AL.getArgAsIdent(0)->getIdentifierInfo(), ArgumentIdx,
6309 TypeTagIdx, IsPointer));
6310}
6311
6313 const ParsedAttr &AL) {
6314 if (!AL.isArgIdent(0)) {
6315 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
6316 << AL << 1 << AANT_ArgumentIdentifier;
6317 return;
6318 }
6319
6320 if (!AL.checkExactlyNumArgs(S, 1))
6321 return;
6322
6323 if (!isa<VarDecl>(D)) {
6324 S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
6326 return;
6327 }
6328
6329 IdentifierInfo *PointerKind = AL.getArgAsIdent(0)->getIdentifierInfo();
6330 TypeSourceInfo *MatchingCTypeLoc = nullptr;
6331 S.GetTypeFromParser(AL.getMatchingCType(), &MatchingCTypeLoc);
6332 assert(MatchingCTypeLoc && "no type source info for attribute argument");
6333
6334 D->addAttr(::new (S.Context) TypeTagForDatatypeAttr(
6335 S.Context, AL, PointerKind, MatchingCTypeLoc, AL.getLayoutCompatible(),
6336 AL.getMustBeNull()));
6337}
6338
6339static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6340 ParamIdx ArgCount;
6341
6343 ArgCount,
6344 true /* CanIndexImplicitThis */))
6345 return;
6346
6347 // ArgCount isn't a parameter index [0;n), it's a count [1;n]
6348 D->addAttr(::new (S.Context)
6349 XRayLogArgsAttr(S.Context, AL, ArgCount.getSourceIndex()));
6350}
6351
6353 const ParsedAttr &AL) {
6354 if (S.Context.getTargetInfo().getTriple().isOSAIX()) {
6355 S.Diag(AL.getLoc(), diag::err_aix_attr_unsupported) << AL;
6356 return;
6357 }
6358 uint32_t Count = 0, Offset = 0;
6359 StringRef Section;
6360 if (!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), Count, 0, true))
6361 return;
6362 if (AL.getNumArgs() >= 2) {
6363 Expr *Arg = AL.getArgAsExpr(1);
6364 if (!S.checkUInt32Argument(AL, Arg, Offset, 1, true))
6365 return;
6366 if (Count < Offset) {
6367 S.Diag(S.getAttrLoc(AL), diag::err_attribute_argument_out_of_range)
6368 << &AL << 0 << Count << Arg->getBeginLoc();
6369 return;
6370 }
6371 }
6372 if (AL.getNumArgs() == 3) {
6373 SourceLocation LiteralLoc;
6374 if (!S.checkStringLiteralArgumentAttr(AL, 2, Section, &LiteralLoc))
6375 return;
6376 if (llvm::Error E = S.isValidSectionSpecifier(Section)) {
6377 S.Diag(LiteralLoc,
6378 diag::err_attribute_patchable_function_entry_invalid_section)
6379 << toString(std::move(E));
6380 return;
6381 }
6382 if (Section.empty()) {
6383 S.Diag(LiteralLoc,
6384 diag::err_attribute_patchable_function_entry_invalid_section)
6385 << "section must not be empty";
6386 return;
6387 }
6388 }
6389 D->addAttr(::new (S.Context) PatchableFunctionEntryAttr(S.Context, AL, Count,
6390 Offset, Section));
6391}
6392
6393static void handleBuiltinAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6394 if (!AL.isArgIdent(0)) {
6395 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
6396 << AL << 1 << AANT_ArgumentIdentifier;
6397 return;
6398 }
6399
6401 unsigned BuiltinID = Ident->getBuiltinID();
6402 StringRef AliasName = cast<FunctionDecl>(D)->getIdentifier()->getName();
6403
6404 bool IsAArch64 = S.Context.getTargetInfo().getTriple().isAArch64();
6405 bool IsARM = S.Context.getTargetInfo().getTriple().isARM();
6406 bool IsRISCV = S.Context.getTargetInfo().getTriple().isRISCV();
6407 bool IsSPIRV = S.Context.getTargetInfo().getTriple().isSPIRV();
6408 bool IsHLSL = S.Context.getLangOpts().HLSL;
6409 if ((IsAArch64 && !S.ARM().SveAliasValid(BuiltinID, AliasName)) ||
6410 (IsARM && !S.ARM().MveAliasValid(BuiltinID, AliasName) &&
6411 !S.ARM().CdeAliasValid(BuiltinID, AliasName)) ||
6412 (IsRISCV && !S.RISCV().isAliasValid(BuiltinID, AliasName)) ||
6413 (!IsAArch64 && !IsARM && !IsRISCV && !IsHLSL && !IsSPIRV)) {
6414 S.Diag(AL.getLoc(), diag::err_attribute_builtin_alias) << AL;
6415 return;
6416 }
6417
6418 D->addAttr(::new (S.Context) BuiltinAliasAttr(S.Context, AL, Ident));
6419}
6420
6421static void handleNullableTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6422 if (AL.isUsedAsTypeAttr())
6423 return;
6424
6425 if (auto *CRD = dyn_cast<CXXRecordDecl>(D);
6426 !CRD || !(CRD->isClass() || CRD->isStruct())) {
6427 S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
6429 return;
6430 }
6431
6433}
6434
6435static void handlePreferredTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6436 if (!AL.hasParsedType()) {
6437 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
6438 return;
6439 }
6440
6441 TypeSourceInfo *ParmTSI = nullptr;
6442 QualType QT = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
6443 assert(ParmTSI && "no type source info for attribute argument");
6444 S.RequireCompleteType(ParmTSI->getTypeLoc().getBeginLoc(), QT,
6445 diag::err_incomplete_type);
6446
6447 D->addAttr(::new (S.Context) PreferredTypeAttr(S.Context, AL, ParmTSI));
6448}
6449
6450//===----------------------------------------------------------------------===//
6451// Microsoft specific attribute handlers.
6452//===----------------------------------------------------------------------===//
6453
6455 StringRef UuidAsWritten, MSGuidDecl *GuidDecl) {
6456 if (const auto *UA = D->getAttr<UuidAttr>()) {
6457 if (declaresSameEntity(UA->getGuidDecl(), GuidDecl))
6458 return nullptr;
6459 if (!UA->getGuid().empty()) {
6460 Diag(UA->getLocation(), diag::err_mismatched_uuid);
6461 Diag(CI.getLoc(), diag::note_previous_uuid);
6462 D->dropAttr<UuidAttr>();
6463 }
6464 }
6465
6466 return ::new (Context) UuidAttr(Context, CI, UuidAsWritten, GuidDecl);
6467}
6468
6469static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6470 if (!S.LangOpts.CPlusPlus) {
6471 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
6472 << AL << AttributeLangSupport::C;
6473 return;
6474 }
6475
6476 StringRef OrigStrRef;
6477 SourceLocation LiteralLoc;
6478 if (!S.checkStringLiteralArgumentAttr(AL, 0, OrigStrRef, &LiteralLoc))
6479 return;
6480
6481 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
6482 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former.
6483 StringRef StrRef = OrigStrRef;
6484 if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}')
6485 StrRef = StrRef.drop_front().drop_back();
6486
6487 // Validate GUID length.
6488 if (StrRef.size() != 36) {
6489 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
6490 return;
6491 }
6492
6493 for (unsigned i = 0; i < 36; ++i) {
6494 if (i == 8 || i == 13 || i == 18 || i == 23) {
6495 if (StrRef[i] != '-') {
6496 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
6497 return;
6498 }
6499 } else if (!isHexDigit(StrRef[i])) {
6500 S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid);
6501 return;
6502 }
6503 }
6504
6505 // Convert to our parsed format and canonicalize.
6506 MSGuidDecl::Parts Parsed;
6507 StrRef.substr(0, 8).getAsInteger(16, Parsed.Part1);
6508 StrRef.substr(9, 4).getAsInteger(16, Parsed.Part2);
6509 StrRef.substr(14, 4).getAsInteger(16, Parsed.Part3);
6510 for (unsigned i = 0; i != 8; ++i)
6511 StrRef.substr(19 + 2 * i + (i >= 2 ? 1 : 0), 2)
6512 .getAsInteger(16, Parsed.Part4And5[i]);
6513 MSGuidDecl *Guid = S.Context.getMSGuidDecl(Parsed);
6514
6515 // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's
6516 // the only thing in the [] list, the [] too), and add an insertion of
6517 // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas
6518 // separating attributes nor of the [ and the ] are in the AST.
6519 // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc"
6520 // on cfe-dev.
6521 if (AL.isMicrosoftAttribute()) // Check for [uuid(...)] spelling.
6522 S.Diag(AL.getLoc(), diag::warn_atl_uuid_deprecated);
6523
6524 UuidAttr *UA = S.mergeUuidAttr(D, AL, OrigStrRef, Guid);
6525 if (UA)
6526 D->addAttr(UA);
6527}
6528
6529static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6530 if (!S.LangOpts.CPlusPlus) {
6531 S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
6532 << AL << AttributeLangSupport::C;
6533 return;
6534 }
6535 MSInheritanceAttr *IA = S.mergeMSInheritanceAttr(
6536 D, AL, /*BestCase=*/true, (MSInheritanceModel)AL.getSemanticSpelling());
6537 if (IA) {
6538 D->addAttr(IA);
6540 }
6541}
6542
6543static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6544 const auto *VD = cast<VarDecl>(D);
6546 S.Diag(AL.getLoc(), diag::err_thread_unsupported);
6547 return;
6548 }
6549 if (VD->getTSCSpec() != TSCS_unspecified) {
6550 S.Diag(AL.getLoc(), diag::err_declspec_thread_on_thread_variable);
6551 return;
6552 }
6553 if (VD->hasLocalStorage()) {
6554 S.Diag(AL.getLoc(), diag::err_thread_non_global) << "__declspec(thread)";
6555 return;
6556 }
6557 D->addAttr(::new (S.Context) ThreadAttr(S.Context, AL));
6558}
6559
6560static void handleMSConstexprAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6562 S.Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
6563 << AL << AL.getRange();
6564 return;
6565 }
6566 auto *FD = cast<FunctionDecl>(D);
6567 if (FD->isConstexprSpecified() || FD->isConsteval()) {
6568 S.Diag(AL.getLoc(), diag::err_ms_constexpr_cannot_be_applied)
6569 << FD->isConsteval() << FD;
6570 return;
6571 }
6572 if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
6573 if (!S.getLangOpts().CPlusPlus20 && MD->isVirtual()) {
6574 S.Diag(AL.getLoc(), diag::err_ms_constexpr_cannot_be_applied)
6575 << /*virtual*/ 2 << MD;
6576 return;
6577 }
6578 }
6579 D->addAttr(::new (S.Context) MSConstexprAttr(S.Context, AL));
6580}
6581
6582static void handleMSStructAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6583 if (const auto *First = D->getAttr<GCCStructAttr>()) {
6584 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
6585 << AL << First << 0;
6586 S.Diag(First->getLocation(), diag::note_conflicting_attribute);
6587 return;
6588 }
6589 if (const auto *Preexisting = D->getAttr<MSStructAttr>()) {
6590 if (Preexisting->isImplicit())
6591 D->dropAttr<MSStructAttr>();
6592 }
6593
6594 D->addAttr(::new (S.Context) MSStructAttr(S.Context, AL));
6595}
6596
6597static void handleGCCStructAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6598 if (const auto *First = D->getAttr<MSStructAttr>()) {
6599 if (First->isImplicit()) {
6600 D->dropAttr<MSStructAttr>();
6601 } else {
6602 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
6603 << AL << First << 0;
6604 S.Diag(First->getLocation(), diag::note_conflicting_attribute);
6605 return;
6606 }
6607 }
6608
6609 D->addAttr(::new (S.Context) GCCStructAttr(S.Context, AL));
6610}
6611
6612static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6614 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
6615 StringRef Tag;
6616 if (!S.checkStringLiteralArgumentAttr(AL, I, Tag))
6617 return;
6618 Tags.push_back(Tag);
6619 }
6620
6621 if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
6622 if (!NS->isInline()) {
6623 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 0;
6624 return;
6625 }
6626 if (NS->isAnonymousNamespace()) {
6627 S.Diag(AL.getLoc(), diag::warn_attr_abi_tag_namespace) << 1;
6628 return;
6629 }
6630 if (AL.getNumArgs() == 0)
6631 Tags.push_back(NS->getName());
6632 } else if (!AL.checkAtLeastNumArgs(S, 1))
6633 return;
6634
6635 // Store tags sorted and without duplicates.
6636 llvm::sort(Tags);
6637 Tags.erase(llvm::unique(Tags), Tags.end());
6638
6639 D->addAttr(::new (S.Context)
6640 AbiTagAttr(S.Context, AL, Tags.data(), Tags.size()));
6641}
6642
6643static bool hasBTFDeclTagAttr(Decl *D, StringRef Tag) {
6644 for (const auto *I : D->specific_attrs<BTFDeclTagAttr>()) {
6645 if (I->getBTFDeclTag() == Tag)
6646 return true;
6647 }
6648 return false;
6649}
6650
6651static void handleBTFDeclTagAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6652 StringRef Str;
6653 if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
6654 return;
6655 if (hasBTFDeclTagAttr(D, Str))
6656 return;
6657
6658 D->addAttr(::new (S.Context) BTFDeclTagAttr(S.Context, AL, Str));
6659}
6660
6661BTFDeclTagAttr *Sema::mergeBTFDeclTagAttr(Decl *D, const BTFDeclTagAttr &AL) {
6662 if (hasBTFDeclTagAttr(D, AL.getBTFDeclTag()))
6663 return nullptr;
6664 return ::new (Context) BTFDeclTagAttr(Context, AL, AL.getBTFDeclTag());
6665}
6666
6667static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6668 // Dispatch the interrupt attribute based on the current target.
6669 switch (S.Context.getTargetInfo().getTriple().getArch()) {
6670 case llvm::Triple::msp430:
6671 S.MSP430().handleInterruptAttr(D, AL);
6672 break;
6673 case llvm::Triple::mipsel:
6674 case llvm::Triple::mips:
6675 S.MIPS().handleInterruptAttr(D, AL);
6676 break;
6677 case llvm::Triple::m68k:
6678 S.M68k().handleInterruptAttr(D, AL);
6679 break;
6680 case llvm::Triple::x86:
6681 case llvm::Triple::x86_64:
6682 S.X86().handleAnyInterruptAttr(D, AL);
6683 break;
6684 case llvm::Triple::avr:
6685 S.AVR().handleInterruptAttr(D, AL);
6686 break;
6687 case llvm::Triple::riscv32:
6688 case llvm::Triple::riscv64:
6689 case llvm::Triple::riscv32be:
6690 case llvm::Triple::riscv64be:
6691 S.RISCV().handleInterruptAttr(D, AL);
6692 break;
6693 default:
6694 S.ARM().handleInterruptAttr(D, AL);
6695 break;
6696 }
6697}
6698
6699static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL) {
6700 uint32_t Version;
6701 Expr *VersionExpr = AL.getArgAsExpr(0);
6702 if (!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), Version))
6703 return;
6704
6705 // TODO: Investigate what happens with the next major version of MSVC.
6706 if (Version != LangOptions::MSVC2015 / 100) {
6707 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
6708 << AL << Version << VersionExpr->getSourceRange();
6709 return;
6710 }
6711
6712 // The attribute expects a "major" version number like 19, but new versions of
6713 // MSVC have moved to updating the "minor", or less significant numbers, so we
6714 // have to multiply by 100 now.
6715 Version *= 100;
6716
6717 D->addAttr(::new (S.Context) LayoutVersionAttr(S.Context, AL, Version));
6718}
6719
6721 const AttributeCommonInfo &CI) {
6722 if (D->hasAttr<DLLExportAttr>()) {
6723 Diag(CI.getLoc(), diag::warn_attribute_ignored) << "'dllimport'";
6724 return nullptr;
6725 }
6726
6727 if (D->hasAttr<DLLImportAttr>())
6728 return nullptr;
6729
6730 return ::new (Context) DLLImportAttr(Context, CI);
6731}
6732
6734 const AttributeCommonInfo &CI) {
6735 if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) {
6736 Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import;
6737 D->dropAttr<DLLImportAttr>();
6738 }
6739
6740 if (D->hasAttr<DLLExportAttr>())
6741 return nullptr;
6742
6743 return ::new (Context) DLLExportAttr(Context, CI);
6744}
6745
6746static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A) {
6749 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) << A;
6750 return;
6751 }
6752
6753 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
6754 if (FD->isInlined() && A.getKind() == ParsedAttr::AT_DLLImport &&
6756 // MinGW doesn't allow dllimport on inline functions.
6757 S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline)
6758 << A;
6759 return;
6760 }
6761 }
6762
6763 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
6765 MD->getParent()->isLambda()) {
6766 S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A;
6767 return;
6768 }
6769 }
6770
6771 if (auto *EA = D->getAttr<ExcludeFromExplicitInstantiationAttr>()) {
6772 S.Diag(A.getRange().getBegin(),
6773 diag::warn_dllattr_ignored_exclusion_takes_precedence)
6774 << A << EA;
6775 return;
6776 }
6777
6778 Attr *NewAttr = A.getKind() == ParsedAttr::AT_DLLExport
6779 ? (Attr *)S.mergeDLLExportAttr(D, A)
6780 : (Attr *)S.mergeDLLImportAttr(D, A);
6781 if (NewAttr)
6782 D->addAttr(NewAttr);
6783}
6784
6785MSInheritanceAttr *
6787 bool BestCase,
6788 MSInheritanceModel Model) {
6789 if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) {
6790 if (IA->getInheritanceModel() == Model)
6791 return nullptr;
6792 Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance)
6793 << 1 /*previous declaration*/;
6794 Diag(CI.getLoc(), diag::note_previous_ms_inheritance);
6795 D->dropAttr<MSInheritanceAttr>();
6796 }
6797
6798 auto *RD = cast<CXXRecordDecl>(D);
6799 if (RD->hasDefinition()) {
6800 if (checkMSInheritanceAttrOnDefinition(RD, CI.getRange(), BestCase,
6801 Model)) {
6802 return nullptr;
6803 }
6804 } else {
6806 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
6807 << 1 /*partial specialization*/;
6808 return nullptr;
6809 }
6810 if (RD->getDescribedClassTemplate()) {
6811 Diag(CI.getLoc(), diag::warn_ignored_ms_inheritance)
6812 << 0 /*primary template*/;
6813 return nullptr;
6814 }
6815 }
6816
6817 return ::new (Context) MSInheritanceAttr(Context, CI, BestCase);
6818}
6819
6820static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6821 // The capability attributes take a single string parameter for the name of
6822 // the capability they represent. The lockable attribute does not take any
6823 // parameters. However, semantically, both attributes represent the same
6824 // concept, and so they use the same semantic attribute. Eventually, the
6825 // lockable attribute will be removed.
6826 //
6827 // For backward compatibility, any capability which has no specified string
6828 // literal will be considered a "mutex."
6829 StringRef N("mutex");
6830 SourceLocation LiteralLoc;
6831 if (AL.getKind() == ParsedAttr::AT_Capability &&
6832 !S.checkStringLiteralArgumentAttr(AL, 0, N, &LiteralLoc))
6833 return;
6834
6835 D->addAttr(::new (S.Context) CapabilityAttr(S.Context, AL, N));
6836}
6837
6839 const ParsedAttr &AL) {
6840 // Do not permit 'reentrant_capability' without 'capability(..)'. Note that
6841 // the check here requires 'capability' to be before 'reentrant_capability'.
6842 // This helps enforce a canonical style. Also avoids placing an additional
6843 // branch into ProcessDeclAttributeList().
6844 if (!D->hasAttr<CapabilityAttr>()) {
6845 S.Diag(AL.getLoc(), diag::warn_thread_attribute_requires_preceded)
6846 << AL << cast<NamedDecl>(D) << "'capability'";
6847 return;
6848 }
6849
6850 D->addAttr(::new (S.Context) ReentrantCapabilityAttr(S.Context, AL));
6851}
6852
6853static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6854 if (!checkThreadSafetyAttrSubject(S, D, AL))
6855 return;
6856
6858 if (!checkLockFunAttrCommon(S, D, AL, Args))
6859 return;
6860
6861 D->addAttr(::new (S.Context)
6862 AssertCapabilityAttr(S.Context, AL, Args.data(), Args.size()));
6863}
6864
6866 const ParsedAttr &AL) {
6867 if (!checkThreadSafetyAttrSubject(S, D, AL, /*CheckParmVar=*/true))
6868 return;
6869
6871 if (!checkLockFunAttrCommon(S, D, AL, Args))
6872 return;
6873
6874 D->addAttr(::new (S.Context) AcquireCapabilityAttr(S.Context, AL, Args.data(),
6875 Args.size()));
6876}
6877
6879 const ParsedAttr &AL) {
6880 if (!checkThreadSafetyAttrSubject(S, D, AL))
6881 return;
6882
6884 if (!checkTryLockFunAttrCommon(S, D, AL, Args))
6885 return;
6886
6887 D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(
6888 S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
6889}
6890
6892 const ParsedAttr &AL) {
6893 if (!checkThreadSafetyAttrSubject(S, D, AL, /*CheckParmVar=*/true))
6894 return;
6895
6896 // Check that all arguments are lockable objects.
6898 checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, true);
6899
6900 D->addAttr(::new (S.Context) ReleaseCapabilityAttr(S.Context, AL, Args.data(),
6901 Args.size()));
6902}
6903
6905 const ParsedAttr &AL) {
6906 if (!checkThreadSafetyAttrSubject(S, D, AL, /*CheckParmVar=*/true))
6907 return;
6908
6909 if (!AL.checkAtLeastNumArgs(S, 1))
6910 return;
6911
6912 // check that all arguments are lockable objects
6914 checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
6915 if (Args.empty())
6916 return;
6917
6918 RequiresCapabilityAttr *RCA = ::new (S.Context)
6919 RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size());
6920
6921 D->addAttr(RCA);
6922}
6923
6924static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6925 if (const auto *NSD = dyn_cast<NamespaceDecl>(D)) {
6926 if (NSD->isAnonymousNamespace()) {
6927 S.Diag(AL.getLoc(), diag::warn_deprecated_anonymous_namespace);
6928 // Do not want to attach the attribute to the namespace because that will
6929 // cause confusing diagnostic reports for uses of declarations within the
6930 // namespace.
6931 return;
6932 }
6935 S.Diag(AL.getRange().getBegin(), diag::warn_deprecated_ignored_on_using)
6936 << AL;
6937 return;
6938 }
6939
6940 // Handle the cases where the attribute has a text message.
6941 StringRef Str, Replacement;
6942 if (AL.isArgExpr(0) && AL.getArgAsExpr(0) &&
6943 !S.checkStringLiteralArgumentAttr(AL, 0, Str))
6944 return;
6945
6946 // Support a single optional message only for Declspec and [[]] spellings.
6948 AL.checkAtMostNumArgs(S, 1);
6949 else if (AL.isArgExpr(1) && AL.getArgAsExpr(1) &&
6950 !S.checkStringLiteralArgumentAttr(AL, 1, Replacement))
6951 return;
6952
6953 if (!S.getLangOpts().CPlusPlus14 && AL.isCXX11Attribute() && !AL.isGNUScope())
6954 S.Diag(AL.getLoc(), diag::ext_cxx14_attr) << AL;
6955
6956 D->addAttr(::new (S.Context) DeprecatedAttr(S.Context, AL, Str, Replacement));
6957}
6958
6959static bool isGlobalVar(const Decl *D) {
6960 if (const auto *S = dyn_cast<VarDecl>(D))
6961 return S->hasGlobalStorage();
6962 return false;
6963}
6964
6965static bool isSanitizerAttributeAllowedOnGlobals(StringRef Sanitizer) {
6966 return Sanitizer == "address" || Sanitizer == "hwaddress" ||
6967 Sanitizer == "memtag";
6968}
6969
6970static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6971 if (!AL.checkAtLeastNumArgs(S, 1))
6972 return;
6973
6974 std::vector<StringRef> Sanitizers;
6975
6976 for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) {
6977 StringRef SanitizerName;
6978 SourceLocation LiteralLoc;
6979
6980 if (!S.checkStringLiteralArgumentAttr(AL, I, SanitizerName, &LiteralLoc))
6981 return;
6982
6983 if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) ==
6984 SanitizerMask() &&
6985 SanitizerName != "coverage")
6986 S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName;
6987 else if (isGlobalVar(D) && !isSanitizerAttributeAllowedOnGlobals(SanitizerName))
6988 S.Diag(D->getLocation(), diag::warn_attribute_type_not_supported_global)
6989 << AL << SanitizerName;
6990 Sanitizers.push_back(SanitizerName);
6991 }
6992
6993 D->addAttr(::new (S.Context) NoSanitizeAttr(S.Context, AL, Sanitizers.data(),
6994 Sanitizers.size()));
6995}
6996
6998getNoSanitizeAttrInfo(const ParsedAttr &NoSanitizeSpecificAttr) {
6999 // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
7000 // NoSanitizeAttr object; but we need to calculate the correct spelling list
7001 // index rather than incorrectly assume the index for NoSanitizeSpecificAttr
7002 // has the same spellings as the index for NoSanitizeAttr. We don't have a
7003 // general way to "translate" between the two, so this hack attempts to work
7004 // around the issue with hard-coded indices. This is critical for calling
7005 // getSpelling() or prettyPrint() on the resulting semantic attribute object
7006 // without failing assertions.
7007 unsigned TranslatedSpellingIndex = 0;
7008 if (NoSanitizeSpecificAttr.isStandardAttributeSyntax())
7009 TranslatedSpellingIndex = 1;
7010
7011 AttributeCommonInfo Info = NoSanitizeSpecificAttr;
7012 Info.setAttributeSpellingListIndex(TranslatedSpellingIndex);
7013 return Info;
7014}
7015
7017 const ParsedAttr &AL) {
7018 StringRef SanitizerName = "address";
7020 D->addAttr(::new (S.Context)
7021 NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
7022}
7023
7024static void handleNoSanitizeThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7025 StringRef SanitizerName = "thread";
7027 D->addAttr(::new (S.Context)
7028 NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
7029}
7030
7031static void handleNoSanitizeMemoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7032 StringRef SanitizerName = "memory";
7034 D->addAttr(::new (S.Context)
7035 NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
7036}
7037
7038static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7039 if (InternalLinkageAttr *Internal = S.mergeInternalLinkageAttr(D, AL))
7040 D->addAttr(Internal);
7041}
7042
7043static void handleZeroCallUsedRegsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7044 // Check that the argument is a string literal.
7045 StringRef KindStr;
7046 SourceLocation LiteralLoc;
7047 if (!S.checkStringLiteralArgumentAttr(AL, 0, KindStr, &LiteralLoc))
7048 return;
7049
7050 ZeroCallUsedRegsAttr::ZeroCallUsedRegsKind Kind;
7051 if (!ZeroCallUsedRegsAttr::ConvertStrToZeroCallUsedRegsKind(KindStr, Kind)) {
7052 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
7053 << AL << KindStr;
7054 return;
7055 }
7056
7057 D->dropAttr<ZeroCallUsedRegsAttr>();
7058 D->addAttr(ZeroCallUsedRegsAttr::Create(S.Context, Kind, AL));
7059}
7060
7061static void handleNoPFPAttrField(Sema &S, Decl *D, const ParsedAttr &AL) {
7062 D->addAttr(NoFieldProtectionAttr::Create(S.Context, AL));
7063}
7064
7065static void handleCountedByAttrField(Sema &S, Decl *D, const ParsedAttr &AL) {
7066 auto *CountExpr = AL.getArgAsExpr(0);
7067 if (!CountExpr)
7068 return;
7069
7070 bool CountInBytes;
7071 bool OrNull;
7072 switch (AL.getKind()) {
7073 case ParsedAttr::AT_CountedBy:
7074 CountInBytes = false;
7075 OrNull = false;
7076 break;
7077 case ParsedAttr::AT_CountedByOrNull:
7078 CountInBytes = false;
7079 OrNull = true;
7080 break;
7081 case ParsedAttr::AT_SizedBy:
7082 CountInBytes = true;
7083 OrNull = false;
7084 break;
7085 case ParsedAttr::AT_SizedByOrNull:
7086 CountInBytes = true;
7087 OrNull = true;
7088 break;
7089 default:
7090 llvm_unreachable("unexpected counted_by family attribute");
7091 }
7092
7093 FieldDecl *FD = cast<FieldDecl>(D);
7094 if (S.CheckCountedByAttrOnField(FD, CountExpr, CountInBytes, OrNull))
7095 return;
7096
7098 FD->getType(), CountExpr, CountInBytes, OrNull);
7099 FD->setType(CAT);
7100}
7101
7103 const ParsedAttr &AL) {
7104 StringRef KindStr;
7105 SourceLocation LiteralLoc;
7106 if (!S.checkStringLiteralArgumentAttr(AL, 0, KindStr, &LiteralLoc))
7107 return;
7108
7109 FunctionReturnThunksAttr::Kind Kind;
7110 if (!FunctionReturnThunksAttr::ConvertStrToKind(KindStr, Kind)) {
7111 S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported)
7112 << AL << KindStr;
7113 return;
7114 }
7115 // FIXME: it would be good to better handle attribute merging rather than
7116 // silently replacing the existing attribute, so long as it does not break
7117 // the expected codegen tests.
7118 D->dropAttr<FunctionReturnThunksAttr>();
7119 D->addAttr(FunctionReturnThunksAttr::Create(S.Context, Kind, AL));
7120}
7121
7123 const ParsedAttr &AL) {
7124 assert(isa<TypedefNameDecl>(D) && "This attribute only applies to a typedef");
7126}
7127
7128static void handleNoMergeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7129 auto *VDecl = dyn_cast<VarDecl>(D);
7130 if (VDecl && !VDecl->isFunctionPointerType()) {
7131 S.Diag(AL.getLoc(), diag::warn_attribute_ignored_non_function_pointer)
7132 << AL << VDecl;
7133 return;
7134 }
7135 D->addAttr(NoMergeAttr::Create(S.Context, AL));
7136}
7137
7138static void handleNoUniqueAddressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7139 D->addAttr(NoUniqueAddressAttr::Create(S.Context, AL));
7140}
7141
7142static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
7143 if (!cast<VarDecl>(D)->hasGlobalStorage()) {
7144 S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
7145 << (A.getKind() == ParsedAttr::AT_AlwaysDestroy);
7146 return;
7147 }
7148
7149 if (A.getKind() == ParsedAttr::AT_AlwaysDestroy)
7151 else
7153}
7154
7155static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7156 assert(cast<VarDecl>(D)->getStorageDuration() == SD_Automatic &&
7157 "uninitialized is only valid on automatic duration variables");
7158 D->addAttr(::new (S.Context) UninitializedAttr(S.Context, AL));
7159}
7160
7161static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7162 // Check that the return type is a `typedef int kern_return_t` or a typedef
7163 // around it, because otherwise MIG convention checks make no sense.
7164 // BlockDecl doesn't store a return type, so it's annoying to check,
7165 // so let's skip it for now.
7166 if (!isa<BlockDecl>(D)) {
7168 bool IsKernReturnT = false;
7169 while (const auto *TT = T->getAs<TypedefType>()) {
7170 IsKernReturnT = (TT->getDecl()->getName() == "kern_return_t");
7171 T = TT->desugar();
7172 }
7173 if (!IsKernReturnT || T.getCanonicalType() != S.getASTContext().IntTy) {
7174 S.Diag(D->getBeginLoc(),
7175 diag::warn_mig_server_routine_does_not_return_kern_return_t);
7176 return;
7177 }
7178 }
7179
7181}
7182
7183static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7184 // Warn if the return type is not a pointer or reference type.
7185 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7186 QualType RetTy = FD->getReturnType();
7187 if (!RetTy->isPointerOrReferenceType()) {
7188 S.Diag(AL.getLoc(), diag::warn_declspec_allocator_nonpointer)
7189 << AL.getRange() << RetTy;
7190 return;
7191 }
7192 }
7193
7195}
7196
7197static void handleAcquireHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7198 if (AL.isUsedAsTypeAttr())
7199 return;
7200 // Warn if the parameter is definitely not an output parameter.
7201 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
7202 if (PVD->getType()->isIntegerType()) {
7203 S.Diag(AL.getLoc(), diag::err_attribute_output_parameter)
7204 << AL.getRange();
7205 return;
7206 }
7207 }
7208 StringRef Argument;
7209 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
7210 return;
7211 D->addAttr(AcquireHandleAttr::Create(S.Context, Argument, AL));
7212}
7213
7214template<typename Attr>
7215static void handleHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7216 StringRef Argument;
7217 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
7218 return;
7219 D->addAttr(Attr::Create(S.Context, Argument, AL));
7220}
7221
7222template<typename Attr>
7223static void handleUnsafeBufferUsage(Sema &S, Decl *D, const ParsedAttr &AL) {
7224 D->addAttr(Attr::Create(S.Context, AL));
7225}
7226
7227static void handleCFGuardAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7228 // The guard attribute takes a single identifier argument.
7229
7230 if (!AL.isArgIdent(0)) {
7231 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
7232 << AL << AANT_ArgumentIdentifier;
7233 return;
7234 }
7235
7236 CFGuardAttr::GuardArg Arg;
7238 if (!CFGuardAttr::ConvertStrToGuardArg(II->getName(), Arg)) {
7239 S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
7240 return;
7241 }
7242
7243 D->addAttr(::new (S.Context) CFGuardAttr(S.Context, AL, Arg));
7244}
7245
7246
7247template <typename AttrTy>
7248static const AttrTy *findEnforceTCBAttrByName(Decl *D, StringRef Name) {
7249 auto Attrs = D->specific_attrs<AttrTy>();
7250 auto I = llvm::find_if(Attrs,
7251 [Name](const AttrTy *A) {
7252 return A->getTCBName() == Name;
7253 });
7254 return I == Attrs.end() ? nullptr : *I;
7255}
7256
7257template <typename AttrTy, typename ConflictingAttrTy>
7258static void handleEnforceTCBAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7259 StringRef Argument;
7260 if (!S.checkStringLiteralArgumentAttr(AL, 0, Argument))
7261 return;
7262
7263 // A function cannot be have both regular and leaf membership in the same TCB.
7264 if (const ConflictingAttrTy *ConflictingAttr =
7266 // We could attach a note to the other attribute but in this case
7267 // there's no need given how the two are very close to each other.
7268 S.Diag(AL.getLoc(), diag::err_tcb_conflicting_attributes)
7269 << AL.getAttrName()->getName() << ConflictingAttr->getAttrName()->getName()
7270 << Argument;
7271
7272 // Error recovery: drop the non-leaf attribute so that to suppress
7273 // all future warnings caused by erroneous attributes. The leaf attribute
7274 // needs to be kept because it can only suppresses warnings, not cause them.
7275 D->dropAttr<EnforceTCBAttr>();
7276 return;
7277 }
7278
7279 D->addAttr(AttrTy::Create(S.Context, Argument, AL));
7280}
7281
7282template <typename AttrTy, typename ConflictingAttrTy>
7283static AttrTy *mergeEnforceTCBAttrImpl(Sema &S, Decl *D, const AttrTy &AL) {
7284 // Check if the new redeclaration has different leaf-ness in the same TCB.
7285 StringRef TCBName = AL.getTCBName();
7286 if (const ConflictingAttrTy *ConflictingAttr =
7288 S.Diag(ConflictingAttr->getLoc(), diag::err_tcb_conflicting_attributes)
7289 << ConflictingAttr->getAttrName()->getName()
7290 << AL.getAttrName()->getName() << TCBName;
7291
7292 // Add a note so that the user could easily find the conflicting attribute.
7293 S.Diag(AL.getLoc(), diag::note_conflicting_attribute);
7294
7295 // More error recovery.
7296 D->dropAttr<EnforceTCBAttr>();
7297 return nullptr;
7298 }
7299
7300 ASTContext &Context = S.getASTContext();
7301 return ::new(Context) AttrTy(Context, AL, AL.getTCBName());
7302}
7303
7304EnforceTCBAttr *Sema::mergeEnforceTCBAttr(Decl *D, const EnforceTCBAttr &AL) {
7306 *this, D, AL);
7307}
7308
7310 Decl *D, const EnforceTCBLeafAttr &AL) {
7312 *this, D, AL);
7313}
7314
7316 const ParsedAttr &AL) {
7318 const uint32_t NumArgs = AL.getNumArgs();
7319 if (NumArgs > 4) {
7320 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 4;
7321 AL.setInvalid();
7322 }
7323
7324 if (NumArgs == 0) {
7325 S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL;
7326 AL.setInvalid();
7327 return;
7328 }
7329
7330 if (D->getAttr<VTablePointerAuthenticationAttr>()) {
7331 S.Diag(AL.getLoc(), diag::err_duplicated_vtable_pointer_auth) << Decl;
7332 AL.setInvalid();
7333 }
7334
7335 auto KeyType = VTablePointerAuthenticationAttr::VPtrAuthKeyType::DefaultKey;
7336 if (AL.isArgIdent(0)) {
7337 IdentifierLoc *IL = AL.getArgAsIdent(0);
7338 if (!VTablePointerAuthenticationAttr::ConvertStrToVPtrAuthKeyType(
7339 IL->getIdentifierInfo()->getName(), KeyType)) {
7340 S.Diag(IL->getLoc(), diag::err_invalid_authentication_key)
7341 << IL->getIdentifierInfo();
7342 AL.setInvalid();
7343 }
7344 if (KeyType == VTablePointerAuthenticationAttr::DefaultKey &&
7345 !S.getLangOpts().PointerAuthCalls) {
7346 S.Diag(AL.getLoc(), diag::err_no_default_vtable_pointer_auth) << 0;
7347 AL.setInvalid();
7348 }
7349 } else {
7350 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
7351 << AL << AANT_ArgumentIdentifier;
7352 return;
7353 }
7354
7355 auto AddressDiversityMode = VTablePointerAuthenticationAttr::
7356 AddressDiscriminationMode::DefaultAddressDiscrimination;
7357 if (AL.getNumArgs() > 1) {
7358 if (AL.isArgIdent(1)) {
7359 IdentifierLoc *IL = AL.getArgAsIdent(1);
7360 if (!VTablePointerAuthenticationAttr::
7361 ConvertStrToAddressDiscriminationMode(
7362 IL->getIdentifierInfo()->getName(), AddressDiversityMode)) {
7363 S.Diag(IL->getLoc(), diag::err_invalid_address_discrimination)
7364 << IL->getIdentifierInfo();
7365 AL.setInvalid();
7366 }
7367 if (AddressDiversityMode ==
7368 VTablePointerAuthenticationAttr::DefaultAddressDiscrimination &&
7369 !S.getLangOpts().PointerAuthCalls) {
7370 S.Diag(IL->getLoc(), diag::err_no_default_vtable_pointer_auth) << 1;
7371 AL.setInvalid();
7372 }
7373 } else {
7374 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
7375 << AL << AANT_ArgumentIdentifier;
7376 }
7377 }
7378
7379 auto ED = VTablePointerAuthenticationAttr::ExtraDiscrimination::
7380 DefaultExtraDiscrimination;
7381 if (AL.getNumArgs() > 2) {
7382 if (AL.isArgIdent(2)) {
7383 IdentifierLoc *IL = AL.getArgAsIdent(2);
7384 if (!VTablePointerAuthenticationAttr::ConvertStrToExtraDiscrimination(
7385 IL->getIdentifierInfo()->getName(), ED)) {
7386 S.Diag(IL->getLoc(), diag::err_invalid_extra_discrimination)
7387 << IL->getIdentifierInfo();
7388 AL.setInvalid();
7389 }
7390 if (ED == VTablePointerAuthenticationAttr::DefaultExtraDiscrimination &&
7391 !S.getLangOpts().PointerAuthCalls) {
7392 S.Diag(AL.getLoc(), diag::err_no_default_vtable_pointer_auth) << 2;
7393 AL.setInvalid();
7394 }
7395 } else {
7396 S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
7397 << AL << AANT_ArgumentIdentifier;
7398 }
7399 }
7400
7401 uint32_t CustomDiscriminationValue = 0;
7402 if (ED == VTablePointerAuthenticationAttr::CustomDiscrimination) {
7403 if (NumArgs < 4) {
7404 S.Diag(AL.getLoc(), diag::err_missing_custom_discrimination) << AL << 4;
7405 AL.setInvalid();
7406 return;
7407 }
7408 if (NumArgs > 4) {
7409 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 4;
7410 AL.setInvalid();
7411 }
7412
7413 if (!AL.isArgExpr(3) || !S.checkUInt32Argument(AL, AL.getArgAsExpr(3),
7414 CustomDiscriminationValue)) {
7415 S.Diag(AL.getLoc(), diag::err_invalid_custom_discrimination);
7416 AL.setInvalid();
7417 }
7418 } else if (NumArgs > 3) {
7419 S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 3;
7420 AL.setInvalid();
7421 }
7422
7423 Decl->addAttr(::new (S.Context) VTablePointerAuthenticationAttr(
7424 S.Context, AL, KeyType, AddressDiversityMode, ED,
7425 CustomDiscriminationValue));
7426}
7427
7428static bool modularFormatAttrsEquiv(const ModularFormatAttr *Existing,
7429 const IdentifierInfo *ModularImplFn,
7430 StringRef ImplName,
7431 ArrayRef<StringRef> Aspects) {
7432 return Existing->getModularImplFn() == ModularImplFn &&
7433 Existing->getImplName() == ImplName &&
7434 Existing->aspects_size() == Aspects.size() &&
7435 llvm::equal(Existing->aspects(), Aspects);
7436}
7437
7439 Decl *D, const AttributeCommonInfo &CI, const IdentifierInfo *ModularImplFn,
7440 StringRef ImplName, MutableArrayRef<StringRef> Aspects) {
7441 if (const auto *Existing = D->getAttr<ModularFormatAttr>()) {
7442 if (!modularFormatAttrsEquiv(Existing, ModularImplFn, ImplName, Aspects)) {
7443 Diag(Existing->getLocation(), diag::err_duplicate_attribute) << *Existing;
7444 Diag(CI.getLoc(), diag::note_conflicting_attribute);
7445 }
7446 return nullptr;
7447 }
7448 return ::new (Context) ModularFormatAttr(Context, CI, ModularImplFn, ImplName,
7449 Aspects.data(), Aspects.size());
7450}
7451
7452static void handleModularFormat(Sema &S, Decl *D, const ParsedAttr &AL) {
7453 bool Valid = true;
7454 if (!AL.isArgIdent(0)) {
7455 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
7456 << AL << 1 << AANT_ArgumentIdentifier;
7457 Valid = false;
7458 }
7459 StringRef ImplName;
7460 if (!S.checkStringLiteralArgumentAttr(AL, 1, ImplName))
7461 Valid = false;
7462 SmallVector<StringRef> Aspects;
7463 llvm::DenseSet<StringRef> SeenAspects;
7464 for (unsigned I = 2, E = AL.getNumArgs(); I != E; ++I) {
7465 StringRef Aspect;
7466 if (!S.checkStringLiteralArgumentAttr(AL, I, Aspect))
7467 return;
7468 if (!SeenAspects.insert(Aspect).second) {
7469 S.Diag(AL.getArgAsExpr(I)->getExprLoc(),
7470 diag::err_modular_format_duplicate_aspect)
7471 << Aspect;
7472 Valid = false;
7473 continue;
7474 }
7475 Aspects.push_back(Aspect);
7476 }
7477 if (!Valid)
7478 return;
7479
7480 // Store aspects sorted.
7481 llvm::sort(Aspects);
7482 IdentifierInfo *ModularImplFn = AL.getArgAsIdent(0)->getIdentifierInfo();
7483
7484 if (const auto *Existing = D->getAttr<ModularFormatAttr>()) {
7485 if (!modularFormatAttrsEquiv(Existing, ModularImplFn, ImplName, Aspects)) {
7486 S.Diag(AL.getLoc(), diag::err_duplicate_attribute) << *Existing;
7487 S.Diag(Existing->getLoc(), diag::note_conflicting_attribute);
7488 }
7489 // Ignore the later declaration in favor of the earlier one.
7490 return;
7491 }
7492
7493 D->addAttr(::new (S.Context) ModularFormatAttr(
7494 S.Context, AL, ModularImplFn, ImplName, Aspects.data(), Aspects.size()));
7495}
7496
7497//===----------------------------------------------------------------------===//
7498// Top Level Sema Entry Points
7499//===----------------------------------------------------------------------===//
7500
7501// Returns true if the attribute must delay setting its arguments until after
7502// template instantiation, and false otherwise.
7504 // Only attributes that accept expression parameter packs can delay arguments.
7505 if (!AL.acceptsExprPack())
7506 return false;
7507
7508 bool AttrHasVariadicArg = AL.hasVariadicArg();
7509 unsigned AttrNumArgs = AL.getNumArgMembers();
7510 for (size_t I = 0; I < std::min(AL.getNumArgs(), AttrNumArgs); ++I) {
7511 bool IsLastAttrArg = I == (AttrNumArgs - 1);
7512 // If the argument is the last argument and it is variadic it can contain
7513 // any expression.
7514 if (IsLastAttrArg && AttrHasVariadicArg)
7515 return false;
7516 Expr *E = AL.getArgAsExpr(I);
7517 bool ArgMemberCanHoldExpr = AL.isParamExpr(I);
7518 // If the expression is a pack expansion then arguments must be delayed
7519 // unless the argument is an expression and it is the last argument of the
7520 // attribute.
7522 return !(IsLastAttrArg && ArgMemberCanHoldExpr);
7523 // Last case is if the expression is value dependent then it must delay
7524 // arguments unless the corresponding argument is able to hold the
7525 // expression.
7526 if (E->isValueDependent() && !ArgMemberCanHoldExpr)
7527 return true;
7528 }
7529 return false;
7530}
7531
7533 const AttributeCommonInfo &CI) {
7534 if (PersonalityAttr *PA = D->getAttr<PersonalityAttr>()) {
7535 const FunctionDecl *Personality = PA->getRoutine();
7536 if (Context.isSameEntity(Personality, Routine))
7537 return nullptr;
7538 Diag(PA->getLocation(), diag::err_mismatched_personality);
7539 Diag(CI.getLoc(), diag::note_previous_attribute);
7540 D->dropAttr<PersonalityAttr>();
7541 }
7542 return ::new (Context) PersonalityAttr(Context, CI, Routine);
7543}
7544
7545static void handlePersonalityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7546 Expr *E = AL.getArgAsExpr(0);
7547 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
7548 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl()))
7549 if (Attr *A = S.mergePersonalityAttr(D, FD, AL))
7550 return D->addAttr(A);
7551 S.Diag(E->getExprLoc(), diag::err_attribute_personality_arg_not_function)
7552 << AL.getAttrName();
7553}
7554
7555/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
7556/// the attribute applies to decls. If the attribute is a type attribute, just
7557/// silently ignore it if a GNU attribute.
7558static void
7560 const Sema::ProcessDeclAttributeOptions &Options) {
7562 return;
7563
7564 // Ignore C++11 attributes on declarator chunks: they appertain to the type
7565 // instead. Note, isCXX11Attribute() will look at whether the attribute is
7566 // [[]] or alignas, while isC23Attribute() will only look at [[]]. This is
7567 // important for ensuring that alignas in C23 is properly handled on a
7568 // structure member declaration because it is a type-specifier-qualifier in
7569 // C but still applies to the declaration rather than the type.
7570 if ((S.getLangOpts().CPlusPlus ? AL.isCXX11Attribute()
7571 : AL.isC23Attribute()) &&
7572 !Options.IncludeCXX11Attributes)
7573 return;
7574
7575 // Unknown attributes are automatically warned on. Target-specific attributes
7576 // which do not apply to the current target architecture are treated as
7577 // though they were unknown attributes.
7580 if (AL.isRegularKeywordAttribute()) {
7581 S.Diag(AL.getLoc(), diag::err_keyword_not_supported_on_target)
7582 << AL.getAttrName() << AL.getRange();
7583 } else if (AL.isDeclspecAttribute()) {
7584 S.Diag(AL.getLoc(), diag::warn_unhandled_ms_attribute_ignored)
7585 << AL.getAttrName() << AL.getRange();
7586 } else {
7588 }
7589 return;
7590 }
7591
7592 if (S.getLangOpts().HLSL && isa<FunctionDecl>(D) &&
7593 AL.getKind() == ParsedAttr::AT_NoInline) {
7594 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
7595 for (const ParmVarDecl *PVD : FD->parameters()) {
7596 if (PVD->hasAttr<HLSLGroupSharedAddressSpaceAttr>()) {
7597 S.Diag(AL.getLoc(), diag::err_hlsl_attr_incompatible)
7598 << "'noinline'" << "'groupshared' parameter";
7599 return;
7600 }
7601 }
7602 }
7603 }
7604
7605 // Check if argument population must delayed to after template instantiation.
7606 bool MustDelayArgs = MustDelayAttributeArguments(AL);
7607
7608 // Argument number check must be skipped if arguments are delayed.
7609 if (S.checkCommonAttributeFeatures(D, AL, MustDelayArgs))
7610 return;
7611
7612 if (MustDelayArgs) {
7614 return;
7615 }
7616
7617 switch (AL.getKind()) {
7618 default:
7620 break;
7621 if (!AL.isStmtAttr()) {
7622 assert(AL.isTypeAttr() && "Non-type attribute not handled");
7623 }
7624 if (AL.isTypeAttr()) {
7625 if (Options.IgnoreTypeAttributes)
7626 break;
7628 // Non-[[]] type attributes are handled in processTypeAttrs(); silently
7629 // move on.
7630 break;
7631 }
7632
7633 // According to the C and C++ standards, we should never see a
7634 // [[]] type attribute on a declaration. However, we have in the past
7635 // allowed some type attributes to "slide" to the `DeclSpec`, so we need
7636 // to continue to support this legacy behavior. We only do this, however,
7637 // if
7638 // - we actually have a `DeclSpec`, i.e. if we're looking at a
7639 // `DeclaratorDecl`, or
7640 // - we are looking at an alias-declaration, where historically we have
7641 // allowed type attributes after the identifier to slide to the type.
7644 // Suggest moving the attribute to the type instead, but only for our
7645 // own vendor attributes; moving other vendors' attributes might hurt
7646 // portability.
7647 if (AL.isClangScope()) {
7648 S.Diag(AL.getLoc(), diag::warn_type_attribute_deprecated_on_decl)
7649 << AL << D->getLocation();
7650 }
7651
7652 // Allow this type attribute to be handled in processTypeAttrs();
7653 // silently move on.
7654 break;
7655 }
7656
7657 if (AL.getKind() == ParsedAttr::AT_Regparm) {
7658 // `regparm` is a special case: It's a type attribute but we still want
7659 // to treat it as if it had been written on the declaration because that
7660 // way we'll be able to handle it directly in `processTypeAttr()`.
7661 // If we treated `regparm` it as if it had been written on the
7662 // `DeclSpec`, the logic in `distributeFunctionTypeAttrFromDeclSepc()`
7663 // would try to move it to the declarator, but that doesn't work: We
7664 // can't remove the attribute from the list of declaration attributes
7665 // because it might be needed by other declarators in the same
7666 // declaration.
7667 break;
7668 }
7669
7670 if (AL.getKind() == ParsedAttr::AT_VectorSize) {
7671 // `vector_size` is a special case: It's a type attribute semantically,
7672 // but GCC expects the [[]] syntax to be written on the declaration (and
7673 // warns that the attribute has no effect if it is placed on the
7674 // decl-specifier-seq).
7675 // Silently move on and allow the attribute to be handled in
7676 // processTypeAttr().
7677 break;
7678 }
7679
7680 if (AL.getKind() == ParsedAttr::AT_NoDeref) {
7681 // FIXME: `noderef` currently doesn't work correctly in [[]] syntax.
7682 // See https://github.com/llvm/llvm-project/issues/55790 for details.
7683 // We allow processTypeAttrs() to emit a warning and silently move on.
7684 break;
7685 }
7686 }
7687 // N.B., ClangAttrEmitter.cpp emits a diagnostic helper that ensures a
7688 // statement attribute is not written on a declaration, but this code is
7689 // needed for type attributes as well as statement attributes in Attr.td
7690 // that do not list any subjects.
7691 S.Diag(AL.getLoc(), diag::err_attribute_invalid_on_decl)
7692 << AL << AL.isRegularKeywordAttribute() << D->getLocation();
7693 break;
7694 case ParsedAttr::AT_Interrupt:
7695 handleInterruptAttr(S, D, AL);
7696 break;
7697 case ParsedAttr::AT_ARMInterruptSaveFP:
7698 S.ARM().handleInterruptSaveFPAttr(D, AL);
7699 break;
7700 case ParsedAttr::AT_X86ForceAlignArgPointer:
7702 break;
7703 case ParsedAttr::AT_ReadOnlyPlacement:
7705 break;
7706 case ParsedAttr::AT_DLLExport:
7707 case ParsedAttr::AT_DLLImport:
7708 handleDLLAttr(S, D, AL);
7709 break;
7710 case ParsedAttr::AT_AMDGPUFlatWorkGroupSize:
7712 break;
7713 case ParsedAttr::AT_AMDGPUWavesPerEU:
7715 break;
7716 case ParsedAttr::AT_AMDGPUNumSGPR:
7718 break;
7719 case ParsedAttr::AT_AMDGPUNumVGPR:
7721 break;
7722 case ParsedAttr::AT_AMDGPUMaxNumWorkGroups:
7724 break;
7725 case ParsedAttr::AT_AVRSignal:
7726 S.AVR().handleSignalAttr(D, AL);
7727 break;
7728 case ParsedAttr::AT_BPFPreserveAccessIndex:
7730 break;
7731 case ParsedAttr::AT_BPFPreserveStaticOffset:
7733 break;
7734 case ParsedAttr::AT_BTFDeclTag:
7735 handleBTFDeclTagAttr(S, D, AL);
7736 break;
7737 case ParsedAttr::AT_WebAssemblyExportName:
7739 break;
7740 case ParsedAttr::AT_WebAssemblyImportModule:
7742 break;
7743 case ParsedAttr::AT_WebAssemblyImportName:
7745 break;
7746 case ParsedAttr::AT_IBOutlet:
7747 S.ObjC().handleIBOutlet(D, AL);
7748 break;
7749 case ParsedAttr::AT_IBOutletCollection:
7750 S.ObjC().handleIBOutletCollection(D, AL);
7751 break;
7752 case ParsedAttr::AT_IFunc:
7753 handleIFuncAttr(S, D, AL);
7754 break;
7755 case ParsedAttr::AT_Alias:
7756 handleAliasAttr(S, D, AL);
7757 break;
7758 case ParsedAttr::AT_Aligned:
7759 handleAlignedAttr(S, D, AL);
7760 break;
7761 case ParsedAttr::AT_AlignValue:
7762 handleAlignValueAttr(S, D, AL);
7763 break;
7764 case ParsedAttr::AT_AllocSize:
7765 handleAllocSizeAttr(S, D, AL);
7766 break;
7767 case ParsedAttr::AT_AlwaysInline:
7768 handleAlwaysInlineAttr(S, D, AL);
7769 break;
7770 case ParsedAttr::AT_AnalyzerNoReturn:
7772 break;
7773 case ParsedAttr::AT_TLSModel:
7774 handleTLSModelAttr(S, D, AL);
7775 break;
7776 case ParsedAttr::AT_Annotate:
7777 handleAnnotateAttr(S, D, AL);
7778 break;
7779 case ParsedAttr::AT_Availability:
7780 handleAvailabilityAttr(S, D, AL);
7781 break;
7782 case ParsedAttr::AT_CarriesDependency:
7783 handleDependencyAttr(S, scope, D, AL);
7784 break;
7785 case ParsedAttr::AT_CPUDispatch:
7786 case ParsedAttr::AT_CPUSpecific:
7787 handleCPUSpecificAttr(S, D, AL);
7788 break;
7789 case ParsedAttr::AT_Common:
7790 handleCommonAttr(S, D, AL);
7791 break;
7792 case ParsedAttr::AT_CUDAConstant:
7793 handleConstantAttr(S, D, AL);
7794 break;
7795 case ParsedAttr::AT_PassObjectSize:
7796 handlePassObjectSizeAttr(S, D, AL);
7797 break;
7798 case ParsedAttr::AT_Constructor:
7799 handleConstructorAttr(S, D, AL);
7800 break;
7801 case ParsedAttr::AT_Deprecated:
7802 handleDeprecatedAttr(S, D, AL);
7803 break;
7804 case ParsedAttr::AT_Destructor:
7805 handleDestructorAttr(S, D, AL);
7806 break;
7807 case ParsedAttr::AT_EnableIf:
7808 handleEnableIfAttr(S, D, AL);
7809 break;
7810 case ParsedAttr::AT_Error:
7811 handleErrorAttr(S, D, AL);
7812 break;
7813 case ParsedAttr::AT_ExcludeFromExplicitInstantiation:
7815 break;
7816 case ParsedAttr::AT_DiagnoseIf:
7817 handleDiagnoseIfAttr(S, D, AL);
7818 break;
7819 case ParsedAttr::AT_DiagnoseAsBuiltin:
7821 break;
7822 case ParsedAttr::AT_NoBuiltin:
7823 handleNoBuiltinAttr(S, D, AL);
7824 break;
7825 case ParsedAttr::AT_CFIUncheckedCallee:
7827 break;
7828 case ParsedAttr::AT_ExtVectorType:
7829 handleExtVectorTypeAttr(S, D, AL);
7830 break;
7831 case ParsedAttr::AT_ExternalSourceSymbol:
7833 break;
7834 case ParsedAttr::AT_MinSize:
7835 handleMinSizeAttr(S, D, AL);
7836 break;
7837 case ParsedAttr::AT_OptimizeNone:
7838 handleOptimizeNoneAttr(S, D, AL);
7839 break;
7840 case ParsedAttr::AT_EnumExtensibility:
7842 break;
7843 case ParsedAttr::AT_SYCLKernel:
7844 S.SYCL().handleKernelAttr(D, AL);
7845 break;
7846 case ParsedAttr::AT_SYCLExternal:
7848 break;
7849 case ParsedAttr::AT_SYCLKernelEntryPoint:
7851 break;
7852 case ParsedAttr::AT_SYCLSpecialClass:
7854 break;
7855 case ParsedAttr::AT_Format:
7856 handleFormatAttr(S, D, AL);
7857 break;
7858 case ParsedAttr::AT_FormatMatches:
7859 handleFormatMatchesAttr(S, D, AL);
7860 break;
7861 case ParsedAttr::AT_FormatArg:
7862 handleFormatArgAttr(S, D, AL);
7863 break;
7864 case ParsedAttr::AT_Callback:
7865 handleCallbackAttr(S, D, AL);
7866 break;
7867 case ParsedAttr::AT_LifetimeCaptureBy:
7869 break;
7870 case ParsedAttr::AT_CalledOnce:
7871 handleCalledOnceAttr(S, D, AL);
7872 break;
7873 case ParsedAttr::AT_CUDAGlobal:
7874 handleGlobalAttr(S, D, AL);
7875 break;
7876 case ParsedAttr::AT_CUDADevice:
7877 handleDeviceAttr(S, D, AL);
7878 break;
7879 case ParsedAttr::AT_CUDAGridConstant:
7880 handleGridConstantAttr(S, D, AL);
7881 break;
7882 case ParsedAttr::AT_HIPManaged:
7883 handleManagedAttr(S, D, AL);
7884 break;
7885 case ParsedAttr::AT_GNUInline:
7886 handleGNUInlineAttr(S, D, AL);
7887 break;
7888 case ParsedAttr::AT_CUDALaunchBounds:
7889 handleLaunchBoundsAttr(S, D, AL);
7890 break;
7891 case ParsedAttr::AT_CUDAClusterDims:
7892 handleClusterDimsAttr(S, D, AL);
7893 break;
7894 case ParsedAttr::AT_CUDANoCluster:
7895 handleNoClusterAttr(S, D, AL);
7896 break;
7897 case ParsedAttr::AT_Restrict:
7898 handleRestrictAttr(S, D, AL);
7899 break;
7900 case ParsedAttr::AT_MallocSpan:
7901 handleMallocSpanAttr(S, D, AL);
7902 break;
7903 case ParsedAttr::AT_Mode:
7904 handleModeAttr(S, D, AL);
7905 break;
7906 case ParsedAttr::AT_NonString:
7907 handleNonStringAttr(S, D, AL);
7908 break;
7909 case ParsedAttr::AT_NonNull:
7910 if (auto *PVD = dyn_cast<ParmVarDecl>(D))
7911 handleNonNullAttrParameter(S, PVD, AL);
7912 else
7913 handleNonNullAttr(S, D, AL);
7914 break;
7915 case ParsedAttr::AT_ReturnsNonNull:
7916 handleReturnsNonNullAttr(S, D, AL);
7917 break;
7918 case ParsedAttr::AT_NoEscape:
7919 handleNoEscapeAttr(S, D, AL);
7920 break;
7921 case ParsedAttr::AT_MaybeUndef:
7923 break;
7924 case ParsedAttr::AT_AssumeAligned:
7925 handleAssumeAlignedAttr(S, D, AL);
7926 break;
7927 case ParsedAttr::AT_AllocAlign:
7928 handleAllocAlignAttr(S, D, AL);
7929 break;
7930 case ParsedAttr::AT_Ownership:
7931 handleOwnershipAttr(S, D, AL);
7932 break;
7933 case ParsedAttr::AT_Naked:
7934 handleNakedAttr(S, D, AL);
7935 break;
7936 case ParsedAttr::AT_NoReturn:
7937 handleNoReturnAttr(S, D, AL);
7938 break;
7939 case ParsedAttr::AT_CXX11NoReturn:
7941 break;
7942 case ParsedAttr::AT_AnyX86NoCfCheck:
7943 handleNoCfCheckAttr(S, D, AL);
7944 break;
7945 case ParsedAttr::AT_NoThrow:
7946 if (!AL.isUsedAsTypeAttr())
7948 break;
7949 case ParsedAttr::AT_CUDAShared:
7950 handleSharedAttr(S, D, AL);
7951 break;
7952 case ParsedAttr::AT_VecReturn:
7953 handleVecReturnAttr(S, D, AL);
7954 break;
7955 case ParsedAttr::AT_ObjCOwnership:
7956 S.ObjC().handleOwnershipAttr(D, AL);
7957 break;
7958 case ParsedAttr::AT_ObjCPreciseLifetime:
7960 break;
7961 case ParsedAttr::AT_ObjCReturnsInnerPointer:
7963 break;
7964 case ParsedAttr::AT_ObjCRequiresSuper:
7965 S.ObjC().handleRequiresSuperAttr(D, AL);
7966 break;
7967 case ParsedAttr::AT_ObjCBridge:
7968 S.ObjC().handleBridgeAttr(D, AL);
7969 break;
7970 case ParsedAttr::AT_ObjCBridgeMutable:
7971 S.ObjC().handleBridgeMutableAttr(D, AL);
7972 break;
7973 case ParsedAttr::AT_ObjCBridgeRelated:
7974 S.ObjC().handleBridgeRelatedAttr(D, AL);
7975 break;
7976 case ParsedAttr::AT_ObjCDesignatedInitializer:
7978 break;
7979 case ParsedAttr::AT_ObjCRuntimeName:
7980 S.ObjC().handleRuntimeName(D, AL);
7981 break;
7982 case ParsedAttr::AT_ObjCBoxable:
7983 S.ObjC().handleBoxable(D, AL);
7984 break;
7985 case ParsedAttr::AT_NSErrorDomain:
7986 S.ObjC().handleNSErrorDomain(D, AL);
7987 break;
7988 case ParsedAttr::AT_CFConsumed:
7989 case ParsedAttr::AT_NSConsumed:
7990 case ParsedAttr::AT_OSConsumed:
7991 S.ObjC().AddXConsumedAttr(D, AL,
7993 /*IsTemplateInstantiation=*/false);
7994 break;
7995 case ParsedAttr::AT_OSReturnsRetainedOnZero:
7997 S, D, AL, S.ObjC().isValidOSObjectOutParameter(D),
7998 diag::warn_ns_attribute_wrong_parameter_type,
7999 /*Extra Args=*/AL, /*pointer-to-OSObject-pointer*/ 3, AL.getRange());
8000 break;
8001 case ParsedAttr::AT_OSReturnsRetainedOnNonZero:
8003 S, D, AL, S.ObjC().isValidOSObjectOutParameter(D),
8004 diag::warn_ns_attribute_wrong_parameter_type,
8005 /*Extra Args=*/AL, /*pointer-to-OSObject-poointer*/ 3, AL.getRange());
8006 break;
8007 case ParsedAttr::AT_NSReturnsAutoreleased:
8008 case ParsedAttr::AT_NSReturnsNotRetained:
8009 case ParsedAttr::AT_NSReturnsRetained:
8010 case ParsedAttr::AT_CFReturnsNotRetained:
8011 case ParsedAttr::AT_CFReturnsRetained:
8012 case ParsedAttr::AT_OSReturnsNotRetained:
8013 case ParsedAttr::AT_OSReturnsRetained:
8015 break;
8016 case ParsedAttr::AT_WorkGroupSizeHint:
8018 break;
8019 case ParsedAttr::AT_ReqdWorkGroupSize:
8021 break;
8022 case ParsedAttr::AT_OpenCLIntelReqdSubGroupSize:
8023 S.OpenCL().handleSubGroupSize(D, AL);
8024 break;
8025 case ParsedAttr::AT_VecTypeHint:
8026 handleVecTypeHint(S, D, AL);
8027 break;
8028 case ParsedAttr::AT_InitPriority:
8029 handleInitPriorityAttr(S, D, AL);
8030 break;
8031 case ParsedAttr::AT_Packed:
8032 handlePackedAttr(S, D, AL);
8033 break;
8034 case ParsedAttr::AT_PreferredName:
8035 handlePreferredName(S, D, AL);
8036 break;
8037 case ParsedAttr::AT_NoSpecializations:
8038 handleNoSpecializations(S, D, AL);
8039 break;
8040 case ParsedAttr::AT_Section:
8041 handleSectionAttr(S, D, AL);
8042 break;
8043 case ParsedAttr::AT_CodeModel:
8044 handleCodeModelAttr(S, D, AL);
8045 break;
8046 case ParsedAttr::AT_RandomizeLayout:
8047 handleRandomizeLayoutAttr(S, D, AL);
8048 break;
8049 case ParsedAttr::AT_NoRandomizeLayout:
8051 break;
8052 case ParsedAttr::AT_CodeSeg:
8053 handleCodeSegAttr(S, D, AL);
8054 break;
8055 case ParsedAttr::AT_Target:
8056 handleTargetAttr(S, D, AL);
8057 break;
8058 case ParsedAttr::AT_TargetVersion:
8059 handleTargetVersionAttr(S, D, AL);
8060 break;
8061 case ParsedAttr::AT_TargetClones:
8062 handleTargetClonesAttr(S, D, AL);
8063 break;
8064 case ParsedAttr::AT_MinVectorWidth:
8065 handleMinVectorWidthAttr(S, D, AL);
8066 break;
8067 case ParsedAttr::AT_Unavailable:
8069 break;
8070 case ParsedAttr::AT_OMPAssume:
8071 S.OpenMP().handleOMPAssumeAttr(D, AL);
8072 break;
8073 case ParsedAttr::AT_ObjCDirect:
8074 S.ObjC().handleDirectAttr(D, AL);
8075 break;
8076 case ParsedAttr::AT_ObjCDirectMembers:
8077 S.ObjC().handleDirectMembersAttr(D, AL);
8079 break;
8080 case ParsedAttr::AT_ObjCExplicitProtocolImpl:
8082 break;
8083 case ParsedAttr::AT_Unused:
8084 handleUnusedAttr(S, D, AL);
8085 break;
8086 case ParsedAttr::AT_Visibility:
8087 handleVisibilityAttr(S, D, AL, false);
8088 break;
8089 case ParsedAttr::AT_TypeVisibility:
8090 handleVisibilityAttr(S, D, AL, true);
8091 break;
8092 case ParsedAttr::AT_WarnUnusedResult:
8093 handleWarnUnusedResult(S, D, AL);
8094 break;
8095 case ParsedAttr::AT_WeakRef:
8096 handleWeakRefAttr(S, D, AL);
8097 break;
8098 case ParsedAttr::AT_WeakImport:
8099 handleWeakImportAttr(S, D, AL);
8100 break;
8101 case ParsedAttr::AT_TransparentUnion:
8103 break;
8104 case ParsedAttr::AT_ObjCMethodFamily:
8105 S.ObjC().handleMethodFamilyAttr(D, AL);
8106 break;
8107 case ParsedAttr::AT_ObjCNSObject:
8108 S.ObjC().handleNSObject(D, AL);
8109 break;
8110 case ParsedAttr::AT_ObjCIndependentClass:
8111 S.ObjC().handleIndependentClass(D, AL);
8112 break;
8113 case ParsedAttr::AT_Blocks:
8114 S.ObjC().handleBlocksAttr(D, AL);
8115 break;
8116 case ParsedAttr::AT_Sentinel:
8117 handleSentinelAttr(S, D, AL);
8118 break;
8119 case ParsedAttr::AT_Cleanup:
8120 handleCleanupAttr(S, D, AL);
8121 break;
8122 case ParsedAttr::AT_NoDebug:
8123 handleNoDebugAttr(S, D, AL);
8124 break;
8125 case ParsedAttr::AT_CmseNSEntry:
8126 S.ARM().handleCmseNSEntryAttr(D, AL);
8127 break;
8128 case ParsedAttr::AT_StdCall:
8129 case ParsedAttr::AT_CDecl:
8130 case ParsedAttr::AT_FastCall:
8131 case ParsedAttr::AT_ThisCall:
8132 case ParsedAttr::AT_Pascal:
8133 case ParsedAttr::AT_RegCall:
8134 case ParsedAttr::AT_SwiftCall:
8135 case ParsedAttr::AT_SwiftAsyncCall:
8136 case ParsedAttr::AT_VectorCall:
8137 case ParsedAttr::AT_MSABI:
8138 case ParsedAttr::AT_SysVABI:
8139 case ParsedAttr::AT_Pcs:
8140 case ParsedAttr::AT_IntelOclBicc:
8141 case ParsedAttr::AT_PreserveMost:
8142 case ParsedAttr::AT_PreserveAll:
8143 case ParsedAttr::AT_AArch64VectorPcs:
8144 case ParsedAttr::AT_AArch64SVEPcs:
8145 case ParsedAttr::AT_M68kRTD:
8146 case ParsedAttr::AT_PreserveNone:
8147 case ParsedAttr::AT_RISCVVectorCC:
8148 case ParsedAttr::AT_RISCVVLSCC:
8149 handleCallConvAttr(S, D, AL);
8150 break;
8151 case ParsedAttr::AT_DeviceKernel:
8152 handleDeviceKernelAttr(S, D, AL);
8153 break;
8154 case ParsedAttr::AT_Suppress:
8155 handleSuppressAttr(S, D, AL);
8156 break;
8157 case ParsedAttr::AT_Owner:
8158 case ParsedAttr::AT_Pointer:
8160 break;
8161 case ParsedAttr::AT_OpenCLAccess:
8162 S.OpenCL().handleAccessAttr(D, AL);
8163 break;
8164 case ParsedAttr::AT_OpenCLNoSVM:
8165 S.OpenCL().handleNoSVMAttr(D, AL);
8166 break;
8167 case ParsedAttr::AT_SwiftContext:
8169 break;
8170 case ParsedAttr::AT_SwiftAsyncContext:
8172 break;
8173 case ParsedAttr::AT_SwiftErrorResult:
8175 break;
8176 case ParsedAttr::AT_SwiftIndirectResult:
8178 break;
8179 case ParsedAttr::AT_InternalLinkage:
8180 handleInternalLinkageAttr(S, D, AL);
8181 break;
8182 case ParsedAttr::AT_ZeroCallUsedRegs:
8184 break;
8185 case ParsedAttr::AT_FunctionReturnThunks:
8187 break;
8188 case ParsedAttr::AT_NoMerge:
8189 handleNoMergeAttr(S, D, AL);
8190 break;
8191 case ParsedAttr::AT_NoUniqueAddress:
8192 handleNoUniqueAddressAttr(S, D, AL);
8193 break;
8194
8195 case ParsedAttr::AT_AvailableOnlyInDefaultEvalMethod:
8197 break;
8198
8199 case ParsedAttr::AT_CountedBy:
8200 case ParsedAttr::AT_CountedByOrNull:
8201 case ParsedAttr::AT_SizedBy:
8202 case ParsedAttr::AT_SizedByOrNull:
8203 handleCountedByAttrField(S, D, AL);
8204 break;
8205
8206 case ParsedAttr::AT_NoFieldProtection:
8207 handleNoPFPAttrField(S, D, AL);
8208 break;
8209
8210 case ParsedAttr::AT_Personality:
8211 handlePersonalityAttr(S, D, AL);
8212 break;
8213
8214 // Microsoft attributes:
8215 case ParsedAttr::AT_LayoutVersion:
8216 handleLayoutVersion(S, D, AL);
8217 break;
8218 case ParsedAttr::AT_Uuid:
8219 handleUuidAttr(S, D, AL);
8220 break;
8221 case ParsedAttr::AT_MSInheritance:
8222 handleMSInheritanceAttr(S, D, AL);
8223 break;
8224 case ParsedAttr::AT_Thread:
8225 handleDeclspecThreadAttr(S, D, AL);
8226 break;
8227 case ParsedAttr::AT_MSConstexpr:
8228 handleMSConstexprAttr(S, D, AL);
8229 break;
8230 case ParsedAttr::AT_HybridPatchable:
8232 break;
8233
8234 // HLSL attributes:
8235 case ParsedAttr::AT_RootSignature:
8236 S.HLSL().handleRootSignatureAttr(D, AL);
8237 break;
8238 case ParsedAttr::AT_HLSLNumThreads:
8239 S.HLSL().handleNumThreadsAttr(D, AL);
8240 break;
8241 case ParsedAttr::AT_HLSLWaveSize:
8242 S.HLSL().handleWaveSizeAttr(D, AL);
8243 break;
8244 case ParsedAttr::AT_HLSLVkExtBuiltinInput:
8246 break;
8247 case ParsedAttr::AT_HLSLVkExtBuiltinOutput:
8249 break;
8250 case ParsedAttr::AT_HLSLVkPushConstant:
8251 S.HLSL().handleVkPushConstantAttr(D, AL);
8252 break;
8253 case ParsedAttr::AT_HLSLVkConstantId:
8254 S.HLSL().handleVkConstantIdAttr(D, AL);
8255 break;
8256 case ParsedAttr::AT_HLSLVkBinding:
8257 S.HLSL().handleVkBindingAttr(D, AL);
8258 break;
8259 case ParsedAttr::AT_HLSLGroupSharedAddressSpace:
8261 break;
8262 case ParsedAttr::AT_HLSLPackOffset:
8263 S.HLSL().handlePackOffsetAttr(D, AL);
8264 break;
8265 case ParsedAttr::AT_HLSLShader:
8266 S.HLSL().handleShaderAttr(D, AL);
8267 break;
8268 case ParsedAttr::AT_HLSLResourceBinding:
8270 break;
8271 case ParsedAttr::AT_HLSLParamModifier:
8272 S.HLSL().handleParamModifierAttr(D, AL);
8273 break;
8274 case ParsedAttr::AT_HLSLUnparsedSemantic:
8275 S.HLSL().handleSemanticAttr(D, AL);
8276 break;
8277 case ParsedAttr::AT_HLSLVkLocation:
8278 S.HLSL().handleVkLocationAttr(D, AL);
8279 break;
8280
8281 case ParsedAttr::AT_AbiTag:
8282 handleAbiTagAttr(S, D, AL);
8283 break;
8284 case ParsedAttr::AT_CFGuard:
8285 handleCFGuardAttr(S, D, AL);
8286 break;
8287
8288 // Thread safety attributes:
8289 case ParsedAttr::AT_PtGuardedVar:
8290 handlePtGuardedVarAttr(S, D, AL);
8291 break;
8292 case ParsedAttr::AT_NoSanitize:
8293 handleNoSanitizeAttr(S, D, AL);
8294 break;
8295 case ParsedAttr::AT_NoSanitizeAddress:
8297 break;
8298 case ParsedAttr::AT_NoSanitizeThread:
8300 break;
8301 case ParsedAttr::AT_NoSanitizeMemory:
8303 break;
8304 case ParsedAttr::AT_GuardedBy:
8305 handleGuardedByAttr(S, D, AL);
8306 break;
8307 case ParsedAttr::AT_PtGuardedBy:
8308 handlePtGuardedByAttr(S, D, AL);
8309 break;
8310 case ParsedAttr::AT_LockReturned:
8311 handleLockReturnedAttr(S, D, AL);
8312 break;
8313 case ParsedAttr::AT_LocksExcluded:
8314 handleLocksExcludedAttr(S, D, AL);
8315 break;
8316 case ParsedAttr::AT_AcquiredBefore:
8317 handleAcquiredBeforeAttr(S, D, AL);
8318 break;
8319 case ParsedAttr::AT_AcquiredAfter:
8320 handleAcquiredAfterAttr(S, D, AL);
8321 break;
8322
8323 // Capability analysis attributes.
8324 case ParsedAttr::AT_Capability:
8325 case ParsedAttr::AT_Lockable:
8326 handleCapabilityAttr(S, D, AL);
8327 break;
8328 case ParsedAttr::AT_ReentrantCapability:
8330 break;
8331 case ParsedAttr::AT_RequiresCapability:
8333 break;
8334
8335 case ParsedAttr::AT_AssertCapability:
8337 break;
8338 case ParsedAttr::AT_AcquireCapability:
8340 break;
8341 case ParsedAttr::AT_ReleaseCapability:
8343 break;
8344 case ParsedAttr::AT_TryAcquireCapability:
8346 break;
8347
8348 // Consumed analysis attributes.
8349 case ParsedAttr::AT_Consumable:
8350 handleConsumableAttr(S, D, AL);
8351 break;
8352 case ParsedAttr::AT_CallableWhen:
8353 handleCallableWhenAttr(S, D, AL);
8354 break;
8355 case ParsedAttr::AT_ParamTypestate:
8356 handleParamTypestateAttr(S, D, AL);
8357 break;
8358 case ParsedAttr::AT_ReturnTypestate:
8359 handleReturnTypestateAttr(S, D, AL);
8360 break;
8361 case ParsedAttr::AT_SetTypestate:
8362 handleSetTypestateAttr(S, D, AL);
8363 break;
8364 case ParsedAttr::AT_TestTypestate:
8365 handleTestTypestateAttr(S, D, AL);
8366 break;
8367
8368 // Type safety attributes.
8369 case ParsedAttr::AT_ArgumentWithTypeTag:
8371 break;
8372 case ParsedAttr::AT_TypeTagForDatatype:
8374 break;
8375
8376 // Swift attributes.
8377 case ParsedAttr::AT_SwiftAsyncName:
8378 S.Swift().handleAsyncName(D, AL);
8379 break;
8380 case ParsedAttr::AT_SwiftAttr:
8381 S.Swift().handleAttrAttr(D, AL);
8382 break;
8383 case ParsedAttr::AT_SwiftBridge:
8384 S.Swift().handleBridge(D, AL);
8385 break;
8386 case ParsedAttr::AT_SwiftError:
8387 S.Swift().handleError(D, AL);
8388 break;
8389 case ParsedAttr::AT_SwiftName:
8390 S.Swift().handleName(D, AL);
8391 break;
8392 case ParsedAttr::AT_SwiftNewType:
8393 S.Swift().handleNewType(D, AL);
8394 break;
8395 case ParsedAttr::AT_SwiftAsync:
8396 S.Swift().handleAsyncAttr(D, AL);
8397 break;
8398 case ParsedAttr::AT_SwiftAsyncError:
8399 S.Swift().handleAsyncError(D, AL);
8400 break;
8401
8402 // XRay attributes.
8403 case ParsedAttr::AT_XRayLogArgs:
8404 handleXRayLogArgsAttr(S, D, AL);
8405 break;
8406
8407 case ParsedAttr::AT_PatchableFunctionEntry:
8409 break;
8410
8411 case ParsedAttr::AT_AlwaysDestroy:
8412 case ParsedAttr::AT_NoDestroy:
8413 handleDestroyAttr(S, D, AL);
8414 break;
8415
8416 case ParsedAttr::AT_Uninitialized:
8417 handleUninitializedAttr(S, D, AL);
8418 break;
8419
8420 case ParsedAttr::AT_ObjCExternallyRetained:
8422 break;
8423
8424 case ParsedAttr::AT_MIGServerRoutine:
8426 break;
8427
8428 case ParsedAttr::AT_MSAllocator:
8429 handleMSAllocatorAttr(S, D, AL);
8430 break;
8431
8432 case ParsedAttr::AT_ArmBuiltinAlias:
8433 S.ARM().handleBuiltinAliasAttr(D, AL);
8434 break;
8435
8436 case ParsedAttr::AT_ArmLocallyStreaming:
8438 break;
8439
8440 case ParsedAttr::AT_ArmNew:
8441 S.ARM().handleNewAttr(D, AL);
8442 break;
8443
8444 case ParsedAttr::AT_AcquireHandle:
8445 handleAcquireHandleAttr(S, D, AL);
8446 break;
8447
8448 case ParsedAttr::AT_ReleaseHandle:
8450 break;
8451
8452 case ParsedAttr::AT_UnsafeBufferUsage:
8454 break;
8455
8456 case ParsedAttr::AT_UseHandle:
8458 break;
8459
8460 case ParsedAttr::AT_EnforceTCB:
8462 break;
8463
8464 case ParsedAttr::AT_EnforceTCBLeaf:
8466 break;
8467
8468 case ParsedAttr::AT_BuiltinAlias:
8469 handleBuiltinAliasAttr(S, D, AL);
8470 break;
8471
8472 case ParsedAttr::AT_PreferredType:
8473 handlePreferredTypeAttr(S, D, AL);
8474 break;
8475
8476 case ParsedAttr::AT_UsingIfExists:
8478 break;
8479
8480 case ParsedAttr::AT_TypeNullable:
8481 handleNullableTypeAttr(S, D, AL);
8482 break;
8483
8484 case ParsedAttr::AT_VTablePointerAuthentication:
8486 break;
8487
8488 case ParsedAttr::AT_ModularFormat:
8489 handleModularFormat(S, D, AL);
8490 break;
8491
8492 case ParsedAttr::AT_MSStruct:
8493 handleMSStructAttr(S, D, AL);
8494 break;
8495
8496 case ParsedAttr::AT_GCCStruct:
8497 handleGCCStructAttr(S, D, AL);
8498 break;
8499
8500 case ParsedAttr::AT_PointerFieldProtection:
8501 if (!S.getLangOpts().PointerFieldProtectionAttr)
8502 S.Diag(AL.getLoc(),
8503 diag::err_attribute_pointer_field_protection_experimental)
8504 << AL << AL.isRegularKeywordAttribute() << D->getLocation();
8506 break;
8507 }
8508}
8509
8510static bool isKernelDecl(Decl *D) {
8511 const FunctionType *FnTy = D->getFunctionType();
8512 return D->hasAttr<DeviceKernelAttr>() ||
8513 (FnTy && FnTy->getCallConv() == CallingConv::CC_DeviceKernel) ||
8514 D->hasAttr<CUDAGlobalAttr>();
8515}
8516
8518 if (!S.Context.getTargetInfo().getTriple().isAMDGPU())
8519 return;
8520
8521 const auto *Flat = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
8522 const auto *Reqd = D->getAttr<ReqdWorkGroupSizeAttr>();
8523 if (!Flat || !Reqd)
8524 return;
8525
8526 auto Eval = [&](Expr *E) -> std::optional<uint64_t> {
8527 if (E->isValueDependent())
8528 return std::nullopt;
8529 std::optional<llvm::APSInt> V = E->getIntegerConstantExpr(S.Context);
8530 if (!V)
8531 return std::nullopt;
8532 return V->getZExtValue();
8533 };
8534
8535 std::optional<uint64_t> X = Eval(Reqd->getXDim());
8536 std::optional<uint64_t> Y = Eval(Reqd->getYDim());
8537 std::optional<uint64_t> Z = Eval(Reqd->getZDim());
8538 std::optional<uint64_t> Min = Eval(Flat->getMin());
8539 std::optional<uint64_t> Max = Eval(Flat->getMax());
8540 if (!X || !Y || !Z || !Min || !Max)
8541 return;
8542
8543 uint64_t Product = *X * *Y * *Z;
8544 if (*Min != Product || *Max != Product) {
8545 S.Diag(Flat->getLocation(),
8546 diag::err_attribute_amdgpu_flat_work_group_size_mismatch);
8547 D->setInvalidDecl();
8548 }
8549}
8550
8552 Scope *S, Decl *D, const ParsedAttributesView &AttrList,
8553 const ProcessDeclAttributeOptions &Options) {
8554 if (AttrList.empty())
8555 return;
8556
8557 for (const ParsedAttr &AL : AttrList)
8558 ProcessDeclAttribute(*this, S, D, AL, Options);
8559
8560 // FIXME: We should be able to handle these cases in TableGen.
8561 // GCC accepts
8562 // static int a9 __attribute__((weakref));
8563 // but that looks really pointless. We reject it.
8564 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
8565 Diag(AttrList.begin()->getLoc(), diag::err_attribute_weakref_without_alias)
8566 << cast<NamedDecl>(D);
8567 D->dropAttr<WeakRefAttr>();
8568 return;
8569 }
8570
8571 // FIXME: We should be able to handle this in TableGen as well. It would be
8572 // good to have a way to specify "these attributes must appear as a group",
8573 // for these. Additionally, it would be good to have a way to specify "these
8574 // attribute must never appear as a group" for attributes like cold and hot.
8575 if (!(D->hasAttr<DeviceKernelAttr>() ||
8576 (D->hasAttr<CUDAGlobalAttr>() &&
8577 Context.getTargetInfo().getTriple().isSPIRV()))) {
8578 // These attributes cannot be applied to a non-kernel function.
8579 if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
8580 // FIXME: This emits a different error message than
8581 // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction.
8582 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
8583 D->setInvalidDecl();
8584 } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
8585 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
8586 D->setInvalidDecl();
8587 } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
8588 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
8589 D->setInvalidDecl();
8590 } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
8591 Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
8592 D->setInvalidDecl();
8593 }
8594 }
8595 if (!isKernelDecl(D)) {
8596 if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
8597 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
8598 << A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
8599 D->setInvalidDecl();
8600 } else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
8601 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
8602 << A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
8603 D->setInvalidDecl();
8604 } else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
8605 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
8606 << A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
8607 D->setInvalidDecl();
8608 } else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
8609 Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
8610 << A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
8611 D->setInvalidDecl();
8612 }
8613 }
8615
8616 // CUDA/HIP: restrict explicit CUDA target attributes on deduction guides.
8617 //
8618 // Deduction guides are not callable functions and never participate in
8619 // codegen; they are always treated as host+device for CUDA/HIP semantic
8620 // checks. We therefore allow either no CUDA target attributes or an explicit
8621 // '__host__ __device__' annotation, but reject guides that are host-only,
8622 // device-only, or marked '__global__'. The use of explicit CUDA/HIP target
8623 // attributes on deduction guides is deprecated and will be rejected in a
8624 // future Clang version.
8625 if (getLangOpts().CUDA)
8626 if (auto *Guide = dyn_cast<CXXDeductionGuideDecl>(D)) {
8627 bool HasHost = Guide->hasAttr<CUDAHostAttr>();
8628 bool HasDevice = Guide->hasAttr<CUDADeviceAttr>();
8629 bool HasGlobal = Guide->hasAttr<CUDAGlobalAttr>();
8630
8631 if (HasGlobal || HasHost != HasDevice) {
8632 Diag(Guide->getLocation(), diag::err_deduction_guide_target_attr);
8633 Guide->setInvalidDecl();
8634 } else if (HasHost && HasDevice) {
8635 Diag(Guide->getLocation(),
8636 diag::warn_deduction_guide_target_attr_deprecated);
8637 }
8638 }
8639
8640 // Do not permit 'constructor' or 'destructor' attributes on __device__ code.
8641 if (getLangOpts().CUDAIsDevice && D->hasAttr<CUDADeviceAttr>() &&
8642 (D->hasAttr<ConstructorAttr>() || D->hasAttr<DestructorAttr>()) &&
8643 !getLangOpts().GPUAllowDeviceInit) {
8644 Diag(D->getLocation(), diag::err_cuda_ctor_dtor_attrs)
8645 << (D->hasAttr<ConstructorAttr>() ? "constructors" : "destructors");
8646 D->setInvalidDecl();
8647 }
8648
8649 // Do this check after processing D's attributes because the attribute
8650 // objc_method_family can change whether the given method is in the init
8651 // family, and it can be applied after objc_designated_initializer. This is a
8652 // bit of a hack, but we need it to be compatible with versions of clang that
8653 // processed the attribute list in the wrong order.
8654 if (D->hasAttr<ObjCDesignatedInitializerAttr>() &&
8655 cast<ObjCMethodDecl>(D)->getMethodFamily() != OMF_init) {
8656 Diag(D->getLocation(), diag::err_designated_init_attr_non_init);
8657 D->dropAttr<ObjCDesignatedInitializerAttr>();
8658 }
8659}
8660
8662 const ParsedAttributesView &AttrList) {
8663 for (const ParsedAttr &AL : AttrList)
8664 if (AL.getKind() == ParsedAttr::AT_TransparentUnion) {
8665 handleTransparentUnionAttr(*this, D, AL);
8666 break;
8667 }
8668
8669 // For BPFPreserveAccessIndexAttr, we want to populate the attributes
8670 // to fields and inner records as well.
8671 if (D && D->hasAttr<BPFPreserveAccessIndexAttr>())
8673}
8674
8676 AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList) {
8677 for (const ParsedAttr &AL : AttrList) {
8678 if (AL.getKind() == ParsedAttr::AT_Annotate) {
8679 ProcessDeclAttribute(*this, nullptr, ASDecl, AL,
8681 } else {
8682 Diag(AL.getLoc(), diag::err_only_annotate_after_access_spec);
8683 return true;
8684 }
8685 }
8686 return false;
8687}
8688
8689/// checkUnusedDeclAttributes - Check a list of attributes to see if it
8690/// contains any decl attributes that we should warn about.
8692 for (const ParsedAttr &AL : A) {
8693 // Only warn if the attribute is an unignored, non-type attribute.
8694 if (AL.isUsedAsTypeAttr() || AL.isInvalid())
8695 continue;
8696 if (AL.getKind() == ParsedAttr::IgnoredAttribute)
8697 continue;
8698
8699 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
8701 } else {
8702 S.Diag(AL.getLoc(), diag::warn_attribute_not_on_decl) << AL
8703 << AL.getRange();
8704 }
8705 }
8706}
8707
8715
8718 StringRef ScopeName = AL.getNormalizedScopeName();
8719 std::optional<StringRef> CorrectedScopeName =
8720 AL.tryGetCorrectedScopeName(ScopeName);
8721 if (CorrectedScopeName) {
8722 ScopeName = *CorrectedScopeName;
8723 }
8724
8725 StringRef AttrName = AL.getNormalizedAttrName(ScopeName);
8726 std::optional<StringRef> CorrectedAttrName = AL.tryGetCorrectedAttrName(
8727 ScopeName, AttrName, Context.getTargetInfo(), getLangOpts());
8728 if (CorrectedAttrName) {
8729 AttrName = *CorrectedAttrName;
8730 }
8731
8732 if (CorrectedScopeName || CorrectedAttrName) {
8733 std::string CorrectedFullName =
8734 AL.getNormalizedFullName(ScopeName, AttrName);
8736 Diag(CorrectedScopeName ? NR.getBegin() : AL.getRange().getBegin(),
8737 diag::warn_unknown_attribute_ignored_suggestion);
8738
8739 D << AL << CorrectedFullName;
8740
8741 if (AL.isExplicitScope()) {
8742 D << FixItHint::CreateReplacement(NR, CorrectedFullName) << NR;
8743 } else {
8744 if (CorrectedScopeName) {
8746 ScopeName);
8747 }
8748 if (CorrectedAttrName) {
8749 D << FixItHint::CreateReplacement(AL.getRange(), AttrName);
8750 }
8751 }
8752 } else {
8753 Diag(NR.getBegin(), diag::warn_unknown_attribute_ignored) << AL << NR;
8754 }
8755}
8756
8758 SourceLocation Loc) {
8759 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
8760 NamedDecl *NewD = nullptr;
8761 if (auto *FD = dyn_cast<FunctionDecl>(ND)) {
8762 FunctionDecl *NewFD;
8763 // FIXME: Missing call to CheckFunctionDeclaration().
8764 // FIXME: Mangling?
8765 // FIXME: Is the qualifier info correct?
8766 // FIXME: Is the DeclContext correct?
8767 NewFD = FunctionDecl::Create(
8768 FD->getASTContext(), FD->getDeclContext(), Loc, Loc,
8770 getCurFPFeatures().isFPConstrained(), false /*isInlineSpecified*/,
8773 NewD = NewFD;
8774
8775 if (FD->getQualifier())
8776 NewFD->setQualifierInfo(FD->getQualifierLoc());
8777
8778 // Fake up parameter variables; they are declared as if this were
8779 // a typedef.
8780 QualType FDTy = FD->getType();
8781 if (const auto *FT = FDTy->getAs<FunctionProtoType>()) {
8783 for (const auto &AI : FT->param_types()) {
8784 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI);
8785 Param->setScopeInfo(0, Params.size());
8786 Params.push_back(Param);
8787 }
8788 NewFD->setParams(Params);
8789 }
8790 } else if (auto *VD = dyn_cast<VarDecl>(ND)) {
8791 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
8792 VD->getInnerLocStart(), VD->getLocation(), II,
8793 VD->getType(), VD->getTypeSourceInfo(),
8794 VD->getStorageClass());
8795 if (VD->getQualifier())
8796 cast<VarDecl>(NewD)->setQualifierInfo(VD->getQualifierLoc());
8797 }
8798 return NewD;
8799}
8800
8802 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
8803 IdentifierInfo *NDId = ND->getIdentifier();
8804 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
8805 NewD->addAttr(
8806 AliasAttr::CreateImplicit(Context, NDId->getName(), W.getLocation()));
8807 NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
8808 WeakTopLevelDecl.push_back(NewD);
8809 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
8810 // to insert Decl at TU scope, sorry.
8811 DeclContext *SavedContext = CurContext;
8812 CurContext = Context.getTranslationUnitDecl();
8815 PushOnScopeChains(NewD, S);
8816 CurContext = SavedContext;
8817 } else { // just add weak to existing
8818 ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation()));
8819 }
8820}
8821
8823 // It's valid to "forward-declare" #pragma weak, in which case we
8824 // have to do this.
8826 if (WeakUndeclaredIdentifiers.empty())
8827 return;
8828 NamedDecl *ND = nullptr;
8829 if (auto *VD = dyn_cast<VarDecl>(D))
8830 if (VD->isExternC())
8831 ND = VD;
8832 if (auto *FD = dyn_cast<FunctionDecl>(D))
8833 if (FD->isExternC())
8834 ND = FD;
8835 if (!ND)
8836 return;
8837 if (IdentifierInfo *Id = ND->getIdentifier()) {
8838 auto I = WeakUndeclaredIdentifiers.find(Id);
8839 if (I != WeakUndeclaredIdentifiers.end()) {
8840 auto &WeakInfos = I->second;
8841 for (const auto &W : WeakInfos)
8842 DeclApplyPragmaWeak(S, ND, W);
8843 std::remove_reference_t<decltype(WeakInfos)> EmptyWeakInfos;
8844 WeakInfos.swap(EmptyWeakInfos);
8845 }
8846 }
8847}
8848
8849/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
8850/// it, apply them to D. This is a bit tricky because PD can have attributes
8851/// specified in many different places, and we need to find and apply them all.
8853 // Ordering of attributes can be important, so we take care to process
8854 // attributes in the order in which they appeared in the source code.
8855
8856 auto ProcessAttributesWithSliding =
8857 [&](const ParsedAttributesView &Src,
8858 const ProcessDeclAttributeOptions &Options) {
8859 ParsedAttributesView NonSlidingAttrs;
8860 for (ParsedAttr &AL : Src) {
8861 // FIXME: this sliding is specific to standard attributes and should
8862 // eventually be deprecated and removed as those are not intended to
8863 // slide to anything.
8864 if ((AL.isStandardAttributeSyntax() || AL.isAlignas()) &&
8865 AL.slidesFromDeclToDeclSpecLegacyBehavior()) {
8866 // Skip processing the attribute, but do check if it appertains to
8867 // the declaration. This is needed for the `MatrixType` attribute,
8868 // which, despite being a type attribute, defines a `SubjectList`
8869 // that only allows it to be used on typedef declarations.
8870 AL.diagnoseAppertainsTo(*this, D);
8871 } else {
8872 NonSlidingAttrs.addAtEnd(&AL);
8873 }
8874 }
8875 ProcessDeclAttributeList(S, D, NonSlidingAttrs, Options);
8876 };
8877
8878 // First, process attributes that appeared on the declaration itself (but
8879 // only if they don't have the legacy behavior of "sliding" to the DeclSepc).
8880 ProcessAttributesWithSliding(PD.getDeclarationAttributes(), {});
8881
8882 // Apply decl attributes from the DeclSpec if present.
8883 ProcessAttributesWithSliding(PD.getDeclSpec().getAttributes(),
8885 .WithIncludeCXX11Attributes(false)
8886 .WithIgnoreTypeAttributes(true));
8887
8888 // Walk the declarator structure, applying decl attributes that were in a type
8889 // position to the decl itself. This handles cases like:
8890 // int *__attr__(x)** D;
8891 // when X is a decl attribute.
8892 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) {
8895 .WithIncludeCXX11Attributes(false)
8896 .WithIgnoreTypeAttributes(true));
8897 }
8898
8899 // Finally, apply any attributes on the decl itself.
8901
8902 // Apply additional attributes specified by '#pragma clang attribute'.
8903 AddPragmaAttributes(S, D);
8904
8905 // Look for API notes that map to attributes.
8906 ProcessAPINotes(D);
8907}
8908
8909/// Is the given declaration allowed to use a forbidden type?
8910/// If so, it'll still be annotated with an attribute that makes it
8911/// illegal to actually use.
8913 const DelayedDiagnostic &diag,
8914 UnavailableAttr::ImplicitReason &reason) {
8915 // Private ivars are always okay. Unfortunately, people don't
8916 // always properly make their ivars private, even in system headers.
8917 // Plus we need to make fields okay, too.
8918 if (!isa<FieldDecl>(D) && !isa<ObjCPropertyDecl>(D) &&
8920 return false;
8921
8922 // Silently accept unsupported uses of __weak in both user and system
8923 // declarations when it's been disabled, for ease of integration with
8924 // -fno-objc-arc files. We do have to take some care against attempts
8925 // to define such things; for now, we've only done that for ivars
8926 // and properties.
8928 if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled ||
8929 diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) {
8930 reason = UnavailableAttr::IR_ForbiddenWeak;
8931 return true;
8932 }
8933 }
8934
8935 // Allow all sorts of things in system headers.
8937 // Currently, all the failures dealt with this way are due to ARC
8938 // restrictions.
8939 reason = UnavailableAttr::IR_ARCForbiddenType;
8940 return true;
8941 }
8942
8943 return false;
8944}
8945
8946/// Handle a delayed forbidden-type diagnostic.
8948 Decl *D) {
8949 auto Reason = UnavailableAttr::IR_None;
8950 if (D && isForbiddenTypeAllowed(S, D, DD, Reason)) {
8951 assert(Reason && "didn't set reason?");
8952 D->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", Reason, DD.Loc));
8953 return;
8954 }
8955 if (S.getLangOpts().ObjCAutoRefCount)
8956 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
8957 // FIXME: we may want to suppress diagnostics for all
8958 // kind of forbidden type messages on unavailable functions.
8959 if (FD->hasAttr<UnavailableAttr>() &&
8961 diag::err_arc_array_param_no_ownership) {
8962 DD.Triggered = true;
8963 return;
8964 }
8965 }
8966
8969 DD.Triggered = true;
8970}
8971
8972
8977
8978 // When delaying diagnostics to run in the context of a parsed
8979 // declaration, we only want to actually emit anything if parsing
8980 // succeeds.
8981 if (!decl) return;
8982
8983 // We emit all the active diagnostics in this pool or any of its
8984 // parents. In general, we'll get one pool for the decl spec
8985 // and a child pool for each declarator; in a decl group like:
8986 // deprecated_typedef foo, *bar, baz();
8987 // only the declarator pops will be passed decls. This is correct;
8988 // we really do need to consider delayed diagnostics from the decl spec
8989 // for each of the different declarations.
8990 const DelayedDiagnosticPool *pool = &poppedPool;
8991 do {
8992 bool AnyAccessFailures = false;
8994 i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) {
8995 // This const_cast is a bit lame. Really, Triggered should be mutable.
8996 DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i);
8997 if (diag.Triggered)
8998 continue;
8999
9000 switch (diag.Kind) {
9002 // Don't bother giving deprecation/unavailable diagnostics if
9003 // the decl is invalid.
9004 if (!decl->isInvalidDecl())
9006 break;
9007
9009 // Only produce one access control diagnostic for a structured binding
9010 // declaration: we don't need to tell the user that all the fields are
9011 // inaccessible one at a time.
9012 if (AnyAccessFailures && isa<DecompositionDecl>(decl))
9013 continue;
9015 if (diag.Triggered)
9016 AnyAccessFailures = true;
9017 break;
9018
9021 break;
9022 }
9023 }
9024 } while ((pool = pool->getParent()));
9025}
9026
9029 assert(curPool && "re-emitting in undelayed context not supported");
9030 curPool->steal(pool);
9031}
9032
9034 VarDecl *VD = cast<VarDecl>(D);
9035 if (VD->isInvalidDecl() || VD->getType()->isDependentType())
9036 return;
9037
9038 // Obtains the FunctionDecl that was found when handling the attribute
9039 // earlier.
9040 CleanupAttr *Attr = D->getAttr<CleanupAttr>();
9041 FunctionDecl *FD = Attr->getFunctionDecl();
9042 DeclarationNameInfo NI = FD->getNameInfo();
9043
9044 // We're currently more strict than GCC about what function types we accept.
9045 // If this ever proves to be a problem it should be easy to fix.
9046 QualType Ty = this->Context.getPointerType(VD->getType());
9047 QualType ParamTy = FD->getParamDecl(0)->getType();
9048 if (QualType ConvertedTy;
9050 FD->getParamDecl(0)->getLocation(), ParamTy, Ty)) &&
9051 !ObjC().isObjCWritebackConversion(Ty, ParamTy, ConvertedTy)) {
9052 this->Diag(Attr->getArgLoc(),
9053 diag::err_attribute_cleanup_func_arg_incompatible_type)
9054 << NI.getName() << ParamTy << Ty;
9055 D->dropAttr<CleanupAttr>();
9056 return;
9057 }
9058}
9059
9061 QualType T = cast<VarDecl>(D)->getType();
9062 if (this->Context.getAsArrayType(T))
9063 T = this->Context.getBaseElementType(T);
9064 if (!T->isRecordType()) {
9065 this->Diag(A->getLoc(), diag::err_init_priority_object_attr);
9066 D->dropAttr<InitPriorityAttr>();
9067 }
9068}
Defines the clang::ASTContext interface.
#define V(N, I)
static SmallString< 64 > normalizeName(StringRef AttrName, StringRef ScopeName, AttributeCommonInfo::Syntax SyntaxUsed)
static OffloadArch getOffloadArch(const CodeGenModule &CGM)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
This file defines the classes used to store parsed information about declaration-specifiers and decla...
Defines the C++ template declaration subclasses.
Defines the classes clang::DelayedDiagnostic and clang::AccessedEntity.
Defines the clang::Expr interface and subclasses for C++ expressions.
TokenType getType() const
Returns the token's type, e.g.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Result
Implement __builtin_bit_cast and related operations.
#define X(type, name)
Definition Value.h:97
Defines the clang::LangOptions interface.
llvm::MachO::Target Target
Definition MachO.h:51
llvm::MachO::Record Record
Definition MachO.h:31
#define SM(sm)
static unsigned getNumAttributeArgs(const ParsedAttr &AL)
Defines the clang::Preprocessor interface.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
This file declares semantic analysis functions specific to AMDGPU.
This file declares semantic analysis functions specific to ARM.
This file declares semantic analysis functions specific to AVR.
This file declares semantic analysis functions specific to BPF.
This file declares semantic analysis for CUDA constructs.
static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static const RecordDecl * getRecordDecl(QualType QT)
Checks that the passed in QualType either is of RecordType or points to RecordType.
static void handlePatchableFunctionEntryAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCountedByAttrField(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkThreadSafetyValueDeclIsFunPtr(Sema &S, const ValueDecl *VD, const AttributeCommonInfo &A)
Checks that thread-safety attributes on variables or fields apply only to function pointer or functio...
static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleUninitializedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleRequiresCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleDeviceKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleBTFDeclTagAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleOptimizeNoneAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleStandardNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &A)
static void handleMIGServerRoutineAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleZeroCallUsedRegsAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleDiagnoseAsBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleEnumExtensibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static T * mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI, typename T::VisibilityType value)
static void handleFormatMatchesAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleLayoutVersion(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, const ParsedAttr &AL)
static void handleDLLAttr(Sema &S, Decl *D, const ParsedAttr &A)
static void handleConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool modularFormatAttrsEquiv(const ModularFormatAttr *Existing, const IdentifierInfo *ModularImplFn, StringRef ImplName, ArrayRef< StringRef > Aspects)
static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static Expr * makeAttributeArgExpr(Sema &S, Expr *E, const Attribute &Attr, const unsigned Idx)
static void handleLifetimeCaptureByAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, SmallVectorImpl< Expr * > &Args)
static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs)
static void handleMallocSpanAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNoSpecializations(Sema &S, Decl *D, const ParsedAttr &AL)
static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordDecl *Record)
static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void checkUnusedDeclAttributes(Sema &S, const ParsedAttributesView &A)
checkUnusedDeclAttributes - Check a list of attributes to see if it contains any decl attributes that...
static void handleGNUInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleBuiltinAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleVTablePointerAuthentication(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNoPFPAttrField(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleEnforceTCBAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, const ParsedAttr &AL, SmallVectorImpl< Expr * > &Args, unsigned Sidx=0, bool ParamIdxOk=false)
Checks that all attribute arguments, starting from Sidx, resolve to a capability object.
static void handleErrorAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc, StringRef CodeSegName)
static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleModularFormat(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleGridConstantAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL)
Handle attribute((format_arg((idx)))) attribute based on https://gcc.gnu.org/onlinedocs/gcc/Common-Fu...
static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL, const Sema::ProcessDeclAttributeOptions &Options)
ProcessDeclAttribute - Apply the specific attribute to the specified decl if the attribute applies to...
static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleXRayLogArgsAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNoMergeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleManagedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleMSStructAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNonStringAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleUnsafeBufferUsage(Sema &S, Decl *D, const ParsedAttr &AL)
static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL, SourceRange AttrParmRange, SourceRange TypeRange, bool isReturnValue=false)
static void handleAcquireCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAbiTagAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool isValidCodeModelAttr(llvm::Triple &Triple, StringRef Str)
static void handleCalledOnceAttr(Sema &S, Decl *D, const ParsedAttr &AL)
Handle 'called_once' attribute.
static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL, Expr *&Cond, StringRef &Msg)
static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, const ParsedAttr &AL)
static bool checkParamIsIntegerType(Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNo)
Checks to be sure that the given parameter number is in bounds, and is an integral type.
static bool checkRecordTypeForCapability(Sema &S, QualType Ty)
static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, SmallVectorImpl< Expr * > &Args)
static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleMSAllocatorAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static AttrTy * mergeEnforceTCBAttrImpl(Sema &S, Decl *D, const AttrTy &AL)
static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleTargetClonesAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool isKernelDecl(Decl *D)
static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL)
FormatAttrKind
@ CFStringFormat
@ IgnoredFormat
@ InvalidFormat
@ StrftimeFormat
@ SupportedFormat
@ NSStringFormat
static void handlePreferredTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleDeviceAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handlePersonalityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAssertCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool isCallbackOrDependent(QualType T)
True if T names a function to call: a function pointer, a function reference, or a reference to a fun...
static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL, bool isTypeVisibility)
static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static FormatAttrKind getFormatAttrKind(StringRef Format)
getFormatAttrKind - Map from format attribute names to supported format types.
static void handleNullableTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAcquireHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleMSConstexprAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool shouldInferAvailabilityAttribute(const ParsedAttr &AL, IdentifierInfo *&II, bool &IsUnavailable, VersionTuple &Introduced, VersionTuple &Deprecated, VersionTuple &Obsolete, Sema &S)
Returns true if the given availability attribute should be inferred, and adjusts the value of the att...
static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, SmallVectorImpl< Expr * > &Args)
static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, bool &IntegerMode, bool &ComplexMode, FloatModeKind &ExplicitType)
parseModeAttrArg - Parses attribute mode string and returns parsed type attribute.
static void handleExcludeFromExplicitInstantiationAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCFIUncheckedCalleeAttr(Sema &S, Decl *D, const ParsedAttr &Attrs)
static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, const ParsedAttr &AL)
static bool checkThreadSafetyAttrSubject(Sema &S, Decl *D, const ParsedAttr &AL, bool CheckParmVar=false)
static void handleNoBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleGCCStructAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleLifetimeCategoryAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNoUniqueAddressAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool validateAlignasAppliedType(Sema &S, Decl *D, const AlignedAttr &Attr, SourceLocation AttrLoc)
Perform checking of type validity.
static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleFunctionReturnThunksAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static unsigned getNumAttributeArgs(const ParsedAttr &AL)
static void handleGlobalAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCodeModelAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void checkAMDGPUReqdWorkGroupSize(Sema &S, Decl *D)
static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool hasBTFDeclTagAttr(Decl *D, StringRef Tag)
static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &DD, Decl *D)
Handle a delayed forbidden-type diagnostic.
static void handleNoClusterAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, SmallVectorImpl< Expr * > &Args)
static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleSharedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static Expr * makeLaunchBoundsArgExpr(Sema &S, Expr *E, const CUDALaunchBoundsAttr &AL, const unsigned Idx)
static bool checkFunParamsAreScopedLockable(Sema &S, const ParmVarDecl *ParamDecl, const AttributeCommonInfo &AL)
static void handleDeclspecThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleInterruptAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL)
Handle attribute((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
static AttributeCommonInfo getNoSanitizeAttrInfo(const ParsedAttr &NoSanitizeSpecificAttr)
static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
Handle attribute((init_priority(priority))) attributes based on http://gcc.gnu.org/onlinedocs/gcc/C_0...
static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkRecordDeclForAttr(const RecordDecl *RD)
static void handleNoSanitizeThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A)
static bool isKnownToAlwaysThrow(const FunctionDecl *FD)
static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleRandomizeLayoutAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void markUsedForAliasOrIfunc(Sema &S, Decl *D, const ParsedAttr &AL, StringRef Str)
static bool isCapabilityExpr(Sema &S, const Expr *Ex)
static void handleNoDebugAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs)
static std::pair< Expr *, int > makeClusterDimsArgExpr(Sema &S, Expr *E, const CUDAClusterDimsAttr &AL, const unsigned Idx)
static void handleNoSanitizeMemoryAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static const AttrTy * findEnforceTCBAttrByName(Decl *D, StringRef Name)
static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAvailableOnlyInDefaultEvalMethod(Sema &S, Decl *D, const ParsedAttr &AL)
static bool MustDelayAttributeArguments(const ParsedAttr &AL)
static void handleNoRandomizeLayoutAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleTargetVersionAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkRecordTypeForScopedCapability(Sema &S, QualType Ty)
static bool isIntOrBool(Expr *Exp)
Check if the passed-in expression is of type int or bool.
static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, bool BeforeIsOkay)
Check whether the two versions match.
static bool isSanitizerAttributeAllowedOnGlobals(StringRef Sanitizer)
static void handleClusterDimsAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool handleFormatAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL, FormatAttrCommon *Info)
Handle attribute((format(type,idx,firstarg))) attributes based on https://gcc.gnu....
static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleMinSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static ExprResult sharedGetConstructorDestructorAttrExpr(Sema &S, const ParsedAttr &AL)
static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkTypedefTypeForCapability(QualType Ty)
static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleReleaseCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool typeHasCapability(Sema &S, QualType Ty)
static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool isFunctionLike(const Type &T)
static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleCFGuardAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleReentrantCapabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, const ParsedAttr &AL)
Check if passed in Decl is a pointer type.
static bool isForbiddenTypeAllowed(Sema &S, Decl *D, const DelayedDiagnostic &diag, UnavailableAttr::ImplicitReason &reason)
Is the given declaration allowed to use a forbidden type?
static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
handleModeAttr - This attribute modifies the width of a decl with primitive type.
static bool checkAvailabilityAttr(Sema &S, SourceRange Range, const IdentifierInfo *Platform, VersionTuple Introduced, VersionTuple Deprecated, VersionTuple Obsoleted)
static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL)
static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr, int &Val, unsigned Idx=UINT_MAX)
Wrapper around checkUInt32Argument, with an extra check to be sure that the result will fit into a re...
static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static bool isGlobalVar(const Decl *D)
static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL)
This file declares semantic analysis for HLSL constructs.
This file declares semantic analysis functions specific to M68k.
This file declares semantic analysis functions specific to MIPS.
This file declares semantic analysis functions specific to MSP430.
This file declares semantic analysis for Objective-C.
This file declares semantic analysis routines for OpenCL.
This file declares semantic analysis for OpenMP constructs and clauses.
This file declares semantic analysis functions specific to PowerPC.
This file declares semantic analysis functions specific to RISC-V.
This file declares semantic analysis for SYCL constructs.
This file declares semantic analysis functions specific to Swift.
This file declares semantic analysis functions specific to Wasm.
This file declares semantic analysis functions specific to X86.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
static QualType getPointeeType(const MemRegion *R)
C Language Family Type Representation.
a trap message and trap category.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
virtual void AssignInheritanceModel(CXXRecordDecl *RD)
Callback invoked when an MSInheritanceAttr has been attached to a CXXRecordDecl.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
MSGuidDecl * getMSGuidDecl(MSGuidDeclParts Parts) const
Return a declaration for the global GUID object representing the given GUID value.
SourceManager & getSourceManager()
Definition ASTContext.h:869
TypedefDecl * getObjCInstanceTypeDecl()
Retrieve the typedef declaration corresponding to the Objective-C "instancetype" type.
DeclarationNameTable DeclarationNames
Definition ASTContext.h:812
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
IdentifierTable & Idents
Definition ASTContext.h:808
const LangOptions & getLangOpts() const
Definition ASTContext.h:965
QualType getConstType(QualType T) const
Return the uniqued reference to the type for a const qualified type.
const TargetInfo * getAuxTargetInfo() const
Definition ASTContext.h:928
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
CanQualType IntTy
QualType getObjCObjectPointerType(QualType OIT) const
Return a ObjCObjectPointerType type for the given ObjCObjectType.
CanQualType OverloadTy
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CanQualType VoidTy
QualType getTypedefType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier Qualifier, const TypedefNameDecl *Decl, QualType UnderlyingType=QualType(), std::optional< bool > TypeMatchesDeclOrNone=std::nullopt) const
Return the unique reference to the type for the specified typedef-name decl.
static bool hasSameType(QualType T1, QualType T2)
Determine whether the given types T1 and T2 are equivalent.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:927
TargetCXXABI::Kind getCXXABIKind() const
Return the C++ ABI kind that should be used.
unsigned getTypeAlign(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in bits.
Represents an access specifier followed by colon ':'.
Definition DeclCXX.h:86
PtrTy get() const
Definition Ownership.h:171
bool isInvalid() const
Definition Ownership.h:167
Attr - This represents one attribute.
Definition Attr.h:46
bool isInherited() const
Definition Attr.h:101
SourceLocation getScopeLoc() const
void setAttributeSpellingListIndex(unsigned V)
std::string getNormalizedFullName() const
Gets the normalized full name, which consists of both scope and name and with surrounding underscores...
unsigned getAttributeSpellingListIndex() const
const IdentifierInfo * getScopeName() const
StringRef getNormalizedAttrName(StringRef ScopeName) const
std::optional< StringRef > tryGetCorrectedAttrName(StringRef ScopeName, StringRef AttrName, const TargetInfo &Target, const LangOptions &LangOpts) const
SourceRange getNormalizedRange() const
std::optional< StringRef > tryGetCorrectedScopeName(StringRef ScopeName) const
SourceLocation getLoc() const
const IdentifierInfo * getAttrName() const
StringRef getNormalizedScopeName() const
bool isStandardAttributeSyntax() const
The attribute is spelled [[]] in either C or C++ mode, including standard attributes spelled with a k...
Type source information for an attributed type.
Definition TypeLoc.h:1008
TypeLoc getModifiedLoc() const
The modified type, which is generally canonically different from the attribute type.
Definition TypeLoc.h:1022
static bool validateAnyAppleOSVersion(const llvm::VersionTuple &Version)
Returns true if the anyAppleOS version is valid (empty or >= 26.0).
Pointer to a block type.
Definition TypeBase.h:3641
This class is used for builtin types like 'int'.
Definition TypeBase.h:3229
static bool isBuiltinFunc(llvm::StringRef Name)
Returns true if this is a libc/libm function without the '__builtin_' prefix.
Definition Builtins.cpp:137
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
QualType getFunctionObjectParameterType() const
Definition DeclCXX.h:2308
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CXXRecordDecl * getDefinition() const
Definition DeclCXX.h:548
bool hasDefinition() const
Definition DeclCXX.h:561
MSInheritanceModel calculateInheritanceModel() const
Calculate what the inheritance model would be for this class.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2949
static CallExpr * Create(const ASTContext &Ctx, Expr *Fn, ArrayRef< Expr * > Args, QualType Ty, ExprValueKind VK, SourceLocation RParenLoc, FPOptionsOverride FPFeatures, unsigned MinNumArgs=0, ADLCallKind UsesADL=NotADL)
Create a call expression.
Definition Expr.cpp:1523
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
Declaration of a class template.
static ConstantExpr * Create(const ASTContext &Context, Expr *E, const APValue &Result)
Definition Expr.cpp:356
The information about the darwin SDK that was used during this compilation.
const RelatedTargetVersionMapping * getVersionMapping(OSEnvPair Kind) const
The results of name lookup within a DeclContext.
Definition DeclBase.h:1399
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1466
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2126
bool isFileContext() const
Definition DeclBase.h:2197
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1276
DeclarationNameInfo getNameInfo() const
Definition Expr.h:1348
static DeclRefExpr * Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, ValueDecl *D, bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T, ExprValueKind VK, NamedDecl *FoundD=nullptr, const TemplateArgumentListInfo *TemplateArgs=nullptr, NonOdrUseReason NOUR=NOUR_None)
Definition Expr.cpp:494
bool hasQualifier() const
Determine whether this declaration reference was preceded by a C++ nested-name-specifier,...
Definition Expr.h:1365
ValueDecl * getDecl()
Definition Expr.h:1344
ParsedAttributes & getAttributes()
Definition DeclSpec.h:929
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
TemplateDecl * getDescribedTemplate() const
If this is a declaration that describes some template, this method returns that template declaration.
Definition DeclBase.cpp:285
T * getAttr() const
Definition DeclBase.h:581
bool hasAttrs() const
Definition DeclBase.h:526
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:550
void addAttr(Attr *A)
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition DeclBase.cpp:178
const FunctionType * getFunctionType(bool BlocksToo=true) const
Looks through the Decl's underlying type to extract a FunctionType when possible.
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition DeclBase.cpp:273
bool canBeWeakImported(bool &IsDefinition) const
Determines whether this symbol can be weak-imported, e.g., whether it would be well-formed to add the...
Definition DeclBase.cpp:847
void dropAttrs()
bool isInvalidDecl() const
Definition DeclBase.h:596
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition DeclBase.h:567
SourceLocation getLocation() const
Definition DeclBase.h:447
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
Definition DeclBase.h:1066
DeclContext * getDeclContext()
Definition DeclBase.h:456
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclBase.h:439
void dropAttr()
Definition DeclBase.h:564
AttrVec & getAttrs()
Definition DeclBase.h:532
void setDeclContext(DeclContext *DC)
setDeclContext - Set both the semantic and lexical DeclContext to DC.
Definition DeclBase.cpp:385
bool hasAttr() const
Definition DeclBase.h:585
void setLexicalDeclContext(DeclContext *DC)
Definition DeclBase.cpp:389
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition DeclBase.h:995
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition DeclBase.h:435
DeclarationName getCXXOperatorName(OverloadedOperatorKind Op)
Get the name of the overloadable C++ operator corresponding to Op.
The name of a declaration.
SourceLocation getTypeSpecStartLoc() const
Definition Decl.cpp:2005
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Decl.h:831
const AssociatedConstraint & getTrailingRequiresClause() const
Get the constraint-expression introduced by the trailing requires-clause in the function/member decla...
Definition Decl.h:855
void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc)
Definition Decl.cpp:2017
NestedNameSpecifierLoc getQualifierLoc() const
Retrieve the nested-name-specifier (with source-location information) that qualifies the name of this...
Definition Decl.h:845
NestedNameSpecifier getQualifier() const
Retrieve the nested-name-specifier that qualifies the name of this declaration, if it was present in ...
Definition Decl.h:837
TypeSourceInfo * getTypeSourceInfo() const
Definition Decl.h:809
Information about one declarator, including the parsed type information and the identifier.
Definition DeclSpec.h:2001
const DeclaratorChunk & getTypeObject(unsigned i) const
Return the specified TypeInfo from this declarator.
Definition DeclSpec.h:2499
const DeclSpec & getDeclSpec() const
getDeclSpec - Return the declaration-specifier that this declarator was declared with.
Definition DeclSpec.h:2148
const ParsedAttributes & getAttributes() const
Definition DeclSpec.h:2784
unsigned getNumTypeObjects() const
Return the number of types applied to this declarator.
Definition DeclSpec.h:2495
const ParsedAttributesView & getDeclarationAttributes() const
Definition DeclSpec.h:2787
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:234
bool isIgnored(unsigned DiagID, SourceLocation Loc) const
Determine whether the diagnostic is known to be ignored.
Definition Diagnostic.h:961
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition Diagnostic.h:599
This represents one expression.
Definition Expr.h:112
bool isIntegerConstantExpr(const ASTContext &Ctx) const
static bool isPotentialConstantExprUnevaluated(Expr *E, const FunctionDecl *FD, SmallVectorImpl< PartialDiagnosticAt > &Diags)
isPotentialConstantExprUnevaluated - Return true if this expression might be usable in a constant exp...
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3106
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition Expr.h:177
bool isTypeDependent() const
Determines whether the type of this expression depends on.
Definition Expr.h:194
bool containsUnexpandedParameterPack() const
Whether this expression contains an unexpanded parameter pack (for C++11 variadic templates).
Definition Expr.h:241
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3101
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
bool isInstantiationDependent() const
Whether this expression is instantiation-dependent, meaning that it depends in some way on.
Definition Expr.h:223
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:144
Represents difference between two FPOptions values.
Represents a member of a struct/union/class.
Definition Decl.h:3204
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition Diagnostic.h:81
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition Diagnostic.h:142
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition Diagnostic.h:105
Represents a function declaration or definition.
Definition Decl.h:2029
static FunctionDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation NLoc, DeclarationName N, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin=false, bool isInlineSpecified=false, bool hasWrittenPrototype=true, ConstexprSpecKind ConstexprKind=ConstexprSpecKind::Unspecified, const AssociatedConstraint &TrailingRequiresClause={})
Definition Decl.h:2225
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2837
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3259
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4185
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4173
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition Decl.h:2350
SourceRange getReturnTypeSourceRange() const
Attempt to compute an informative source range covering the function return type.
Definition Decl.cpp:4004
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3742
param_iterator param_end()
Definition Decl.h:2827
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition Decl.h:2961
void setIsMultiVersion(bool V=true)
Sets the multiversion state for this declaration and all of its redeclarations.
Definition Decl.h:2735
bool isNoReturn() const
Determines whether this function is known to be 'noreturn', through an attribute on its declaration o...
Definition Decl.cpp:3631
QualType getReturnType() const
Definition Decl.h:2885
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2814
bool hasPrototype() const
Whether this function has a prototype, either because one was explicitly written or because it was "i...
Definition Decl.h:2479
param_iterator param_begin()
Definition Decl.h:2826
bool isVariadic() const
Whether this function is variadic.
Definition Decl.cpp:3112
bool isConstexprSpecified() const
Definition Decl.h:2515
bool isExternC() const
Determines whether this function is a function with external, C linkage.
Definition Decl.cpp:3598
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template instantiation this function represents.
Definition Decl.cpp:4397
bool isConsteval() const
Definition Decl.h:2518
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3806
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3179
bool isInlineSpecified() const
Determine whether the "inline" keyword was specified for this function.
Definition Decl.h:2939
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5406
Declaration of a template function.
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4602
CallingConv getCallConv() const
Definition TypeBase.h:4957
QualType getReturnType() const
Definition TypeBase.h:4942
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
One of these records is kept for each identifier that is lexed.
unsigned getBuiltinID() const
Return a value indicating whether this is a builtin function.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
StringRef getName() const
Return the actual identifier string.
A simple pair of identifier info and location.
SourceLocation getLoc() const
IdentifierInfo * getIdentifierInfo() const
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Describes an entity that is being initialized.
static InitializedEntity InitializeParameter(ASTContext &Context, ParmVarDecl *Parm)
Create the initialization entity for a parameter.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool isCompatibleWithMSVC() const
bool isTargetDevice() const
True when compiling for an offloading target device.
void push_back(const T &LocalValue)
Represents the results of name lookup.
Definition Lookup.h:147
A global _GUID constant.
Definition DeclCXX.h:4424
MSGuidDeclParts Parts
Definition DeclCXX.h:4426
Describes a module or submodule.
Definition Module.h:340
This represents a decl that may have a name.
Definition Decl.h:274
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
bool isCXXInstanceMember() const
Determine whether the given declaration is an instance member of a C++ class.
Definition Decl.cpp:1977
bool isExternallyVisible() const
Definition Decl.h:433
A C++ nested-name-specifier augmented with source location information.
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
Represents one property declaration in an Objective-C interface.
Definition DeclObjC.h:731
void * getAsOpaquePtr() const
Definition Ownership.h:91
static OpaquePtr getFromOpaquePtr(void *P)
Definition Ownership.h:92
A single parameter index whose accessors require each use to make explicit the parameter index encodi...
Definition Attr.h:279
bool isValid() const
Is this parameter index valid?
Definition Attr.h:343
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition Attr.h:351
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition Attr.h:362
Represents a parameter to a function.
Definition Decl.h:1819
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2959
ParsedAttr - Represents a syntactic attribute.
Definition ParsedAttr.h:119
bool isPackExpansion() const
Definition ParsedAttr.h:367
const AvailabilityChange & getAvailabilityDeprecated() const
Definition ParsedAttr.h:399
unsigned getSemanticSpelling() const
If the parsed attribute has a semantic equivalent, and it would have a semantic Spelling enumeration ...
bool existsInTarget(const TargetInfo &Target) const
bool checkExactlyNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has exactly as many args as Num.
IdentifierLoc * getArgAsIdent(unsigned Arg) const
Definition ParsedAttr.h:389
bool hasParsedType() const
Definition ParsedAttr.h:337
const AvailabilityChange & getAvailabilityIntroduced() const
Definition ParsedAttr.h:393
void setInvalid(bool b=true) const
Definition ParsedAttr.h:345
bool hasVariadicArg() const
const ParsedAttrInfo & getInfo() const
Definition ParsedAttr.h:613
void handleAttrWithDelayedArgs(Sema &S, Decl *D) const
const Expr * getReplacementExpr() const
Definition ParsedAttr.h:429
bool hasProcessingCache() const
Definition ParsedAttr.h:347
SourceLocation getUnavailableLoc() const
Definition ParsedAttr.h:417
unsigned getProcessingCache() const
Definition ParsedAttr.h:349
const IdentifierLoc * getEnvironment() const
Definition ParsedAttr.h:435
bool acceptsExprPack() const
const Expr * getMessageExpr() const
Definition ParsedAttr.h:423
const ParsedType & getMatchingCType() const
Definition ParsedAttr.h:441
const ParsedType & getTypeArg() const
Definition ParsedAttr.h:459
SourceLocation getStrictLoc() const
Definition ParsedAttr.h:411
bool isTypeAttr() const
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this attribute.
Definition ParsedAttr.h:371
bool isArgIdent(unsigned Arg) const
Definition ParsedAttr.h:385
Expr * getArgAsExpr(unsigned Arg) const
Definition ParsedAttr.h:383
bool getMustBeNull() const
Definition ParsedAttr.h:453
bool checkAtLeastNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at least as many args as Num.
bool isUsedAsTypeAttr() const
Definition ParsedAttr.h:359
unsigned getNumArgMembers() const
bool isStmtAttr() const
bool isPragmaClangAttribute() const
True if the attribute is specified using 'pragma clang attribute'.
Definition ParsedAttr.h:363
bool slidesFromDeclToDeclSpecLegacyBehavior() const
Returns whether a [[]] attribute, if specified ahead of a declaration, should be applied to the decl-...
AttributeCommonInfo::Kind getKind() const
Definition ParsedAttr.h:610
void setProcessingCache(unsigned value) const
Definition ParsedAttr.h:354
bool isParamExpr(size_t N) const
bool isArgExpr(unsigned Arg) const
Definition ParsedAttr.h:379
bool getLayoutCompatible() const
Definition ParsedAttr.h:447
ArgsUnion getArg(unsigned Arg) const
getArg - Return the specified argument.
Definition ParsedAttr.h:374
SourceLocation getEllipsisLoc() const
Definition ParsedAttr.h:368
bool isInvalid() const
Definition ParsedAttr.h:344
bool checkAtMostNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at most as many args as Num.
const AvailabilityChange & getAvailabilityObsoleted() const
Definition ParsedAttr.h:405
void addAtEnd(ParsedAttr *newAttr)
Definition ParsedAttr.h:827
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3393
QualType getPointeeType() const
Definition TypeBase.h:3403
IdentifierTable & getIdentifierTable()
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8489
QualType getCanonicalType() const
Definition TypeBase.h:8541
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8583
const Type * getTypePtrOrNull() const
Definition TypeBase.h:8493
Represents a struct/union/class.
Definition Decl.h:4369
field_iterator field_end() const
Definition Decl.h:4575
field_range fields() const
Definition Decl.h:4572
specific_decl_iterator< FieldDecl > field_iterator
Definition Decl.h:4569
RecordDecl * getDefinitionOrSelf() const
Definition Decl.h:4557
field_iterator field_begin() const
Definition Decl.cpp:5275
Base for LValueReferenceType and RValueReferenceType.
Definition TypeBase.h:3672
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
unsigned getFlags() const
getFlags - Return the flags for this scope.
Definition Scope.h:269
@ FunctionDeclarationScope
This is a scope that corresponds to the parameters within a function prototype for a function declara...
Definition Scope.h:91
void handleAMDGPUMaxNumWorkGroupsAttr(Decl *D, const ParsedAttr &AL)
void handleAMDGPUFlatWorkGroupSizeAttr(Decl *D, const ParsedAttr &AL)
void handleAMDGPUNumSGPRAttr(Decl *D, const ParsedAttr &AL)
void handleAMDGPUNumVGPRAttr(Decl *D, const ParsedAttr &AL)
void handleAMDGPUWavesPerEUAttr(Decl *D, const ParsedAttr &AL)
void handleInterruptSaveFPAttr(Decl *D, const ParsedAttr &AL)
Definition SemaARM.cpp:1421
void handleInterruptAttr(Decl *D, const ParsedAttr &AL)
Definition SemaARM.cpp:1389
void handleBuiltinAliasAttr(Decl *D, const ParsedAttr &AL)
Definition SemaARM.cpp:1274
void handleNewAttr(Decl *D, const ParsedAttr &AL)
Definition SemaARM.cpp:1321
bool checkTargetVersionAttr(const StringRef Param, const SourceLocation Loc, SmallString< 64 > &NewParam)
Definition SemaARM.cpp:1644
bool SveAliasValid(unsigned BuiltinID, llvm::StringRef AliasName)
Definition SemaARM.cpp:1260
bool MveAliasValid(unsigned BuiltinID, llvm::StringRef AliasName)
Definition SemaARM.cpp:1247
void handleCmseNSEntryAttr(Decl *D, const ParsedAttr &AL)
Definition SemaARM.cpp:1374
bool checkTargetClonesAttr(SmallVectorImpl< StringRef > &Params, SmallVectorImpl< SourceLocation > &Locs, SmallVectorImpl< SmallString< 64 > > &NewParams)
Definition SemaARM.cpp:1678
bool CdeAliasValid(unsigned BuiltinID, llvm::StringRef AliasName)
Definition SemaARM.cpp:1255
void handleSignalAttr(Decl *D, const ParsedAttr &AL)
Definition SemaAVR.cpp:48
void handleInterruptAttr(Decl *D, const ParsedAttr &AL)
Definition SemaAVR.cpp:23
void handlePreserveAIRecord(RecordDecl *RD)
Definition SemaBPF.cpp:169
void handlePreserveAccessIndexAttr(Decl *D, const ParsedAttr &AL)
Definition SemaBPF.cpp:181
A generic diagnostic builder for errors which may or may not be deferred.
Definition SemaBase.h:111
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
CUDAFunctionTarget IdentifyTarget(const FunctionDecl *D, bool IgnoreImplicitHDAttr=false)
Determines whether the given function is a CUDA device/host/kernel/etc.
Definition SemaCUDA.cpp:208
CUDAFunctionTarget CurrentTarget()
Gets the CUDA target for the current context.
Definition SemaCUDA.h:153
SemaDiagnosticBuilder DiagIfHostCode(SourceLocation Loc, unsigned DiagID)
Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as host cod...
Definition SemaCUDA.cpp:942
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL)
void handleVkLocationAttr(Decl *D, const ParsedAttr &AL)
void handleSemanticAttr(Decl *D, const ParsedAttr &AL)
void handleShaderAttr(Decl *D, const ParsedAttr &AL)
void handleVkExtBuiltinOutputAttr(Decl *D, const ParsedAttr &AL)
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL)
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL)
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL)
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL)
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL)
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL)
void handleVkPushConstantAttr(Decl *D, const ParsedAttr &AL)
void handleVkBindingAttr(Decl *D, const ParsedAttr &AL)
void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL)
void handleInterruptAttr(Decl *D, const ParsedAttr &AL)
Definition SemaM68k.cpp:23
void handleInterruptAttr(Decl *D, const ParsedAttr &AL)
Definition SemaMIPS.cpp:243
void handleInterruptAttr(Decl *D, const ParsedAttr &AL)
void handleRuntimeName(Decl *D, const ParsedAttr &AL)
void handleNSObject(Decl *D, const ParsedAttr &AL)
bool isValidOSObjectOutParameter(const Decl *D)
void handleNSErrorDomain(Decl *D, const ParsedAttr &Attr)
void handleXReturnsXRetainedAttr(Decl *D, const ParsedAttr &AL)
void handleExternallyRetainedAttr(Decl *D, const ParsedAttr &AL)
void handleMethodFamilyAttr(Decl *D, const ParsedAttr &AL)
void handleIndependentClass(Decl *D, const ParsedAttr &AL)
void handleIBOutlet(Decl *D, const ParsedAttr &AL)
void handleReturnsInnerPointerAttr(Decl *D, const ParsedAttr &Attrs)
bool isObjCWritebackConversion(QualType FromType, QualType ToType, QualType &ConvertedType)
Determine whether this is an Objective-C writeback conversion, used for parameter passing when perfor...
void handleSuppresProtocolAttr(Decl *D, const ParsedAttr &AL)
void handleOwnershipAttr(Decl *D, const ParsedAttr &AL)
void handleBlocksAttr(Decl *D, const ParsedAttr &AL)
void handleBridgeMutableAttr(Decl *D, const ParsedAttr &AL)
Sema::RetainOwnershipKind parsedAttrToRetainOwnershipKind(const ParsedAttr &AL)
void handleRequiresSuperAttr(Decl *D, const ParsedAttr &Attrs)
void AddXConsumedAttr(Decl *D, const AttributeCommonInfo &CI, Sema::RetainOwnershipKind K, bool IsTemplateInstantiation)
void handleDesignatedInitializer(Decl *D, const ParsedAttr &AL)
void handleBridgeRelatedAttr(Decl *D, const ParsedAttr &AL)
void handleIBOutletCollection(Decl *D, const ParsedAttr &AL)
bool isCFStringType(QualType T)
void handleDirectAttr(Decl *D, const ParsedAttr &AL)
bool isNSStringType(QualType T, bool AllowNSAttributedString=false)
void handleBoxable(Decl *D, const ParsedAttr &AL)
void handleDirectMembersAttr(Decl *D, const ParsedAttr &AL)
void handleBridgeAttr(Decl *D, const ParsedAttr &AL)
void handlePreciseLifetimeAttr(Decl *D, const ParsedAttr &AL)
void handleSubGroupSize(Decl *D, const ParsedAttr &AL)
void handleNoSVMAttr(Decl *D, const ParsedAttr &AL)
void handleAccessAttr(Decl *D, const ParsedAttr &AL)
void handleOMPAssumeAttr(Decl *D, const ParsedAttr &AL)
bool checkTargetClonesAttr(const SmallVectorImpl< StringRef > &Params, const SmallVectorImpl< SourceLocation > &Locs, SmallVectorImpl< SmallString< 64 > > &NewParams, SourceLocation AttrLoc)
Definition SemaPPC.cpp:605
bool isAliasValid(unsigned BuiltinID, llvm::StringRef AliasName)
bool checkTargetClonesAttr(const SmallVectorImpl< StringRef > &Params, const SmallVectorImpl< SourceLocation > &Locs, SmallVectorImpl< SmallString< 64 > > &NewParams, SourceLocation AttrLoc)
void handleInterruptAttr(Decl *D, const ParsedAttr &AL)
bool checkTargetVersionAttr(const StringRef Param, const SourceLocation Loc, SmallString< 64 > &NewParam)
void handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL)
Definition SemaSYCL.cpp:216
void handleKernelAttr(Decl *D, const ParsedAttr &AL)
Definition SemaSYCL.cpp:177
void handleBridge(Decl *D, const ParsedAttr &AL)
Definition SemaSwift.cpp:99
void handleAsyncAttr(Decl *D, const ParsedAttr &AL)
void handleAsyncName(Decl *D, const ParsedAttr &AL)
void handleNewType(Decl *D, const ParsedAttr &AL)
void handleError(Decl *D, const ParsedAttr &AL)
void AddParameterABIAttr(Decl *D, const AttributeCommonInfo &CI, ParameterABI abi)
void handleAsyncError(Decl *D, const ParsedAttr &AL)
void handleName(Decl *D, const ParsedAttr &AL)
void handleAttrAttr(Decl *D, const ParsedAttr &AL)
Definition SemaSwift.cpp:84
void handleWebAssemblyImportNameAttr(Decl *D, const ParsedAttr &AL)
Definition SemaWasm.cpp:376
void handleWebAssemblyImportModuleAttr(Decl *D, const ParsedAttr &AL)
Definition SemaWasm.cpp:359
void handleWebAssemblyExportNameAttr(Decl *D, const ParsedAttr &AL)
Definition SemaWasm.cpp:392
void handleForceAlignArgPointerAttr(Decl *D, const ParsedAttr &AL)
Definition SemaX86.cpp:1032
void handleAnyInterruptAttr(Decl *D, const ParsedAttr &AL)
Definition SemaX86.cpp:963
bool checkTargetClonesAttr(const SmallVectorImpl< StringRef > &Params, const SmallVectorImpl< SourceLocation > &Locs, SmallVectorImpl< SmallString< 64 > > &NewParams, SourceLocation AttrLoc)
Definition SemaX86.cpp:1055
A class which encapsulates the logic for delaying diagnostics during parsing and other processing.
Definition Sema.h:1387
sema::DelayedDiagnosticPool * getCurrentPool() const
Returns the current delayed-diagnostics pool.
Definition Sema.h:1402
void popWithoutEmitting(DelayedDiagnosticsState state)
Leave a delayed-diagnostic state that was previously pushed.
Definition Sema.h:1416
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
SemaAMDGPU & AMDGPU()
Definition Sema.h:1449
BTFDeclTagAttr * mergeBTFDeclTagAttr(Decl *D, const BTFDeclTagAttr &AL)
void LoadExternalWeakUndeclaredIdentifiers()
Load weak undeclared identifiers from the external source.
Definition Sema.cpp:1094
@ LookupOrdinaryName
Ordinary name lookup, which finds ordinary names (functions, variables, typedefs, etc....
Definition Sema.h:9420
EnforceTCBAttr * mergeEnforceTCBAttr(Decl *D, const EnforceTCBAttr &AL)
SemaM68k & M68k()
Definition Sema.h:1499
DelayedDiagnosticsState ParsingDeclState
Definition Sema.h:1382
bool isValidPointerAttrType(QualType T, bool RefOkay=false)
Determine if type T is a valid subject for a nonnull and similar attributes.
static std::enable_if_t< std::is_base_of_v< Attr, AttrInfo >, SourceLocation > getAttrLoc(const AttrInfo &AL)
A helper function to provide Attribute Location for the Attr types AND the ParsedAttr.
Definition Sema.h:4905
SemaOpenMP & OpenMP()
Definition Sema.h:1534
TypeVisibilityAttr * mergeTypeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI, TypeVisibilityAttr::VisibilityType Vis)
void AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, bool IsPackExpansion)
AddAlignedAttr - Adds an aligned attribute to a particular declaration.
AvailabilityAttr * mergeAvailabilityAttr(NamedDecl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Platform, bool Implicit, VersionTuple Introduced, VersionTuple Deprecated, VersionTuple Obsoleted, bool IsUnavailable, StringRef Message, bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK, int Priority, const IdentifierInfo *IIEnvironment, const IdentifierInfo *InferredPlatformII=nullptr)
bool checkFunctionOrMethodParameterIndex(const Decl *D, const AttrInfo &AI, unsigned AttrArgNum, const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis=false, bool CanIndexVariadicArguments=false)
Check if IdxExpr is a valid parameter index for a function or instance method D.
Definition Sema.h:5233
void AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, Expr *OE)
AddAssumeAlignedAttr - Adds an assume_aligned attribute to a particular declaration.
bool checkSectionName(SourceLocation LiteralLoc, StringRef Str)
void AddPragmaAttributes(Scope *S, Decl *D)
Adds the attributes that have been specified using the '#pragma clang attribute push' directives to t...
SemaCUDA & CUDA()
Definition Sema.h:1474
bool checkCommonAttributeFeatures(const Decl *D, const ParsedAttr &A, bool SkipArgCountCheck=false)
Handles semantic checking for features that are common to all attributes, such as checking whether a ...
bool ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, const ParsedAttributesView &AttrList)
Annotation attributes are the only attributes allowed after an access specifier.
DLLImportAttr * mergeDLLImportAttr(Decl *D, const AttributeCommonInfo &CI)
ExtVectorDeclsType ExtVectorDecls
ExtVectorDecls - This is a list all the extended vector types.
Definition Sema.h:4965
void PopParsingDeclaration(ParsingDeclState state, Decl *decl)
ErrorAttr * mergeErrorAttr(Decl *D, const AttributeCommonInfo &CI, StringRef NewUserDiagnostic)
bool CheckFormatStringsCompatible(FormatStringType FST, const StringLiteral *AuthoritativeFormatString, const StringLiteral *TestedFormatString, const Expr *FunctionCallArg=nullptr)
Verify that two format strings (as understood by attribute(format) and attribute(format_matches) are ...
bool CheckVarDeclSizeAddressSpace(const VarDecl *VD, LangAS AS)
Check whether the given variable declaration has a size that fits within the address space it is decl...
void redelayDiagnostics(sema::DelayedDiagnosticPool &pool)
Given a set of delayed diagnostics, re-emit them as if they had been delayed in the current context i...
PersonalityAttr * mergePersonalityAttr(Decl *D, FunctionDecl *Routine, const AttributeCommonInfo &CI)
SemaSYCL & SYCL()
Definition Sema.h:1559
AvailabilityAttr * mergeAndInferAvailabilityAttr(NamedDecl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Platform, bool Implicit, VersionTuple Introduced, VersionTuple Deprecated, VersionTuple Obsoleted, bool IsUnavailable, StringRef Message, bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK, int Priority, const IdentifierInfo *IIEnvironment, const IdentifierInfo *InferredPlatformII)
VisibilityAttr * mergeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI, VisibilityAttr::VisibilityType Vis)
SemaX86 & X86()
Definition Sema.h:1579
ParmVarDecl * BuildParmVarDeclForTypedef(DeclContext *DC, SourceLocation Loc, QualType T)
Synthesizes a variable for a parameter arising from a typedef.
ASTContext & Context
Definition Sema.h:1309
void LazyProcessLifetimeCaptureByParams(FunctionDecl *FD)
DiagnosticsEngine & getDiagnostics() const
Definition Sema.h:937
SemaObjC & ObjC()
Definition Sema.h:1519
void PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext=true)
Add this decl to the scope shadowed decl chains.
void AddModeAttr(Decl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Name, bool InInstantiation=false)
AddModeAttr - Adds a mode attribute to a particular declaration.
ASTContext & getASTContext() const
Definition Sema.h:940
void mergeVisibilityType(Decl *D, SourceLocation Loc, VisibilityAttr::VisibilityType Type)
bool CheckCallingConvAttr(const ParsedAttr &attr, CallingConv &CC, const FunctionDecl *FD=nullptr, CUDAFunctionTarget CFT=CUDAFunctionTarget::InvalidTarget)
Check validaty of calling convention attribute attr.
QualType BuildCountAttributedArrayOrPointerType(QualType WrappedTy, Expr *CountExpr, bool CountInBytes, bool OrNull)
void ProcessPragmaWeak(Scope *S, Decl *D)
bool CheckAttrNoArgs(const ParsedAttr &CurrAttr)
bool UnifySection(StringRef SectionName, int SectionFlags, NamedDecl *TheDecl)
Definition SemaAttr.cpp:838
void addNoClusterAttr(Decl *D, const AttributeCommonInfo &CI)
Add a no_cluster attribute to a particular declaration.
FPOptions & getCurFPFeatures()
Definition Sema.h:935
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Calls Lexer::getLocForEndOfToken()
Definition Sema.cpp:83
@ UPPC_Expression
An arbitrary expression.
Definition Sema.h:14539
const LangOptions & getLangOpts() const
Definition Sema.h:933
ModularFormatAttr * mergeModularFormatAttr(Decl *D, const AttributeCommonInfo &CI, const IdentifierInfo *ModularImplFn, StringRef ImplName, MutableArrayRef< StringRef > Aspects)
SemaBPF & BPF()
Definition Sema.h:1464
Preprocessor & PP
Definition Sema.h:1308
bool DiagnoseUnexpandedParameterPack(SourceLocation Loc, TypeSourceInfo *T, UnexpandedParameterPackContext UPPC)
If the given type contains an unexpanded parameter pack, diagnose the error.
MinSizeAttr * mergeMinSizeAttr(Decl *D, const AttributeCommonInfo &CI)
SemaMSP430 & MSP430()
Definition Sema.h:1509
AssignConvertType CheckAssignmentConstraints(SourceLocation Loc, QualType LHSType, QualType RHSType)
CheckAssignmentConstraints - Perform type checking for assignment, argument passing,...
const LangOptions & LangOpts
Definition Sema.h:1307
static const uint64_t MaximumAlignment
Definition Sema.h:1236
CUDAClusterDimsAttr * createClusterDimsAttr(const AttributeCommonInfo &CI, Expr *X, Expr *Y, Expr *Z)
Add a cluster_dims attribute to a particular declaration.
SemaHLSL & HLSL()
Definition Sema.h:1484
AlwaysInlineAttr * mergeAlwaysInlineAttr(Decl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Ident)
SemaMIPS & MIPS()
Definition Sema.h:1504
SemaRISCV & RISCV()
Definition Sema.h:1549
bool CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes, bool OrNull)
Check if applying the specified attribute variant from the "counted by" family of attributes to Field...
void ProcessDeclAttributeList(Scope *S, Decl *D, const ParsedAttributesView &AttrList, const ProcessDeclAttributeOptions &Options=ProcessDeclAttributeOptions())
ProcessDeclAttributeList - Apply all the decl attributes in the specified attribute list to the speci...
SemaSwift & Swift()
Definition Sema.h:1564
NamedDecl * getCurFunctionOrMethodDecl() const
getCurFunctionOrMethodDecl - Return the Decl for the current ObjC method or C function we're in,...
Definition Sema.cpp:1760
void AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI, Expr *ParamExpr)
AddAllocAlignAttr - Adds an alloc_align attribute to a particular declaration.
bool CheckRegparmAttr(const ParsedAttr &attr, unsigned &value)
Checks a regparm attribute, returning true if it is ill-formed and otherwise setting numParams to the...
void ProcessDeclAttributeDelayed(Decl *D, const ParsedAttributesView &AttrList)
Helper for delayed processing TransparentUnion or BPFPreserveAccessIndexAttr attribute.
bool checkUInt32Argument(const AttrInfo &AI, const Expr *Expr, uint32_t &Val, unsigned Idx=UINT_MAX, bool StrictlyUnsigned=false)
If Expr is a valid integer constant, get the value of the integer expression and return success or fa...
Definition Sema.h:4916
MSInheritanceAttr * mergeMSInheritanceAttr(Decl *D, const AttributeCommonInfo &CI, bool BestCase, MSInheritanceModel Model)
InternalLinkageAttr * mergeInternalLinkageAttr(Decl *D, const ParsedAttr &AL)
bool IsAssignConvertCompatible(AssignConvertType ConvTy)
Definition Sema.h:8138
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition Sema.h:1447
void ActOnInitPriorityAttr(Decl *D, const Attr *A)
SemaOpenCL & OpenCL()
Definition Sema.h:1529
ExprResult PerformContextuallyConvertToBool(Expr *From)
PerformContextuallyConvertToBool - Perform a contextual conversion of the expression From to bool (C+...
FunctionDecl * ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, bool Complain=false, DeclAccessPair *Found=nullptr, TemplateSpecCandidateSet *FailedTSC=nullptr, bool ForTypeDeduction=false)
Given an expression that refers to an overloaded function, try to resolve that overloaded function ex...
NamedDecl * DeclClonePragmaWeak(NamedDecl *ND, const IdentifierInfo *II, SourceLocation Loc)
DeclClonePragmaWeak - clone existing decl (maybe definition), #pragma weak needs a non-definition dec...
DLLExportAttr * mergeDLLExportAttr(Decl *D, const AttributeCommonInfo &CI)
CodeSegAttr * mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI, StringRef Name)
SectionAttr * mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI, StringRef Name)
bool inTemplateInstantiation() const
Determine whether we are currently performing template instantiation.
Definition Sema.h:14088
SourceManager & getSourceManager() const
Definition Sema.h:938
void ActOnCleanupAttr(Decl *D, const Attr *A)
static FormatStringType GetFormatStringType(StringRef FormatFlavor)
bool checkTargetAttr(SourceLocation LiteralLoc, StringRef Str)
bool ValidateFormatString(FormatStringType FST, const StringLiteral *Str)
Verify that one format string (as understood by attribute(format)) is self-consistent; for instance,...
FormatMatchesAttr * mergeFormatMatchesAttr(Decl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Format, int FormatIdx, StringLiteral *FormatStr)
llvm::Error isValidSectionSpecifier(StringRef Str)
Used to implement to perform semantic checking on attribute((section("foo"))) specifiers.
void AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI, Expr *MaxThreads, Expr *MinBlocks, Expr *MaxBlocks)
AddLaunchBoundsAttr - Adds a launch_bounds attribute to a particular declaration.
void DiagnoseUnknownAttribute(const ParsedAttr &AL)
bool isCompleteType(SourceLocation Loc, QualType T, CompleteTypeKind Kind=CompleteTypeKind::Default)
Definition Sema.h:15602
OptimizeNoneAttr * mergeOptimizeNoneAttr(Decl *D, const AttributeCommonInfo &CI)
void checkUnusedDeclAttributes(Declarator &D)
checkUnusedDeclAttributes - Given a declarator which is not being used to build a declaration,...
bool CheckAttrTarget(const ParsedAttr &CurrAttr)
EnforceTCBLeafAttr * mergeEnforceTCBLeafAttr(Decl *D, const EnforceTCBLeafAttr &AL)
ExprResult VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, VerifyICEDiagnoser &Diagnoser, AllowFoldKind CanFold=AllowFoldKind::No)
VerifyIntegerConstantExpression - Verifies that an expression is an ICE, and reports the appropriate ...
ASTConsumer & Consumer
Definition Sema.h:1310
void NoteAllOverloadCandidates(Expr *E, QualType DestType=QualType(), bool TakingAddress=false)
bool checkInstantiatedThreadSafetyAttrs(const Decl *D, const Attr *A)
Recheck instantiated thread-safety attributes that could not be validated on the dependent pattern de...
CUDALaunchBoundsAttr * CreateLaunchBoundsAttr(const AttributeCommonInfo &CI, Expr *MaxThreads, Expr *MinBlocks, Expr *MaxBlocks, bool IgnoreArch=false)
Create a CUDALaunchBoundsAttr attribute.
void addClusterDimsAttr(Decl *D, const AttributeCommonInfo &CI, Expr *X, Expr *Y, Expr *Z)
@ AP_InferredFromAnyAppleOS
The availability attribute was inferred from an 'anyAppleOS' availability attribute.
Definition Sema.h:4884
@ AP_PragmaClangAttribute
The availability attribute was applied using 'pragma clang attribute'.
Definition Sema.h:4876
@ AP_InferredFromOtherPlatform
The availability attribute for a specific platform was inferred from an availability attribute for an...
Definition Sema.h:4880
@ AP_PragmaClangAttribute_InferredFromAnyAppleOS
The availability attribute was inferred from an 'anyAppleOS' availability attribute that was applied ...
Definition Sema.h:4889
@ AP_Explicit
The availability attribute was specified explicitly next to the declaration.
Definition Sema.h:4873
SemaPPC & PPC()
Definition Sema.h:1539
SmallVector< Decl *, 2 > WeakTopLevelDecl
WeakTopLevelDecl - Translation-unit scoped declarations generated by #pragma weak during processing o...
Definition Sema.h:4953
bool RequireCompleteType(SourceLocation Loc, QualType T, CompleteTypeKind Kind, TypeDiagnoser &Diagnoser)
Ensure that the type T is a complete type.
Scope * TUScope
Translation Unit Scope - useful to Objective-C actions that need to lookup file scope declarations in...
Definition Sema.h:1268
UuidAttr * mergeUuidAttr(Decl *D, const AttributeCommonInfo &CI, StringRef UuidAsWritten, MSGuidDecl *GuidDecl)
ExprResult PerformCopyInitialization(const InitializedEntity &Entity, SourceLocation EqualLoc, ExprResult Init, bool TopLevelOfInitList=false, bool AllowExplicit=false)
Attr * CreateAnnotationAttr(const AttributeCommonInfo &CI, StringRef Annot, MutableArrayRef< Expr * > Args)
CreateAnnotationAttr - Creates an annotation Annot with Args arguments.
Definition Sema.cpp:3072
SemaAVR & AVR()
Definition Sema.h:1459
void handleDelayedAvailabilityCheck(sema::DelayedDiagnostic &DD, Decl *Ctx)
void ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD)
ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in it, apply them to D.
void DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, const WeakInfo &W)
DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak applied to it,...
bool CheckSpanLikeType(const AttributeCommonInfo &CI, const QualType &Ty)
Check that the type is a plain record with one field being a pointer type and the other field being a...
llvm::MapVector< IdentifierInfo *, llvm::SetVector< WeakInfo, llvm::SmallVector< WeakInfo, 1u >, llvm::SmallDenseSet< WeakInfo, 2u, WeakInfo::DenseMapInfoByAliasOnly > > > WeakUndeclaredIdentifiers
WeakUndeclaredIdentifiers - Identifiers contained in #pragma weak before declared.
Definition Sema.h:3601
void ProcessAPINotes(Decl *D)
Map any API notes provided for this declaration to attributes on the declaration.
void CheckAlignasUnderalignment(Decl *D)
void HandleDelayedAccessCheck(sema::DelayedDiagnostic &DD, Decl *Ctx)
DarwinSDKInfo * getDarwinSDKInfoForAvailabilityChecking(SourceLocation Loc, StringRef Platform)
Definition Sema.cpp:112
bool LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation=false, bool ForceNoCPlusPlus=false)
Perform unqualified name lookup starting from a given scope.
static QualType GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo=nullptr)
void AddAlignValueAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E)
AddAlignValueAttr - Adds an align_value attribute to a particular declaration.
SemaWasm & Wasm()
Definition Sema.h:1574
FormatAttr * mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI, const IdentifierInfo *Format, int FormatIdx, int FirstArg)
LifetimeCaptureByAttr * ParseLifetimeCaptureByAttr(const ParsedAttr &AL, StringRef ParamName)
bool checkMSInheritanceAttrOnDefinition(CXXRecordDecl *RD, SourceRange Range, bool BestCase, MSInheritanceModel SemanticSpelling)
bool checkStringLiteralArgumentAttr(const AttributeCommonInfo &CI, const Expr *E, StringRef &Str, SourceLocation *ArgLocation=nullptr)
Check if the argument E is a ASCII string literal.
SemaARM & ARM()
Definition Sema.h:1454
bool CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, const FunctionProtoType *Proto)
CheckFunctionCall - Check a direct function call for various correctness and safety properties not st...
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
bool isInSystemMacro(SourceLocation loc) const
Returns whether Loc is expanded from a macro in a system header.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
Definition Stmt.h:85
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1805
bool isBeingDefined() const
Return true if this decl is currently being defined.
Definition Decl.h:3882
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3862
bool isUnion() const
Definition Decl.h:3972
Exposes information about the current target.
Definition TargetInfo.h:227
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition TargetInfo.h:330
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual bool hasFeatureEnabled(const llvm::StringMap< bool > &Features, StringRef Name) const
Check if target has a given feature enabled.
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition TargetInfo.h:493
virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const
Determines whether a given calling convention is valid for the target.
bool isTLSSupported() const
Whether the target supports thread-local storage.
virtual unsigned getRegisterWidth() const
Return the "preferred" register width on this target.
Definition TargetInfo.h:910
virtual bool validateCPUSpecificCPUDispatch(StringRef Name) const
virtual bool hasProtectedVisibility() const
Does this target support "protected" visibility?
virtual unsigned getUnwindWordWidth() const
Definition TargetInfo.h:905
unsigned getCharWidth() const
Definition TargetInfo.h:524
virtual bool shouldDLLImportComdatSymbols() const
Does this target aim for semantic compatibility with Microsoft C++ code using dllimport/export attrib...
const llvm::VersionTuple & getSDKVersion() const
std::string CPU
If given, the name of the target CPU to generate code for.
llvm::StringMap< bool > FeatureMap
The map of which features have been enabled disabled based on the command line.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
SourceRange getSourceRange() const LLVM_READONLY
Get the full source range.
Definition TypeLoc.h:154
T getAsAdjusted() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition TypeLoc.h:2766
SourceLocation getBeginLoc() const
Get the begin source location.
Definition TypeLoc.cpp:193
A container of type source information.
Definition TypeBase.h:8460
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:267
QualType getType() const
Return the type wrapped by this type source info.
Definition TypeBase.h:8471
The base class of the type hierarchy.
Definition TypeBase.h:1876
bool isSizelessType() const
As an extension, we classify types as one of "sized" or "sizeless"; every type is one or the other.
Definition Type.cpp:2665
bool isBlockPointerType() const
Definition TypeBase.h:8746
bool isVoidType() const
Definition TypeBase.h:9092
bool isBooleanType() const
Definition TypeBase.h:9229
const Type * getPointeeOrArrayElementType() const
If this is a pointer type, return the pointee type.
Definition TypeBase.h:9279
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2270
bool isComplexType() const
isComplexType() does not include complex integers (a GCC extension).
Definition Type.cpp:761
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isArrayType() const
Definition TypeBase.h:8825
bool isCharType() const
Definition Type.cpp:2197
bool isFunctionPointerType() const
Definition TypeBase.h:8793
bool isPointerType() const
Definition TypeBase.h:8726
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9136
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9386
bool isReferenceType() const
Definition TypeBase.h:8750
bool isEnumeralType() const
Definition TypeBase.h:8857
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
Definition Type.cpp:1958
bool isIntegralType(const ASTContext &Ctx) const
Determine whether this type is an integral type.
Definition Type.cpp:2160
bool isAlignValT() const
Definition Type.cpp:3310
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9214
bool isExtVectorType() const
Definition TypeBase.h:8869
bool isAnyCharacterType() const
Determine whether this type is any of the built-in character types.
Definition Type.cpp:2233
bool isInstantiationDependentType() const
Determine whether this type is an instantiation-dependent type, meaning that the type involves a temp...
Definition TypeBase.h:2855
bool isBitIntType() const
Definition TypeBase.h:9001
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition TypeBase.h:2847
bool containsUnexpandedParameterPack() const
Whether this type is or contains an unexpanded parameter pack, used to support C++0x variadic templat...
Definition TypeBase.h:2466
bool isPointerOrReferenceType() const
Definition TypeBase.h:8730
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition Type.cpp:2531
bool hasFloatingRepresentation() const
Determine whether this type has a floating-point representation of some sort, e.g....
Definition Type.cpp:2401
bool isVectorType() const
Definition TypeBase.h:8865
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:2986
bool isFloatingType() const
Definition Type.cpp:2393
bool isAnyPointerType() const
Definition TypeBase.h:8734
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9319
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition Type.cpp:690
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3606
static UnaryOperator * Create(const ASTContext &C, Expr *input, Opcode opc, QualType type, ExprValueKind VK, ExprObjectKind OK, SourceLocation l, bool CanOverflow, FPOptionsOverride FPFeatures)
Definition Expr.cpp:5164
Represents a dependent using declaration which was marked with typename.
Definition DeclCXX.h:4058
Represents a dependent using declaration which was not marked with typename.
Definition DeclCXX.h:3961
Represents a C++ using-declaration.
Definition DeclCXX.h:3612
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
void setType(QualType newType)
Definition Decl.h:724
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:932
static VarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S)
Definition Decl.cpp:2132
@ TLS_None
Not a TLS variable.
Definition Decl.h:952
Represents a GCC generic vector type.
Definition TypeBase.h:4274
Captures information about a #pragma weak directive.
Definition Weak.h:25
const IdentifierInfo * getAlias() const
Definition Weak.h:32
SourceLocation getLocation() const
Definition Weak.h:33
A collection of diagnostics which were delayed.
const DelayedDiagnosticPool * getParent() const
void steal(DelayedDiagnosticPool &pool)
Steal the diagnostics from the given pool.
SmallVectorImpl< DelayedDiagnostic >::const_iterator pool_iterator
A diagnostic message which has been conditionally emitted pending the complete parsing of the current...
unsigned getForbiddenTypeDiagnostic() const
The diagnostic ID to emit.
Defines the clang::TargetInfo interface.
#define UINT_MAX
Definition limits.h:64
Enums for the diagnostics of target, target_version and target_clones.
Definition Sema.h:854
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
@ Match
This is not an overload because the signature exactly matches an existing declaration.
Definition Sema.h:829
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus23
@ ExpectedFunctionMethodOrBlock
@ ExpectedClass
@ ExpectedTypeOrNamespace
@ ExpectedVariableFieldOrTag
@ ExpectedVariableOrField
@ ExpectedUnion
@ ExpectedFunctionOrMethod
@ ExpectedVariable
@ ExpectedFunctionOrClassOrEnum
@ ExpectedVariableOrFunction
@ ExpectedKernelFunction
@ ExpectedFunctionVariableOrClass
@ ExpectedNonMemberFunction
void handleSimpleAttributeOrDiagnose(SemaBase &S, Decl *D, const AttributeCommonInfo &CI, bool PassesCheck, unsigned DiagID, DiagnosticArgs &&...ExtraArgs)
Add an attribute AttrType to declaration D, provided that PassesCheck is true.
Definition Attr.h:185
bool hasDeclarator(const Decl *D)
Return true if the given decl has a declarator that should have been processed by Sema::GetTypeForDec...
Definition Attr.h:46
CUDAFunctionTarget
Definition Cuda.h:63
QualType getFunctionOrMethodResultType(const Decl *D)
Definition Attr.h:98
@ OK_Ordinary
An ordinary object is located at an address in memory.
Definition Specifiers.h:152
AvailabilityMergeKind
Describes the kind of merge to perform for availability attributes (including "deprecated",...
Definition Sema.h:627
@ None
Don't merge availability attributes at all.
Definition Sema.h:629
@ Override
Merge availability attributes for an override, which requires an exact match or a weakening of constr...
Definition Sema.h:635
@ OptionalProtocolImplementation
Merge availability attributes for an implementation of an optional protocol requirement.
Definition Sema.h:641
@ Redeclaration
Merge availability attributes for a redeclaration, which requires an exact match.
Definition Sema.h:632
@ ProtocolImplementation
Merge availability attributes for an implementation of a protocol requirement.
Definition Sema.h:638
@ VectorLength
'vector_length' clause, allowed on 'parallel', 'kernels', 'parallel loop', and 'kernels loop' constru...
CudaVersion ToCudaVersion(llvm::VersionTuple)
Definition Cuda.cpp:72
SmallVector< Attr *, 4 > AttrVec
AttrVec - A vector of Attr, which is how they are stored on the AST.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool checkAttrMutualExclusion(SemaBase &S, Decl *D, const ParsedAttr &AL)
Diagnose mutually exclusive attributes when present on a given declaration.
Definition Attr.h:135
@ SC_Extern
Definition Specifiers.h:252
@ SC_Register
Definition Specifiers.h:258
@ SC_None
Definition Specifiers.h:251
@ TSCS_unspecified
Definition Specifiers.h:237
void inferNoReturnAttr(Sema &S, Decl *D)
Expr * Cond
};
SourceRange getFunctionOrMethodResultSourceRange(const Decl *D)
Definition Attr.h:104
bool isFunctionOrMethodOrBlockForAttrSubject(const Decl *D)
Return true if the given decl has function type (function or function-typed variable) or an Objective...
Definition Attr.h:40
QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx)
Definition Attr.h:83
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition Linkage.h:35
Language
The language for the input, used to select and validate the language standard and possible actions.
AttributeArgumentNType
These constants match the enumerated choices of err_attribute_argument_n_type and err_attribute_argum...
@ AANT_ArgumentIntegerConstant
@ AANT_ArgumentBuiltinFunction
@ AANT_ArgumentIntOrBool
@ AANT_ArgumentIdentifier
@ AANT_ArgumentString
@ Default
Set to the current date and time.
@ SD_Automatic
Automatic storage duration (most local variables).
Definition Specifiers.h:342
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition ASTLambda.h:28
InheritableAttr * getDLLAttr(Decl *D)
Return a DLL attribute from the declaration.
@ SwiftAsyncContext
This parameter (which must have pointer type) uses the special Swift asynchronous context-pointer ABI...
Definition Specifiers.h:402
@ SwiftErrorResult
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
Definition Specifiers.h:392
@ SwiftIndirectResult
This parameter (which must have pointer type) is a Swift indirect result parameter.
Definition Specifiers.h:387
@ SwiftContext
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment.
Definition Specifiers.h:397
const FunctionProtoType * T
bool isFunctionOrMethodVariadic(const Decl *D)
Definition Attr.h:112
@ Template
We are parsing a template declaration.
Definition Parser.h:81
bool isFuncOrMethodForAttrSubject(const Decl *D)
isFuncOrMethodForAttrSubject - Return true if the given decl has function type (function or function-...
Definition Attr.h:34
ExprResult ExprError()
Definition Ownership.h:265
OffloadArch StringToOffloadArch(llvm::StringRef S)
CudaVersion
Definition Cuda.h:22
LLVM_READONLY bool isHexDigit(unsigned char c)
Return true if this character is an ASCII hex digit: [0-9a-fA-F].
Definition CharInfo.h:144
FormatStringType
Definition Sema.h:498
SanitizerMask parseSanitizerValue(StringRef Value, bool AllowGroups)
Parse a single value from a -fsanitize= or -fno-sanitize= value list.
const char * OffloadArchToString(OffloadArch A)
void handleSimpleAttribute(SemaBase &S, Decl *D, const AttributeCommonInfo &CI)
Applies the given attribute to the Decl without performing any additional semantic checking.
Definition Attr.h:175
std::pair< SourceLocation, PartialDiagnostic > PartialDiagnosticAt
A partial diagnostic along with the source location where this diagnostic occurs.
bool hasImplicitObjectParameter(const Decl *D)
Definition Attr.h:126
FloatModeKind
Definition TargetInfo.h:75
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:136
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:140
MSInheritanceModel
Assigned inheritance model for a class in the MS C++ ABI.
Definition Specifiers.h:413
bool hasFunctionProto(const Decl *D)
hasFunctionProto - Return true if the given decl has a argument information.
Definition Attr.h:55
unsigned getFunctionOrMethodNumParams(const Decl *D)
getFunctionOrMethodNumParams - Return number of function or method parameters.
Definition Attr.h:64
bool declaresSameEntity(const Decl *D1, const Decl *D2)
Determine whether two declarations declare the same entity.
Definition DeclBase.h:1305
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
@ TSK_ExplicitSpecialization
This template specialization was declared or defined by an explicit specialization (C++ [temp....
Definition Specifiers.h:199
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:279
@ CC_X86Pascal
Definition Specifiers.h:285
@ CC_Swift
Definition Specifiers.h:294
@ CC_IntelOclBicc
Definition Specifiers.h:291
@ CC_PreserveMost
Definition Specifiers.h:296
@ CC_Win64
Definition Specifiers.h:286
@ CC_X86ThisCall
Definition Specifiers.h:283
@ CC_AArch64VectorCall
Definition Specifiers.h:298
@ CC_DeviceKernel
Definition Specifiers.h:293
@ CC_AAPCS
Definition Specifiers.h:289
@ CC_PreserveNone
Definition Specifiers.h:301
@ CC_M68kRTD
Definition Specifiers.h:300
@ CC_SwiftAsync
Definition Specifiers.h:295
@ CC_X86RegCall
Definition Specifiers.h:288
@ CC_RISCVVectorCall
Definition Specifiers.h:302
@ CC_X86VectorCall
Definition Specifiers.h:284
@ CC_AArch64SVEPCS
Definition Specifiers.h:299
@ CC_RISCVVLSCall_32
Definition Specifiers.h:303
@ CC_X86StdCall
Definition Specifiers.h:281
@ CC_X86_64SysV
Definition Specifiers.h:287
@ CC_PreserveAll
Definition Specifiers.h:297
@ CC_X86FastCall
Definition Specifiers.h:282
@ CC_AAPCS_VFP
Definition Specifiers.h:290
@ Generic
not a target-specific vector type
Definition TypeBase.h:4235
U cast(CodeGen::Address addr)
Definition Address.h:327
SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx)
Definition Attr.h:92
OpaquePtr< QualType > ParsedType
An opaque type for threading parsed type information through the parser.
Definition Ownership.h:230
@ Interface
The "__interface" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:6010
@ None
No keyword precedes the qualified type name.
Definition TypeBase.h:6026
@ Union
The "union" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:6013
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
@ Other
Other implicit parameter.
Definition Decl.h:1774
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 int32_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
IdentifierInfo * Identifier
FormatAttrKind Kind
Represents information about a change in availability for an entity, which is part of the encoding of...
Definition ParsedAttr.h:47
VersionTuple Version
The version number at which the change occurred.
Definition ParsedAttr.h:52
bool isValid() const
Determine whether this availability change is valid.
Definition ParsedAttr.h:58
SourceLocation KeywordLoc
The location of the keyword indicating the kind of change.
Definition ParsedAttr.h:49
A value that describes two os-environment pairs that can be used as a key to the version map in the S...
static constexpr OSEnvPair macOStoMacCatalystPair()
Returns the os-environment mapping pair that's used to represent the macOS -> Mac Catalyst version ma...
static constexpr OSEnvPair iOStoWatchOSPair()
Returns the os-environment mapping pair that's used to represent the iOS -> watchOS version mapping.
static constexpr OSEnvPair iOStoTvOSPair()
Returns the os-environment mapping pair that's used to represent the iOS -> tvOS version mapping.
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
DeclarationName getName() const
getName - Returns the embedded declaration name.
const ParsedAttributesView & getAttrs() const
If there are attributes applied to this declaratorchunk, return them.
Definition DeclSpec.h:1756
uint16_t Part2
...-89ab-...
Definition DeclCXX.h:4403
uint32_t Part1
{01234567-...
Definition DeclCXX.h:4401
uint16_t Part3
...-cdef-...
Definition DeclCXX.h:4405
uint8_t Part4And5[8]
...-0123-456789abcdef}
Definition DeclCXX.h:4407
virtual AttrHandling handleDeclAttribute(Sema &S, Decl *D, const ParsedAttr &Attr) const
If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this Decl then do so and return...
Contains information gathered from parsing the contents of TargetAttr.
Definition TargetInfo.h:60
std::vector< std::string > Features
Definition TargetInfo.h:61