blob: 54ffa95ce1bbd4a57b33ae664c2b37f9c534637c [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremeneka18d7d82009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000039
Chris Lattner6b6b5372008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000044
John McCall183700f2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000046}
47
Daniel Dunbar35682492008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopesd20254f2009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000062}
63
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000064/// 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.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
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();
74 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076}
77
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000084 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000086 return true;
87 }
88}
89
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).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000106
Chris Lattner89951a82009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000108}
109
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000121 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000122 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner6b6b5372008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000131
John McCall506b57e2010-05-17 21:00:27 +0000132 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
133 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000134 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000135
John McCall506b57e2010-05-17 21:00:27 +0000136 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000137
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000153 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000163// 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).
166
Mike Stumpbf916502009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000172 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 }
Mike Stumpbf916502009-07-24 19:02:52 +0000174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000193
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());
197 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204}
205
Chris Lattner803d0802008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 else
Anders Carlssona860e752009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226}
227
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpbf916502009-07-24 19:02:52 +0000234
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000235 // 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());
239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static 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;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000255 d->addAttr(::new (S.Context) IBOutletAttr());
256 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000257 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000260}
261
Ted Kremenek857e9182010-05-19 17:38:06 +0000262static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
263 Sema &S) {
264
265 // The iboutletcollection attribute can have zero or one arguments.
266 if (Attr.getNumArgs() > 1) {
267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
268 return;
269 }
270
271 // The IBOutletCollection attributes only apply to instance variables of
272 // Objective-C classes.
273 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
274 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
275 return;
276 }
277
278 // FIXME: Eventually accept the type argument.
279 d->addAttr(::new (S.Context) IBOutletCollectionAttr());
280}
281
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000282static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000283 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
284 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000285 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000286 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000287 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000288 return;
289 }
Mike Stumpbf916502009-07-24 19:02:52 +0000290
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000291 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000292
293 // The nonnull attribute only applies to pointers.
294 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000295
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000296 for (AttributeList::arg_iterator I=Attr.arg_begin(),
297 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000298
299
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000300 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000301 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000302 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000303 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
304 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000305 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
306 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000307 return;
308 }
Mike Stumpbf916502009-07-24 19:02:52 +0000309
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000310 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000311
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000312 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000314 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000315 return;
316 }
Mike Stumpbf916502009-07-24 19:02:52 +0000317
Ted Kremenek465172f2008-07-21 22:09:15 +0000318 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000319
320 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000321 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000322 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000323 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000324 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
325 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000326 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000327 }
Mike Stumpbf916502009-07-24 19:02:52 +0000328
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329 NonNullArgs.push_back(x);
330 }
Mike Stumpbf916502009-07-24 19:02:52 +0000331
332 // If no arguments were specified to __attribute__((nonnull)) then all pointer
333 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000334 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000335 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
336 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000337 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000338 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000339 }
Mike Stumpbf916502009-07-24 19:02:52 +0000340
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000341 if (NonNullArgs.empty()) {
342 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
343 return;
344 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000345 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000346
347 unsigned* start = &NonNullArgs[0];
348 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000349 llvm::array_pod_sort(start, start + size);
Ted Kremenek59616112010-02-11 07:31:47 +0000350 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351}
352
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000353static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
354 // This attribute must be applied to a function declaration.
355 // The first argument to the attribute must be a string,
356 // the name of the resource, for example "malloc".
357 // The following arguments must be argument indexes, the arguments must be
358 // of integer type for Returns, otherwise of pointer type.
359 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000360 // after being held. free() should be __attribute((ownership_takes)), whereas
361 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000362
363 if (!AL.getParameterName()) {
364 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
365 << AL.getName()->getName() << 1;
366 return;
367 }
368 // Figure out our Kind, and check arguments while we're at it.
Jordy Rose2a479922010-08-12 08:54:03 +0000369 attr::Kind K;
370 switch (AL.getKind()) {
371 case AttributeList::AT_ownership_takes:
372 K = attr::OwnershipTakes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000373 if (AL.getNumArgs() < 1) {
374 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
375 return;
376 }
Jordy Rose2a479922010-08-12 08:54:03 +0000377 break;
378 case AttributeList::AT_ownership_holds:
379 K = attr::OwnershipHolds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000380 if (AL.getNumArgs() < 1) {
381 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
382 return;
383 }
Jordy Rose2a479922010-08-12 08:54:03 +0000384 break;
385 case AttributeList::AT_ownership_returns:
386 K = attr::OwnershipReturns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000387 if (AL.getNumArgs() > 1) {
388 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
389 << AL.getNumArgs() + 1;
390 return;
391 }
Jordy Rose2a479922010-08-12 08:54:03 +0000392 break;
393 default:
394 // This should never happen given how we are called.
395 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000396 }
397
398 if (!isFunction(d) || !hasFunctionProto(d)) {
399 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
400 << 0 /*function*/;
401 return;
402 }
403
404 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
405
406 llvm::StringRef Module = AL.getParameterName()->getName();
407
408 // Normalize the argument, __foo__ becomes foo.
409 if (Module.startswith("__") && Module.endswith("__"))
410 Module = Module.substr(2, Module.size() - 4);
411
412 llvm::SmallVector<unsigned, 10> OwnershipArgs;
413
Jordy Rose2a479922010-08-12 08:54:03 +0000414 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
415 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000416
417 Expr *IdxExpr = static_cast<Expr *>(*I);
418 llvm::APSInt ArgNum(32);
419 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
420 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
421 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
422 << AL.getName()->getName() << IdxExpr->getSourceRange();
423 continue;
424 }
425
426 unsigned x = (unsigned) ArgNum.getZExtValue();
427
428 if (x > NumArgs || x < 1) {
429 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
430 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
431 continue;
432 }
433 --x;
434 switch (K) {
Jordy Rose2a479922010-08-12 08:54:03 +0000435 case attr::OwnershipTakes:
436 case attr::OwnershipHolds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000437 // Is the function argument a pointer type?
438 QualType T = getFunctionOrMethodArgType(d, x);
439 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
440 // FIXME: Should also highlight argument in decl.
441 S.Diag(AL.getLoc(), diag::err_ownership_type)
Jordy Rose2a479922010-08-12 08:54:03 +0000442 << ((K==attr::OwnershipTakes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000443 << "pointer"
444 << IdxExpr->getSourceRange();
445 continue;
446 }
447 break;
448 }
Jordy Rose2a479922010-08-12 08:54:03 +0000449 case attr::OwnershipReturns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000450 if (AL.getNumArgs() > 1) {
451 // Is the function argument an integer type?
452 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
453 llvm::APSInt ArgNum(32);
454 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
455 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
456 S.Diag(AL.getLoc(), diag::err_ownership_type)
457 << "ownership_returns" << "integer"
458 << IdxExpr->getSourceRange();
459 return;
460 }
461 }
462 break;
463 }
Jordy Rose2a479922010-08-12 08:54:03 +0000464 default:
465 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000466 } // switch
467
468 // Check we don't have a conflict with another ownership attribute.
Jordy Rose2a479922010-08-12 08:54:03 +0000469 if (K != attr::OwnershipReturns && d->hasAttrs()) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000470 for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) {
471 if (const OwnershipAttr* Att = dyn_cast<OwnershipAttr>(attr)) {
472 // Two ownership attributes of the same kind can't conflict,
473 // except returns attributes.
Jordy Rose2a479922010-08-12 08:54:03 +0000474 if (Att->getKind() != K) {
475 for (const unsigned *I = Att->begin(), *E = Att->end(); I!=E; ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000476 if (x == *I) {
477 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
478 << AL.getName()->getName() << "ownership_*";
479 }
480 }
481 }
482 }
483 }
484 }
485 OwnershipArgs.push_back(x);
486 }
487
488 unsigned* start = OwnershipArgs.data();
489 unsigned size = OwnershipArgs.size();
490 llvm::array_pod_sort(start, start + size);
491 switch (K) {
Jordy Rose2a479922010-08-12 08:54:03 +0000492 case attr::OwnershipTakes: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000493 if (OwnershipArgs.empty()) {
494 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
495 return;
496 }
497 d->addAttr(::new (S.Context) OwnershipTakesAttr(S.Context, start, size,
498 Module));
499 break;
500 }
Jordy Rose2a479922010-08-12 08:54:03 +0000501 case attr::OwnershipHolds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000502 if (OwnershipArgs.empty()) {
503 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
504 return;
505 }
506 d->addAttr(::new (S.Context) OwnershipHoldsAttr(S.Context, start, size,
507 Module));
508 break;
509 }
Jordy Rose2a479922010-08-12 08:54:03 +0000510 case attr::OwnershipReturns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000511 d->addAttr(::new (S.Context) OwnershipReturnsAttr(S.Context, start, size,
512 Module));
513 break;
514 }
515 default:
Jordy Rose2a479922010-08-12 08:54:03 +0000516 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000517 }
518}
519
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000520static bool isStaticVarOrStaticFunciton(Decl *D) {
521 if (VarDecl *VD = dyn_cast<VarDecl>(D))
522 return VD->getStorageClass() == VarDecl::Static;
523 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
524 return FD->getStorageClass() == FunctionDecl::Static;
525 return false;
526}
527
528static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
529 // Check the attribute arguments.
530 if (Attr.getNumArgs() > 1) {
531 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
532 return;
533 }
534
535 // gcc rejects
536 // class c {
537 // static int a __attribute__((weakref ("v2")));
538 // static int b() __attribute__((weakref ("f3")));
539 // };
540 // and ignores the attributes of
541 // void f(void) {
542 // static int a __attribute__((weakref ("v2")));
543 // }
544 // we reject them
545 if (const DeclContext *Ctx = d->getDeclContext()) {
546 Ctx = Ctx->getLookupContext();
547 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
548 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000549 dyn_cast<NamedDecl>(d)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000550 return;
551 }
552 }
553
554 // The GCC manual says
555 //
556 // At present, a declaration to which `weakref' is attached can only
557 // be `static'.
558 //
559 // It also says
560 //
561 // Without a TARGET,
562 // given as an argument to `weakref' or to `alias', `weakref' is
563 // equivalent to `weak'.
564 //
565 // gcc 4.4.1 will accept
566 // int a7 __attribute__((weakref));
567 // as
568 // int a7 __attribute__((weak));
569 // This looks like a bug in gcc. We reject that for now. We should revisit
570 // it if this behaviour is actually used.
571
572 if (!isStaticVarOrStaticFunciton(d)) {
573 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
574 dyn_cast<NamedDecl>(d)->getNameAsString();
575 return;
576 }
577
578 // GCC rejects
579 // static ((alias ("y"), weakref)).
580 // Should we? How to check that weakref is before or after alias?
581
582 if (Attr.getNumArgs() == 1) {
583 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
584 Arg = Arg->IgnoreParenCasts();
585 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
586
587 if (Str == 0 || Str->isWide()) {
588 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
589 << "weakref" << 1;
590 return;
591 }
592 // GCC will accept anything as the argument of weakref. Should we
593 // check for an existing decl?
594 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
595 }
596
597 d->addAttr(::new (S.Context) WeakRefAttr());
598}
599
Chris Lattner803d0802008-06-29 00:43:07 +0000600static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000601 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000602 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000604 return;
605 }
Mike Stumpbf916502009-07-24 19:02:52 +0000606
Chris Lattner545dd342008-06-28 23:36:30 +0000607 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000608 Arg = Arg->IgnoreParenCasts();
609 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000610
Chris Lattner6b6b5372008-06-26 18:38:35 +0000611 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000612 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000613 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000614 return;
615 }
Mike Stumpbf916502009-07-24 19:02:52 +0000616
Chris Lattner6b6b5372008-06-26 18:38:35 +0000617 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000618
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000619 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000620}
621
Mike Stumpbf916502009-07-24 19:02:52 +0000622static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000623 Sema &S) {
624 // check the attribute arguments.
625 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000626 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000627 return;
628 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000629
Chris Lattnerc5197432009-04-14 17:02:11 +0000630 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000632 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000633 return;
634 }
Mike Stumpbf916502009-07-24 19:02:52 +0000635
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000636 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000637}
638
Ryan Flynn76168e22009-08-09 20:07:29 +0000639static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
640 // check the attribute arguments.
641 if (Attr.getNumArgs() != 0) {
642 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
643 return;
644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000646 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000647 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000648 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
649 d->addAttr(::new (S.Context) MallocAttr());
650 return;
651 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000652 }
653
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000654 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000655}
656
Ted Kremenekb7252322009-04-10 00:01:14 +0000657static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnarae215f722010-04-30 13:10:51 +0000658 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000659 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000660 if (Attr.getNumArgs() != 0) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000661 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000662 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000663 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000664
Mike Stump19c30c02009-04-29 19:03:13 +0000665 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
666 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000667 if (VD == 0 || (!VD->getType()->isBlockPointerType()
668 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000669 S.Diag(Attr.getLoc(),
670 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
671 : diag::warn_attribute_wrong_decl_type)
672 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000673 return false;
674 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000675 }
Mike Stumpbf916502009-07-24 19:02:52 +0000676
Ted Kremenekb7252322009-04-10 00:01:14 +0000677 return true;
678}
679
680static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000681 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
682 assert(Attr.isInvalid() == false);
683 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000684}
685
686static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
687 Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000688 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000689 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000690}
691
John Thompson35cc9622010-08-09 21:53:52 +0000692// PS3 PPU-specific.
693static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
694 Sema &S) {
695/*
696 Returning a Vector Class in Registers
697
698 According to the PPU ABI specifications, a class with a single member of vector type is returned in
699 memory when used as the return value of a function. This results in inefficient code when implementing
700 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
701 definition. This attribute is also applicable to struct types.
702
703 Example:
704
705 struct Vector
706 {
707 __vector float xyzw;
708 } __attribute__((vecreturn));
709
710 Vector Add(Vector lhs, Vector rhs)
711 {
712 Vector result;
713 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
714 return result; // This will be returned in a register
715 }
716*/
717 if (!isa<CXXRecordDecl>(d)) {
718 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
719 << Attr.getName() << 9 /*class*/;
720 return;
721 }
722
723 if (d->getAttr<VecReturnAttr>()) {
724 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
725 return;
726 }
727
728 d->addAttr(::new (S.Context) VecReturnAttr());
729}
730
Sean Huntbbd37c62009-11-21 08:43:09 +0000731static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
732 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
733 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000734 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000735 return;
736 }
737 // FIXME: Actually store the attribute on the declaration
738}
739
Ted Kremenek73798892008-07-25 04:39:19 +0000740static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
741 // check the attribute arguments.
742 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000743 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000744 return;
745 }
Mike Stumpbf916502009-07-24 19:02:52 +0000746
John McCallaec58602010-03-31 02:47:45 +0000747 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
748 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000750 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000751 return;
752 }
Mike Stumpbf916502009-07-24 19:02:52 +0000753
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000754 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000755}
756
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000757static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
758 // check the attribute arguments.
759 if (Attr.getNumArgs() != 0) {
760 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
761 return;
762 }
Mike Stumpbf916502009-07-24 19:02:52 +0000763
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000764 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000765 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000766 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
767 return;
768 }
769 } else if (!isFunctionOrMethod(d)) {
770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000771 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000772 return;
773 }
Mike Stumpbf916502009-07-24 19:02:52 +0000774
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000775 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000776}
777
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000778static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
779 // check the attribute arguments.
780 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
782 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000783 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000784 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000785
786 int priority = 65535; // FIXME: Do not hardcode such constants.
787 if (Attr.getNumArgs() > 0) {
788 Expr *E = static_cast<Expr *>(Attr.getArg(0));
789 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000790 if (E->isTypeDependent() || E->isValueDependent() ||
791 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000792 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000793 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000794 return;
795 }
796 priority = Idx.getZExtValue();
797 }
Mike Stumpbf916502009-07-24 19:02:52 +0000798
Chris Lattnerc5197432009-04-14 17:02:11 +0000799 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000800 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000801 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000802 return;
803 }
804
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000805 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000806}
807
808static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
809 // check the attribute arguments.
810 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000811 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
812 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000813 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000814 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000815
816 int priority = 65535; // FIXME: Do not hardcode such constants.
817 if (Attr.getNumArgs() > 0) {
818 Expr *E = static_cast<Expr *>(Attr.getArg(0));
819 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000820 if (E->isTypeDependent() || E->isValueDependent() ||
821 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000822 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000823 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000824 return;
825 }
826 priority = Idx.getZExtValue();
827 }
Mike Stumpbf916502009-07-24 19:02:52 +0000828
Anders Carlsson6782fc62008-08-22 22:10:48 +0000829 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000830 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000831 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000832 return;
833 }
834
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000835 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000836}
837
Chris Lattner803d0802008-06-29 00:43:07 +0000838static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000839 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000840 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000841 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000842 return;
843 }
Mike Stumpbf916502009-07-24 19:02:52 +0000844
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000845 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000846}
847
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000848static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
849 // check the attribute arguments.
850 if (Attr.getNumArgs() != 0) {
851 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
852 return;
853 }
Mike Stumpbf916502009-07-24 19:02:52 +0000854
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000855 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000856}
857
Chris Lattner803d0802008-06-29 00:43:07 +0000858static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000859 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000860 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000861 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000862 return;
863 }
Mike Stumpbf916502009-07-24 19:02:52 +0000864
Chris Lattner545dd342008-06-28 23:36:30 +0000865 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000866 Arg = Arg->IgnoreParenCasts();
867 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000868
Chris Lattner6b6b5372008-06-26 18:38:35 +0000869 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000871 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000872 return;
873 }
Mike Stumpbf916502009-07-24 19:02:52 +0000874
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000875 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000876 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000877
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000878 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000879 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000880 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000881 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000882 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000883 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000884 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000885 type = VisibilityAttr::ProtectedVisibility;
886 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000887 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000888 return;
889 }
Mike Stumpbf916502009-07-24 19:02:52 +0000890
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000891 d->addAttr(::new (S.Context) VisibilityAttr(type, false));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000892}
893
Chris Lattner0db29ec2009-02-14 08:09:34 +0000894static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
895 Sema &S) {
896 if (Attr.getNumArgs() != 0) {
897 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
898 return;
899 }
Mike Stumpbf916502009-07-24 19:02:52 +0000900
Chris Lattner0db29ec2009-02-14 08:09:34 +0000901 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
902 if (OCI == 0) {
903 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
904 return;
905 }
Mike Stumpbf916502009-07-24 19:02:52 +0000906
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000907 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000908}
909
910static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000911 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000912 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000913 return;
914 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000915 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000916 QualType T = TD->getUnderlyingType();
917 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000918 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000919 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
920 return;
921 }
922 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000923 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000924}
925
Mike Stumpbf916502009-07-24 19:02:52 +0000926static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000927HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
928 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000930 return;
931 }
932
933 if (!isa<FunctionDecl>(D)) {
934 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
935 return;
936 }
937
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000938 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000939}
940
Steve Naroff9eae5762008-09-18 16:44:58 +0000941static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000942 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000943 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000944 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000945 return;
946 }
Mike Stumpbf916502009-07-24 19:02:52 +0000947
Steve Naroff9eae5762008-09-18 16:44:58 +0000948 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000949 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000950 return;
951 }
Mike Stumpbf916502009-07-24 19:02:52 +0000952
Steve Naroff9eae5762008-09-18 16:44:58 +0000953 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000954 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000955 type = BlocksAttr::ByRef;
956 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000957 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000958 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000959 return;
960 }
Mike Stumpbf916502009-07-24 19:02:52 +0000961
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000962 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000963}
964
Anders Carlsson77091822008-10-05 18:05:59 +0000965static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
966 // check the attribute arguments.
967 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000968 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
969 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000970 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000971 }
972
Anders Carlsson77091822008-10-05 18:05:59 +0000973 int sentinel = 0;
974 if (Attr.getNumArgs() > 0) {
975 Expr *E = static_cast<Expr *>(Attr.getArg(0));
976 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000977 if (E->isTypeDependent() || E->isValueDependent() ||
978 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000979 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000980 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000981 return;
982 }
983 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000984
Anders Carlsson77091822008-10-05 18:05:59 +0000985 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000986 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
987 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000988 return;
989 }
990 }
991
992 int nullPos = 0;
993 if (Attr.getNumArgs() > 1) {
994 Expr *E = static_cast<Expr *>(Attr.getArg(1));
995 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000996 if (E->isTypeDependent() || E->isValueDependent() ||
997 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000998 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000999 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001000 return;
1001 }
1002 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001003
Anders Carlsson77091822008-10-05 18:05:59 +00001004 if (nullPos > 1 || nullPos < 0) {
1005 // FIXME: This error message could be improved, it would be nice
1006 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001007 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1008 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001009 return;
1010 }
1011 }
1012
1013 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001014 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001015 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001016
Chris Lattner897cd902009-03-17 23:03:47 +00001017 if (isa<FunctionNoProtoType>(FT)) {
1018 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1019 return;
1020 }
Mike Stumpbf916502009-07-24 19:02:52 +00001021
Chris Lattner897cd902009-03-17 23:03:47 +00001022 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001023 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001024 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001025 }
Anders Carlsson77091822008-10-05 18:05:59 +00001026 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1027 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001028 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001029 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001030 }
1031 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001032 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1033 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001034 ;
1035 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001036 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001037 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001038 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001039 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001040 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001041 int m = Ty->isFunctionPointerType() ? 0 : 1;
1042 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001043 return;
1044 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001045 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001047 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001048 return;
1049 }
Anders Carlsson77091822008-10-05 18:05:59 +00001050 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001051 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001052 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001053 return;
1054 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001055 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001056}
1057
Chris Lattner026dc962009-02-14 07:37:35 +00001058static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1059 // check the attribute arguments.
1060 if (Attr.getNumArgs() != 0) {
1061 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1062 return;
1063 }
1064
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001065 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001066 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001067 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001068 return;
1069 }
Mike Stumpbf916502009-07-24 19:02:52 +00001070
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001071 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1072 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1073 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001074 return;
1075 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001076 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1077 if (MD->getResultType()->isVoidType()) {
1078 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1079 << Attr.getName() << 1;
1080 return;
1081 }
1082
Nuno Lopesd20254f2009-12-20 23:11:08 +00001083 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +00001084}
1085
1086static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001087 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001088 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001089 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001090 return;
1091 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001092
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001093 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001094 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001095 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1096 dyn_cast<NamedDecl>(D)->getNameAsString();
1097 return;
1098 }
1099
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001100 // TODO: could also be applied to methods?
1101 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1102 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001103 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001104 return;
1105 }
Mike Stumpbf916502009-07-24 19:02:52 +00001106
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001107 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001108}
1109
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001110static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1111 // check the attribute arguments.
1112 if (Attr.getNumArgs() != 0) {
1113 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1114 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001115 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001116
1117 // weak_import only applies to variable & function declarations.
1118 bool isDef = false;
1119 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1120 isDef = (!VD->hasExternalStorage() || VD->getInit());
1121 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001122 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001123 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1124 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001125 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001126 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001127 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001128 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001129 !isa<ObjCInterfaceDecl>(D))
1130 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001131 << Attr.getName() << 2 /*variable and function*/;
1132 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001133 }
1134
1135 // Merge should handle any subsequent violations.
1136 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001137 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001138 diag::warn_attribute_weak_import_invalid_on_definition)
1139 << "weak_import" << 2 /*variable and function*/;
1140 return;
1141 }
1142
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001143 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001144}
1145
Nate Begeman6f3d8382009-06-26 06:32:41 +00001146static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1147 Sema &S) {
1148 // Attribute has 3 arguments.
1149 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001150 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001151 return;
1152 }
1153
1154 unsigned WGSize[3];
1155 for (unsigned i = 0; i < 3; ++i) {
1156 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1157 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001158 if (E->isTypeDependent() || E->isValueDependent() ||
1159 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001160 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1161 << "reqd_work_group_size" << E->getSourceRange();
1162 return;
1163 }
1164 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1165 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001166 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001167 WGSize[2]));
1168}
1169
Chris Lattner026dc962009-02-14 07:37:35 +00001170static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001171 // Attribute has no arguments.
1172 if (Attr.getNumArgs() != 1) {
1173 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1174 return;
1175 }
1176
1177 // Make sure that there is a string literal as the sections's single
1178 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001179 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1180 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001181 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001182 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001183 return;
1184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattner797c3c42009-08-10 19:03:04 +00001186 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001187 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001188 if (!Error.empty()) {
1189 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1190 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001191 return;
1192 }
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001194 // This attribute cannot be applied to local variables.
1195 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1196 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1197 return;
1198 }
1199
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001200 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001201}
1202
Chris Lattner6b6b5372008-06-26 18:38:35 +00001203
Chris Lattner803d0802008-06-29 00:43:07 +00001204static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001205 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001206 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001207 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001208 return;
1209 }
Mike Stumpbf916502009-07-24 19:02:52 +00001210
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001211 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001212}
1213
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001214static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1215 // check the attribute arguments.
1216 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001218 return;
1219 }
Mike Stumpbf916502009-07-24 19:02:52 +00001220
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001221 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001222}
1223
1224static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1225 // check the attribute arguments.
1226 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001228 return;
1229 }
Mike Stumpbf916502009-07-24 19:02:52 +00001230
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001231 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001232}
1233
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001234static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001235 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001236 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1237 return;
1238 }
Mike Stumpbf916502009-07-24 19:02:52 +00001239
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001240 if (Attr.getNumArgs() != 0) {
1241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1242 return;
1243 }
Mike Stumpbf916502009-07-24 19:02:52 +00001244
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001245 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001246
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001247 if (!VD || !VD->hasLocalStorage()) {
1248 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1249 return;
1250 }
Mike Stumpbf916502009-07-24 19:02:52 +00001251
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001252 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001253 // FIXME: Lookup probably isn't looking in the right place
1254 // FIXME: The lookup source location should be in the attribute, not the
1255 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001256 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001257 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001258 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001259 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001260 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001261 Attr.getParameterName();
1262 return;
1263 }
Mike Stumpbf916502009-07-24 19:02:52 +00001264
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001265 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1266 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001267 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001268 Attr.getParameterName();
1269 return;
1270 }
1271
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001272 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001274 Attr.getParameterName();
1275 return;
1276 }
Mike Stumpbf916502009-07-24 19:02:52 +00001277
Anders Carlsson89941c12009-02-07 23:16:50 +00001278 // We're currently more strict than GCC about what function types we accept.
1279 // If this ever proves to be a problem it should be easy to fix.
1280 QualType Ty = S.Context.getPointerType(VD->getType());
1281 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001282 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001283 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001284 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1285 Attr.getParameterName() << ParamTy << Ty;
1286 return;
1287 }
Mike Stumpbf916502009-07-24 19:02:52 +00001288
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001289 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001290}
1291
Mike Stumpbf916502009-07-24 19:02:52 +00001292/// Handle __attribute__((format_arg((idx)))) attribute based on
1293/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1294static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001295 if (Attr.getNumArgs() != 1) {
1296 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1297 return;
1298 }
1299 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1301 << Attr.getName() << 0 /*function*/;
1302 return;
1303 }
Mike Stumpbf916502009-07-24 19:02:52 +00001304 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1305 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001306 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1307 unsigned FirstIdx = 1;
1308 // checks for the 2nd argument
1309 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1310 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001311 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1312 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1314 << "format" << 2 << IdxExpr->getSourceRange();
1315 return;
1316 }
Mike Stumpbf916502009-07-24 19:02:52 +00001317
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001318 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1319 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1320 << "format" << 2 << IdxExpr->getSourceRange();
1321 return;
1322 }
Mike Stumpbf916502009-07-24 19:02:52 +00001323
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001324 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001325
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001326 // make sure the format string is really a string
1327 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001328
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001329 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1330 if (not_nsstring_type &&
1331 !isCFStringType(Ty, S.Context) &&
1332 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001333 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001334 // FIXME: Should highlight the actual expression that has the wrong type.
1335 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001336 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001337 << IdxExpr->getSourceRange();
1338 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001339 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001340 Ty = getFunctionOrMethodResultType(d);
1341 if (!isNSStringType(Ty, S.Context) &&
1342 !isCFStringType(Ty, S.Context) &&
1343 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001344 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001345 // FIXME: Should highlight the actual expression that has the wrong type.
1346 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001347 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001348 << IdxExpr->getSourceRange();
1349 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001350 }
1351
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001352 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001353}
1354
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001355enum FormatAttrKind {
1356 CFStringFormat,
1357 NSStringFormat,
1358 StrftimeFormat,
1359 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001360 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001361 InvalidFormat
1362};
1363
1364/// getFormatAttrKind - Map from format attribute names to supported format
1365/// types.
1366static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1367 // Check for formats that get handled specially.
1368 if (Format == "NSString")
1369 return NSStringFormat;
1370 if (Format == "CFString")
1371 return CFStringFormat;
1372 if (Format == "strftime")
1373 return StrftimeFormat;
1374
1375 // Otherwise, check for supported formats.
1376 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1377 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1378 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1379 Format == "zcmn_err")
1380 return SupportedFormat;
1381
Duncan Sandsbc525952010-03-23 14:44:19 +00001382 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1383 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001384 return IgnoredFormat;
1385
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001386 return InvalidFormat;
1387}
1388
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001389/// Handle __attribute__((init_priority(priority))) attributes based on
1390/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1391static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1392 Sema &S) {
1393 if (!S.getLangOptions().CPlusPlus) {
1394 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1395 return;
1396 }
1397
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001398 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1399 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1400 Attr.setInvalid();
1401 return;
1402 }
1403 QualType T = dyn_cast<VarDecl>(d)->getType();
1404 if (S.Context.getAsArrayType(T))
1405 T = S.Context.getBaseElementType(T);
1406 if (!T->getAs<RecordType>()) {
1407 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1408 Attr.setInvalid();
1409 return;
1410 }
1411
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001412 if (Attr.getNumArgs() != 1) {
1413 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1414 Attr.setInvalid();
1415 return;
1416 }
1417 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001418
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001419 llvm::APSInt priority(32);
1420 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1421 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1422 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1423 << "init_priority" << priorityExpr->getSourceRange();
1424 Attr.setInvalid();
1425 return;
1426 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001427 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001428 if (prioritynum < 101 || prioritynum > 65535) {
1429 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1430 << priorityExpr->getSourceRange();
1431 Attr.setInvalid();
1432 return;
1433 }
1434 d->addAttr(::new (S.Context) InitPriorityAttr(prioritynum));
1435}
1436
Mike Stumpbf916502009-07-24 19:02:52 +00001437/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1438/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001439static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001440
Chris Lattner545dd342008-06-28 23:36:30 +00001441 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001442 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001443 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001444 return;
1445 }
1446
Chris Lattner545dd342008-06-28 23:36:30 +00001447 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001448 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001449 return;
1450 }
1451
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001452 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001453 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001454 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001455 return;
1456 }
1457
Daniel Dunbar35682492008-09-26 04:12:28 +00001458 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001459 unsigned FirstIdx = 1;
1460
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001461 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001462
1463 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001464 if (Format.startswith("__") && Format.endswith("__"))
1465 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001466
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001467 // Check for supported formats.
1468 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001469
1470 if (Kind == IgnoredFormat)
1471 return;
1472
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001473 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001474 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001475 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001476 return;
1477 }
1478
1479 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001480 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001481 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001482 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1483 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001484 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001485 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001486 return;
1487 }
1488
Anders Carlsson4fb77202009-08-25 14:12:34 +00001489 // FIXME: We should handle the implicit 'this' parameter in a more generic
1490 // way that can be used for other arguments.
1491 bool HasImplicitThisParam = false;
1492 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1493 if (MD->isInstance()) {
1494 HasImplicitThisParam = true;
1495 NumArgs++;
1496 }
1497 }
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Chris Lattner6b6b5372008-06-26 18:38:35 +00001499 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001501 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001502 return;
1503 }
1504
1505 // FIXME: Do we need to bounds check?
1506 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001507
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001508 if (HasImplicitThisParam) {
1509 if (ArgIdx == 0) {
1510 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1511 << "a string type" << IdxExpr->getSourceRange();
1512 return;
1513 }
1514 ArgIdx--;
1515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattner6b6b5372008-06-26 18:38:35 +00001517 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001518 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001519
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001520 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001521 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001522 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1523 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001524 return;
1525 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001526 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001527 // FIXME: do we need to check if the type is NSString*? What are the
1528 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001529 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001530 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001531 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1532 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001533 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001534 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001535 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001536 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001537 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001538 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1539 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001540 return;
1541 }
1542
1543 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001544 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001545 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001546 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1547 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001549 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001550 return;
1551 }
1552
1553 // check if the function is variadic if the 3rd argument non-zero
1554 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001555 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001556 ++NumArgs; // +1 for ...
1557 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001558 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001559 return;
1560 }
1561 }
1562
Chris Lattner3c73c412008-11-19 08:23:25 +00001563 // strftime requires FirstArg to be 0 because it doesn't read from any
1564 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001565 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001566 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001567 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1568 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001569 return;
1570 }
1571 // if 0 it disables parameter checking (to use with e.g. va_list)
1572 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001573 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001574 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001575 return;
1576 }
1577
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001578 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001579 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001580}
1581
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001582static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1583 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001584 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001585 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001587 return;
1588 }
1589
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001590 // Try to find the underlying union declaration.
1591 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001592 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001593 if (TD && TD->getUnderlyingType()->isUnionType())
1594 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1595 else
1596 RD = dyn_cast<RecordDecl>(d);
1597
1598 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001600 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001601 return;
1602 }
1603
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001604 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001605 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001606 diag::warn_transparent_union_attribute_not_definition);
1607 return;
1608 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001609
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001610 RecordDecl::field_iterator Field = RD->field_begin(),
1611 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001612 if (Field == FieldEnd) {
1613 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1614 return;
1615 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001616
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001617 FieldDecl *FirstField = *Field;
1618 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001619 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001620 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001621 diag::warn_transparent_union_attribute_floating)
1622 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001623 return;
1624 }
1625
1626 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1627 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1628 for (; Field != FieldEnd; ++Field) {
1629 QualType FieldType = Field->getType();
1630 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1631 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1632 // Warn if we drop the attribute.
1633 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001634 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001635 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001636 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001637 diag::warn_transparent_union_attribute_field_size_align)
1638 << isSize << Field->getDeclName() << FieldBits;
1639 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001640 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001641 diag::note_transparent_union_first_field_size_align)
1642 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001643 return;
1644 }
1645 }
1646
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001647 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001648}
1649
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001650static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001651 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001652 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001654 return;
1655 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001656 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1657 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001658
Chris Lattner6b6b5372008-06-26 18:38:35 +00001659 // Make sure that there is a string literal as the annotation's single
1660 // argument.
1661 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001662 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001663 return;
1664 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001665 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001666}
1667
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001668static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001669 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001670 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001672 return;
1673 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001674
1675 //FIXME: The C++0x version of this attribute has more limited applicabilty
1676 // than GNU's, and should error out when it is used to specify a
1677 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001678
Chris Lattner545dd342008-06-28 23:36:30 +00001679 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001680 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001681 // (For now we just use 128 bits which is the maximum on X86).
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001682 D->addAttr(::new (S.Context) AlignedAttr(128));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001683 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001684 }
Mike Stumpbf916502009-07-24 19:02:52 +00001685
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001686 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1687}
1688
1689void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1690 if (E->isTypeDependent() || E->isValueDependent()) {
1691 // Save dependent expressions in the AST to be instantiated.
1692 D->addAttr(::new (Context) AlignedAttr(E));
1693 return;
1694 }
1695
Chris Lattner49e2d342008-06-28 23:50:44 +00001696 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001697 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1698 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1699 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001700 return;
1701 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001702 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001703 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1704 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001705 return;
1706 }
1707
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001708 D->addAttr(::new (Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001709}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001710
Mike Stumpbf916502009-07-24 19:02:52 +00001711/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1712/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001713///
Mike Stumpbf916502009-07-24 19:02:52 +00001714/// Despite what would be logical, the mode attribute is a decl attribute, not a
1715/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1716/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001717static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001718 // This attribute isn't documented, but glibc uses it. It changes
1719 // the width of an int or unsigned int to the specified size.
1720
1721 // Check that there aren't any arguments
1722 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001723 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001724 return;
1725 }
1726
1727 IdentifierInfo *Name = Attr.getParameterName();
1728 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001729 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001730 return;
1731 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001732
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001733 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001734
1735 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001736 if (Str.startswith("__") && Str.endswith("__"))
1737 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001738
1739 unsigned DestWidth = 0;
1740 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001741 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001742 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001743 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001744 switch (Str[0]) {
1745 case 'Q': DestWidth = 8; break;
1746 case 'H': DestWidth = 16; break;
1747 case 'S': DestWidth = 32; break;
1748 case 'D': DestWidth = 64; break;
1749 case 'X': DestWidth = 96; break;
1750 case 'T': DestWidth = 128; break;
1751 }
1752 if (Str[1] == 'F') {
1753 IntegerMode = false;
1754 } else if (Str[1] == 'C') {
1755 IntegerMode = false;
1756 ComplexMode = true;
1757 } else if (Str[1] != 'I') {
1758 DestWidth = 0;
1759 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001760 break;
1761 case 4:
1762 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1763 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001764 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001765 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001766 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001767 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001768 break;
1769 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001770 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001771 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001772 break;
1773 }
1774
1775 QualType OldTy;
1776 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1777 OldTy = TD->getUnderlyingType();
1778 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1779 OldTy = VD->getType();
1780 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001781 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1782 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001783 return;
1784 }
Eli Friedman73397492009-03-03 06:41:03 +00001785
John McCall183700f2009-09-21 23:43:11 +00001786 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001787 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1788 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001789 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001790 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1791 } else if (ComplexMode) {
1792 if (!OldTy->isComplexType())
1793 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1794 } else {
1795 if (!OldTy->isFloatingType())
1796 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1797 }
1798
Mike Stump390b4cc2009-05-16 07:39:55 +00001799 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1800 // and friends, at least with glibc.
1801 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1802 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001803 // FIXME: Make sure floating-point mappings are accurate
1804 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001805 QualType NewTy;
1806 switch (DestWidth) {
1807 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001808 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001809 return;
1810 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001811 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001812 return;
1813 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001814 if (!IntegerMode) {
1815 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1816 return;
1817 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001818 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001819 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001820 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001821 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001822 break;
1823 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001824 if (!IntegerMode) {
1825 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1826 return;
1827 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001828 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001829 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001830 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001831 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001832 break;
1833 case 32:
1834 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001835 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001836 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001837 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001838 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001839 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001840 break;
1841 case 64:
1842 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001843 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001844 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001845 if (S.Context.Target.getLongWidth() == 64)
1846 NewTy = S.Context.LongTy;
1847 else
1848 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001849 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001850 if (S.Context.Target.getLongWidth() == 64)
1851 NewTy = S.Context.UnsignedLongTy;
1852 else
1853 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001854 break;
Eli Friedman73397492009-03-03 06:41:03 +00001855 case 96:
1856 NewTy = S.Context.LongDoubleTy;
1857 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001858 case 128:
1859 if (!IntegerMode) {
1860 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1861 return;
1862 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001863 if (OldTy->isSignedIntegerType())
1864 NewTy = S.Context.Int128Ty;
1865 else
1866 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001867 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001868 }
1869
Eli Friedman73397492009-03-03 06:41:03 +00001870 if (ComplexMode) {
1871 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001872 }
1873
1874 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001875 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1876 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001877 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001878 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001879 cast<ValueDecl>(D)->setType(NewTy);
1880}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001881
Mike Stump1feade82009-08-26 22:31:08 +00001882static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001883 // check the attribute arguments.
1884 if (Attr.getNumArgs() > 0) {
1885 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1886 return;
1887 }
Anders Carlssone896d982009-02-13 08:11:52 +00001888
Anders Carlsson5bab7882009-02-19 19:16:48 +00001889 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001890 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001891 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001892 return;
1893 }
Mike Stumpbf916502009-07-24 19:02:52 +00001894
Mike Stump1feade82009-08-26 22:31:08 +00001895 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001896}
1897
Mike Stump1feade82009-08-26 22:31:08 +00001898static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001899 // check the attribute arguments.
1900 if (Attr.getNumArgs() != 0) {
1901 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1902 return;
1903 }
Mike Stumpbf916502009-07-24 19:02:52 +00001904
Chris Lattnerc5197432009-04-14 17:02:11 +00001905 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001906 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001907 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001908 return;
1909 }
Mike Stumpbf916502009-07-24 19:02:52 +00001910
Mike Stump1feade82009-08-26 22:31:08 +00001911 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001912}
1913
Chris Lattner7255a2d2010-06-22 00:03:40 +00001914static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1915 Sema &S) {
1916 // check the attribute arguments.
1917 if (Attr.getNumArgs() != 0) {
1918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1919 return;
1920 }
1921
1922 if (!isa<FunctionDecl>(d)) {
1923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1924 << Attr.getName() << 0 /*function*/;
1925 return;
1926 }
1927
1928 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr());
1929}
1930
Chris Lattnercf2a7212009-04-20 19:12:28 +00001931static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001932 // check the attribute arguments.
1933 if (Attr.getNumArgs() != 0) {
1934 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1935 return;
1936 }
Mike Stumpbf916502009-07-24 19:02:52 +00001937
Chris Lattnerc5197432009-04-14 17:02:11 +00001938 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1939 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001941 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001942 return;
1943 }
Mike Stumpbf916502009-07-24 19:02:52 +00001944
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001945 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001946 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001947 return;
1948 }
Mike Stumpbf916502009-07-24 19:02:52 +00001949
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001950 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001951}
1952
Abramo Bagnarae215f722010-04-30 13:10:51 +00001953static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1954 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1955 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1956 assert(Attr.isInvalid() == false);
1957
1958 switch (Attr.getKind()) {
1959 case AttributeList::AT_fastcall:
1960 d->addAttr(::new (S.Context) FastCallAttr());
1961 return;
1962 case AttributeList::AT_stdcall:
1963 d->addAttr(::new (S.Context) StdCallAttr());
1964 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001965 case AttributeList::AT_thiscall:
1966 d->addAttr(::new (S.Context) ThisCallAttr());
Abramo Bagnarae215f722010-04-30 13:10:51 +00001967 case AttributeList::AT_cdecl:
1968 d->addAttr(::new (S.Context) CDeclAttr());
1969 return;
1970 default:
1971 llvm_unreachable("unexpected attribute kind");
1972 return;
1973 }
1974}
1975
Fariborz Jahanianee760332009-03-27 18:38:55 +00001976static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1977 // check the attribute arguments.
1978 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001980 return;
1981 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001982
Fariborz Jahanianee760332009-03-27 18:38:55 +00001983 if (!isFunctionOrMethod(d)) {
1984 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001985 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001986 return;
1987 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001988
1989 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1990 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001991 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1992 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001993 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1994 << "regparm" << NumParamsExpr->getSourceRange();
1995 return;
1996 }
1997
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001998 if (S.Context.Target.getRegParmMax() == 0) {
1999 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002000 << NumParamsExpr->getSourceRange();
2001 return;
2002 }
2003
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002004 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002005 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2006 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002007 return;
2008 }
2009
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002010 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002011}
2012
Sean Huntbbd37c62009-11-21 08:43:09 +00002013static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2014 // check the attribute arguments.
2015 if (Attr.getNumArgs() != 0) {
2016 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2017 return;
2018 }
2019
2020 if (!isa<CXXRecordDecl>(d)
2021 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2022 S.Diag(Attr.getLoc(),
2023 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2024 : diag::warn_attribute_wrong_decl_type)
2025 << Attr.getName() << 7 /*virtual method or class*/;
2026 return;
2027 }
Sean Hunt7725e672009-11-25 04:20:27 +00002028
2029 // FIXME: Conform to C++0x redeclaration rules.
2030
2031 if (d->getAttr<FinalAttr>()) {
2032 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2033 return;
2034 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002035
2036 d->addAttr(::new (S.Context) FinalAttr());
2037}
2038
Chris Lattner0744e5f2008-06-29 00:23:49 +00002039//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002040// C++0x member checking attributes
2041//===----------------------------------------------------------------------===//
2042
2043static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2044 if (Attr.getNumArgs() != 0) {
2045 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2046 return;
2047 }
2048
2049 if (!isa<CXXRecordDecl>(d)) {
2050 S.Diag(Attr.getLoc(),
2051 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2052 : diag::warn_attribute_wrong_decl_type)
2053 << Attr.getName() << 9 /*class*/;
2054 return;
2055 }
2056
2057 if (d->getAttr<BaseCheckAttr>()) {
2058 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2059 return;
2060 }
2061
2062 d->addAttr(::new (S.Context) BaseCheckAttr());
2063}
2064
2065static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2066 if (Attr.getNumArgs() != 0) {
2067 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2068 return;
2069 }
2070
2071 if (!isa<RecordDecl>(d->getDeclContext())) {
2072 // FIXME: It's not the type that's the problem
2073 S.Diag(Attr.getLoc(),
2074 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2075 : diag::warn_attribute_wrong_decl_type)
2076 << Attr.getName() << 11 /*member*/;
2077 return;
2078 }
2079
2080 // FIXME: Conform to C++0x redeclaration rules.
2081
2082 if (d->getAttr<HidingAttr>()) {
2083 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2084 return;
2085 }
2086
2087 d->addAttr(::new (S.Context) HidingAttr());
2088}
2089
2090static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2091 if (Attr.getNumArgs() != 0) {
2092 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2093 return;
2094 }
2095
2096 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2097 // FIXME: It's not the type that's the problem
2098 S.Diag(Attr.getLoc(),
2099 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2100 : diag::warn_attribute_wrong_decl_type)
2101 << Attr.getName() << 10 /*virtual method*/;
2102 return;
2103 }
2104
2105 // FIXME: Conform to C++0x redeclaration rules.
2106
2107 if (d->getAttr<OverrideAttr>()) {
2108 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2109 return;
2110 }
2111
2112 d->addAttr(::new (S.Context) OverrideAttr());
2113}
2114
2115//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002116// Checker-specific attribute handlers.
2117//===----------------------------------------------------------------------===//
2118
2119static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2120 Sema &S) {
2121
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002122 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002123
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002124 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2125 RetTy = MD->getResultType();
2126 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2127 RetTy = FD->getResultType();
2128 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002129 SourceLocation L = Attr.getLoc();
2130 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2131 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002132 return;
2133 }
Mike Stumpbf916502009-07-24 19:02:52 +00002134
Ted Kremenek6217b802009-07-29 21:53:49 +00002135 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002136 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002137 SourceLocation L = Attr.getLoc();
2138 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2139 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002140 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002141 }
Mike Stumpbf916502009-07-24 19:02:52 +00002142
Ted Kremenekb71368d2009-05-09 02:44:38 +00002143 switch (Attr.getKind()) {
2144 default:
2145 assert(0 && "invalid ownership attribute");
2146 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002147 case AttributeList::AT_cf_returns_not_retained:
2148 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
2149 return;
2150 case AttributeList::AT_ns_returns_not_retained:
2151 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
2152 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002153 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002154 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002155 return;
2156 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002157 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002158 return;
2159 };
2160}
2161
Charles Davisf0122fe2010-02-16 18:27:26 +00002162static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2163 return Attr.getKind() == AttributeList::AT_dllimport ||
2164 Attr.getKind() == AttributeList::AT_dllexport;
2165}
2166
Ted Kremenekb71368d2009-05-09 02:44:38 +00002167//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002168// Top Level Sema Entry Points
2169//===----------------------------------------------------------------------===//
2170
Sebastian Redla89d82c2008-12-21 19:24:58 +00002171/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002172/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002173/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2174/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002175static void ProcessDeclAttribute(Scope *scope, Decl *D,
2176 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002177 if (Attr.isInvalid())
2178 return;
2179
Charles Davisf0122fe2010-02-16 18:27:26 +00002180 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2181 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002182 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002183 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002184 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002185 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2186 case AttributeList::AT_IBOutletCollection:
2187 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002188 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002189 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002190 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002191 // Ignore these, these are type attributes, handled by
2192 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002193 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002194 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2195 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002196 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002197 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002198 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002199 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002200 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2201 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002202 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002203 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002204 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2205 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2206 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002207 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002208 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002209 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002210 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2211 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2212 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2213 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2214 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2215 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2216 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2217 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002218 case AttributeList::AT_ownership_returns:
2219 case AttributeList::AT_ownership_takes:
2220 case AttributeList::AT_ownership_holds:
2221 HandleOwnershipAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002222 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2223 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2224 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002225 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002226
2227 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002228 case AttributeList::AT_ns_returns_not_retained:
2229 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002230 case AttributeList::AT_ns_returns_retained:
2231 case AttributeList::AT_cf_returns_retained:
2232 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2233
Nate Begeman6f3d8382009-06-26 06:32:41 +00002234 case AttributeList::AT_reqd_wg_size:
2235 HandleReqdWorkGroupSize(D, Attr, S); break;
2236
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002237 case AttributeList::AT_init_priority:
2238 HandleInitPriorityAttr(D, Attr, S); break;
2239
Sean Hunt7725e672009-11-25 04:20:27 +00002240 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2241 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002242 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2243 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2244 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002245 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002246 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2247 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002248 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002249 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002250 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002251 case AttributeList::AT_transparent_union:
2252 HandleTransparentUnionAttr(D, Attr, S);
2253 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002254 case AttributeList::AT_objc_exception:
2255 HandleObjCExceptionAttr(D, Attr, S);
2256 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002257 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002258 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2259 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2260 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2261 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2262 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2263 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2264 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2265 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2266 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002267 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002268 // Just ignore
2269 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002270 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2271 HandleNoInstrumentFunctionAttr(D, Attr, S);
2272 break;
John McCall04a67a62010-02-05 21:31:56 +00002273 case AttributeList::AT_stdcall:
2274 case AttributeList::AT_cdecl:
2275 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002276 case AttributeList::AT_thiscall:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002277 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002278 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002279 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002280 // Ask target about the attribute.
2281 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2282 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002283 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2284 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002285 break;
2286 }
2287}
2288
2289/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2290/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002291void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002292 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2293 ProcessDeclAttribute(S, D, *l, *this);
2294 }
2295
2296 // GCC accepts
2297 // static int a9 __attribute__((weakref));
2298 // but that looks really pointless. We reject it.
2299 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2300 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002301 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002302 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002303 }
2304}
2305
Ryan Flynne25ff832009-07-30 03:15:39 +00002306/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2307/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002308NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002309 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002310 NamedDecl *NewD = 0;
2311 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2312 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2313 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002314 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002315 if (FD->getQualifier()) {
2316 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2317 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2318 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002319 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2320 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2321 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002322 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002323 VD->getStorageClass(),
2324 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002325 if (VD->getQualifier()) {
2326 VarDecl *NewVD = cast<VarDecl>(NewD);
2327 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2328 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002329 }
2330 return NewD;
2331}
2332
2333/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2334/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002335void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002336 if (W.getUsed()) return; // only do this once
2337 W.setUsed(true);
2338 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2339 IdentifierInfo *NDId = ND->getIdentifier();
2340 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00002341 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002342 NewD->addAttr(::new (Context) WeakAttr());
2343 WeakTopLevelDecl.push_back(NewD);
2344 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2345 // to insert Decl at TU scope, sorry.
2346 DeclContext *SavedContext = CurContext;
2347 CurContext = Context.getTranslationUnitDecl();
2348 PushOnScopeChains(NewD, S);
2349 CurContext = SavedContext;
2350 } else { // just add weak to existing
2351 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002352 }
2353}
2354
Chris Lattner0744e5f2008-06-29 00:23:49 +00002355/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2356/// it, apply them to D. This is a bit tricky because PD can have attributes
2357/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002358void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002359 // Handle #pragma weak
2360 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2361 if (ND->hasLinkage()) {
2362 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2363 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002364 // Identifier referenced by #pragma weak before it was declared
2365 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002366 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2367 }
2368 }
2369 }
2370
Chris Lattner0744e5f2008-06-29 00:23:49 +00002371 // Apply decl attributes from the DeclSpec if present.
2372 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002373 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002374
Chris Lattner0744e5f2008-06-29 00:23:49 +00002375 // Walk the declarator structure, applying decl attributes that were in a type
2376 // position to the decl itself. This handles cases like:
2377 // int *__attr__(x)** D;
2378 // when X is a decl attribute.
2379 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2380 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002381 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002382
Chris Lattner0744e5f2008-06-29 00:23:49 +00002383 // Finally, apply any attributes on the decl itself.
2384 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002385 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002386}
John McCall54abf7d2009-11-04 02:18:39 +00002387
2388/// PushParsingDeclaration - Enter a new "scope" of deprecation
2389/// warnings.
2390///
2391/// The state token we use is the start index of this scope
2392/// on the warning stack.
2393Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2394 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002395 return (ParsingDeclStackState) DelayedDiagnostics.size();
2396}
2397
2398void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2399 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2400 ParsingDeclDepth--;
2401
2402 if (DelayedDiagnostics.empty())
2403 return;
2404
2405 unsigned SavedIndex = (unsigned) S;
2406 assert(SavedIndex <= DelayedDiagnostics.size() &&
2407 "saved index is out of bounds");
2408
John McCall58e6f342010-03-16 05:22:47 +00002409 unsigned E = DelayedDiagnostics.size();
2410
John McCall2f514482010-01-27 03:50:35 +00002411 // We only want to actually emit delayed diagnostics when we
2412 // successfully parsed a decl.
2413 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2414 if (D) {
2415 // We really do want to start with 0 here. We get one push for a
2416 // decl spec and another for each declarator; in a decl group like:
2417 // deprecated_typedef foo, *bar, baz();
2418 // only the declarator pops will be passed decls. This is correct;
2419 // we really do need to consider delayed diagnostics from the decl spec
2420 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002421 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002422 if (DelayedDiagnostics[I].Triggered)
2423 continue;
2424
2425 switch (DelayedDiagnostics[I].Kind) {
2426 case DelayedDiagnostic::Deprecation:
2427 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2428 break;
2429
2430 case DelayedDiagnostic::Access:
2431 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2432 break;
2433 }
2434 }
2435 }
2436
John McCall58e6f342010-03-16 05:22:47 +00002437 // Destroy all the delayed diagnostics we're about to pop off.
2438 for (unsigned I = SavedIndex; I != E; ++I)
2439 DelayedDiagnostics[I].destroy();
2440
John McCall2f514482010-01-27 03:50:35 +00002441 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002442}
2443
2444static bool isDeclDeprecated(Decl *D) {
2445 do {
2446 if (D->hasAttr<DeprecatedAttr>())
2447 return true;
2448 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2449 return false;
2450}
2451
John McCall2f514482010-01-27 03:50:35 +00002452void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2453 Decl *Ctx) {
2454 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002455 return;
2456
John McCall2f514482010-01-27 03:50:35 +00002457 DD.Triggered = true;
2458 Diag(DD.Loc, diag::warn_deprecated)
2459 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002460}
2461
2462void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2463 // Delay if we're currently parsing a declaration.
2464 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002465 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002466 return;
2467 }
2468
2469 // Otherwise, don't warn if our current context is deprecated.
2470 if (isDeclDeprecated(cast<Decl>(CurContext)))
2471 return;
2472
2473 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2474}