1 //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements decl-related attribute processing.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Sema/SemaInternal.h"
15 #include "TargetAttributesSema.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "clang/Sema/DeclSpec.h"
24 #include "clang/Sema/DelayedDiagnostic.h"
25 #include "clang/Sema/Lookup.h"
26 #include "llvm/ADT/StringExtras.h"
27 using namespace clang;
30 /// These constants match the enumerated choices of
31 /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
32 enum AttributeDeclKind {
35 ExpectedVariableOrFunction,
36 ExpectedFunctionOrMethod,
38 ExpectedFunctionMethodOrBlock,
39 ExpectedFunctionMethodOrParameter,
43 ExpectedVariableFunctionOrLabel,
44 ExpectedFieldOrGlobalVar,
48 //===----------------------------------------------------------------------===//
50 //===----------------------------------------------------------------------===//
52 static const FunctionType *getFunctionType(const Decl *D,
53 bool blocksToo = true) {
55 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
57 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
59 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
60 Ty = decl->getUnderlyingType();
64 if (Ty->isFunctionPointerType())
65 Ty = Ty->getAs<PointerType>()->getPointeeType();
66 else if (blocksToo && Ty->isBlockPointerType())
67 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
69 return Ty->getAs<FunctionType>();
72 // FIXME: We should provide an abstraction around a method or function
73 // to provide the following bits of information.
75 /// isFunction - Return true if the given decl has function
76 /// type (function or function-typed variable).
77 static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
81 /// isFunctionOrMethod - Return true if the given decl has function
82 /// type (function or function-typed variable) or an Objective-C
84 static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
88 /// isFunctionOrMethodOrBlock - Return true if the given decl has function
89 /// type (function or function-typed variable) or an Objective-C
90 /// method or a block.
91 static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
94 // check for block is more involved.
95 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
96 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
99 return isa<BlockDecl>(D);
102 /// Return true if the given decl has a declarator that should have
103 /// been processed by Sema::GetTypeForDeclarator.
104 static bool hasDeclarator(const Decl *D) {
105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
110 /// hasFunctionProto - Return true if the given decl has a argument
111 /// information. This decl should have already passed
112 /// isFunctionOrMethod or isFunctionOrMethodOrBlock.
113 static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
115 return isa<FunctionProtoType>(FnTy);
117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
122 /// getFunctionOrMethodNumArgs - Return number of function or method
123 /// arguments. It is an error to call this on a K&R function (use
124 /// hasFunctionProto first).
125 static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
129 return BD->getNumParams();
130 return cast<ObjCMethodDecl>(D)->param_size();
133 static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
137 return BD->getParamDecl(Idx)->getType();
139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
142 static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
144 return cast<FunctionProtoType>(FnTy)->getResultType();
145 return cast<ObjCMethodDecl>(D)->getResultType();
148 static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
151 return proto->isVariadic();
152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
153 return BD->isVariadic();
155 return cast<ObjCMethodDecl>(D)->isVariadic();
159 static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
161 return MethodDecl->isInstance();
165 static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
174 IdentifierInfo* ClsName = Cls->getIdentifier();
176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
181 static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
182 const PointerType *PT = T->getAs<PointerType>();
186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
190 const RecordDecl *RD = RT->getDecl();
191 if (RD->getTagKind() != TTK_Struct)
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
197 /// \brief Check if the attribute has exactly as many args as Num. May
199 static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr,
201 if (Attr.getNumArgs() != Num) {
202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num;
210 /// \brief Check if the attribute has at least as many args as Num. May
212 static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr,
214 if (Attr.getNumArgs() < Num) {
215 S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num;
223 /// \brief Check if passed in Decl is a field or potentially shared global var
224 /// \return true if the Decl is a field or potentially shared global variable
226 static bool mayBeSharedVariable(const Decl *D) {
227 if (isa<FieldDecl>(D))
229 if (const VarDecl *vd = dyn_cast<VarDecl>(D))
230 return (vd->hasGlobalStorage() && !(vd->isThreadSpecified()));
235 /// \brief Check if the passed-in expression is of type int or bool.
236 static bool isIntOrBool(Expr *Exp) {
237 QualType QT = Exp->getType();
238 return QT->isBooleanType() || QT->isIntegerType();
242 /// \brief Check if passed in Decl is a pointer type.
243 /// Note that this function may produce an error message.
244 /// \return true if the Decl is a pointer type; false otherwise
246 static bool checkIsPointer(Sema &S, const Decl *D, const AttributeList &Attr) {
247 if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) {
248 QualType QT = vd->getType();
249 if (QT->isAnyPointerType())
251 S.Diag(Attr.getLoc(), diag::warn_pointer_attribute_wrong_type)
252 << Attr.getName()->getName() << QT;
254 S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl)
260 /// \brief Checks that the passed in QualType either is of RecordType or points
261 /// to RecordType. Returns the relevant RecordType, null if it does not exit.
262 static const RecordType *getRecordType(QualType QT) {
263 if (const RecordType *RT = QT->getAs<RecordType>())
266 // Now check if we point to record type.
267 if (const PointerType *PT = QT->getAs<PointerType>())
268 return PT->getPointeeType()->getAs<RecordType>();
273 /// \brief Thread Safety Analysis: Checks that the passed in RecordType
274 /// resolves to a lockable object. May flag an error.
275 static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr,
277 const RecordType *RT = getRecordType(Ty);
279 // Warn if could not get record type for this argument.
281 S.Diag(Attr.getLoc(), diag::warn_attribute_argument_not_class)
282 << Attr.getName() << Ty.getAsString();
285 // Don't check for lockable if the class hasn't been defined yet.
286 if (RT->isIncompleteType())
288 // Warn if the type is not lockable.
289 if (!RT->getDecl()->getAttr<LockableAttr>()) {
290 S.Diag(Attr.getLoc(), diag::warn_attribute_argument_not_lockable)
291 << Attr.getName() << Ty.getAsString();
296 /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting
297 /// from Sidx, resolve to a lockable object. May flag an error.
298 /// \param Sidx The attribute argument index to start checking with.
299 /// \param ParamIdxOk Whether an argument can be indexing into a function
301 static bool checkAttrArgsAreLockableObjs(Sema &S, Decl *D,
302 const AttributeList &Attr,
303 SmallVectorImpl<Expr*> &Args,
305 bool ParamIdxOk = false) {
306 for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) {
307 Expr *ArgExp = Attr.getArg(Idx);
309 if (ArgExp->isTypeDependent()) {
310 // FIXME -- need to processs this again on template instantiation
311 Args.push_back(ArgExp);
315 QualType ArgTy = ArgExp->getType();
317 // First see if we can just cast to record type, or point to record type.
318 const RecordType *RT = getRecordType(ArgTy);
320 // Now check if we index into a record type function param.
321 if(!RT && ParamIdxOk) {
322 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
323 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
325 unsigned int NumParams = FD->getNumParams();
326 llvm::APInt ArgValue = IL->getValue();
327 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
328 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
329 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
330 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
331 << Attr.getName() << Idx + 1 << NumParams;
334 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
338 checkForLockableRecord(S, D, Attr, ArgTy);
340 Args.push_back(ArgExp);
345 //===----------------------------------------------------------------------===//
346 // Attribute Implementations
347 //===----------------------------------------------------------------------===//
349 // FIXME: All this manual attribute parsing code is gross. At the
350 // least add some helper functions to check most argument patterns (#
351 // and types of args).
353 static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
354 bool pointer = false) {
355 assert(!Attr.isInvalid());
357 if (!checkAttributeNumArgs(S, Attr, 0))
360 // D must be either a member field or global (potentially shared) variable.
361 if (!mayBeSharedVariable(D)) {
362 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
363 << Attr.getName() << ExpectedFieldOrGlobalVar;
367 if (pointer && !checkIsPointer(S, D, Attr))
371 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
373 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
376 static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
377 bool pointer = false) {
378 assert(!Attr.isInvalid());
380 if (!checkAttributeNumArgs(S, Attr, 1))
383 Expr *Arg = Attr.getArg(0);
385 // D must be either a member field or global (potentially shared) variable.
386 if (!mayBeSharedVariable(D)) {
387 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
388 << Attr.getName() << ExpectedFieldOrGlobalVar;
392 if (pointer && !checkIsPointer(S, D, Attr))
395 if (!Arg->isTypeDependent()) {
396 checkForLockableRecord(S, D, Attr, Arg->getType());
400 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
403 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
407 static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
408 bool scoped = false) {
409 assert(!Attr.isInvalid());
411 if (!checkAttributeNumArgs(S, Attr, 0))
414 // FIXME: Lockable structs for C code.
415 if (!isa<CXXRecordDecl>(D)) {
416 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
417 << Attr.getName() << ExpectedClass;
422 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
424 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
427 static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
428 const AttributeList &Attr) {
429 assert(!Attr.isInvalid());
431 if (!checkAttributeNumArgs(S, Attr, 0))
434 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
436 << Attr.getName() << ExpectedFunctionOrMethod;
440 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
444 static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
445 const AttributeList &Attr) {
446 assert(!Attr.isInvalid());
448 if (!checkAttributeNumArgs(S, Attr, 0))
451 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
452 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
453 << Attr.getName() << ExpectedFunctionOrMethod;
457 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
461 static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
463 assert(!Attr.isInvalid());
465 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
468 // D must be either a member field or global (potentially shared) variable.
469 ValueDecl *VD = dyn_cast<ValueDecl>(D);
470 if (!VD || !mayBeSharedVariable(D)) {
471 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
472 << Attr.getName() << ExpectedFieldOrGlobalVar;
476 // Check that this attribute only applies to lockable types
477 QualType QT = VD->getType();
478 if (!QT->isDependentType()) {
479 const RecordType *RT = getRecordType(QT);
480 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
481 S.Diag(Attr.getLoc(), diag::warn_attribute_decl_not_lockable)
487 SmallVector<Expr*, 1> Args;
488 // check that all arguments are lockable objects
489 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
492 unsigned Size = Args.size();
493 assert(Size == Attr.getNumArgs());
494 Expr **StartArg = Size == 0 ? 0 : &Args[0];
497 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
500 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
504 static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
505 bool exclusive = false) {
506 assert(!Attr.isInvalid());
508 // zero or more arguments ok
510 // check that the attribute is applied to a function
511 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
512 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
513 << Attr.getName() << ExpectedFunctionOrMethod;
517 // check that all arguments are lockable objects
518 SmallVector<Expr*, 1> Args;
519 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
522 unsigned Size = Args.size();
523 assert(Size == Attr.getNumArgs());
524 Expr **StartArg = Size == 0 ? 0 : &Args[0];
527 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
531 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
536 static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
537 bool exclusive = false) {
538 assert(!Attr.isInvalid());
540 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
544 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
545 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
546 << Attr.getName() << ExpectedFunctionOrMethod;
550 if (!isIntOrBool(Attr.getArg(0))) {
551 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
556 SmallVector<Expr*, 2> Args;
557 // check that all arguments are lockable objects
558 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1))
561 unsigned Size = Args.size();
562 Expr **StartArg = Size == 0 ? 0 : &Args[0];
565 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
570 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
576 static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
577 bool exclusive = false) {
578 assert(!Attr.isInvalid());
580 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
583 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
585 << Attr.getName() << ExpectedFunctionOrMethod;
589 // check that all arguments are lockable objects
590 SmallVector<Expr*, 1> Args;
591 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
594 unsigned Size = Args.size();
595 assert(Size == Attr.getNumArgs());
596 Expr **StartArg = Size == 0 ? 0 : &Args[0];
599 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
603 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
608 static void handleUnlockFunAttr(Sema &S, Decl *D,
609 const AttributeList &Attr) {
610 assert(!Attr.isInvalid());
612 // zero or more arguments ok
614 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
615 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
616 << Attr.getName() << ExpectedFunctionOrMethod;
620 // check that all arguments are lockable objects
621 SmallVector<Expr*, 1> Args;
622 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true))
625 unsigned Size = Args.size();
626 assert(Size == Attr.getNumArgs());
627 Expr **StartArg = Size == 0 ? 0 : &Args[0];
629 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
633 static void handleLockReturnedAttr(Sema &S, Decl *D,
634 const AttributeList &Attr) {
635 assert(!Attr.isInvalid());
637 if (!checkAttributeNumArgs(S, Attr, 1))
639 Expr *Arg = Attr.getArg(0);
641 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
642 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
643 << Attr.getName() << ExpectedFunctionOrMethod;
647 if (Arg->isTypeDependent())
650 // check that the argument is lockable object
651 checkForLockableRecord(S, D, Attr, Arg->getType());
653 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
656 static void handleLocksExcludedAttr(Sema &S, Decl *D,
657 const AttributeList &Attr) {
658 assert(!Attr.isInvalid());
660 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
663 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
664 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
665 << Attr.getName() << ExpectedFunctionOrMethod;
669 // check that all arguments are lockable objects
670 SmallVector<Expr*, 1> Args;
671 if (!checkAttrArgsAreLockableObjs(S, D, Attr, Args))
674 unsigned Size = Args.size();
675 assert(Size == Attr.getNumArgs());
676 Expr **StartArg = Size == 0 ? 0 : &Args[0];
678 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
683 static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
684 const AttributeList &Attr) {
685 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
687 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
691 QualType curType = tDecl->getUnderlyingType();
695 // Special case where the argument is a template id.
696 if (Attr.getParameterName()) {
698 SourceLocation TemplateKWLoc;
700 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
702 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
704 if (Size.isInvalid())
707 sizeExpr = Size.get();
709 // check the attribute arguments.
710 if (!checkAttributeNumArgs(S, Attr, 1))
713 sizeExpr = Attr.getArg(0);
716 // Instantiate/Install the vector type, and let Sema build the type for us.
717 // This will run the reguired checks.
718 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
720 // FIXME: preserve the old source info.
721 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
723 // Remember this typedef decl, we will need it later for diagnostics.
724 S.ExtVectorDecls.push_back(tDecl);
728 static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
729 // check the attribute arguments.
730 if (!checkAttributeNumArgs(S, Attr, 0))
733 if (TagDecl *TD = dyn_cast<TagDecl>(D))
734 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
735 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
736 // If the alignment is less than or equal to 8 bits, the packed attribute
738 if (!FD->getType()->isIncompleteType() &&
739 S.Context.getTypeAlign(FD->getType()) <= 8)
740 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
741 << Attr.getName() << FD->getType();
743 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
745 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
748 static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
749 if (TagDecl *TD = dyn_cast<TagDecl>(D))
750 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
752 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
755 static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
756 // check the attribute arguments.
757 if (!checkAttributeNumArgs(S, Attr, 0))
760 // The IBAction attributes only apply to instance methods.
761 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
762 if (MD->isInstanceMethod()) {
763 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
767 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
770 static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
771 // The IBOutlet/IBOutletCollection attributes only apply to instance
772 // variables or properties of Objective-C classes. The outlet must also
773 // have an object reference type.
774 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
775 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
776 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
777 << Attr.getName() << VD->getType() << 0;
781 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
782 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
783 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
784 << Attr.getName() << PD->getType() << 1;
789 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
796 static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
797 // check the attribute arguments.
798 if (!checkAttributeNumArgs(S, Attr, 0))
801 if (!checkIBOutletCommon(S, D, Attr))
804 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
807 static void handleIBOutletCollection(Sema &S, Decl *D,
808 const AttributeList &Attr) {
810 // The iboutletcollection attribute can have zero or one arguments.
811 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
812 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
816 if (!checkIBOutletCommon(S, D, Attr))
819 IdentifierInfo *II = Attr.getParameterName();
821 II = &S.Context.Idents.get("NSObject");
823 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
824 S.getScopeForContext(D->getDeclContext()->getParent()));
826 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
829 QualType QT = TypeRep.get();
830 // Diagnose use of non-object type in iboutletcollection attribute.
831 // FIXME. Gnu attribute extension ignores use of builtin types in
832 // attributes. So, __attribute__((iboutletcollection(char))) will be
833 // treated as __attribute__((iboutletcollection())).
834 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
835 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
838 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
839 QT, Attr.getParameterLoc()));
842 static void possibleTransparentUnionPointerType(QualType &T) {
843 if (const RecordType *UT = T->getAsUnionType())
844 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
845 RecordDecl *UD = UT->getDecl();
846 for (RecordDecl::field_iterator it = UD->field_begin(),
847 itend = UD->field_end(); it != itend; ++it) {
848 QualType QT = it->getType();
849 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
857 static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
858 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
860 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
861 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
862 << Attr.getName() << ExpectedFunction;
866 // In C++ the implicit 'this' function parameter also counts, and they are
868 bool HasImplicitThisParam = isInstanceMethod(D);
869 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
871 // The nonnull attribute only applies to pointers.
872 SmallVector<unsigned, 10> NonNullArgs;
874 for (AttributeList::arg_iterator I=Attr.arg_begin(),
875 E=Attr.arg_end(); I!=E; ++I) {
878 // The argument must be an integer constant expression.
880 llvm::APSInt ArgNum(32);
881 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
882 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
883 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
884 << "nonnull" << Ex->getSourceRange();
888 unsigned x = (unsigned) ArgNum.getZExtValue();
890 if (x < 1 || x > NumArgs) {
891 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
892 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
897 if (HasImplicitThisParam) {
899 S.Diag(Attr.getLoc(),
900 diag::err_attribute_invalid_implicit_this_argument)
901 << "nonnull" << Ex->getSourceRange();
907 // Is the function argument a pointer type?
908 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
909 possibleTransparentUnionPointerType(T);
911 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
912 // FIXME: Should also highlight argument in decl.
913 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
914 << "nonnull" << Ex->getSourceRange();
918 NonNullArgs.push_back(x);
921 // If no arguments were specified to __attribute__((nonnull)) then all pointer
922 // arguments have a nonnull attribute.
923 if (NonNullArgs.empty()) {
924 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
925 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
926 possibleTransparentUnionPointerType(T);
927 if (T->isAnyPointerType() || T->isBlockPointerType())
928 NonNullArgs.push_back(I);
931 // No pointer arguments?
932 if (NonNullArgs.empty()) {
933 // Warn the trivial case only if attribute is not coming from a
934 // macro instantiation.
935 if (Attr.getLoc().isFileID())
936 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
941 unsigned* start = &NonNullArgs[0];
942 unsigned size = NonNullArgs.size();
943 llvm::array_pod_sort(start, start + size);
944 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
948 static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
949 // This attribute must be applied to a function declaration.
950 // The first argument to the attribute must be a string,
951 // the name of the resource, for example "malloc".
952 // The following arguments must be argument indexes, the arguments must be
953 // of integer type for Returns, otherwise of pointer type.
954 // The difference between Holds and Takes is that a pointer may still be used
955 // after being held. free() should be __attribute((ownership_takes)), whereas
956 // a list append function may well be __attribute((ownership_holds)).
958 if (!AL.getParameterName()) {
959 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
960 << AL.getName()->getName() << 1;
963 // Figure out our Kind, and check arguments while we're at it.
964 OwnershipAttr::OwnershipKind K;
965 switch (AL.getKind()) {
966 case AttributeList::AT_ownership_takes:
967 K = OwnershipAttr::Takes;
968 if (AL.getNumArgs() < 1) {
969 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
973 case AttributeList::AT_ownership_holds:
974 K = OwnershipAttr::Holds;
975 if (AL.getNumArgs() < 1) {
976 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
980 case AttributeList::AT_ownership_returns:
981 K = OwnershipAttr::Returns;
982 if (AL.getNumArgs() > 1) {
983 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
984 << AL.getNumArgs() + 1;
989 // This should never happen given how we are called.
990 llvm_unreachable("Unknown ownership attribute");
993 if (!isFunction(D) || !hasFunctionProto(D)) {
994 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
995 << AL.getName() << ExpectedFunction;
999 // In C++ the implicit 'this' function parameter also counts, and they are
1000 // counted from one.
1001 bool HasImplicitThisParam = isInstanceMethod(D);
1002 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
1004 StringRef Module = AL.getParameterName()->getName();
1006 // Normalize the argument, __foo__ becomes foo.
1007 if (Module.startswith("__") && Module.endswith("__"))
1008 Module = Module.substr(2, Module.size() - 4);
1010 SmallVector<unsigned, 10> OwnershipArgs;
1012 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1016 llvm::APSInt ArgNum(32);
1017 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1018 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1019 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1020 << AL.getName()->getName() << IdxExpr->getSourceRange();
1024 unsigned x = (unsigned) ArgNum.getZExtValue();
1026 if (x > NumArgs || x < 1) {
1027 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1028 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1032 if (HasImplicitThisParam) {
1034 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1035 << "ownership" << IdxExpr->getSourceRange();
1042 case OwnershipAttr::Takes:
1043 case OwnershipAttr::Holds: {
1044 // Is the function argument a pointer type?
1045 QualType T = getFunctionOrMethodArgType(D, x);
1046 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1047 // FIXME: Should also highlight argument in decl.
1048 S.Diag(AL.getLoc(), diag::err_ownership_type)
1049 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
1051 << IdxExpr->getSourceRange();
1056 case OwnershipAttr::Returns: {
1057 if (AL.getNumArgs() > 1) {
1058 // Is the function argument an integer type?
1059 Expr *IdxExpr = AL.getArg(0);
1060 llvm::APSInt ArgNum(32);
1061 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1062 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1063 S.Diag(AL.getLoc(), diag::err_ownership_type)
1064 << "ownership_returns" << "integer"
1065 << IdxExpr->getSourceRange();
1073 // Check we don't have a conflict with another ownership attribute.
1074 for (specific_attr_iterator<OwnershipAttr>
1075 i = D->specific_attr_begin<OwnershipAttr>(),
1076 e = D->specific_attr_end<OwnershipAttr>();
1078 if ((*i)->getOwnKind() != K) {
1079 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1082 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1083 << AL.getName()->getName() << "ownership_*";
1088 OwnershipArgs.push_back(x);
1091 unsigned* start = OwnershipArgs.data();
1092 unsigned size = OwnershipArgs.size();
1093 llvm::array_pod_sort(start, start + size);
1095 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1096 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1100 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
1104 /// Whether this declaration has internal linkage for the purposes of
1105 /// things that want to complain about things not have internal linkage.
1106 static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1107 switch (D->getLinkage()) {
1109 case InternalLinkage:
1112 // Template instantiations that go from external to unique-external
1113 // shouldn't get diagnosed.
1114 case UniqueExternalLinkage:
1117 case ExternalLinkage:
1120 llvm_unreachable("unknown linkage kind!");
1123 static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1124 // Check the attribute arguments.
1125 if (Attr.getNumArgs() > 1) {
1126 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1130 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1132 << Attr.getName() << ExpectedVariableOrFunction;
1136 NamedDecl *nd = cast<NamedDecl>(D);
1140 // static int a __attribute__((weakref ("v2")));
1141 // static int b() __attribute__((weakref ("f3")));
1143 // and ignores the attributes of
1145 // static int a __attribute__((weakref ("v2")));
1148 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
1149 if (!Ctx->isFileContext()) {
1150 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
1151 nd->getNameAsString();
1155 // The GCC manual says
1157 // At present, a declaration to which `weakref' is attached can only
1162 // Without a TARGET,
1163 // given as an argument to `weakref' or to `alias', `weakref' is
1164 // equivalent to `weak'.
1166 // gcc 4.4.1 will accept
1167 // int a7 __attribute__((weakref));
1169 // int a7 __attribute__((weak));
1170 // This looks like a bug in gcc. We reject that for now. We should revisit
1171 // it if this behaviour is actually used.
1173 if (!hasEffectivelyInternalLinkage(nd)) {
1174 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
1179 // static ((alias ("y"), weakref)).
1180 // Should we? How to check that weakref is before or after alias?
1182 if (Attr.getNumArgs() == 1) {
1183 Expr *Arg = Attr.getArg(0);
1184 Arg = Arg->IgnoreParenCasts();
1185 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1187 if (!Str || !Str->isAscii()) {
1188 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1192 // GCC will accept anything as the argument of weakref. Should we
1193 // check for an existing decl?
1194 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
1198 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
1201 static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1202 // check the attribute arguments.
1203 if (Attr.getNumArgs() != 1) {
1204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1208 Expr *Arg = Attr.getArg(0);
1209 Arg = Arg->IgnoreParenCasts();
1210 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1212 if (!Str || !Str->isAscii()) {
1213 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1218 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
1219 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1223 // FIXME: check if target symbol exists in current file
1225 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
1229 static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1230 // Check the attribute arguments.
1231 if (!checkAttributeNumArgs(S, Attr, 0))
1234 if (!isa<FunctionDecl>(D)) {
1235 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1236 << Attr.getName() << ExpectedFunction;
1240 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
1243 static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1244 const AttributeList &Attr) {
1245 // Check the attribute arguments.
1246 if (Attr.hasParameterOrArguments()) {
1247 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1251 if (!isa<FunctionDecl>(D)) {
1252 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1253 << Attr.getName() << ExpectedFunction;
1257 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
1260 static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1261 // Check the attribute arguments.
1262 if (Attr.hasParameterOrArguments()) {
1263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1267 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1268 QualType RetTy = FD->getResultType();
1269 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
1270 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
1275 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
1278 static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1279 // check the attribute arguments.
1280 if (!checkAttributeNumArgs(S, Attr, 0))
1283 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
1286 static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1287 assert(!Attr.isInvalid());
1288 if (isa<VarDecl>(D))
1289 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
1291 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1292 << Attr.getName() << ExpectedVariable;
1295 static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1296 assert(!Attr.isInvalid());
1297 if (isa<VarDecl>(D))
1298 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
1300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1301 << Attr.getName() << ExpectedVariable;
1304 static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
1305 if (hasDeclarator(D)) return;
1307 if (S.CheckNoReturnAttr(attr)) return;
1309 if (!isa<ObjCMethodDecl>(D)) {
1310 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1311 << attr.getName() << ExpectedFunctionOrMethod;
1315 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
1318 bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
1319 if (attr.hasParameterOrArguments()) {
1320 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1328 static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1329 const AttributeList &Attr) {
1331 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1332 // because 'analyzer_noreturn' does not impact the type.
1334 if(!checkAttributeNumArgs(S, Attr, 0))
1337 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1338 ValueDecl *VD = dyn_cast<ValueDecl>(D);
1339 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1340 && !VD->getType()->isFunctionPointerType())) {
1341 S.Diag(Attr.getLoc(),
1342 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1343 : diag::warn_attribute_wrong_decl_type)
1344 << Attr.getName() << ExpectedFunctionMethodOrBlock;
1349 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
1352 // PS3 PPU-specific.
1353 static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1355 Returning a Vector Class in Registers
1357 According to the PPU ABI specifications, a class with a single member of
1358 vector type is returned in memory when used as the return value of a function.
1359 This results in inefficient code when implementing vector classes. To return
1360 the value in a single vector register, add the vecreturn attribute to the
1361 class definition. This attribute is also applicable to struct types.
1367 __vector float xyzw;
1368 } __attribute__((vecreturn));
1370 Vector Add(Vector lhs, Vector rhs)
1373 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1374 return result; // This will be returned in a register
1377 if (!isa<RecordDecl>(D)) {
1378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1379 << Attr.getName() << ExpectedClass;
1383 if (D->getAttr<VecReturnAttr>()) {
1384 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1388 RecordDecl *record = cast<RecordDecl>(D);
1391 if (!isa<CXXRecordDecl>(record)) {
1392 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1396 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1397 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1401 for (RecordDecl::field_iterator iter = record->field_begin();
1402 iter != record->field_end(); iter++) {
1403 if ((count == 1) || !iter->getType()->isVectorType()) {
1404 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1410 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
1413 static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1414 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
1415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1416 << Attr.getName() << ExpectedFunctionMethodOrParameter;
1419 // FIXME: Actually store the attribute on the declaration
1422 static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1423 // check the attribute arguments.
1424 if (Attr.hasParameterOrArguments()) {
1425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1429 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1430 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
1431 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1432 << Attr.getName() << ExpectedVariableFunctionOrLabel;
1436 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
1439 static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1440 const AttributeList &Attr) {
1441 // check the attribute arguments.
1442 if (Attr.hasParameterOrArguments()) {
1443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1447 if (!isa<FunctionDecl>(D)) {
1448 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1449 << Attr.getName() << ExpectedFunction;
1453 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1456 static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1457 // check the attribute arguments.
1458 if (Attr.hasParameterOrArguments()) {
1459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1463 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1464 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
1465 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1468 } else if (!isFunctionOrMethod(D)) {
1469 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1470 << Attr.getName() << ExpectedVariableOrFunction;
1474 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
1477 static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1478 // check the attribute arguments.
1479 if (Attr.getNumArgs() > 1) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
1484 int priority = 65535; // FIXME: Do not hardcode such constants.
1485 if (Attr.getNumArgs() > 0) {
1486 Expr *E = Attr.getArg(0);
1487 llvm::APSInt Idx(32);
1488 if (E->isTypeDependent() || E->isValueDependent() ||
1489 !E->isIntegerConstantExpr(Idx, S.Context)) {
1490 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1491 << "constructor" << 1 << E->getSourceRange();
1494 priority = Idx.getZExtValue();
1497 if (!isa<FunctionDecl>(D)) {
1498 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1499 << Attr.getName() << ExpectedFunction;
1503 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
1507 static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1508 // check the attribute arguments.
1509 if (Attr.getNumArgs() > 1) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
1514 int priority = 65535; // FIXME: Do not hardcode such constants.
1515 if (Attr.getNumArgs() > 0) {
1516 Expr *E = Attr.getArg(0);
1517 llvm::APSInt Idx(32);
1518 if (E->isTypeDependent() || E->isValueDependent() ||
1519 !E->isIntegerConstantExpr(Idx, S.Context)) {
1520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1521 << "destructor" << 1 << E->getSourceRange();
1524 priority = Idx.getZExtValue();
1527 if (!isa<FunctionDecl>(D)) {
1528 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1529 << Attr.getName() << ExpectedFunction;
1533 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
1537 static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1538 unsigned NumArgs = Attr.getNumArgs();
1540 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
1544 // Handle the case where deprecated attribute has a text message.
1547 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
1549 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1553 Str = SE->getString();
1556 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
1559 static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1560 unsigned NumArgs = Attr.getNumArgs();
1562 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
1566 // Handle the case where unavailable attribute has a text message.
1569 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
1571 S.Diag(Attr.getArg(0)->getLocStart(),
1572 diag::err_attribute_not_string) << "unavailable";
1575 Str = SE->getString();
1577 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
1580 static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1581 const AttributeList &Attr) {
1582 unsigned NumArgs = Attr.getNumArgs();
1584 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1588 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1589 Attr.getRange(), S.Context));
1592 static void handleObjCRootClassAttr(Sema &S, Decl *D,
1593 const AttributeList &Attr) {
1594 if (!isa<ObjCInterfaceDecl>(D)) {
1595 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1599 unsigned NumArgs = Attr.getNumArgs();
1601 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1605 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1608 static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
1609 const AttributeList &Attr) {
1610 if (!isa<ObjCInterfaceDecl>(D)) {
1611 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1615 unsigned NumArgs = Attr.getNumArgs();
1617 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1621 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
1622 Attr.getRange(), S.Context));
1625 static void handleAvailabilityAttr(Sema &S, Decl *D,
1626 const AttributeList &Attr) {
1627 IdentifierInfo *Platform = Attr.getParameterName();
1628 SourceLocation PlatformLoc = Attr.getParameterLoc();
1630 StringRef PlatformName
1631 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1632 if (PlatformName.empty()) {
1633 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1636 PlatformName = Platform->getName();
1639 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1640 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1641 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
1642 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
1644 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
1645 // of these steps are needed).
1646 if (Introduced.isValid() && Deprecated.isValid() &&
1647 !(Introduced.Version <= Deprecated.Version)) {
1648 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1649 << 1 << PlatformName << Deprecated.Version.getAsString()
1650 << 0 << Introduced.Version.getAsString();
1654 if (Introduced.isValid() && Obsoleted.isValid() &&
1655 !(Introduced.Version <= Obsoleted.Version)) {
1656 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1657 << 2 << PlatformName << Obsoleted.Version.getAsString()
1658 << 0 << Introduced.Version.getAsString();
1662 if (Deprecated.isValid() && Obsoleted.isValid() &&
1663 !(Deprecated.Version <= Obsoleted.Version)) {
1664 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1665 << 2 << PlatformName << Obsoleted.Version.getAsString()
1666 << 1 << Deprecated.Version.getAsString();
1671 const StringLiteral *SE =
1672 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1674 Str = SE->getString();
1676 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
1685 static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1686 // check the attribute arguments.
1687 if(!checkAttributeNumArgs(S, Attr, 1))
1690 Expr *Arg = Attr.getArg(0);
1691 Arg = Arg->IgnoreParenCasts();
1692 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1694 if (!Str || !Str->isAscii()) {
1695 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1696 << "visibility" << 1;
1700 StringRef TypeStr = Str->getString();
1701 VisibilityAttr::VisibilityType type;
1703 if (TypeStr == "default")
1704 type = VisibilityAttr::Default;
1705 else if (TypeStr == "hidden")
1706 type = VisibilityAttr::Hidden;
1707 else if (TypeStr == "internal")
1708 type = VisibilityAttr::Hidden; // FIXME
1709 else if (TypeStr == "protected") {
1710 // Complain about attempts to use protected visibility on targets
1711 // (like Darwin) that don't support it.
1712 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1713 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1714 type = VisibilityAttr::Default;
1716 type = VisibilityAttr::Protected;
1719 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
1723 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
1726 static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1727 const AttributeList &Attr) {
1728 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
1735 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1736 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1738 << "objc_method_family" << 1;
1740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1746 StringRef param = Attr.getParameterName()->getName();
1747 ObjCMethodFamilyAttr::FamilyKind family;
1748 if (param == "none")
1749 family = ObjCMethodFamilyAttr::OMF_None;
1750 else if (param == "alloc")
1751 family = ObjCMethodFamilyAttr::OMF_alloc;
1752 else if (param == "copy")
1753 family = ObjCMethodFamilyAttr::OMF_copy;
1754 else if (param == "init")
1755 family = ObjCMethodFamilyAttr::OMF_init;
1756 else if (param == "mutableCopy")
1757 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1758 else if (param == "new")
1759 family = ObjCMethodFamilyAttr::OMF_new;
1761 // Just warn and ignore it. This is future-proof against new
1762 // families being used in system headers.
1763 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
1767 if (family == ObjCMethodFamilyAttr::OMF_init &&
1768 !method->getResultType()->isObjCObjectPointerType()) {
1769 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1770 << method->getResultType();
1771 // Ignore the attribute.
1775 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
1776 S.Context, family));
1779 static void handleObjCExceptionAttr(Sema &S, Decl *D,
1780 const AttributeList &Attr) {
1781 if (!checkAttributeNumArgs(S, Attr, 0))
1784 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1786 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1790 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
1793 static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
1794 if (Attr.getNumArgs() != 0) {
1795 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1798 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
1799 QualType T = TD->getUnderlyingType();
1800 if (!T->isPointerType() ||
1801 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
1802 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1806 else if (!isa<ObjCPropertyDecl>(D)) {
1807 // It is okay to include this attribute on properties, e.g.:
1809 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1811 // In this case it follows tradition and suppresses an error in the above
1813 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
1815 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
1819 handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1820 if (Attr.getNumArgs() != 0) {
1821 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1825 if (!isa<FunctionDecl>(D)) {
1826 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1830 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
1833 static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1834 if (!Attr.getParameterName()) {
1835 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1840 if (Attr.getNumArgs() != 0) {
1841 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1845 BlocksAttr::BlockType type;
1846 if (Attr.getParameterName()->isStr("byref"))
1847 type = BlocksAttr::ByRef;
1849 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1850 << "blocks" << Attr.getParameterName();
1854 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
1857 static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1858 // check the attribute arguments.
1859 if (Attr.getNumArgs() > 2) {
1860 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
1864 unsigned sentinel = 0;
1865 if (Attr.getNumArgs() > 0) {
1866 Expr *E = Attr.getArg(0);
1867 llvm::APSInt Idx(32);
1868 if (E->isTypeDependent() || E->isValueDependent() ||
1869 !E->isIntegerConstantExpr(Idx, S.Context)) {
1870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1871 << "sentinel" << 1 << E->getSourceRange();
1875 if (Idx.isSigned() && Idx.isNegative()) {
1876 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1877 << E->getSourceRange();
1881 sentinel = Idx.getZExtValue();
1884 unsigned nullPos = 0;
1885 if (Attr.getNumArgs() > 1) {
1886 Expr *E = Attr.getArg(1);
1887 llvm::APSInt Idx(32);
1888 if (E->isTypeDependent() || E->isValueDependent() ||
1889 !E->isIntegerConstantExpr(Idx, S.Context)) {
1890 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1891 << "sentinel" << 2 << E->getSourceRange();
1894 nullPos = Idx.getZExtValue();
1896 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
1897 // FIXME: This error message could be improved, it would be nice
1898 // to say what the bounds actually are.
1899 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1900 << E->getSourceRange();
1905 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1906 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
1907 if (isa<FunctionNoProtoType>(FT)) {
1908 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1912 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
1913 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
1916 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
1917 if (!MD->isVariadic()) {
1918 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
1921 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1922 if (!BD->isVariadic()) {
1923 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1926 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
1927 QualType Ty = V->getType();
1928 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
1929 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
1930 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
1931 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
1932 int m = Ty->isFunctionPointerType() ? 0 : 1;
1933 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
1937 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1938 << Attr.getName() << ExpectedFunctionMethodOrBlock;
1942 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1943 << Attr.getName() << ExpectedFunctionMethodOrBlock;
1946 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
1950 static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
1951 // check the attribute arguments.
1952 if (!checkAttributeNumArgs(S, Attr, 0))
1955 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
1956 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1957 << Attr.getName() << ExpectedFunctionOrMethod;
1961 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1962 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1963 << Attr.getName() << 0;
1966 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1967 if (MD->getResultType()->isVoidType()) {
1968 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1969 << Attr.getName() << 1;
1973 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
1976 static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
1977 // check the attribute arguments.
1978 if (Attr.hasParameterOrArguments()) {
1979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1983 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1984 if (isa<CXXRecordDecl>(D)) {
1985 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
1988 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1989 << Attr.getName() << ExpectedVariableOrFunction;
1993 NamedDecl *nd = cast<NamedDecl>(D);
1995 // 'weak' only applies to declarations with external linkage.
1996 if (hasEffectivelyInternalLinkage(nd)) {
1997 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
2001 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
2004 static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2005 // check the attribute arguments.
2006 if (!checkAttributeNumArgs(S, Attr, 0))
2010 // weak_import only applies to variable & function declarations.
2012 if (!D->canBeWeakImported(isDef)) {
2014 S.Diag(Attr.getLoc(),
2015 diag::warn_attribute_weak_import_invalid_on_definition)
2016 << "weak_import" << 2 /*variable and function*/;
2017 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
2018 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
2019 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
2020 // Nothing to warn about here.
2022 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2023 << Attr.getName() << ExpectedVariableOrFunction;
2028 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
2031 static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2032 const AttributeList &Attr) {
2033 // Attribute has 3 arguments.
2034 if (!checkAttributeNumArgs(S, Attr, 3))
2038 for (unsigned i = 0; i < 3; ++i) {
2039 Expr *E = Attr.getArg(i);
2040 llvm::APSInt ArgNum(32);
2041 if (E->isTypeDependent() || E->isValueDependent() ||
2042 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
2043 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2044 << "reqd_work_group_size" << E->getSourceRange();
2047 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2049 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
2050 WGSize[0], WGSize[1],
2054 static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2055 // Attribute has no arguments.
2056 if (!checkAttributeNumArgs(S, Attr, 1))
2059 // Make sure that there is a string literal as the sections's single
2061 Expr *ArgExpr = Attr.getArg(0);
2062 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
2064 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
2068 // If the target wants to validate the section specifier, make it happen.
2069 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
2070 if (!Error.empty()) {
2071 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2076 // This attribute cannot be applied to local variables.
2077 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2078 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2082 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
2087 static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2088 // check the attribute arguments.
2089 if (Attr.hasParameterOrArguments()) {
2090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2094 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
2095 if (Existing->getLocation().isInvalid())
2096 Existing->setRange(Attr.getRange());
2098 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
2102 static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2103 // check the attribute arguments.
2104 if (Attr.hasParameterOrArguments()) {
2105 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2109 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
2110 if (Existing->getLocation().isInvalid())
2111 Existing->setRange(Attr.getRange());
2113 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
2117 static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2118 // check the attribute arguments.
2119 if (!checkAttributeNumArgs(S, Attr, 0))
2122 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
2125 static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2126 if (!Attr.getParameterName()) {
2127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2131 if (Attr.getNumArgs() != 0) {
2132 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2136 VarDecl *VD = dyn_cast<VarDecl>(D);
2138 if (!VD || !VD->hasLocalStorage()) {
2139 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2143 // Look up the function
2144 // FIXME: Lookup probably isn't looking in the right place
2145 NamedDecl *CleanupDecl
2146 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2147 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
2149 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
2150 Attr.getParameterName();
2154 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2156 S.Diag(Attr.getParameterLoc(),
2157 diag::err_attribute_cleanup_arg_not_function)
2158 << Attr.getParameterName();
2162 if (FD->getNumParams() != 1) {
2163 S.Diag(Attr.getParameterLoc(),
2164 diag::err_attribute_cleanup_func_must_take_one_arg)
2165 << Attr.getParameterName();
2169 // We're currently more strict than GCC about what function types we accept.
2170 // If this ever proves to be a problem it should be easy to fix.
2171 QualType Ty = S.Context.getPointerType(VD->getType());
2172 QualType ParamTy = FD->getParamDecl(0)->getType();
2173 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2174 ParamTy, Ty) != Sema::Compatible) {
2175 S.Diag(Attr.getParameterLoc(),
2176 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2177 Attr.getParameterName() << ParamTy << Ty;
2181 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
2182 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
2185 /// Handle __attribute__((format_arg((idx)))) attribute based on
2186 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
2187 static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2188 if (!checkAttributeNumArgs(S, Attr, 1))
2191 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
2192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2193 << Attr.getName() << ExpectedFunction;
2197 // In C++ the implicit 'this' function parameter also counts, and they are
2198 // counted from one.
2199 bool HasImplicitThisParam = isInstanceMethod(D);
2200 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
2201 unsigned FirstIdx = 1;
2203 // checks for the 2nd argument
2204 Expr *IdxExpr = Attr.getArg(0);
2205 llvm::APSInt Idx(32);
2206 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2207 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
2208 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2209 << "format" << 2 << IdxExpr->getSourceRange();
2213 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2214 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2215 << "format" << 2 << IdxExpr->getSourceRange();
2219 unsigned ArgIdx = Idx.getZExtValue() - 1;
2221 if (HasImplicitThisParam) {
2223 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2224 << "format_arg" << IdxExpr->getSourceRange();
2230 // make sure the format string is really a string
2231 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
2233 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2234 if (not_nsstring_type &&
2235 !isCFStringType(Ty, S.Context) &&
2236 (!Ty->isPointerType() ||
2237 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
2238 // FIXME: Should highlight the actual expression that has the wrong type.
2239 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2240 << (not_nsstring_type ? "a string type" : "an NSString")
2241 << IdxExpr->getSourceRange();
2244 Ty = getFunctionOrMethodResultType(D);
2245 if (!isNSStringType(Ty, S.Context) &&
2246 !isCFStringType(Ty, S.Context) &&
2247 (!Ty->isPointerType() ||
2248 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
2249 // FIXME: Should highlight the actual expression that has the wrong type.
2250 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
2251 << (not_nsstring_type ? "string type" : "NSString")
2252 << IdxExpr->getSourceRange();
2256 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
2257 Idx.getZExtValue()));
2260 enum FormatAttrKind {
2269 /// getFormatAttrKind - Map from format attribute names to supported format
2271 static FormatAttrKind getFormatAttrKind(StringRef Format) {
2272 // Check for formats that get handled specially.
2273 if (Format == "NSString")
2274 return NSStringFormat;
2275 if (Format == "CFString")
2276 return CFStringFormat;
2277 if (Format == "strftime")
2278 return StrftimeFormat;
2280 // Otherwise, check for supported formats.
2281 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
2282 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
2283 Format == "zcmn_err" ||
2284 Format == "kprintf") // OpenBSD.
2285 return SupportedFormat;
2287 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2288 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
2289 return IgnoredFormat;
2291 return InvalidFormat;
2294 /// Handle __attribute__((init_priority(priority))) attributes based on
2295 /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
2296 static void handleInitPriorityAttr(Sema &S, Decl *D,
2297 const AttributeList &Attr) {
2298 if (!S.getLangOpts().CPlusPlus) {
2299 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2303 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
2304 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2308 QualType T = dyn_cast<VarDecl>(D)->getType();
2309 if (S.Context.getAsArrayType(T))
2310 T = S.Context.getBaseElementType(T);
2311 if (!T->getAs<RecordType>()) {
2312 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2317 if (Attr.getNumArgs() != 1) {
2318 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2322 Expr *priorityExpr = Attr.getArg(0);
2324 llvm::APSInt priority(32);
2325 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2326 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2327 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2328 << "init_priority" << priorityExpr->getSourceRange();
2332 unsigned prioritynum = priority.getZExtValue();
2333 if (prioritynum < 101 || prioritynum > 65535) {
2334 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2335 << priorityExpr->getSourceRange();
2339 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
2343 /// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2344 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
2345 static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2347 if (!Attr.getParameterName()) {
2348 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2353 if (Attr.getNumArgs() != 2) {
2354 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
2358 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
2359 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2360 << Attr.getName() << ExpectedFunction;
2364 // In C++ the implicit 'this' function parameter also counts, and they are
2365 // counted from one.
2366 bool HasImplicitThisParam = isInstanceMethod(D);
2367 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
2368 unsigned FirstIdx = 1;
2370 StringRef Format = Attr.getParameterName()->getName();
2372 // Normalize the argument, __foo__ becomes foo.
2373 if (Format.startswith("__") && Format.endswith("__"))
2374 Format = Format.substr(2, Format.size() - 4);
2376 // Check for supported formats.
2377 FormatAttrKind Kind = getFormatAttrKind(Format);
2379 if (Kind == IgnoredFormat)
2382 if (Kind == InvalidFormat) {
2383 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
2384 << "format" << Attr.getParameterName()->getName();
2388 // checks for the 2nd argument
2389 Expr *IdxExpr = Attr.getArg(0);
2390 llvm::APSInt Idx(32);
2391 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2392 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
2393 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2394 << "format" << 2 << IdxExpr->getSourceRange();
2398 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2399 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2400 << "format" << 2 << IdxExpr->getSourceRange();
2404 // FIXME: Do we need to bounds check?
2405 unsigned ArgIdx = Idx.getZExtValue() - 1;
2407 if (HasImplicitThisParam) {
2409 S.Diag(Attr.getLoc(),
2410 diag::err_format_attribute_implicit_this_format_string)
2411 << IdxExpr->getSourceRange();
2417 // make sure the format string is really a string
2418 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
2420 if (Kind == CFStringFormat) {
2421 if (!isCFStringType(Ty, S.Context)) {
2422 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2423 << "a CFString" << IdxExpr->getSourceRange();
2426 } else if (Kind == NSStringFormat) {
2427 // FIXME: do we need to check if the type is NSString*? What are the
2429 if (!isNSStringType(Ty, S.Context)) {
2430 // FIXME: Should highlight the actual expression that has the wrong type.
2431 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2432 << "an NSString" << IdxExpr->getSourceRange();
2435 } else if (!Ty->isPointerType() ||
2436 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
2437 // FIXME: Should highlight the actual expression that has the wrong type.
2438 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2439 << "a string type" << IdxExpr->getSourceRange();
2443 // check the 3rd argument
2444 Expr *FirstArgExpr = Attr.getArg(1);
2445 llvm::APSInt FirstArg(32);
2446 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2447 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
2448 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2449 << "format" << 3 << FirstArgExpr->getSourceRange();
2453 // check if the function is variadic if the 3rd argument non-zero
2454 if (FirstArg != 0) {
2455 if (isFunctionOrMethodVariadic(D)) {
2456 ++NumArgs; // +1 for ...
2458 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
2463 // strftime requires FirstArg to be 0 because it doesn't read from any
2464 // variable the input is just the current time + the format string.
2465 if (Kind == StrftimeFormat) {
2466 if (FirstArg != 0) {
2467 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2468 << FirstArgExpr->getSourceRange();
2471 // if 0 it disables parameter checking (to use with e.g. va_list)
2472 } else if (FirstArg != 0 && FirstArg != NumArgs) {
2473 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2474 << "format" << 3 << FirstArgExpr->getSourceRange();
2478 // Check whether we already have an equivalent format attribute.
2479 for (specific_attr_iterator<FormatAttr>
2480 i = D->specific_attr_begin<FormatAttr>(),
2481 e = D->specific_attr_end<FormatAttr>();
2484 if (f->getType() == Format &&
2485 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2486 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2487 // If we don't have a valid location for this attribute, adopt the
2489 if (f->getLocation().isInvalid())
2490 f->setRange(Attr.getRange());
2495 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
2497 FirstArg.getZExtValue()));
2500 static void handleTransparentUnionAttr(Sema &S, Decl *D,
2501 const AttributeList &Attr) {
2502 // check the attribute arguments.
2503 if (!checkAttributeNumArgs(S, Attr, 0))
2507 // Try to find the underlying union declaration.
2509 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
2510 if (TD && TD->getUnderlyingType()->isUnionType())
2511 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2513 RD = dyn_cast<RecordDecl>(D);
2515 if (!RD || !RD->isUnion()) {
2516 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2517 << Attr.getName() << ExpectedUnion;
2521 if (!RD->isCompleteDefinition()) {
2522 S.Diag(Attr.getLoc(),
2523 diag::warn_transparent_union_attribute_not_definition);
2527 RecordDecl::field_iterator Field = RD->field_begin(),
2528 FieldEnd = RD->field_end();
2529 if (Field == FieldEnd) {
2530 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2534 FieldDecl *FirstField = *Field;
2535 QualType FirstType = FirstField->getType();
2536 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
2537 S.Diag(FirstField->getLocation(),
2538 diag::warn_transparent_union_attribute_floating)
2539 << FirstType->isVectorType() << FirstType;
2543 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2544 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2545 for (; Field != FieldEnd; ++Field) {
2546 QualType FieldType = Field->getType();
2547 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2548 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2549 // Warn if we drop the attribute.
2550 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
2551 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
2552 : S.Context.getTypeAlign(FieldType);
2553 S.Diag(Field->getLocation(),
2554 diag::warn_transparent_union_attribute_field_size_align)
2555 << isSize << Field->getDeclName() << FieldBits;
2556 unsigned FirstBits = isSize? FirstSize : FirstAlign;
2557 S.Diag(FirstField->getLocation(),
2558 diag::note_transparent_union_first_field_size_align)
2559 << isSize << FirstBits;
2564 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
2567 static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2568 // check the attribute arguments.
2569 if (!checkAttributeNumArgs(S, Attr, 1))
2572 Expr *ArgExpr = Attr.getArg(0);
2573 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
2575 // Make sure that there is a string literal as the annotation's single
2578 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
2582 // Don't duplicate annotations that are already set.
2583 for (specific_attr_iterator<AnnotateAttr>
2584 i = D->specific_attr_begin<AnnotateAttr>(),
2585 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2586 if ((*i)->getAnnotation() == SE->getString())
2589 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
2593 static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2594 // check the attribute arguments.
2595 if (Attr.getNumArgs() > 1) {
2596 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2600 //FIXME: The C++0x version of this attribute has more limited applicabilty
2601 // than GNU's, and should error out when it is used to specify a
2602 // weaker alignment, rather than being silently ignored.
2604 if (Attr.getNumArgs() == 0) {
2605 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
2609 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
2612 void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
2613 // FIXME: Handle pack-expansions here.
2614 if (DiagnoseUnexpandedParameterPack(E))
2617 if (E->isTypeDependent() || E->isValueDependent()) {
2618 // Save dependent expressions in the AST to be instantiated.
2619 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
2623 SourceLocation AttrLoc = AttrRange.getBegin();
2624 // FIXME: Cache the number on the Attr object?
2625 llvm::APSInt Alignment(32);
2627 VerifyIntegerConstantExpression(E, &Alignment,
2628 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2629 /*AllowFold*/ false);
2630 if (ICE.isInvalid())
2632 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
2633 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2634 << E->getSourceRange();
2638 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
2641 void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
2642 // FIXME: Cache the number on the Attr object if non-dependent?
2643 // FIXME: Perform checking of type validity
2644 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
2648 /// handleModeAttr - This attribute modifies the width of a decl with primitive
2651 /// Despite what would be logical, the mode attribute is a decl attribute, not a
2652 /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2653 /// HImode, not an intermediate pointer.
2654 static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2655 // This attribute isn't documented, but glibc uses it. It changes
2656 // the width of an int or unsigned int to the specified size.
2658 // Check that there aren't any arguments
2659 if (!checkAttributeNumArgs(S, Attr, 0))
2663 IdentifierInfo *Name = Attr.getParameterName();
2665 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
2669 StringRef Str = Attr.getParameterName()->getName();
2671 // Normalize the attribute name, __foo__ becomes foo.
2672 if (Str.startswith("__") && Str.endswith("__"))
2673 Str = Str.substr(2, Str.size() - 4);
2675 unsigned DestWidth = 0;
2676 bool IntegerMode = true;
2677 bool ComplexMode = false;
2678 switch (Str.size()) {
2681 case 'Q': DestWidth = 8; break;
2682 case 'H': DestWidth = 16; break;
2683 case 'S': DestWidth = 32; break;
2684 case 'D': DestWidth = 64; break;
2685 case 'X': DestWidth = 96; break;
2686 case 'T': DestWidth = 128; break;
2688 if (Str[1] == 'F') {
2689 IntegerMode = false;
2690 } else if (Str[1] == 'C') {
2691 IntegerMode = false;
2693 } else if (Str[1] != 'I') {
2698 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2699 // pointer on PIC16 and other embedded platforms.
2701 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
2702 else if (Str == "byte")
2703 DestWidth = S.Context.getTargetInfo().getCharWidth();
2706 if (Str == "pointer")
2707 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
2712 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
2713 OldTy = TD->getUnderlyingType();
2714 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2715 OldTy = VD->getType();
2717 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2718 << "mode" << Attr.getRange();
2722 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
2723 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2724 else if (IntegerMode) {
2725 if (!OldTy->isIntegralOrEnumerationType())
2726 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2727 } else if (ComplexMode) {
2728 if (!OldTy->isComplexType())
2729 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2731 if (!OldTy->isFloatingType())
2732 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2735 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2736 // and friends, at least with glibc.
2737 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2738 // width on unusual platforms.
2739 // FIXME: Make sure floating-point mappings are accurate
2740 // FIXME: Support XF and TF types
2742 switch (DestWidth) {
2744 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
2747 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2751 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2754 if (OldTy->isSignedIntegerType())
2755 NewTy = S.Context.SignedCharTy;
2757 NewTy = S.Context.UnsignedCharTy;
2761 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2764 if (OldTy->isSignedIntegerType())
2765 NewTy = S.Context.ShortTy;
2767 NewTy = S.Context.UnsignedShortTy;
2771 NewTy = S.Context.FloatTy;
2772 else if (OldTy->isSignedIntegerType())
2773 NewTy = S.Context.IntTy;
2775 NewTy = S.Context.UnsignedIntTy;
2779 NewTy = S.Context.DoubleTy;
2780 else if (OldTy->isSignedIntegerType())
2781 if (S.Context.getTargetInfo().getLongWidth() == 64)
2782 NewTy = S.Context.LongTy;
2784 NewTy = S.Context.LongLongTy;
2786 if (S.Context.getTargetInfo().getLongWidth() == 64)
2787 NewTy = S.Context.UnsignedLongTy;
2789 NewTy = S.Context.UnsignedLongLongTy;
2792 NewTy = S.Context.LongDoubleTy;
2796 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2799 if (OldTy->isSignedIntegerType())
2800 NewTy = S.Context.Int128Ty;
2802 NewTy = S.Context.UnsignedInt128Ty;
2807 NewTy = S.Context.getComplexType(NewTy);
2810 // Install the new type.
2811 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
2812 // FIXME: preserve existing source info.
2813 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
2815 cast<ValueDecl>(D)->setType(NewTy);
2818 static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2819 // check the attribute arguments.
2820 if (!checkAttributeNumArgs(S, Attr, 0))
2823 if (!isFunctionOrMethod(D)) {
2824 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2825 << Attr.getName() << ExpectedFunction;
2829 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
2832 static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2833 // check the attribute arguments.
2834 if (!checkAttributeNumArgs(S, Attr, 0))
2838 if (!isa<FunctionDecl>(D)) {
2839 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2840 << Attr.getName() << ExpectedFunction;
2844 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
2847 static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2848 const AttributeList &Attr) {
2849 // check the attribute arguments.
2850 if (!checkAttributeNumArgs(S, Attr, 0))
2854 if (!isa<FunctionDecl>(D)) {
2855 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2856 << Attr.getName() << ExpectedFunction;
2860 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
2864 static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2865 if (S.LangOpts.CUDA) {
2866 // check the attribute arguments.
2867 if (Attr.hasParameterOrArguments()) {
2868 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2872 if (!isa<VarDecl>(D)) {
2873 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2874 << Attr.getName() << ExpectedVariable;
2878 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
2880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2884 static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2885 if (S.LangOpts.CUDA) {
2886 // check the attribute arguments.
2887 if (Attr.getNumArgs() != 0) {
2888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2892 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
2893 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2894 << Attr.getName() << ExpectedVariableOrFunction;
2898 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
2900 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2904 static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2905 if (S.LangOpts.CUDA) {
2906 // check the attribute arguments.
2907 if (!checkAttributeNumArgs(S, Attr, 0))
2910 if (!isa<FunctionDecl>(D)) {
2911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2912 << Attr.getName() << ExpectedFunction;
2916 FunctionDecl *FD = cast<FunctionDecl>(D);
2917 if (!FD->getResultType()->isVoidType()) {
2918 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
2919 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2920 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2922 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2925 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2931 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
2933 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2937 static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2938 if (S.LangOpts.CUDA) {
2939 // check the attribute arguments.
2940 if (!checkAttributeNumArgs(S, Attr, 0))
2944 if (!isa<FunctionDecl>(D)) {
2945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2946 << Attr.getName() << ExpectedFunction;
2950 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
2952 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2956 static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2957 if (S.LangOpts.CUDA) {
2958 // check the attribute arguments.
2959 if (!checkAttributeNumArgs(S, Attr, 0))
2963 if (!isa<VarDecl>(D)) {
2964 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2965 << Attr.getName() << ExpectedVariable;
2969 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
2971 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2975 static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2976 // check the attribute arguments.
2977 if (!checkAttributeNumArgs(S, Attr, 0))
2980 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
2982 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2983 << Attr.getName() << ExpectedFunction;
2987 if (!Fn->isInlineSpecified()) {
2988 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
2992 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
2995 static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
2996 if (hasDeclarator(D)) return;
2998 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2999 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3001 if (S.CheckCallingConvAttr(Attr, CC))
3004 if (!isa<ObjCMethodDecl>(D)) {
3005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3006 << Attr.getName() << ExpectedFunctionOrMethod;
3010 switch (Attr.getKind()) {
3011 case AttributeList::AT_fastcall:
3012 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
3014 case AttributeList::AT_stdcall:
3015 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
3017 case AttributeList::AT_thiscall:
3018 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
3020 case AttributeList::AT_cdecl:
3021 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
3023 case AttributeList::AT_pascal:
3024 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
3026 case AttributeList::AT_pcs: {
3027 Expr *Arg = Attr.getArg(0);
3028 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
3029 if (!Str || !Str->isAscii()) {
3030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3036 StringRef StrRef = Str->getString();
3037 PcsAttr::PCSType PCS;
3038 if (StrRef == "aapcs")
3039 PCS = PcsAttr::AAPCS;
3040 else if (StrRef == "aapcs-vfp")
3041 PCS = PcsAttr::AAPCS_VFP;
3043 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3048 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
3051 llvm_unreachable("unexpected attribute kind");
3055 static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
3056 assert(!Attr.isInvalid());
3057 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
3060 bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3061 if (attr.isInvalid())
3064 if ((attr.getNumArgs() != 0 &&
3065 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3066 attr.getParameterName()) {
3067 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3072 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3073 // move to TargetAttributesSema one day.
3074 switch (attr.getKind()) {
3075 case AttributeList::AT_cdecl: CC = CC_C; break;
3076 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3077 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3078 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3079 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
3080 case AttributeList::AT_pcs: {
3081 Expr *Arg = attr.getArg(0);
3082 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
3083 if (!Str || !Str->isAscii()) {
3084 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3090 StringRef StrRef = Str->getString();
3091 if (StrRef == "aapcs") {
3094 } else if (StrRef == "aapcs-vfp") {
3100 default: llvm_unreachable("unexpected attribute kind");
3106 static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3107 if (hasDeclarator(D)) return;
3110 if (S.CheckRegparmAttr(Attr, numParams))
3113 if (!isa<ObjCMethodDecl>(D)) {
3114 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3115 << Attr.getName() << ExpectedFunctionOrMethod;
3119 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
3122 /// Checks a regparm attribute, returning true if it is ill-formed and
3123 /// otherwise setting numParams to the appropriate value.
3124 bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3125 if (Attr.isInvalid())
3128 if (Attr.getNumArgs() != 1) {
3129 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3134 Expr *NumParamsExpr = Attr.getArg(0);
3135 llvm::APSInt NumParams(32);
3136 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
3137 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
3138 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3139 << "regparm" << NumParamsExpr->getSourceRange();
3144 if (Context.getTargetInfo().getRegParmMax() == 0) {
3145 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
3146 << NumParamsExpr->getSourceRange();
3151 numParams = NumParams.getZExtValue();
3152 if (numParams > Context.getTargetInfo().getRegParmMax()) {
3153 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
3154 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
3162 static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
3163 if (S.LangOpts.CUDA) {
3164 // check the attribute arguments.
3165 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
3166 // FIXME: 0 is not okay.
3167 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
3171 if (!isFunctionOrMethod(D)) {
3172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3173 << Attr.getName() << ExpectedFunctionOrMethod;
3177 Expr *MaxThreadsExpr = Attr.getArg(0);
3178 llvm::APSInt MaxThreads(32);
3179 if (MaxThreadsExpr->isTypeDependent() ||
3180 MaxThreadsExpr->isValueDependent() ||
3181 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3182 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3183 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3187 llvm::APSInt MinBlocks(32);
3188 if (Attr.getNumArgs() > 1) {
3189 Expr *MinBlocksExpr = Attr.getArg(1);
3190 if (MinBlocksExpr->isTypeDependent() ||
3191 MinBlocksExpr->isValueDependent() ||
3192 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3193 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3194 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3199 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
3200 MaxThreads.getZExtValue(),
3201 MinBlocks.getZExtValue()));
3203 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3207 //===----------------------------------------------------------------------===//
3208 // Checker-specific attribute handlers.
3209 //===----------------------------------------------------------------------===//
3211 static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
3212 return type->isDependentType() ||
3213 type->isObjCObjectPointerType() ||
3214 S.Context.isObjCNSObjectType(type);
3216 static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
3217 return type->isDependentType() ||
3218 type->isPointerType() ||
3219 isValidSubjectOfNSAttribute(S, type);
3222 static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3223 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
3225 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3226 << Attr.getRange() << Attr.getName() << ExpectedParameter;
3231 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
3232 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3235 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3240 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
3241 << Attr.getRange() << Attr.getName() << cf;
3246 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
3248 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
3251 static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3252 const AttributeList &Attr) {
3253 if (!isa<ObjCMethodDecl>(D)) {
3254 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3255 << Attr.getRange() << Attr.getName() << ExpectedMethod;
3259 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
3262 static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3263 const AttributeList &Attr) {
3265 QualType returnType;
3267 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
3268 returnType = MD->getResultType();
3269 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
3270 returnType = PD->getType();
3271 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
3272 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
3273 return; // ignore: was handled as a type attribute
3274 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
3275 returnType = FD->getResultType();
3277 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
3278 << Attr.getRange() << Attr.getName()
3279 << ExpectedFunctionOrMethod;
3285 switch (Attr.getKind()) {
3286 default: llvm_unreachable("invalid ownership attribute");
3287 case AttributeList::AT_ns_returns_autoreleased:
3288 case AttributeList::AT_ns_returns_retained:
3289 case AttributeList::AT_ns_returns_not_retained:
3290 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3294 case AttributeList::AT_cf_returns_retained:
3295 case AttributeList::AT_cf_returns_not_retained:
3296 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3302 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3303 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
3307 switch (Attr.getKind()) {
3309 llvm_unreachable("invalid ownership attribute");
3310 case AttributeList::AT_ns_returns_autoreleased:
3311 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
3314 case AttributeList::AT_cf_returns_not_retained:
3315 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
3318 case AttributeList::AT_ns_returns_not_retained:
3319 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
3322 case AttributeList::AT_cf_returns_retained:
3323 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
3326 case AttributeList::AT_ns_returns_retained:
3327 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
3333 static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3334 const AttributeList &attr) {
3335 SourceLocation loc = attr.getLoc();
3337 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3339 if (!isa<ObjCMethodDecl>(method)) {
3340 S.Diag(method->getLocStart(), diag::err_attribute_wrong_decl_type)
3341 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
3345 // Check that the method returns a normal pointer.
3346 QualType resultType = method->getResultType();
3348 if (!resultType->isReferenceType() &&
3349 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
3350 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3352 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3354 // Drop the attribute.
3359 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
3362 /// Handle cf_audited_transfer and cf_unknown_transfer.
3363 static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3364 if (!isa<FunctionDecl>(D)) {
3365 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3366 << A.getRange() << A.getName() << ExpectedFunction;
3370 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3372 // Check whether there's a conflicting attribute already present.
3375 Existing = D->getAttr<CFUnknownTransferAttr>();
3377 Existing = D->getAttr<CFAuditedTransferAttr>();
3380 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3382 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3383 << A.getRange() << Existing->getRange();
3387 // All clear; add the attribute.
3390 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3393 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3397 static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3398 const AttributeList &Attr) {
3399 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3400 if (!RD || RD->isUnion()) {
3401 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3402 << Attr.getRange() << Attr.getName() << ExpectedStruct;
3405 IdentifierInfo *ParmName = Attr.getParameterName();
3407 // In Objective-C, verify that the type names an Objective-C type.
3408 // We don't want to check this outside of ObjC because people sometimes
3409 // do crazy C declarations of Objective-C types.
3410 if (ParmName && S.getLangOpts().ObjC1) {
3411 // Check for an existing type with this name.
3412 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3413 Sema::LookupOrdinaryName);
3414 if (S.LookupName(R, Sc)) {
3415 NamedDecl *Target = R.getFoundDecl();
3416 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3417 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3418 S.Diag(Target->getLocStart(), diag::note_declared_at);
3423 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3427 static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3428 const AttributeList &Attr) {
3429 if (hasDeclarator(D)) return;
3431 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3432 << Attr.getRange() << Attr.getName() << ExpectedVariable;
3435 static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3436 const AttributeList &Attr) {
3437 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
3438 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
3439 << Attr.getRange() << Attr.getName() << ExpectedVariable;
3443 ValueDecl *vd = cast<ValueDecl>(D);
3444 QualType type = vd->getType();
3446 if (!type->isDependentType() &&
3447 !type->isObjCLifetimeType()) {
3448 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
3453 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3455 // If we have no lifetime yet, check the lifetime we're presumably
3457 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3458 lifetime = type->getObjCARCImplicitLifetime();
3461 case Qualifiers::OCL_None:
3462 assert(type->isDependentType() &&
3463 "didn't infer lifetime for non-dependent type?");
3466 case Qualifiers::OCL_Weak: // meaningful
3467 case Qualifiers::OCL_Strong: // meaningful
3470 case Qualifiers::OCL_ExplicitNone:
3471 case Qualifiers::OCL_Autoreleasing:
3472 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
3473 << (lifetime == Qualifiers::OCL_Autoreleasing);
3477 D->addAttr(::new (S.Context)
3478 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
3481 static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
3482 switch (Attr.getKind()) {
3485 case AttributeList::AT_dllimport:
3486 case AttributeList::AT_dllexport:
3487 case AttributeList::AT_uuid:
3488 case AttributeList::AT_deprecated:
3489 case AttributeList::AT_noreturn:
3490 case AttributeList::AT_nothrow:
3491 case AttributeList::AT_naked:
3492 case AttributeList::AT_noinline:
3497 //===----------------------------------------------------------------------===//
3498 // Microsoft specific attribute handlers.
3499 //===----------------------------------------------------------------------===//
3501 static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
3502 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
3503 // check the attribute arguments.
3504 if (!checkAttributeNumArgs(S, Attr, 1))
3507 Expr *Arg = Attr.getArg(0);
3508 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
3509 if (!Str || !Str->isAscii()) {
3510 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3515 StringRef StrRef = Str->getString();
3517 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3518 StrRef.back() == '}';
3520 // Validate GUID length.
3521 if (IsCurly && StrRef.size() != 38) {
3522 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3525 if (!IsCurly && StrRef.size() != 36) {
3526 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3530 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
3531 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
3532 StringRef::iterator I = StrRef.begin();
3533 if (IsCurly) // Skip the optional '{'
3536 for (int i = 0; i < 36; ++i) {
3537 if (i == 8 || i == 13 || i == 18 || i == 23) {
3539 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3542 } else if (!isxdigit(*I)) {
3543 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3549 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
3552 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
3555 //===----------------------------------------------------------------------===//
3556 // Top Level Sema Entry Points
3557 //===----------------------------------------------------------------------===//
3559 static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3560 const AttributeList &Attr) {
3561 switch (Attr.getKind()) {
3562 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3563 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3564 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
3570 static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3571 const AttributeList &Attr) {
3572 switch (Attr.getKind()) {
3573 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3574 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3575 case AttributeList::AT_iboutletcollection:
3576 handleIBOutletCollection(S, D, Attr); break;
3577 case AttributeList::AT_address_space:
3578 case AttributeList::AT_opencl_image_access:
3579 case AttributeList::AT_objc_gc:
3580 case AttributeList::AT_vector_size:
3581 case AttributeList::AT_neon_vector_type:
3582 case AttributeList::AT_neon_polyvector_type:
3583 // Ignore these, these are type attributes, handled by
3584 // ProcessTypeAttributes.
3586 case AttributeList::AT_device:
3587 case AttributeList::AT_host:
3588 case AttributeList::AT_overloadable:
3589 // Ignore, this is a non-inheritable attribute, handled
3590 // by ProcessNonInheritableDeclAttr.
3592 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3593 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
3594 case AttributeList::AT_always_inline:
3595 handleAlwaysInlineAttr (S, D, Attr); break;
3596 case AttributeList::AT_analyzer_noreturn:
3597 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3598 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3599 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
3600 case AttributeList::AT_carries_dependency:
3601 handleDependencyAttr (S, D, Attr); break;
3602 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3603 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3604 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3605 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3606 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
3607 case AttributeList::AT_ext_vector_type:
3608 handleExtVectorTypeAttr(S, scope, D, Attr);
3610 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3611 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3612 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3613 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
3614 case AttributeList::AT_launch_bounds:
3615 handleLaunchBoundsAttr(S, D, Attr);
3617 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3618 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3619 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3620 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3621 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
3622 case AttributeList::AT_ownership_returns:
3623 case AttributeList::AT_ownership_takes:
3624 case AttributeList::AT_ownership_holds:
3625 handleOwnershipAttr (S, D, Attr); break;
3626 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3627 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3628 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3629 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3630 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
3632 case AttributeList::AT_objc_ownership:
3633 handleObjCOwnershipAttr(S, D, Attr); break;
3634 case AttributeList::AT_objc_precise_lifetime:
3635 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
3637 case AttributeList::AT_objc_returns_inner_pointer:
3638 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3640 case AttributeList::AT_ns_bridged:
3641 handleNSBridgedAttr(S, scope, D, Attr); break;
3643 case AttributeList::AT_cf_audited_transfer:
3644 case AttributeList::AT_cf_unknown_transfer:
3645 handleCFTransferAttr(S, D, Attr); break;
3647 // Checker-specific.
3648 case AttributeList::AT_cf_consumed:
3649 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
3650 case AttributeList::AT_ns_consumes_self:
3651 handleNSConsumesSelfAttr(S, D, Attr); break;
3653 case AttributeList::AT_ns_returns_autoreleased:
3654 case AttributeList::AT_ns_returns_not_retained:
3655 case AttributeList::AT_cf_returns_not_retained:
3656 case AttributeList::AT_ns_returns_retained:
3657 case AttributeList::AT_cf_returns_retained:
3658 handleNSReturnsRetainedAttr(S, D, Attr); break;
3660 case AttributeList::AT_reqd_work_group_size:
3661 handleReqdWorkGroupSize(S, D, Attr); break;
3663 case AttributeList::AT_init_priority:
3664 handleInitPriorityAttr(S, D, Attr); break;
3666 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3667 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
3668 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3669 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
3670 case AttributeList::AT_objc_arc_weak_reference_unavailable:
3671 handleArcWeakrefUnavailableAttr (S, D, Attr);
3673 case AttributeList::AT_objc_root_class:
3674 handleObjCRootClassAttr(S, D, Attr);
3676 case AttributeList::AT_objc_requires_property_definitions:
3677 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
3679 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3680 case AttributeList::AT_returns_twice:
3681 handleReturnsTwiceAttr(S, D, Attr);
3683 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3684 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3685 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
3687 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3688 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3689 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
3690 case AttributeList::AT_transparent_union:
3691 handleTransparentUnionAttr(S, D, Attr);
3693 case AttributeList::AT_objc_exception:
3694 handleObjCExceptionAttr(S, D, Attr);
3696 case AttributeList::AT_objc_method_family:
3697 handleObjCMethodFamilyAttr(S, D, Attr);
3699 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
3700 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3701 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3702 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3703 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3704 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3705 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3706 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3707 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
3708 case AttributeList::IgnoredAttribute:
3711 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
3712 handleNoInstrumentFunctionAttr(S, D, Attr);
3714 case AttributeList::AT_stdcall:
3715 case AttributeList::AT_cdecl:
3716 case AttributeList::AT_fastcall:
3717 case AttributeList::AT_thiscall:
3718 case AttributeList::AT_pascal:
3719 case AttributeList::AT_pcs:
3720 handleCallConvAttr(S, D, Attr);
3722 case AttributeList::AT_opencl_kernel_function:
3723 handleOpenCLKernelAttr(S, D, Attr);
3725 case AttributeList::AT_uuid:
3726 handleUuidAttr(S, D, Attr);
3729 // Thread safety attributes:
3730 case AttributeList::AT_guarded_var:
3731 handleGuardedVarAttr(S, D, Attr);
3733 case AttributeList::AT_pt_guarded_var:
3734 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3736 case AttributeList::AT_scoped_lockable:
3737 handleLockableAttr(S, D, Attr, /*scoped = */true);
3739 case AttributeList::AT_no_address_safety_analysis:
3740 handleNoAddressSafetyAttr(S, D, Attr);
3742 case AttributeList::AT_no_thread_safety_analysis:
3743 handleNoThreadSafetyAttr(S, D, Attr);
3745 case AttributeList::AT_lockable:
3746 handleLockableAttr(S, D, Attr);
3748 case AttributeList::AT_guarded_by:
3749 handleGuardedByAttr(S, D, Attr);
3751 case AttributeList::AT_pt_guarded_by:
3752 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3754 case AttributeList::AT_exclusive_lock_function:
3755 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3757 case AttributeList::AT_exclusive_locks_required:
3758 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3760 case AttributeList::AT_exclusive_trylock_function:
3761 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3763 case AttributeList::AT_lock_returned:
3764 handleLockReturnedAttr(S, D, Attr);
3766 case AttributeList::AT_locks_excluded:
3767 handleLocksExcludedAttr(S, D, Attr);
3769 case AttributeList::AT_shared_lock_function:
3770 handleLockFunAttr(S, D, Attr);
3772 case AttributeList::AT_shared_locks_required:
3773 handleLocksRequiredAttr(S, D, Attr);
3775 case AttributeList::AT_shared_trylock_function:
3776 handleTrylockFunAttr(S, D, Attr);
3778 case AttributeList::AT_unlock_function:
3779 handleUnlockFunAttr(S, D, Attr);
3781 case AttributeList::AT_acquired_before:
3782 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3784 case AttributeList::AT_acquired_after:
3785 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3789 // Ask target about the attribute.
3790 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3791 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
3792 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3798 /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3799 /// the attribute applies to decls. If the attribute is a type attribute, just
3800 /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3801 /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
3802 static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3803 const AttributeList &Attr,
3804 bool NonInheritable, bool Inheritable) {
3805 if (Attr.isInvalid())
3808 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3809 // FIXME: Try to deal with other __declspec attributes!
3813 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
3816 ProcessInheritableDeclAttr(S, scope, D, Attr);
3819 /// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3820 /// attribute list to the specified decl, ignoring any type attributes.
3821 void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
3822 const AttributeList *AttrList,
3823 bool NonInheritable, bool Inheritable) {
3824 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3825 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
3829 // static int a9 __attribute__((weakref));
3830 // but that looks really pointless. We reject it.
3831 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
3832 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
3833 dyn_cast<NamedDecl>(D)->getNameAsString();
3838 // Annotation attributes are the only attributes allowed after an access
3840 bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3841 const AttributeList *AttrList) {
3842 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3843 if (l->getKind() == AttributeList::AT_annotate) {
3844 handleAnnotateAttr(*this, ASDecl, *l);
3846 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3854 /// checkUnusedDeclAttributes - Check a list of attributes to see if it
3855 /// contains any decl attributes that we should warn about.
3856 static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3857 for ( ; A; A = A->getNext()) {
3858 // Only warn if the attribute is an unignored, non-type attribute.
3859 if (A->isUsedAsTypeAttr()) continue;
3860 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3862 if (A->getKind() == AttributeList::UnknownAttribute) {
3863 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3864 << A->getName() << A->getRange();
3866 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3867 << A->getName() << A->getRange();
3872 /// checkUnusedDeclAttributes - Given a declarator which is not being
3873 /// used to build a declaration, complain about any decl attributes
3874 /// which might be lying around on it.
3875 void Sema::checkUnusedDeclAttributes(Declarator &D) {
3876 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3877 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3878 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3879 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3882 /// DeclClonePragmaWeak - clone existing decl (maybe definition),
3883 /// #pragma weak needs a non-definition decl and source may not have one
3884 NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3885 SourceLocation Loc) {
3886 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
3887 NamedDecl *NewD = 0;
3888 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3889 FunctionDecl *NewFD;
3890 // FIXME: Missing call to CheckFunctionDeclaration().
3892 // FIXME: Is the qualifier info correct?
3893 // FIXME: Is the DeclContext correct?
3894 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3895 Loc, Loc, DeclarationName(II),
3896 FD->getType(), FD->getTypeSourceInfo(),
3898 false/*isInlineSpecified*/,
3900 false/*isConstexprSpecified*/);
3903 if (FD->getQualifier())
3904 NewFD->setQualifierInfo(FD->getQualifierLoc());
3906 // Fake up parameter variables; they are declared as if this were
3908 QualType FDTy = FD->getType();
3909 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3910 SmallVector<ParmVarDecl*, 16> Params;
3911 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3912 AE = FT->arg_type_end(); AI != AE; ++AI) {
3913 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3914 Param->setScopeInfo(0, Params.size());
3915 Params.push_back(Param);
3917 NewFD->setParams(Params);
3919 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3920 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
3921 VD->getInnerLocStart(), VD->getLocation(), II,
3922 VD->getType(), VD->getTypeSourceInfo(),
3923 VD->getStorageClass(),
3924 VD->getStorageClassAsWritten());
3925 if (VD->getQualifier()) {
3926 VarDecl *NewVD = cast<VarDecl>(NewD);
3927 NewVD->setQualifierInfo(VD->getQualifierLoc());
3933 /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3934 /// applied to it, possibly with an alias.
3935 void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
3936 if (W.getUsed()) return; // only do this once
3938 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3939 IdentifierInfo *NDId = ND->getIdentifier();
3940 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
3941 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3943 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
3944 WeakTopLevelDecl.push_back(NewD);
3945 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3946 // to insert Decl at TU scope, sorry.
3947 DeclContext *SavedContext = CurContext;
3948 CurContext = Context.getTranslationUnitDecl();
3949 PushOnScopeChains(NewD, S);
3950 CurContext = SavedContext;
3951 } else { // just add weak to existing
3952 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
3956 /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3957 /// it, apply them to D. This is a bit tricky because PD can have attributes
3958 /// specified in many different places, and we need to find and apply them all.
3959 void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3960 bool NonInheritable, bool Inheritable) {
3961 // It's valid to "forward-declare" #pragma weak, in which case we
3964 LoadExternalWeakUndeclaredIdentifiers();
3965 if (!WeakUndeclaredIdentifiers.empty()) {
3966 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3967 if (IdentifierInfo *Id = ND->getIdentifier()) {
3968 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3969 = WeakUndeclaredIdentifiers.find(Id);
3970 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3971 WeakInfo W = I->second;
3972 DeclApplyPragmaWeak(S, ND, W);
3973 WeakUndeclaredIdentifiers[Id] = W;
3980 // Apply decl attributes from the DeclSpec if present.
3981 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
3982 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
3984 // Walk the declarator structure, applying decl attributes that were in a type
3985 // position to the decl itself. This handles cases like:
3986 // int *__attr__(x)** D;
3987 // when X is a decl attribute.
3988 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3989 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
3990 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
3992 // Finally, apply any attributes on the decl itself.
3993 if (const AttributeList *Attrs = PD.getAttributes())
3994 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
3997 /// Is the given declaration allowed to use a forbidden type?
3998 static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3999 // Private ivars are always okay. Unfortunately, people don't
4000 // always properly make their ivars private, even in system headers.
4001 // Plus we need to make fields okay, too.
4002 // Function declarations in sys headers will be marked unavailable.
4003 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4004 !isa<FunctionDecl>(decl))
4007 // Require it to be declared in a system header.
4008 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4011 /// Handle a delayed forbidden-type diagnostic.
4012 static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4014 if (decl && isForbiddenTypeAllowed(S, decl)) {
4015 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4016 "this system declaration uses an unsupported type"));
4019 if (S.getLangOpts().ObjCAutoRefCount)
4020 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4021 // FIXME. we may want to supress diagnostics for all
4022 // kind of forbidden type messages on unavailable functions.
4023 if (FD->hasAttr<UnavailableAttr>() &&
4024 diag.getForbiddenTypeDiagnostic() ==
4025 diag::err_arc_array_param_no_ownership) {
4026 diag.Triggered = true;
4031 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4032 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4033 diag.Triggered = true;
4036 // This duplicates a vector push_back but hides the need to know the
4037 // size of the type.
4038 void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4039 assert(StackSize <= StackCapacity);
4041 // Grow the stack if necessary.
4042 if (StackSize == StackCapacity) {
4043 unsigned newCapacity = 2 * StackCapacity + 2;
4044 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4045 const char *oldBuffer = (const char*) Stack;
4048 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4051 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4052 StackCapacity = newCapacity;
4055 assert(StackSize < StackCapacity);
4056 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
4059 void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4061 DelayedDiagnostics &DD = S.DelayedDiagnostics;
4063 // Check the invariants.
4064 assert(DD.StackSize >= state.SavedStackSize);
4065 assert(state.SavedStackSize >= DD.ActiveStackBase);
4066 assert(DD.ParsingDepth > 0);
4068 // Drop the parsing depth.
4071 // If there are no active diagnostics, we're done.
4072 if (DD.StackSize == DD.ActiveStackBase)
4075 // We only want to actually emit delayed diagnostics when we
4076 // successfully parsed a decl.
4078 // We emit all the active diagnostics, not just those starting
4079 // from the saved state. The idea is this: we get one push for a
4080 // decl spec and another for each declarator; in a decl group like:
4081 // deprecated_typedef foo, *bar, baz();
4082 // only the declarator pops will be passed decls. This is correct;
4083 // we really do need to consider delayed diagnostics from the decl spec
4084 // for each of the different declarations.
4085 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4086 DelayedDiagnostic &diag = DD.Stack[i];
4090 switch (diag.Kind) {
4091 case DelayedDiagnostic::Deprecation:
4092 // Don't bother giving deprecation diagnostics if the decl is invalid.
4093 if (!decl->isInvalidDecl())
4094 S.HandleDelayedDeprecationCheck(diag, decl);
4097 case DelayedDiagnostic::Access:
4098 S.HandleDelayedAccessCheck(diag, decl);
4101 case DelayedDiagnostic::ForbiddenType:
4102 handleDelayedForbiddenType(S, diag, decl);
4108 // Destroy all the delayed diagnostics we're about to pop off.
4109 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
4110 DD.Stack[i].Destroy();
4112 DD.StackSize = state.SavedStackSize;
4115 static bool isDeclDeprecated(Decl *D) {
4117 if (D->isDeprecated())
4119 // A category implicitly has the availability of the interface.
4120 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4121 return CatD->getClassInterface()->isDeprecated();
4122 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4126 void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
4128 if (isDeclDeprecated(Ctx))
4131 DD.Triggered = true;
4132 if (!DD.getDeprecationMessage().empty())
4133 Diag(DD.Loc, diag::warn_deprecated_message)
4134 << DD.getDeprecationDecl()->getDeclName()
4135 << DD.getDeprecationMessage();
4136 else if (DD.getUnknownObjCClass()) {
4137 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4138 << DD.getDeprecationDecl()->getDeclName();
4139 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4142 Diag(DD.Loc, diag::warn_deprecated)
4143 << DD.getDeprecationDecl()->getDeclName();
4146 void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
4148 const ObjCInterfaceDecl *UnknownObjCClass) {
4149 // Delay if we're currently parsing a declaration.
4150 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
4151 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4157 // Otherwise, don't warn if our current context is deprecated.
4158 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
4160 if (!Message.empty())
4161 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4164 if (!UnknownObjCClass)
4165 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
4167 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
4168 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);