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 //===----------------------------------------------------------------------===//
15 #include "TargetAttributesSema.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/Parse/DeclSpec.h"
21 #include "llvm/ADT/StringExtras.h"
22 using namespace clang;
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
31 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
33 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
35 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
36 Ty = decl->getUnderlyingType();
40 if (Ty->isFunctionPointerType())
41 Ty = Ty->getAs<PointerType>()->getPointeeType();
42 else if (blocksToo && Ty->isBlockPointerType())
43 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
45 return Ty->getAs<FunctionType>();
48 // FIXME: We should provide an abstraction around a method or function
49 // to provide the following bits of information.
51 /// isFunction - Return true if the given decl has function
52 /// type (function or function-typed variable).
53 static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
57 /// isFunctionOrMethod - Return true if the given decl has function
58 /// type (function or function-typed variable) or an Objective-C
60 static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
64 /// isFunctionOrMethodOrBlock - Return true if the given decl has function
65 /// type (function or function-typed variable) or an Objective-C
66 /// method or a block.
67 static bool isFunctionOrMethodOrBlock(const Decl *d) {
68 if (isFunctionOrMethod(d))
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
75 return isa<BlockDecl>(d);
78 /// hasFunctionProto - Return true if the given decl has a argument
79 /// information. This decl should have already passed
80 /// isFunctionOrMethod or isFunctionOrMethodOrBlock.
81 static bool hasFunctionProto(const Decl *d) {
82 if (const FunctionType *FnTy = getFunctionType(d))
83 return isa<FunctionProtoType>(FnTy);
85 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
90 /// getFunctionOrMethodNumArgs - Return number of function or method
91 /// arguments. It is an error to call this on a K&R function (use
92 /// hasFunctionProto first).
93 static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
94 if (const FunctionType *FnTy = getFunctionType(d))
95 return cast<FunctionProtoType>(FnTy)->getNumArgs();
96 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
98 return cast<ObjCMethodDecl>(d)->param_size();
101 static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
102 if (const FunctionType *FnTy = getFunctionType(d))
103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
110 static QualType getFunctionOrMethodResultType(const Decl *d) {
111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
116 static bool isFunctionOrMethodVariadic(const Decl *d) {
117 if (const FunctionType *FnTy = getFunctionType(d)) {
118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
119 return proto->isVariadic();
120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
121 return BD->IsVariadic();
123 return cast<ObjCMethodDecl>(d)->isVariadic();
127 static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
132 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
136 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
143 static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
144 const PointerType *PT = T->getAs<PointerType>();
148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
152 const RecordDecl *RD = RT->getDecl();
153 if (RD->getTagKind() != TagDecl::TK_struct)
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
159 //===----------------------------------------------------------------------===//
160 // Attribute Implementations
161 //===----------------------------------------------------------------------===//
163 // FIXME: All this manual attribute parsing code is gross. At the
164 // least add some helper functions to check most argument patterns (#
165 // and types of args).
167 static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
168 const AttributeList &Attr, Sema &S) {
169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
175 QualType curType = tDecl->getUnderlyingType();
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
198 // FIXME: preserve the old source info.
199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
206 static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
207 // check the attribute arguments.
208 if (Attr.getNumArgs() > 0) {
209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
214 TD->addAttr(::new (S.Context) PackedAttr);
215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
218 if (!FD->getType()->isIncompleteType() &&
219 S.Context.getTypeAlign(FD->getType()) <= 8)
220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
221 << Attr.getName() << FD->getType();
223 FD->addAttr(::new (S.Context) PackedAttr);
225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
228 static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
238 d->addAttr(::new (S.Context) IBActionAttr());
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
245 static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
252 // The IBOutlet attributes only apply to instance variables of
253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
255 d->addAttr(::new (S.Context) IBOutletAttr());
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
262 static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
263 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
265 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
267 << Attr.getName() << 0 /*function*/;
271 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
273 // The nonnull attribute only applies to pointers.
274 llvm::SmallVector<unsigned, 10> NonNullArgs;
276 for (AttributeList::arg_iterator I=Attr.arg_begin(),
277 E=Attr.arg_end(); I!=E; ++I) {
280 // The argument must be an integer constant expression.
281 Expr *Ex = static_cast<Expr *>(*I);
282 llvm::APSInt ArgNum(32);
283 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
284 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
285 << "nonnull" << Ex->getSourceRange();
289 unsigned x = (unsigned) ArgNum.getZExtValue();
291 if (x < 1 || x > NumArgs) {
292 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
293 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
299 // Is the function argument a pointer type?
300 QualType T = getFunctionOrMethodArgType(d, x);
301 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
302 // FIXME: Should also highlight argument in decl.
303 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
304 << "nonnull" << Ex->getSourceRange();
308 NonNullArgs.push_back(x);
311 // If no arguments were specified to __attribute__((nonnull)) then all pointer
312 // arguments have a nonnull attribute.
313 if (NonNullArgs.empty()) {
314 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
315 QualType T = getFunctionOrMethodArgType(d, I);
316 if (T->isAnyPointerType() || T->isBlockPointerType())
317 NonNullArgs.push_back(I);
320 if (NonNullArgs.empty()) {
321 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
326 unsigned* start = &NonNullArgs[0];
327 unsigned size = NonNullArgs.size();
328 std::sort(start, start + size);
329 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
332 static bool isStaticVarOrStaticFunciton(Decl *D) {
333 if (VarDecl *VD = dyn_cast<VarDecl>(D))
334 return VD->getStorageClass() == VarDecl::Static;
335 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
336 return FD->getStorageClass() == FunctionDecl::Static;
340 static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
341 // Check the attribute arguments.
342 if (Attr.getNumArgs() > 1) {
343 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
349 // static int a __attribute__((weakref ("v2")));
350 // static int b() __attribute__((weakref ("f3")));
352 // and ignores the attributes of
354 // static int a __attribute__((weakref ("v2")));
357 if (const DeclContext *Ctx = d->getDeclContext()) {
358 Ctx = Ctx->getLookupContext();
359 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
360 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
361 dyn_cast<NamedDecl>(d)->getNameAsString();
366 // The GCC manual says
368 // At present, a declaration to which `weakref' is attached can only
374 // given as an argument to `weakref' or to `alias', `weakref' is
375 // equivalent to `weak'.
377 // gcc 4.4.1 will accept
378 // int a7 __attribute__((weakref));
380 // int a7 __attribute__((weak));
381 // This looks like a bug in gcc. We reject that for now. We should revisit
382 // it if this behaviour is actually used.
384 if (!isStaticVarOrStaticFunciton(d)) {
385 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
386 dyn_cast<NamedDecl>(d)->getNameAsString();
391 // static ((alias ("y"), weakref)).
392 // Should we? How to check that weakref is before or after alias?
394 if (Attr.getNumArgs() == 1) {
395 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
396 Arg = Arg->IgnoreParenCasts();
397 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
399 if (Str == 0 || Str->isWide()) {
400 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
404 // GCC will accept anything as the argument of weakref. Should we
405 // check for an existing decl?
406 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
409 d->addAttr(::new (S.Context) WeakRefAttr());
412 static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
413 // check the attribute arguments.
414 if (Attr.getNumArgs() != 1) {
415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
419 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
420 Arg = Arg->IgnoreParenCasts();
421 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
423 if (Str == 0 || Str->isWide()) {
424 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
429 // FIXME: check if target symbol exists in current file
431 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
434 static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
436 // check the attribute arguments.
437 if (Attr.getNumArgs() != 0) {
438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
442 if (!isa<FunctionDecl>(d)) {
443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
444 << Attr.getName() << 0 /*function*/;
448 d->addAttr(::new (S.Context) AlwaysInlineAttr());
451 static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
452 // check the attribute arguments.
453 if (Attr.getNumArgs() != 0) {
454 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
459 QualType RetTy = FD->getResultType();
460 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
461 d->addAttr(::new (S.Context) MallocAttr());
466 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
469 static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
471 // check the attribute arguments.
472 if (Attr.getNumArgs() != 0) {
473 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
477 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
478 ValueDecl *VD = dyn_cast<ValueDecl>(d);
479 if (VD == 0 || (!VD->getType()->isBlockPointerType()
480 && !VD->getType()->isFunctionPointerType())) {
481 S.Diag(Attr.getLoc(),
482 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
483 : diag::warn_attribute_wrong_decl_type)
484 << Attr.getName() << 0 /*function*/;
492 static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
493 // NOTE: We don't add the attribute to a FunctionDecl because the noreturn
494 // trait will be part of the function's type.
496 // Don't apply as a decl attribute to ValueDecl.
497 // FIXME: probably ought to diagnose this.
498 if (isa<ValueDecl>(d))
501 if (HandleCommonNoReturnAttr(d, Attr, S))
502 d->addAttr(::new (S.Context) NoReturnAttr());
505 static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
507 if (HandleCommonNoReturnAttr(d, Attr, S))
508 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
511 static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
512 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
513 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
514 << Attr.getName() << 8 /*function, method, or parameter*/;
517 // FIXME: Actually store the attribute on the declaration
520 static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
521 // check the attribute arguments.
522 if (Attr.getNumArgs() != 0) {
523 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
527 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
529 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
530 << Attr.getName() << 2 /*variable and function*/;
534 d->addAttr(::new (S.Context) UnusedAttr());
537 static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
538 // check the attribute arguments.
539 if (Attr.getNumArgs() != 0) {
540 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
544 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
545 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
546 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
549 } else if (!isFunctionOrMethod(d)) {
550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
551 << Attr.getName() << 2 /*variable and function*/;
555 d->addAttr(::new (S.Context) UsedAttr());
558 static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
559 // check the attribute arguments.
560 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
561 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
566 int priority = 65535; // FIXME: Do not hardcode such constants.
567 if (Attr.getNumArgs() > 0) {
568 Expr *E = static_cast<Expr *>(Attr.getArg(0));
569 llvm::APSInt Idx(32);
570 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
571 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
572 << "constructor" << 1 << E->getSourceRange();
575 priority = Idx.getZExtValue();
578 if (!isa<FunctionDecl>(d)) {
579 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
580 << Attr.getName() << 0 /*function*/;
584 d->addAttr(::new (S.Context) ConstructorAttr(priority));
587 static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
588 // check the attribute arguments.
589 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
590 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
595 int priority = 65535; // FIXME: Do not hardcode such constants.
596 if (Attr.getNumArgs() > 0) {
597 Expr *E = static_cast<Expr *>(Attr.getArg(0));
598 llvm::APSInt Idx(32);
599 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
601 << "destructor" << 1 << E->getSourceRange();
604 priority = Idx.getZExtValue();
607 if (!isa<FunctionDecl>(d)) {
608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
609 << Attr.getName() << 0 /*function*/;
613 d->addAttr(::new (S.Context) DestructorAttr(priority));
616 static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
617 // check the attribute arguments.
618 if (Attr.getNumArgs() != 0) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
623 d->addAttr(::new (S.Context) DeprecatedAttr());
626 static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
627 // check the attribute arguments.
628 if (Attr.getNumArgs() != 0) {
629 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
633 d->addAttr(::new (S.Context) UnavailableAttr());
636 static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
637 // check the attribute arguments.
638 if (Attr.getNumArgs() != 1) {
639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
643 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
644 Arg = Arg->IgnoreParenCasts();
645 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
647 if (Str == 0 || Str->isWide()) {
648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
649 << "visibility" << 1;
653 llvm::StringRef TypeStr = Str->getString();
654 VisibilityAttr::VisibilityTypes type;
656 if (TypeStr == "default")
657 type = VisibilityAttr::DefaultVisibility;
658 else if (TypeStr == "hidden")
659 type = VisibilityAttr::HiddenVisibility;
660 else if (TypeStr == "internal")
661 type = VisibilityAttr::HiddenVisibility; // FIXME
662 else if (TypeStr == "protected")
663 type = VisibilityAttr::ProtectedVisibility;
665 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
669 d->addAttr(::new (S.Context) VisibilityAttr(type));
672 static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
674 if (Attr.getNumArgs() != 0) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
679 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
681 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
685 D->addAttr(::new (S.Context) ObjCExceptionAttr());
688 static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
689 if (Attr.getNumArgs() != 0) {
690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
693 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
694 QualType T = TD->getUnderlyingType();
695 if (!T->isPointerType() ||
696 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
697 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
701 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
705 HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
706 if (Attr.getNumArgs() != 0) {
707 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
711 if (!isa<FunctionDecl>(D)) {
712 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
716 D->addAttr(::new (S.Context) OverloadableAttr());
719 static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
720 if (!Attr.getParameterName()) {
721 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
726 if (Attr.getNumArgs() != 0) {
727 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
731 BlocksAttr::BlocksAttrTypes type;
732 if (Attr.getParameterName()->isStr("byref"))
733 type = BlocksAttr::ByRef;
735 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
736 << "blocks" << Attr.getParameterName();
740 d->addAttr(::new (S.Context) BlocksAttr(type));
743 static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
744 // check the attribute arguments.
745 if (Attr.getNumArgs() > 2) {
746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
752 if (Attr.getNumArgs() > 0) {
753 Expr *E = static_cast<Expr *>(Attr.getArg(0));
754 llvm::APSInt Idx(32);
755 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
756 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
757 << "sentinel" << 1 << E->getSourceRange();
760 sentinel = Idx.getZExtValue();
763 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
764 << E->getSourceRange();
770 if (Attr.getNumArgs() > 1) {
771 Expr *E = static_cast<Expr *>(Attr.getArg(1));
772 llvm::APSInt Idx(32);
773 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
774 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
775 << "sentinel" << 2 << E->getSourceRange();
778 nullPos = Idx.getZExtValue();
780 if (nullPos > 1 || nullPos < 0) {
781 // FIXME: This error message could be improved, it would be nice
782 // to say what the bounds actually are.
783 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
784 << E->getSourceRange();
789 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
790 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
791 assert(FT && "FunctionDecl has non-function type?");
793 if (isa<FunctionNoProtoType>(FT)) {
794 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
798 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
799 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
802 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
803 if (!MD->isVariadic()) {
804 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
807 } else if (isa<BlockDecl>(d)) {
808 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
811 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
812 QualType Ty = V->getType();
813 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
814 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
815 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
816 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
817 int m = Ty->isFunctionPointerType() ? 0 : 1;
818 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
822 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
823 << Attr.getName() << 6 /*function, method or block */;
827 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
828 << Attr.getName() << 6 /*function, method or block */;
831 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
834 static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
835 // check the attribute arguments.
836 if (Attr.getNumArgs() != 0) {
837 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
841 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
842 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
843 << Attr.getName() << 0 /*function*/;
847 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
848 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
849 << Attr.getName() << 0;
852 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
853 if (MD->getResultType()->isVoidType()) {
854 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
855 << Attr.getName() << 1;
859 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
862 static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
863 // check the attribute arguments.
864 if (Attr.getNumArgs() != 0) {
865 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
869 /* weak only applies to non-static declarations */
870 if (isStaticVarOrStaticFunciton(D)) {
871 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
872 dyn_cast<NamedDecl>(D)->getNameAsString();
876 // TODO: could also be applied to methods?
877 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
878 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
879 << Attr.getName() << 2 /*variable and function*/;
883 D->addAttr(::new (S.Context) WeakAttr());
886 static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
887 // check the attribute arguments.
888 if (Attr.getNumArgs() != 0) {
889 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
893 // weak_import only applies to variable & function declarations.
895 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
896 isDef = (!VD->hasExternalStorage() || VD->getInit());
897 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
898 isDef = FD->getBody();
899 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
900 // We ignore weak import on properties and methods
902 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
903 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
904 << Attr.getName() << 2 /*variable and function*/;
908 // Merge should handle any subsequent violations.
910 S.Diag(Attr.getLoc(),
911 diag::warn_attribute_weak_import_invalid_on_definition)
912 << "weak_import" << 2 /*variable and function*/;
916 D->addAttr(::new (S.Context) WeakImportAttr());
919 static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
921 // Attribute has 3 arguments.
922 if (Attr.getNumArgs() != 3) {
923 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
928 for (unsigned i = 0; i < 3; ++i) {
929 Expr *E = static_cast<Expr *>(Attr.getArg(i));
930 llvm::APSInt ArgNum(32);
931 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
932 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
933 << "reqd_work_group_size" << E->getSourceRange();
936 WGSize[i] = (unsigned) ArgNum.getZExtValue();
938 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
942 static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
943 // Attribute has no arguments.
944 if (Attr.getNumArgs() != 1) {
945 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
949 // Make sure that there is a string literal as the sections's single
951 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
952 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
954 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
958 // If the target wants to validate the section specifier, make it happen.
959 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
960 if (!Error.empty()) {
961 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
966 // This attribute cannot be applied to local variables.
967 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
968 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
972 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
976 static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
977 // check the attribute arguments.
978 if (Attr.getNumArgs() != 0) {
979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
983 d->addAttr(::new (S.Context) NoThrowAttr());
986 static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
987 // check the attribute arguments.
988 if (Attr.getNumArgs() != 0) {
989 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
993 d->addAttr(::new (S.Context) ConstAttr());
996 static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
997 // check the attribute arguments.
998 if (Attr.getNumArgs() != 0) {
999 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1003 d->addAttr(::new (S.Context) PureAttr());
1006 static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1007 if (!Attr.getParameterName()) {
1008 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1012 if (Attr.getNumArgs() != 0) {
1013 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1017 VarDecl *VD = dyn_cast<VarDecl>(d);
1019 if (!VD || !VD->hasLocalStorage()) {
1020 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1024 // Look up the function
1025 NamedDecl *CleanupDecl
1026 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1027 Sema::LookupOrdinaryName);
1029 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
1030 Attr.getParameterName();
1034 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1036 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
1037 Attr.getParameterName();
1041 if (FD->getNumParams() != 1) {
1042 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
1043 Attr.getParameterName();
1047 // We're currently more strict than GCC about what function types we accept.
1048 // If this ever proves to be a problem it should be easy to fix.
1049 QualType Ty = S.Context.getPointerType(VD->getType());
1050 QualType ParamTy = FD->getParamDecl(0)->getType();
1051 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
1052 S.Diag(Attr.getLoc(),
1053 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1054 Attr.getParameterName() << ParamTy << Ty;
1058 d->addAttr(::new (S.Context) CleanupAttr(FD));
1061 /// Handle __attribute__((format_arg((idx)))) attribute based on
1062 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1063 static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1064 if (Attr.getNumArgs() != 1) {
1065 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1068 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1069 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1070 << Attr.getName() << 0 /*function*/;
1073 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1074 // needed in order to be compatible with GCC the index must start with 1.
1075 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1076 unsigned FirstIdx = 1;
1077 // checks for the 2nd argument
1078 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1079 llvm::APSInt Idx(32);
1080 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1081 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1082 << "format" << 2 << IdxExpr->getSourceRange();
1086 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1087 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1088 << "format" << 2 << IdxExpr->getSourceRange();
1092 unsigned ArgIdx = Idx.getZExtValue() - 1;
1094 // make sure the format string is really a string
1095 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
1097 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1098 if (not_nsstring_type &&
1099 !isCFStringType(Ty, S.Context) &&
1100 (!Ty->isPointerType() ||
1101 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
1102 // FIXME: Should highlight the actual expression that has the wrong type.
1103 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1104 << (not_nsstring_type ? "a string type" : "an NSString")
1105 << IdxExpr->getSourceRange();
1108 Ty = getFunctionOrMethodResultType(d);
1109 if (!isNSStringType(Ty, S.Context) &&
1110 !isCFStringType(Ty, S.Context) &&
1111 (!Ty->isPointerType() ||
1112 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
1113 // FIXME: Should highlight the actual expression that has the wrong type.
1114 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
1115 << (not_nsstring_type ? "string type" : "NSString")
1116 << IdxExpr->getSourceRange();
1120 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
1123 enum FormatAttrKind {
1132 /// getFormatAttrKind - Map from format attribute names to supported format
1134 static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1135 // Check for formats that get handled specially.
1136 if (Format == "NSString")
1137 return NSStringFormat;
1138 if (Format == "CFString")
1139 return CFStringFormat;
1140 if (Format == "strftime")
1141 return StrftimeFormat;
1143 // Otherwise, check for supported formats.
1144 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1145 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1146 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1147 Format == "zcmn_err")
1148 return SupportedFormat;
1150 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1151 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
1152 return IgnoredFormat;
1154 return InvalidFormat;
1157 /// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1158 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1159 static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1161 if (!Attr.getParameterName()) {
1162 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1167 if (Attr.getNumArgs() != 2) {
1168 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
1172 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
1173 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1174 << Attr.getName() << 0 /*function*/;
1178 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1179 unsigned FirstIdx = 1;
1181 llvm::StringRef Format = Attr.getParameterName()->getName();
1183 // Normalize the argument, __foo__ becomes foo.
1184 if (Format.startswith("__") && Format.endswith("__"))
1185 Format = Format.substr(2, Format.size() - 4);
1187 // Check for supported formats.
1188 FormatAttrKind Kind = getFormatAttrKind(Format);
1190 if (Kind == IgnoredFormat)
1193 if (Kind == InvalidFormat) {
1194 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1195 << "format" << Attr.getParameterName()->getName();
1199 // checks for the 2nd argument
1200 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1201 llvm::APSInt Idx(32);
1202 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1203 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1204 << "format" << 2 << IdxExpr->getSourceRange();
1208 // FIXME: We should handle the implicit 'this' parameter in a more generic
1209 // way that can be used for other arguments.
1210 bool HasImplicitThisParam = false;
1211 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1212 if (MD->isInstance()) {
1213 HasImplicitThisParam = true;
1218 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1219 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1220 << "format" << 2 << IdxExpr->getSourceRange();
1224 // FIXME: Do we need to bounds check?
1225 unsigned ArgIdx = Idx.getZExtValue() - 1;
1227 if (HasImplicitThisParam) {
1229 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1230 << "a string type" << IdxExpr->getSourceRange();
1236 // make sure the format string is really a string
1237 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
1239 if (Kind == CFStringFormat) {
1240 if (!isCFStringType(Ty, S.Context)) {
1241 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1242 << "a CFString" << IdxExpr->getSourceRange();
1245 } else if (Kind == NSStringFormat) {
1246 // FIXME: do we need to check if the type is NSString*? What are the
1248 if (!isNSStringType(Ty, S.Context)) {
1249 // FIXME: Should highlight the actual expression that has the wrong type.
1250 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1251 << "an NSString" << IdxExpr->getSourceRange();
1254 } else if (!Ty->isPointerType() ||
1255 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
1256 // FIXME: Should highlight the actual expression that has the wrong type.
1257 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1258 << "a string type" << IdxExpr->getSourceRange();
1262 // check the 3rd argument
1263 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
1264 llvm::APSInt FirstArg(32);
1265 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
1266 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1267 << "format" << 3 << FirstArgExpr->getSourceRange();
1271 // check if the function is variadic if the 3rd argument non-zero
1272 if (FirstArg != 0) {
1273 if (isFunctionOrMethodVariadic(d)) {
1274 ++NumArgs; // +1 for ...
1276 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
1281 // strftime requires FirstArg to be 0 because it doesn't read from any
1282 // variable the input is just the current time + the format string.
1283 if (Kind == StrftimeFormat) {
1284 if (FirstArg != 0) {
1285 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1286 << FirstArgExpr->getSourceRange();
1289 // if 0 it disables parameter checking (to use with e.g. va_list)
1290 } else if (FirstArg != 0 && FirstArg != NumArgs) {
1291 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1292 << "format" << 3 << FirstArgExpr->getSourceRange();
1296 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
1297 FirstArg.getZExtValue()));
1300 static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1302 // check the attribute arguments.
1303 if (Attr.getNumArgs() != 0) {
1304 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1308 // Try to find the underlying union declaration.
1310 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
1311 if (TD && TD->getUnderlyingType()->isUnionType())
1312 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1314 RD = dyn_cast<RecordDecl>(d);
1316 if (!RD || !RD->isUnion()) {
1317 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1318 << Attr.getName() << 1 /*union*/;
1322 if (!RD->isDefinition()) {
1323 S.Diag(Attr.getLoc(),
1324 diag::warn_transparent_union_attribute_not_definition);
1328 RecordDecl::field_iterator Field = RD->field_begin(),
1329 FieldEnd = RD->field_end();
1330 if (Field == FieldEnd) {
1331 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1335 FieldDecl *FirstField = *Field;
1336 QualType FirstType = FirstField->getType();
1337 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1338 S.Diag(FirstField->getLocation(),
1339 diag::warn_transparent_union_attribute_floating);
1343 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1344 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1345 for (; Field != FieldEnd; ++Field) {
1346 QualType FieldType = Field->getType();
1347 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1348 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1349 // Warn if we drop the attribute.
1350 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1351 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1352 : S.Context.getTypeAlign(FieldType);
1353 S.Diag(Field->getLocation(),
1354 diag::warn_transparent_union_attribute_field_size_align)
1355 << isSize << Field->getDeclName() << FieldBits;
1356 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1357 S.Diag(FirstField->getLocation(),
1358 diag::note_transparent_union_first_field_size_align)
1359 << isSize << FirstBits;
1364 RD->addAttr(::new (S.Context) TransparentUnionAttr());
1367 static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1368 // check the attribute arguments.
1369 if (Attr.getNumArgs() != 1) {
1370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1373 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1374 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
1376 // Make sure that there is a string literal as the annotation's single
1379 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
1382 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
1385 static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1386 // check the attribute arguments.
1387 if (Attr.getNumArgs() > 1) {
1388 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1392 //FIXME: The C++0x version of this attribute has more limited applicabilty
1393 // than GNU's, and should error out when it is used to specify a
1394 // weaker alignment, rather than being silently ignored.
1397 if (Attr.getNumArgs() == 0) {
1398 // FIXME: This should be the target specific maximum alignment.
1399 // (For now we just use 128 bits which is the maximum on X86).
1401 d->addAttr(::new (S.Context) AlignedAttr(Align));
1405 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1406 llvm::APSInt Alignment(32);
1407 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
1408 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1409 << "aligned" << alignmentExpr->getSourceRange();
1412 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1413 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1414 << alignmentExpr->getSourceRange();
1418 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
1421 /// HandleModeAttr - This attribute modifies the width of a decl with primitive
1424 /// Despite what would be logical, the mode attribute is a decl attribute, not a
1425 /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1426 /// HImode, not an intermediate pointer.
1427 static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1428 // This attribute isn't documented, but glibc uses it. It changes
1429 // the width of an int or unsigned int to the specified size.
1431 // Check that there aren't any arguments
1432 if (Attr.getNumArgs() != 0) {
1433 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1437 IdentifierInfo *Name = Attr.getParameterName();
1439 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
1443 llvm::StringRef Str = Attr.getParameterName()->getName();
1445 // Normalize the attribute name, __foo__ becomes foo.
1446 if (Str.startswith("__") && Str.endswith("__"))
1447 Str = Str.substr(2, Str.size() - 4);
1449 unsigned DestWidth = 0;
1450 bool IntegerMode = true;
1451 bool ComplexMode = false;
1452 switch (Str.size()) {
1455 case 'Q': DestWidth = 8; break;
1456 case 'H': DestWidth = 16; break;
1457 case 'S': DestWidth = 32; break;
1458 case 'D': DestWidth = 64; break;
1459 case 'X': DestWidth = 96; break;
1460 case 'T': DestWidth = 128; break;
1462 if (Str[1] == 'F') {
1463 IntegerMode = false;
1464 } else if (Str[1] == 'C') {
1465 IntegerMode = false;
1467 } else if (Str[1] != 'I') {
1472 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1473 // pointer on PIC16 and other embedded platforms.
1475 DestWidth = S.Context.Target.getPointerWidth(0);
1476 else if (Str == "byte")
1477 DestWidth = S.Context.Target.getCharWidth();
1480 if (Str == "pointer")
1481 DestWidth = S.Context.Target.getPointerWidth(0);
1486 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1487 OldTy = TD->getUnderlyingType();
1488 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1489 OldTy = VD->getType();
1491 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1492 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
1496 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
1497 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1498 else if (IntegerMode) {
1499 if (!OldTy->isIntegralType())
1500 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1501 } else if (ComplexMode) {
1502 if (!OldTy->isComplexType())
1503 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1505 if (!OldTy->isFloatingType())
1506 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1509 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1510 // and friends, at least with glibc.
1511 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1512 // width on unusual platforms.
1513 // FIXME: Make sure floating-point mappings are accurate
1514 // FIXME: Support XF and TF types
1516 switch (DestWidth) {
1518 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
1521 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1525 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1528 if (OldTy->isSignedIntegerType())
1529 NewTy = S.Context.SignedCharTy;
1531 NewTy = S.Context.UnsignedCharTy;
1535 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1538 if (OldTy->isSignedIntegerType())
1539 NewTy = S.Context.ShortTy;
1541 NewTy = S.Context.UnsignedShortTy;
1545 NewTy = S.Context.FloatTy;
1546 else if (OldTy->isSignedIntegerType())
1547 NewTy = S.Context.IntTy;
1549 NewTy = S.Context.UnsignedIntTy;
1553 NewTy = S.Context.DoubleTy;
1554 else if (OldTy->isSignedIntegerType())
1555 if (S.Context.Target.getLongWidth() == 64)
1556 NewTy = S.Context.LongTy;
1558 NewTy = S.Context.LongLongTy;
1560 if (S.Context.Target.getLongWidth() == 64)
1561 NewTy = S.Context.UnsignedLongTy;
1563 NewTy = S.Context.UnsignedLongLongTy;
1566 NewTy = S.Context.LongDoubleTy;
1570 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1573 if (OldTy->isSignedIntegerType())
1574 NewTy = S.Context.Int128Ty;
1576 NewTy = S.Context.UnsignedInt128Ty;
1581 NewTy = S.Context.getComplexType(NewTy);
1584 // Install the new type.
1585 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1586 // FIXME: preserve existing source info.
1587 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
1589 cast<ValueDecl>(D)->setType(NewTy);
1592 static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1593 // check the attribute arguments.
1594 if (Attr.getNumArgs() > 0) {
1595 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1599 if (!isFunctionOrMethod(d)) {
1600 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1601 << Attr.getName() << 0 /*function*/;
1605 d->addAttr(::new (S.Context) NoDebugAttr());
1608 static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1609 // check the attribute arguments.
1610 if (Attr.getNumArgs() != 0) {
1611 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1615 if (!isa<FunctionDecl>(d)) {
1616 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1617 << Attr.getName() << 0 /*function*/;
1621 d->addAttr(::new (S.Context) NoInlineAttr());
1624 static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1625 // check the attribute arguments.
1626 if (Attr.getNumArgs() != 0) {
1627 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1631 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1633 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1634 << Attr.getName() << 0 /*function*/;
1638 if (!Fn->isInlineSpecified()) {
1639 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
1643 d->addAttr(::new (S.Context) GNUInlineAttr());
1646 static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1647 // check the attribute arguments.
1648 if (Attr.getNumArgs() != 1) {
1649 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1653 if (!isFunctionOrMethod(d)) {
1654 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1655 << Attr.getName() << 0 /*function*/;
1659 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1660 llvm::APSInt NumParams(32);
1661 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1662 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1663 << "regparm" << NumParamsExpr->getSourceRange();
1667 if (S.Context.Target.getRegParmMax() == 0) {
1668 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
1669 << NumParamsExpr->getSourceRange();
1673 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
1674 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1675 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
1679 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
1682 static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1683 // check the attribute arguments.
1684 if (Attr.getNumArgs() != 0) {
1685 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1689 if (!isa<CXXRecordDecl>(d)
1690 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1691 S.Diag(Attr.getLoc(),
1692 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1693 : diag::warn_attribute_wrong_decl_type)
1694 << Attr.getName() << 7 /*virtual method or class*/;
1698 // FIXME: Conform to C++0x redeclaration rules.
1700 if (d->getAttr<FinalAttr>()) {
1701 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1705 d->addAttr(::new (S.Context) FinalAttr());
1708 //===----------------------------------------------------------------------===//
1709 // C++0x member checking attributes
1710 //===----------------------------------------------------------------------===//
1712 static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1713 if (Attr.getNumArgs() != 0) {
1714 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1718 if (!isa<CXXRecordDecl>(d)) {
1719 S.Diag(Attr.getLoc(),
1720 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1721 : diag::warn_attribute_wrong_decl_type)
1722 << Attr.getName() << 9 /*class*/;
1726 if (d->getAttr<BaseCheckAttr>()) {
1727 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1731 d->addAttr(::new (S.Context) BaseCheckAttr());
1734 static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1735 if (Attr.getNumArgs() != 0) {
1736 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1740 if (!isa<RecordDecl>(d->getDeclContext())) {
1741 // FIXME: It's not the type that's the problem
1742 S.Diag(Attr.getLoc(),
1743 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1744 : diag::warn_attribute_wrong_decl_type)
1745 << Attr.getName() << 11 /*member*/;
1749 // FIXME: Conform to C++0x redeclaration rules.
1751 if (d->getAttr<HidingAttr>()) {
1752 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1756 d->addAttr(::new (S.Context) HidingAttr());
1759 static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1760 if (Attr.getNumArgs() != 0) {
1761 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1765 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1766 // FIXME: It's not the type that's the problem
1767 S.Diag(Attr.getLoc(),
1768 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1769 : diag::warn_attribute_wrong_decl_type)
1770 << Attr.getName() << 10 /*virtual method*/;
1774 // FIXME: Conform to C++0x redeclaration rules.
1776 if (d->getAttr<OverrideAttr>()) {
1777 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1781 d->addAttr(::new (S.Context) OverrideAttr());
1784 //===----------------------------------------------------------------------===//
1785 // Checker-specific attribute handlers.
1786 //===----------------------------------------------------------------------===//
1788 static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1793 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1794 RetTy = MD->getResultType();
1795 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1796 RetTy = FD->getResultType();
1798 SourceLocation L = Attr.getLoc();
1799 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1800 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
1804 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
1805 || RetTy->getAs<ObjCObjectPointerType>())) {
1806 SourceLocation L = Attr.getLoc();
1807 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1808 << SourceRange(L, L) << Attr.getName();
1812 switch (Attr.getKind()) {
1814 assert(0 && "invalid ownership attribute");
1816 case AttributeList::AT_cf_returns_not_retained:
1817 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1819 case AttributeList::AT_ns_returns_not_retained:
1820 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1822 case AttributeList::AT_cf_returns_retained:
1823 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1825 case AttributeList::AT_ns_returns_retained:
1826 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1831 static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1832 return Attr.getKind() == AttributeList::AT_dllimport ||
1833 Attr.getKind() == AttributeList::AT_dllexport;
1836 //===----------------------------------------------------------------------===//
1837 // Top Level Sema Entry Points
1838 //===----------------------------------------------------------------------===//
1840 /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
1841 /// the attribute applies to decls. If the attribute is a type attribute, just
1842 /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1843 /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
1844 static void ProcessDeclAttribute(Scope *scope, Decl *D,
1845 const AttributeList &Attr, Sema &S) {
1846 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1847 // FIXME: Try to deal with other __declspec attributes!
1849 switch (Attr.getKind()) {
1850 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
1851 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
1852 case AttributeList::AT_address_space:
1853 case AttributeList::AT_objc_gc:
1854 case AttributeList::AT_vector_size:
1855 // Ignore these, these are type attributes, handled by
1856 // ProcessTypeAttributes.
1858 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1859 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
1860 case AttributeList::AT_always_inline:
1861 HandleAlwaysInlineAttr (D, Attr, S); break;
1862 case AttributeList::AT_analyzer_noreturn:
1863 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
1864 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1865 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
1866 case AttributeList::AT_carries_dependency:
1867 HandleDependencyAttr (D, Attr, S); break;
1868 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1869 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1870 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1871 case AttributeList::AT_ext_vector_type:
1872 HandleExtVectorTypeAttr(scope, D, Attr, S);
1874 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1875 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1876 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1877 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1878 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1879 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1880 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1881 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1882 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1883 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1884 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
1886 // Checker-specific.
1887 case AttributeList::AT_ns_returns_not_retained:
1888 case AttributeList::AT_cf_returns_not_retained:
1889 case AttributeList::AT_ns_returns_retained:
1890 case AttributeList::AT_cf_returns_retained:
1891 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1893 case AttributeList::AT_reqd_wg_size:
1894 HandleReqdWorkGroupSize(D, Attr, S); break;
1896 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1897 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1898 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1899 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1900 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
1901 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
1902 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1904 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1905 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
1906 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
1907 case AttributeList::AT_transparent_union:
1908 HandleTransparentUnionAttr(D, Attr, S);
1910 case AttributeList::AT_objc_exception:
1911 HandleObjCExceptionAttr(D, Attr, S);
1913 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
1914 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1915 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1916 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1917 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1918 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1919 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1920 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1921 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1922 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
1923 case AttributeList::IgnoredAttribute:
1924 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
1927 case AttributeList::AT_stdcall:
1928 case AttributeList::AT_cdecl:
1929 case AttributeList::AT_fastcall:
1930 // These are all treated as type attributes.
1933 // Ask target about the attribute.
1934 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
1935 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
1936 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1941 /// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1942 /// attribute list to the specified decl, ignoring any type attributes.
1943 void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
1944 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
1945 ProcessDeclAttribute(S, D, *l, *this);
1949 // static int a9 __attribute__((weakref));
1950 // but that looks really pointless. We reject it.
1951 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
1952 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
1953 dyn_cast<NamedDecl>(D)->getNameAsString();
1958 /// DeclClonePragmaWeak - clone existing decl (maybe definition),
1959 /// #pragma weak needs a non-definition decl and source may not have one
1960 NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
1961 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
1962 NamedDecl *NewD = 0;
1963 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1964 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1965 FD->getLocation(), DeclarationName(II),
1966 FD->getType(), FD->getTypeSourceInfo());
1967 if (FD->getQualifier()) {
1968 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
1969 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
1971 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1972 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1973 VD->getLocation(), II,
1974 VD->getType(), VD->getTypeSourceInfo(),
1975 VD->getStorageClass());
1976 if (VD->getQualifier()) {
1977 VarDecl *NewVD = cast<VarDecl>(NewD);
1978 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
1984 /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1985 /// applied to it, possibly with an alias.
1986 void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
1987 if (W.getUsed()) return; // only do this once
1989 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1990 IdentifierInfo *NDId = ND->getIdentifier();
1991 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1992 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
1993 NewD->addAttr(::new (Context) WeakAttr());
1994 WeakTopLevelDecl.push_back(NewD);
1995 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
1996 // to insert Decl at TU scope, sorry.
1997 DeclContext *SavedContext = CurContext;
1998 CurContext = Context.getTranslationUnitDecl();
1999 PushOnScopeChains(NewD, S);
2000 CurContext = SavedContext;
2001 } else { // just add weak to existing
2002 ND->addAttr(::new (Context) WeakAttr());
2006 /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2007 /// it, apply them to D. This is a bit tricky because PD can have attributes
2008 /// specified in many different places, and we need to find and apply them all.
2009 void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
2010 // Handle #pragma weak
2011 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2012 if (ND->hasLinkage()) {
2013 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2014 if (W != WeakInfo()) {
2015 // Identifier referenced by #pragma weak before it was declared
2016 DeclApplyPragmaWeak(S, ND, W);
2017 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2022 // Apply decl attributes from the DeclSpec if present.
2023 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
2024 ProcessDeclAttributeList(S, D, Attrs);
2026 // Walk the declarator structure, applying decl attributes that were in a type
2027 // position to the decl itself. This handles cases like:
2028 // int *__attr__(x)** D;
2029 // when X is a decl attribute.
2030 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2031 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
2032 ProcessDeclAttributeList(S, D, Attrs);
2034 // Finally, apply any attributes on the decl itself.
2035 if (const AttributeList *Attrs = PD.getAttributes())
2036 ProcessDeclAttributeList(S, D, Attrs);
2039 /// PushParsingDeclaration - Enter a new "scope" of deprecation
2042 /// The state token we use is the start index of this scope
2043 /// on the warning stack.
2044 Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2046 return (ParsingDeclStackState) DelayedDiagnostics.size();
2049 void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2050 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2053 if (DelayedDiagnostics.empty())
2056 unsigned SavedIndex = (unsigned) S;
2057 assert(SavedIndex <= DelayedDiagnostics.size() &&
2058 "saved index is out of bounds");
2060 unsigned E = DelayedDiagnostics.size();
2062 // We only want to actually emit delayed diagnostics when we
2063 // successfully parsed a decl.
2064 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2066 // We really do want to start with 0 here. We get one push for a
2067 // decl spec and another for each declarator; in a decl group like:
2068 // deprecated_typedef foo, *bar, baz();
2069 // only the declarator pops will be passed decls. This is correct;
2070 // we really do need to consider delayed diagnostics from the decl spec
2071 // for each of the different declarations.
2072 for (unsigned I = 0; I != E; ++I) {
2073 if (DelayedDiagnostics[I].Triggered)
2076 switch (DelayedDiagnostics[I].Kind) {
2077 case DelayedDiagnostic::Deprecation:
2078 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2081 case DelayedDiagnostic::Access:
2082 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2088 // Destroy all the delayed diagnostics we're about to pop off.
2089 for (unsigned I = SavedIndex; I != E; ++I)
2090 DelayedDiagnostics[I].destroy();
2092 DelayedDiagnostics.set_size(SavedIndex);
2095 static bool isDeclDeprecated(Decl *D) {
2097 if (D->hasAttr<DeprecatedAttr>())
2099 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2103 void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2105 if (isDeclDeprecated(Ctx))
2108 DD.Triggered = true;
2109 Diag(DD.Loc, diag::warn_deprecated)
2110 << DD.DeprecationData.Decl->getDeclName();
2113 void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2114 // Delay if we're currently parsing a declaration.
2115 if (ParsingDeclDepth) {
2116 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
2120 // Otherwise, don't warn if our current context is deprecated.
2121 if (isDeclDeprecated(cast<Decl>(CurContext)))
2124 Diag(Loc, diag::warn_deprecated) << D->getDeclName();