blob: 2af3102ad759c64911bbdc1e2cd4bd4f48bb093a [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
360 // after being held. free() should be __attribute((ownership_takes)), whereas a list
361 // append function may well be __attribute((ownership_holds)).
362
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.
369 OwnershipAttr::OwnershipKind K = OwnershipAttr::None;
370 if (AL.getName()->getName().equals("ownership_takes")) {
371 K = OwnershipAttr::Takes;
372 if (AL.getNumArgs() < 1) {
373 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
374 return;
375 }
376 } else if (AL.getName()->getName().equals("ownership_holds")) {
377 K = OwnershipAttr::Holds;
378 if (AL.getNumArgs() < 1) {
379 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
380 return;
381 }
382 } else if (AL.getName()->getName().equals("ownership_returns")) {
383 K = OwnershipAttr::Returns;
384 if (AL.getNumArgs() > 1) {
385 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
386 << AL.getNumArgs() + 1;
387 return;
388 }
389 }
390 // This should never happen given how we are called.
391 if (K == OwnershipAttr::None) {
392 return;
393 }
394
395 if (!isFunction(d) || !hasFunctionProto(d)) {
396 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
397 << 0 /*function*/;
398 return;
399 }
400
401 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
402
403 llvm::StringRef Module = AL.getParameterName()->getName();
404
405 // Normalize the argument, __foo__ becomes foo.
406 if (Module.startswith("__") && Module.endswith("__"))
407 Module = Module.substr(2, Module.size() - 4);
408
409 llvm::SmallVector<unsigned, 10> OwnershipArgs;
410
411 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; ++I) {
412
413 Expr *IdxExpr = static_cast<Expr *>(*I);
414 llvm::APSInt ArgNum(32);
415 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
416 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
417 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
418 << AL.getName()->getName() << IdxExpr->getSourceRange();
419 continue;
420 }
421
422 unsigned x = (unsigned) ArgNum.getZExtValue();
423
424 if (x > NumArgs || x < 1) {
425 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
426 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
427 continue;
428 }
429 --x;
430 switch (K) {
431 case OwnershipAttr::Takes:
432 case OwnershipAttr::Holds: {
433 // Is the function argument a pointer type?
434 QualType T = getFunctionOrMethodArgType(d, x);
435 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
436 // FIXME: Should also highlight argument in decl.
437 S.Diag(AL.getLoc(), diag::err_ownership_type)
438 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
439 << "pointer"
440 << IdxExpr->getSourceRange();
441 continue;
442 }
443 break;
444 }
445 case OwnershipAttr::Returns: {
446 if (AL.getNumArgs() > 1) {
447 // Is the function argument an integer type?
448 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
449 llvm::APSInt ArgNum(32);
450 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
451 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
452 S.Diag(AL.getLoc(), diag::err_ownership_type)
453 << "ownership_returns" << "integer"
454 << IdxExpr->getSourceRange();
455 return;
456 }
457 }
458 break;
459 }
460 // Should never happen, here to silence a warning.
461 default: {
462 return;
463 }
464 } // switch
465
466 // Check we don't have a conflict with another ownership attribute.
467 if (d->hasAttrs()) {
468 for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) {
469 if (const OwnershipAttr* Att = dyn_cast<OwnershipAttr>(attr)) {
470 // Two ownership attributes of the same kind can't conflict,
471 // except returns attributes.
472 if (K == OwnershipAttr::Returns || Att->getKind() != K) {
473 for (const unsigned *I = Att->begin(), *E = Att->end(); I != E; ++I) {
474 if (x == *I) {
475 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
476 << AL.getName()->getName() << "ownership_*";
477 }
478 }
479 }
480 }
481 }
482 }
483 OwnershipArgs.push_back(x);
484 }
485
486 unsigned* start = OwnershipArgs.data();
487 unsigned size = OwnershipArgs.size();
488 llvm::array_pod_sort(start, start + size);
489 switch (K) {
490 case OwnershipAttr::Takes: {
491 if (OwnershipArgs.empty()) {
492 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
493 return;
494 }
495 d->addAttr(::new (S.Context) OwnershipTakesAttr(S.Context, start, size,
496 Module));
497 break;
498 }
499 case OwnershipAttr::Holds: {
500 if (OwnershipArgs.empty()) {
501 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
502 return;
503 }
504 d->addAttr(::new (S.Context) OwnershipHoldsAttr(S.Context, start, size,
505 Module));
506 break;
507 }
508 case OwnershipAttr::Returns: {
509 d->addAttr(::new (S.Context) OwnershipReturnsAttr(S.Context, start, size,
510 Module));
511 break;
512 }
513 default:
514 break;
515 }
516}
517
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000518static bool isStaticVarOrStaticFunciton(Decl *D) {
519 if (VarDecl *VD = dyn_cast<VarDecl>(D))
520 return VD->getStorageClass() == VarDecl::Static;
521 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
522 return FD->getStorageClass() == FunctionDecl::Static;
523 return false;
524}
525
526static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
527 // Check the attribute arguments.
528 if (Attr.getNumArgs() > 1) {
529 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
530 return;
531 }
532
533 // gcc rejects
534 // class c {
535 // static int a __attribute__((weakref ("v2")));
536 // static int b() __attribute__((weakref ("f3")));
537 // };
538 // and ignores the attributes of
539 // void f(void) {
540 // static int a __attribute__((weakref ("v2")));
541 // }
542 // we reject them
543 if (const DeclContext *Ctx = d->getDeclContext()) {
544 Ctx = Ctx->getLookupContext();
545 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
546 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000547 dyn_cast<NamedDecl>(d)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000548 return;
549 }
550 }
551
552 // The GCC manual says
553 //
554 // At present, a declaration to which `weakref' is attached can only
555 // be `static'.
556 //
557 // It also says
558 //
559 // Without a TARGET,
560 // given as an argument to `weakref' or to `alias', `weakref' is
561 // equivalent to `weak'.
562 //
563 // gcc 4.4.1 will accept
564 // int a7 __attribute__((weakref));
565 // as
566 // int a7 __attribute__((weak));
567 // This looks like a bug in gcc. We reject that for now. We should revisit
568 // it if this behaviour is actually used.
569
570 if (!isStaticVarOrStaticFunciton(d)) {
571 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
572 dyn_cast<NamedDecl>(d)->getNameAsString();
573 return;
574 }
575
576 // GCC rejects
577 // static ((alias ("y"), weakref)).
578 // Should we? How to check that weakref is before or after alias?
579
580 if (Attr.getNumArgs() == 1) {
581 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
582 Arg = Arg->IgnoreParenCasts();
583 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
584
585 if (Str == 0 || Str->isWide()) {
586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
587 << "weakref" << 1;
588 return;
589 }
590 // GCC will accept anything as the argument of weakref. Should we
591 // check for an existing decl?
592 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
593 }
594
595 d->addAttr(::new (S.Context) WeakRefAttr());
596}
597
Chris Lattner803d0802008-06-29 00:43:07 +0000598static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000599 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000600 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000602 return;
603 }
Mike Stumpbf916502009-07-24 19:02:52 +0000604
Chris Lattner545dd342008-06-28 23:36:30 +0000605 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 Arg = Arg->IgnoreParenCasts();
607 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000608
Chris Lattner6b6b5372008-06-26 18:38:35 +0000609 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000611 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000612 return;
613 }
Mike Stumpbf916502009-07-24 19:02:52 +0000614
Chris Lattner6b6b5372008-06-26 18:38:35 +0000615 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000616
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000617 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000618}
619
Mike Stumpbf916502009-07-24 19:02:52 +0000620static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000621 Sema &S) {
622 // check the attribute arguments.
623 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000625 return;
626 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000627
Chris Lattnerc5197432009-04-14 17:02:11 +0000628 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000630 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000631 return;
632 }
Mike Stumpbf916502009-07-24 19:02:52 +0000633
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000634 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000635}
636
Ryan Flynn76168e22009-08-09 20:07:29 +0000637static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
638 // check the attribute arguments.
639 if (Attr.getNumArgs() != 0) {
640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
641 return;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000644 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000645 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000646 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
647 d->addAttr(::new (S.Context) MallocAttr());
648 return;
649 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000650 }
651
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000652 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000653}
654
Ted Kremenekb7252322009-04-10 00:01:14 +0000655static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnarae215f722010-04-30 13:10:51 +0000656 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000657 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000658 if (Attr.getNumArgs() != 0) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000660 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000661 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000662
Mike Stump19c30c02009-04-29 19:03:13 +0000663 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
664 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000665 if (VD == 0 || (!VD->getType()->isBlockPointerType()
666 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000667 S.Diag(Attr.getLoc(),
668 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
669 : diag::warn_attribute_wrong_decl_type)
670 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000671 return false;
672 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000673 }
Mike Stumpbf916502009-07-24 19:02:52 +0000674
Ted Kremenekb7252322009-04-10 00:01:14 +0000675 return true;
676}
677
678static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000679 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
680 assert(Attr.isInvalid() == false);
681 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000682}
683
684static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
685 Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000686 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000687 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000688}
689
Sean Huntbbd37c62009-11-21 08:43:09 +0000690static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
691 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000693 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000694 return;
695 }
696 // FIXME: Actually store the attribute on the declaration
697}
698
Ted Kremenek73798892008-07-25 04:39:19 +0000699static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
700 // check the attribute arguments.
701 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000703 return;
704 }
Mike Stumpbf916502009-07-24 19:02:52 +0000705
John McCallaec58602010-03-31 02:47:45 +0000706 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
707 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000708 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000709 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000710 return;
711 }
Mike Stumpbf916502009-07-24 19:02:52 +0000712
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000713 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000714}
715
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000716static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
717 // check the attribute arguments.
718 if (Attr.getNumArgs() != 0) {
719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
720 return;
721 }
Mike Stumpbf916502009-07-24 19:02:52 +0000722
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000723 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000724 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000725 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
726 return;
727 }
728 } else if (!isFunctionOrMethod(d)) {
729 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000730 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000731 return;
732 }
Mike Stumpbf916502009-07-24 19:02:52 +0000733
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000734 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000735}
736
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000737static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
738 // check the attribute arguments.
739 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
741 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000742 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000743 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000744
745 int priority = 65535; // FIXME: Do not hardcode such constants.
746 if (Attr.getNumArgs() > 0) {
747 Expr *E = static_cast<Expr *>(Attr.getArg(0));
748 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000749 if (E->isTypeDependent() || E->isValueDependent() ||
750 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000752 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000753 return;
754 }
755 priority = Idx.getZExtValue();
756 }
Mike Stumpbf916502009-07-24 19:02:52 +0000757
Chris Lattnerc5197432009-04-14 17:02:11 +0000758 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000759 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000760 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000761 return;
762 }
763
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000764 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000765}
766
767static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
768 // check the attribute arguments.
769 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000770 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
771 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000772 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000773 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000774
775 int priority = 65535; // FIXME: Do not hardcode such constants.
776 if (Attr.getNumArgs() > 0) {
777 Expr *E = static_cast<Expr *>(Attr.getArg(0));
778 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000779 if (E->isTypeDependent() || E->isValueDependent() ||
780 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000781 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000782 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000783 return;
784 }
785 priority = Idx.getZExtValue();
786 }
Mike Stumpbf916502009-07-24 19:02:52 +0000787
Anders Carlsson6782fc62008-08-22 22:10:48 +0000788 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000790 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000791 return;
792 }
793
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000794 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000795}
796
Chris Lattner803d0802008-06-29 00:43:07 +0000797static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000798 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000799 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000801 return;
802 }
Mike Stumpbf916502009-07-24 19:02:52 +0000803
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000804 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000805}
806
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000807static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
808 // check the attribute arguments.
809 if (Attr.getNumArgs() != 0) {
810 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
811 return;
812 }
Mike Stumpbf916502009-07-24 19:02:52 +0000813
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000814 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000815}
816
Chris Lattner803d0802008-06-29 00:43:07 +0000817static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000818 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000819 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000821 return;
822 }
Mike Stumpbf916502009-07-24 19:02:52 +0000823
Chris Lattner545dd342008-06-28 23:36:30 +0000824 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000825 Arg = Arg->IgnoreParenCasts();
826 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000827
Chris Lattner6b6b5372008-06-26 18:38:35 +0000828 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000829 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000830 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000831 return;
832 }
Mike Stumpbf916502009-07-24 19:02:52 +0000833
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000834 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000835 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000836
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000837 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000838 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000839 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000840 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000841 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000842 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000843 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000844 type = VisibilityAttr::ProtectedVisibility;
845 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000846 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000847 return;
848 }
Mike Stumpbf916502009-07-24 19:02:52 +0000849
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000850 d->addAttr(::new (S.Context) VisibilityAttr(type, false));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000851}
852
Chris Lattner0db29ec2009-02-14 08:09:34 +0000853static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
854 Sema &S) {
855 if (Attr.getNumArgs() != 0) {
856 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
857 return;
858 }
Mike Stumpbf916502009-07-24 19:02:52 +0000859
Chris Lattner0db29ec2009-02-14 08:09:34 +0000860 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
861 if (OCI == 0) {
862 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
863 return;
864 }
Mike Stumpbf916502009-07-24 19:02:52 +0000865
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000866 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000867}
868
869static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000870 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000871 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000872 return;
873 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000874 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000875 QualType T = TD->getUnderlyingType();
876 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000877 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000878 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
879 return;
880 }
881 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000882 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000883}
884
Mike Stumpbf916502009-07-24 19:02:52 +0000885static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000886HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
887 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000888 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000889 return;
890 }
891
892 if (!isa<FunctionDecl>(D)) {
893 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
894 return;
895 }
896
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000897 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000898}
899
Steve Naroff9eae5762008-09-18 16:44:58 +0000900static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000901 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000902 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000903 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000904 return;
905 }
Mike Stumpbf916502009-07-24 19:02:52 +0000906
Steve Naroff9eae5762008-09-18 16:44:58 +0000907 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000908 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000909 return;
910 }
Mike Stumpbf916502009-07-24 19:02:52 +0000911
Steve Naroff9eae5762008-09-18 16:44:58 +0000912 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000913 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000914 type = BlocksAttr::ByRef;
915 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000916 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000917 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000918 return;
919 }
Mike Stumpbf916502009-07-24 19:02:52 +0000920
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000921 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000922}
923
Anders Carlsson77091822008-10-05 18:05:59 +0000924static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
925 // check the attribute arguments.
926 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000927 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
928 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000929 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000930 }
931
Anders Carlsson77091822008-10-05 18:05:59 +0000932 int sentinel = 0;
933 if (Attr.getNumArgs() > 0) {
934 Expr *E = static_cast<Expr *>(Attr.getArg(0));
935 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000936 if (E->isTypeDependent() || E->isValueDependent() ||
937 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000938 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000939 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000940 return;
941 }
942 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000943
Anders Carlsson77091822008-10-05 18:05:59 +0000944 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000945 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
946 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000947 return;
948 }
949 }
950
951 int nullPos = 0;
952 if (Attr.getNumArgs() > 1) {
953 Expr *E = static_cast<Expr *>(Attr.getArg(1));
954 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000955 if (E->isTypeDependent() || E->isValueDependent() ||
956 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000957 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000958 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000959 return;
960 }
961 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000962
Anders Carlsson77091822008-10-05 18:05:59 +0000963 if (nullPos > 1 || nullPos < 0) {
964 // FIXME: This error message could be improved, it would be nice
965 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000966 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
967 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000968 return;
969 }
970 }
971
972 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000973 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000974 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000975
Chris Lattner897cd902009-03-17 23:03:47 +0000976 if (isa<FunctionNoProtoType>(FT)) {
977 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
978 return;
979 }
Mike Stumpbf916502009-07-24 19:02:52 +0000980
Chris Lattner897cd902009-03-17 23:03:47 +0000981 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000982 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000983 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000984 }
Anders Carlsson77091822008-10-05 18:05:59 +0000985 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
986 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000987 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000988 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000989 }
990 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000991 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
992 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000993 ;
994 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000995 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000996 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000997 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000998 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000999 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001000 int m = Ty->isFunctionPointerType() ? 0 : 1;
1001 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001002 return;
1003 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001004 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001005 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001006 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001007 return;
1008 }
Anders Carlsson77091822008-10-05 18:05:59 +00001009 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001010 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001011 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001012 return;
1013 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001014 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001015}
1016
Chris Lattner026dc962009-02-14 07:37:35 +00001017static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1018 // check the attribute arguments.
1019 if (Attr.getNumArgs() != 0) {
1020 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1021 return;
1022 }
1023
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001024 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001025 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001026 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001027 return;
1028 }
Mike Stumpbf916502009-07-24 19:02:52 +00001029
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001030 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1031 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1032 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001033 return;
1034 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001035 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1036 if (MD->getResultType()->isVoidType()) {
1037 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1038 << Attr.getName() << 1;
1039 return;
1040 }
1041
Nuno Lopesd20254f2009-12-20 23:11:08 +00001042 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +00001043}
1044
1045static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001046 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001047 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001048 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001049 return;
1050 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001051
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001052 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001053 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001054 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1055 dyn_cast<NamedDecl>(D)->getNameAsString();
1056 return;
1057 }
1058
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001059 // TODO: could also be applied to methods?
1060 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1061 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001062 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001063 return;
1064 }
Mike Stumpbf916502009-07-24 19:02:52 +00001065
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001066 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001067}
1068
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001069static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1070 // check the attribute arguments.
1071 if (Attr.getNumArgs() != 0) {
1072 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1073 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001074 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001075
1076 // weak_import only applies to variable & function declarations.
1077 bool isDef = false;
1078 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1079 isDef = (!VD->hasExternalStorage() || VD->getInit());
1080 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001081 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001082 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1083 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001084 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001085 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001086 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001087 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001088 !isa<ObjCInterfaceDecl>(D))
1089 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001090 << Attr.getName() << 2 /*variable and function*/;
1091 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001092 }
1093
1094 // Merge should handle any subsequent violations.
1095 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001096 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001097 diag::warn_attribute_weak_import_invalid_on_definition)
1098 << "weak_import" << 2 /*variable and function*/;
1099 return;
1100 }
1101
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001102 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001103}
1104
Nate Begeman6f3d8382009-06-26 06:32:41 +00001105static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1106 Sema &S) {
1107 // Attribute has 3 arguments.
1108 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001110 return;
1111 }
1112
1113 unsigned WGSize[3];
1114 for (unsigned i = 0; i < 3; ++i) {
1115 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1116 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001117 if (E->isTypeDependent() || E->isValueDependent() ||
1118 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001119 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1120 << "reqd_work_group_size" << E->getSourceRange();
1121 return;
1122 }
1123 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1124 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001125 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001126 WGSize[2]));
1127}
1128
Chris Lattner026dc962009-02-14 07:37:35 +00001129static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001130 // Attribute has no arguments.
1131 if (Attr.getNumArgs() != 1) {
1132 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1133 return;
1134 }
1135
1136 // Make sure that there is a string literal as the sections's single
1137 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001138 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1139 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001140 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001141 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001142 return;
1143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattner797c3c42009-08-10 19:03:04 +00001145 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001146 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001147 if (!Error.empty()) {
1148 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1149 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001150 return;
1151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001153 // This attribute cannot be applied to local variables.
1154 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1155 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1156 return;
1157 }
1158
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001159 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001160}
1161
Chris Lattner6b6b5372008-06-26 18:38:35 +00001162
Chris Lattner803d0802008-06-29 00:43:07 +00001163static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001164 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001165 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001166 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001167 return;
1168 }
Mike Stumpbf916502009-07-24 19:02:52 +00001169
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001170 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001171}
1172
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001173static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1174 // check the attribute arguments.
1175 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001176 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001177 return;
1178 }
Mike Stumpbf916502009-07-24 19:02:52 +00001179
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001180 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001181}
1182
1183static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1184 // check the attribute arguments.
1185 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001187 return;
1188 }
Mike Stumpbf916502009-07-24 19:02:52 +00001189
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001190 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001191}
1192
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001193static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001194 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001195 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1196 return;
1197 }
Mike Stumpbf916502009-07-24 19:02:52 +00001198
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001199 if (Attr.getNumArgs() != 0) {
1200 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1201 return;
1202 }
Mike Stumpbf916502009-07-24 19:02:52 +00001203
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001204 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001205
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001206 if (!VD || !VD->hasLocalStorage()) {
1207 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1208 return;
1209 }
Mike Stumpbf916502009-07-24 19:02:52 +00001210
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001211 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001212 // FIXME: Lookup probably isn't looking in the right place
1213 // FIXME: The lookup source location should be in the attribute, not the
1214 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001215 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001216 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001217 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001218 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001219 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001220 Attr.getParameterName();
1221 return;
1222 }
Mike Stumpbf916502009-07-24 19:02:52 +00001223
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001224 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1225 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001226 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001227 Attr.getParameterName();
1228 return;
1229 }
1230
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001231 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001232 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001233 Attr.getParameterName();
1234 return;
1235 }
Mike Stumpbf916502009-07-24 19:02:52 +00001236
Anders Carlsson89941c12009-02-07 23:16:50 +00001237 // We're currently more strict than GCC about what function types we accept.
1238 // If this ever proves to be a problem it should be easy to fix.
1239 QualType Ty = S.Context.getPointerType(VD->getType());
1240 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001241 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001242 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001243 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1244 Attr.getParameterName() << ParamTy << Ty;
1245 return;
1246 }
Mike Stumpbf916502009-07-24 19:02:52 +00001247
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001248 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001249}
1250
Mike Stumpbf916502009-07-24 19:02:52 +00001251/// Handle __attribute__((format_arg((idx)))) attribute based on
1252/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1253static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001254 if (Attr.getNumArgs() != 1) {
1255 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1256 return;
1257 }
1258 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1259 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1260 << Attr.getName() << 0 /*function*/;
1261 return;
1262 }
Mike Stumpbf916502009-07-24 19:02:52 +00001263 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1264 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001265 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1266 unsigned FirstIdx = 1;
1267 // checks for the 2nd argument
1268 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1269 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001270 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1271 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001272 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1273 << "format" << 2 << IdxExpr->getSourceRange();
1274 return;
1275 }
Mike Stumpbf916502009-07-24 19:02:52 +00001276
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001277 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1279 << "format" << 2 << IdxExpr->getSourceRange();
1280 return;
1281 }
Mike Stumpbf916502009-07-24 19:02:52 +00001282
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001283 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001284
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001285 // make sure the format string is really a string
1286 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001287
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001288 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1289 if (not_nsstring_type &&
1290 !isCFStringType(Ty, S.Context) &&
1291 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001292 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001293 // FIXME: Should highlight the actual expression that has the wrong type.
1294 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001295 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001296 << IdxExpr->getSourceRange();
1297 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001298 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001299 Ty = getFunctionOrMethodResultType(d);
1300 if (!isNSStringType(Ty, S.Context) &&
1301 !isCFStringType(Ty, S.Context) &&
1302 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001303 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001304 // FIXME: Should highlight the actual expression that has the wrong type.
1305 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001306 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001307 << IdxExpr->getSourceRange();
1308 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001309 }
1310
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001311 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001312}
1313
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001314enum FormatAttrKind {
1315 CFStringFormat,
1316 NSStringFormat,
1317 StrftimeFormat,
1318 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001319 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001320 InvalidFormat
1321};
1322
1323/// getFormatAttrKind - Map from format attribute names to supported format
1324/// types.
1325static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1326 // Check for formats that get handled specially.
1327 if (Format == "NSString")
1328 return NSStringFormat;
1329 if (Format == "CFString")
1330 return CFStringFormat;
1331 if (Format == "strftime")
1332 return StrftimeFormat;
1333
1334 // Otherwise, check for supported formats.
1335 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1336 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1337 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1338 Format == "zcmn_err")
1339 return SupportedFormat;
1340
Duncan Sandsbc525952010-03-23 14:44:19 +00001341 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1342 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001343 return IgnoredFormat;
1344
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001345 return InvalidFormat;
1346}
1347
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001348/// Handle __attribute__((init_priority(priority))) attributes based on
1349/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1350static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1351 Sema &S) {
1352 if (!S.getLangOptions().CPlusPlus) {
1353 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1354 return;
1355 }
1356
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001357 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1358 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1359 Attr.setInvalid();
1360 return;
1361 }
1362 QualType T = dyn_cast<VarDecl>(d)->getType();
1363 if (S.Context.getAsArrayType(T))
1364 T = S.Context.getBaseElementType(T);
1365 if (!T->getAs<RecordType>()) {
1366 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1367 Attr.setInvalid();
1368 return;
1369 }
1370
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001371 if (Attr.getNumArgs() != 1) {
1372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1373 Attr.setInvalid();
1374 return;
1375 }
1376 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001377
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001378 llvm::APSInt priority(32);
1379 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1380 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1381 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1382 << "init_priority" << priorityExpr->getSourceRange();
1383 Attr.setInvalid();
1384 return;
1385 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001386 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001387 if (prioritynum < 101 || prioritynum > 65535) {
1388 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1389 << priorityExpr->getSourceRange();
1390 Attr.setInvalid();
1391 return;
1392 }
1393 d->addAttr(::new (S.Context) InitPriorityAttr(prioritynum));
1394}
1395
Mike Stumpbf916502009-07-24 19:02:52 +00001396/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1397/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001398static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001399
Chris Lattner545dd342008-06-28 23:36:30 +00001400 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001401 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001402 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001403 return;
1404 }
1405
Chris Lattner545dd342008-06-28 23:36:30 +00001406 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001407 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001408 return;
1409 }
1410
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001411 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001412 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001413 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001414 return;
1415 }
1416
Daniel Dunbar35682492008-09-26 04:12:28 +00001417 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001418 unsigned FirstIdx = 1;
1419
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001420 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001421
1422 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001423 if (Format.startswith("__") && Format.endswith("__"))
1424 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001426 // Check for supported formats.
1427 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001428
1429 if (Kind == IgnoredFormat)
1430 return;
1431
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001432 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001433 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001434 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001435 return;
1436 }
1437
1438 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001439 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001440 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001441 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1442 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001443 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001444 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001445 return;
1446 }
1447
Anders Carlsson4fb77202009-08-25 14:12:34 +00001448 // FIXME: We should handle the implicit 'this' parameter in a more generic
1449 // way that can be used for other arguments.
1450 bool HasImplicitThisParam = false;
1451 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1452 if (MD->isInstance()) {
1453 HasImplicitThisParam = true;
1454 NumArgs++;
1455 }
1456 }
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Chris Lattner6b6b5372008-06-26 18:38:35 +00001458 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001459 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001460 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001461 return;
1462 }
1463
1464 // FIXME: Do we need to bounds check?
1465 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001466
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001467 if (HasImplicitThisParam) {
1468 if (ArgIdx == 0) {
1469 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1470 << "a string type" << IdxExpr->getSourceRange();
1471 return;
1472 }
1473 ArgIdx--;
1474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Chris Lattner6b6b5372008-06-26 18:38:35 +00001476 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001477 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001478
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001479 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001480 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001481 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1482 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001483 return;
1484 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001485 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001486 // FIXME: do we need to check if the type is NSString*? What are the
1487 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001488 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001489 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001490 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1491 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001492 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001493 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001494 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001495 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001496 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001497 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1498 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001499 return;
1500 }
1501
1502 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001503 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001504 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001505 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1506 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001507 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001508 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001509 return;
1510 }
1511
1512 // check if the function is variadic if the 3rd argument non-zero
1513 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001514 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001515 ++NumArgs; // +1 for ...
1516 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001517 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001518 return;
1519 }
1520 }
1521
Chris Lattner3c73c412008-11-19 08:23:25 +00001522 // strftime requires FirstArg to be 0 because it doesn't read from any
1523 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001524 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001525 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001526 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1527 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001528 return;
1529 }
1530 // if 0 it disables parameter checking (to use with e.g. va_list)
1531 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001532 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001533 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001534 return;
1535 }
1536
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001537 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001538 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001539}
1540
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001541static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1542 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001543 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001544 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001546 return;
1547 }
1548
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001549 // Try to find the underlying union declaration.
1550 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001551 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001552 if (TD && TD->getUnderlyingType()->isUnionType())
1553 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1554 else
1555 RD = dyn_cast<RecordDecl>(d);
1556
1557 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001558 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001559 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001560 return;
1561 }
1562
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001563 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001564 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001565 diag::warn_transparent_union_attribute_not_definition);
1566 return;
1567 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001568
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001569 RecordDecl::field_iterator Field = RD->field_begin(),
1570 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001571 if (Field == FieldEnd) {
1572 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1573 return;
1574 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001575
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001576 FieldDecl *FirstField = *Field;
1577 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001578 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001579 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001580 diag::warn_transparent_union_attribute_floating)
1581 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001582 return;
1583 }
1584
1585 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1586 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1587 for (; Field != FieldEnd; ++Field) {
1588 QualType FieldType = Field->getType();
1589 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1590 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1591 // Warn if we drop the attribute.
1592 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001593 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001594 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001595 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001596 diag::warn_transparent_union_attribute_field_size_align)
1597 << isSize << Field->getDeclName() << FieldBits;
1598 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001599 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001600 diag::note_transparent_union_first_field_size_align)
1601 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001602 return;
1603 }
1604 }
1605
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001606 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001607}
1608
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001609static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001610 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001611 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001612 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001613 return;
1614 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001615 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1616 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001617
Chris Lattner6b6b5372008-06-26 18:38:35 +00001618 // Make sure that there is a string literal as the annotation's single
1619 // argument.
1620 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001621 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001622 return;
1623 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001624 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001625}
1626
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001627static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001628 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001629 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001630 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001631 return;
1632 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001633
1634 //FIXME: The C++0x version of this attribute has more limited applicabilty
1635 // than GNU's, and should error out when it is used to specify a
1636 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001637
Chris Lattner545dd342008-06-28 23:36:30 +00001638 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001639 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001640 // (For now we just use 128 bits which is the maximum on X86).
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001641 D->addAttr(::new (S.Context) AlignedAttr(128));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001642 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001643 }
Mike Stumpbf916502009-07-24 19:02:52 +00001644
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001645 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1646}
1647
1648void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1649 if (E->isTypeDependent() || E->isValueDependent()) {
1650 // Save dependent expressions in the AST to be instantiated.
1651 D->addAttr(::new (Context) AlignedAttr(E));
1652 return;
1653 }
1654
Chris Lattner49e2d342008-06-28 23:50:44 +00001655 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001656 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1657 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1658 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001659 return;
1660 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001661 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001662 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1663 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001664 return;
1665 }
1666
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001667 D->addAttr(::new (Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001668}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001669
Mike Stumpbf916502009-07-24 19:02:52 +00001670/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1671/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001672///
Mike Stumpbf916502009-07-24 19:02:52 +00001673/// Despite what would be logical, the mode attribute is a decl attribute, not a
1674/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1675/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001676static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001677 // This attribute isn't documented, but glibc uses it. It changes
1678 // the width of an int or unsigned int to the specified size.
1679
1680 // Check that there aren't any arguments
1681 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001683 return;
1684 }
1685
1686 IdentifierInfo *Name = Attr.getParameterName();
1687 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001688 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001689 return;
1690 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001691
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001692 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001693
1694 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001695 if (Str.startswith("__") && Str.endswith("__"))
1696 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001697
1698 unsigned DestWidth = 0;
1699 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001700 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001701 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001702 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001703 switch (Str[0]) {
1704 case 'Q': DestWidth = 8; break;
1705 case 'H': DestWidth = 16; break;
1706 case 'S': DestWidth = 32; break;
1707 case 'D': DestWidth = 64; break;
1708 case 'X': DestWidth = 96; break;
1709 case 'T': DestWidth = 128; break;
1710 }
1711 if (Str[1] == 'F') {
1712 IntegerMode = false;
1713 } else if (Str[1] == 'C') {
1714 IntegerMode = false;
1715 ComplexMode = true;
1716 } else if (Str[1] != 'I') {
1717 DestWidth = 0;
1718 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001719 break;
1720 case 4:
1721 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1722 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001723 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001724 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001725 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001726 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001727 break;
1728 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001729 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001730 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001731 break;
1732 }
1733
1734 QualType OldTy;
1735 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1736 OldTy = TD->getUnderlyingType();
1737 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1738 OldTy = VD->getType();
1739 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001740 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1741 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001742 return;
1743 }
Eli Friedman73397492009-03-03 06:41:03 +00001744
John McCall183700f2009-09-21 23:43:11 +00001745 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001746 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1747 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001748 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001749 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1750 } else if (ComplexMode) {
1751 if (!OldTy->isComplexType())
1752 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1753 } else {
1754 if (!OldTy->isFloatingType())
1755 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1756 }
1757
Mike Stump390b4cc2009-05-16 07:39:55 +00001758 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1759 // and friends, at least with glibc.
1760 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1761 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001762 // FIXME: Make sure floating-point mappings are accurate
1763 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001764 QualType NewTy;
1765 switch (DestWidth) {
1766 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001767 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001768 return;
1769 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001770 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001771 return;
1772 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001773 if (!IntegerMode) {
1774 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1775 return;
1776 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001777 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001778 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001779 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001780 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001781 break;
1782 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001783 if (!IntegerMode) {
1784 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1785 return;
1786 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001787 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001788 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001789 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001790 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001791 break;
1792 case 32:
1793 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001794 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001795 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001796 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001797 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001798 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001799 break;
1800 case 64:
1801 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001802 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001803 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001804 if (S.Context.Target.getLongWidth() == 64)
1805 NewTy = S.Context.LongTy;
1806 else
1807 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001808 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001809 if (S.Context.Target.getLongWidth() == 64)
1810 NewTy = S.Context.UnsignedLongTy;
1811 else
1812 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001813 break;
Eli Friedman73397492009-03-03 06:41:03 +00001814 case 96:
1815 NewTy = S.Context.LongDoubleTy;
1816 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001817 case 128:
1818 if (!IntegerMode) {
1819 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1820 return;
1821 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001822 if (OldTy->isSignedIntegerType())
1823 NewTy = S.Context.Int128Ty;
1824 else
1825 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001826 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001827 }
1828
Eli Friedman73397492009-03-03 06:41:03 +00001829 if (ComplexMode) {
1830 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001831 }
1832
1833 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001834 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1835 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001836 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001837 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001838 cast<ValueDecl>(D)->setType(NewTy);
1839}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001840
Mike Stump1feade82009-08-26 22:31:08 +00001841static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001842 // check the attribute arguments.
1843 if (Attr.getNumArgs() > 0) {
1844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1845 return;
1846 }
Anders Carlssone896d982009-02-13 08:11:52 +00001847
Anders Carlsson5bab7882009-02-19 19:16:48 +00001848 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001849 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001850 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001851 return;
1852 }
Mike Stumpbf916502009-07-24 19:02:52 +00001853
Mike Stump1feade82009-08-26 22:31:08 +00001854 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001855}
1856
Mike Stump1feade82009-08-26 22:31:08 +00001857static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001858 // check the attribute arguments.
1859 if (Attr.getNumArgs() != 0) {
1860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1861 return;
1862 }
Mike Stumpbf916502009-07-24 19:02:52 +00001863
Chris Lattnerc5197432009-04-14 17:02:11 +00001864 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001866 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001867 return;
1868 }
Mike Stumpbf916502009-07-24 19:02:52 +00001869
Mike Stump1feade82009-08-26 22:31:08 +00001870 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001871}
1872
Chris Lattner7255a2d2010-06-22 00:03:40 +00001873static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1874 Sema &S) {
1875 // check the attribute arguments.
1876 if (Attr.getNumArgs() != 0) {
1877 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1878 return;
1879 }
1880
1881 if (!isa<FunctionDecl>(d)) {
1882 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1883 << Attr.getName() << 0 /*function*/;
1884 return;
1885 }
1886
1887 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr());
1888}
1889
Chris Lattnercf2a7212009-04-20 19:12:28 +00001890static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001891 // check the attribute arguments.
1892 if (Attr.getNumArgs() != 0) {
1893 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1894 return;
1895 }
Mike Stumpbf916502009-07-24 19:02:52 +00001896
Chris Lattnerc5197432009-04-14 17:02:11 +00001897 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1898 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001899 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001900 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001901 return;
1902 }
Mike Stumpbf916502009-07-24 19:02:52 +00001903
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001904 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001905 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001906 return;
1907 }
Mike Stumpbf916502009-07-24 19:02:52 +00001908
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001909 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001910}
1911
Abramo Bagnarae215f722010-04-30 13:10:51 +00001912static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1913 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1914 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1915 assert(Attr.isInvalid() == false);
1916
1917 switch (Attr.getKind()) {
1918 case AttributeList::AT_fastcall:
1919 d->addAttr(::new (S.Context) FastCallAttr());
1920 return;
1921 case AttributeList::AT_stdcall:
1922 d->addAttr(::new (S.Context) StdCallAttr());
1923 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001924 case AttributeList::AT_thiscall:
1925 d->addAttr(::new (S.Context) ThisCallAttr());
Abramo Bagnarae215f722010-04-30 13:10:51 +00001926 case AttributeList::AT_cdecl:
1927 d->addAttr(::new (S.Context) CDeclAttr());
1928 return;
1929 default:
1930 llvm_unreachable("unexpected attribute kind");
1931 return;
1932 }
1933}
1934
Fariborz Jahanianee760332009-03-27 18:38:55 +00001935static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1936 // check the attribute arguments.
1937 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001938 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001939 return;
1940 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001941
Fariborz Jahanianee760332009-03-27 18:38:55 +00001942 if (!isFunctionOrMethod(d)) {
1943 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001944 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001945 return;
1946 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001947
1948 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1949 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001950 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1951 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001952 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1953 << "regparm" << NumParamsExpr->getSourceRange();
1954 return;
1955 }
1956
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001957 if (S.Context.Target.getRegParmMax() == 0) {
1958 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001959 << NumParamsExpr->getSourceRange();
1960 return;
1961 }
1962
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001963 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001964 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1965 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001966 return;
1967 }
1968
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001969 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001970}
1971
Sean Huntbbd37c62009-11-21 08:43:09 +00001972static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1973 // check the attribute arguments.
1974 if (Attr.getNumArgs() != 0) {
1975 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1976 return;
1977 }
1978
1979 if (!isa<CXXRecordDecl>(d)
1980 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1981 S.Diag(Attr.getLoc(),
1982 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1983 : diag::warn_attribute_wrong_decl_type)
1984 << Attr.getName() << 7 /*virtual method or class*/;
1985 return;
1986 }
Sean Hunt7725e672009-11-25 04:20:27 +00001987
1988 // FIXME: Conform to C++0x redeclaration rules.
1989
1990 if (d->getAttr<FinalAttr>()) {
1991 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1992 return;
1993 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001994
1995 d->addAttr(::new (S.Context) FinalAttr());
1996}
1997
Chris Lattner0744e5f2008-06-29 00:23:49 +00001998//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001999// C++0x member checking attributes
2000//===----------------------------------------------------------------------===//
2001
2002static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2003 if (Attr.getNumArgs() != 0) {
2004 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2005 return;
2006 }
2007
2008 if (!isa<CXXRecordDecl>(d)) {
2009 S.Diag(Attr.getLoc(),
2010 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2011 : diag::warn_attribute_wrong_decl_type)
2012 << Attr.getName() << 9 /*class*/;
2013 return;
2014 }
2015
2016 if (d->getAttr<BaseCheckAttr>()) {
2017 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2018 return;
2019 }
2020
2021 d->addAttr(::new (S.Context) BaseCheckAttr());
2022}
2023
2024static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2025 if (Attr.getNumArgs() != 0) {
2026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2027 return;
2028 }
2029
2030 if (!isa<RecordDecl>(d->getDeclContext())) {
2031 // FIXME: It's not the type that's the problem
2032 S.Diag(Attr.getLoc(),
2033 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2034 : diag::warn_attribute_wrong_decl_type)
2035 << Attr.getName() << 11 /*member*/;
2036 return;
2037 }
2038
2039 // FIXME: Conform to C++0x redeclaration rules.
2040
2041 if (d->getAttr<HidingAttr>()) {
2042 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2043 return;
2044 }
2045
2046 d->addAttr(::new (S.Context) HidingAttr());
2047}
2048
2049static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2050 if (Attr.getNumArgs() != 0) {
2051 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2052 return;
2053 }
2054
2055 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2056 // FIXME: It's not the type that's the problem
2057 S.Diag(Attr.getLoc(),
2058 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2059 : diag::warn_attribute_wrong_decl_type)
2060 << Attr.getName() << 10 /*virtual method*/;
2061 return;
2062 }
2063
2064 // FIXME: Conform to C++0x redeclaration rules.
2065
2066 if (d->getAttr<OverrideAttr>()) {
2067 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2068 return;
2069 }
2070
2071 d->addAttr(::new (S.Context) OverrideAttr());
2072}
2073
2074//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002075// Checker-specific attribute handlers.
2076//===----------------------------------------------------------------------===//
2077
2078static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2079 Sema &S) {
2080
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002081 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002082
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002083 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2084 RetTy = MD->getResultType();
2085 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2086 RetTy = FD->getResultType();
2087 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002088 SourceLocation L = Attr.getLoc();
2089 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2090 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002091 return;
2092 }
Mike Stumpbf916502009-07-24 19:02:52 +00002093
Ted Kremenek6217b802009-07-29 21:53:49 +00002094 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002095 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002096 SourceLocation L = Attr.getLoc();
2097 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2098 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002099 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002100 }
Mike Stumpbf916502009-07-24 19:02:52 +00002101
Ted Kremenekb71368d2009-05-09 02:44:38 +00002102 switch (Attr.getKind()) {
2103 default:
2104 assert(0 && "invalid ownership attribute");
2105 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002106 case AttributeList::AT_cf_returns_not_retained:
2107 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
2108 return;
2109 case AttributeList::AT_ns_returns_not_retained:
2110 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
2111 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002112 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002113 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002114 return;
2115 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002116 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002117 return;
2118 };
2119}
2120
Charles Davisf0122fe2010-02-16 18:27:26 +00002121static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2122 return Attr.getKind() == AttributeList::AT_dllimport ||
2123 Attr.getKind() == AttributeList::AT_dllexport;
2124}
2125
Ted Kremenekb71368d2009-05-09 02:44:38 +00002126//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002127// Top Level Sema Entry Points
2128//===----------------------------------------------------------------------===//
2129
Sebastian Redla89d82c2008-12-21 19:24:58 +00002130/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002131/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002132/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2133/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002134static void ProcessDeclAttribute(Scope *scope, Decl *D,
2135 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002136 if (Attr.isInvalid())
2137 return;
2138
Charles Davisf0122fe2010-02-16 18:27:26 +00002139 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2140 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002141 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002142 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002143 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002144 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2145 case AttributeList::AT_IBOutletCollection:
2146 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002147 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002148 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002149 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002150 // Ignore these, these are type attributes, handled by
2151 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002152 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002153 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2154 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002155 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002156 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002157 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002158 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002159 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2160 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002161 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002162 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002163 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2164 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2165 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002166 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002167 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002168 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002169 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2170 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2171 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2172 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2173 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2174 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2175 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2176 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002177 case AttributeList::AT_ownership_returns:
2178 case AttributeList::AT_ownership_takes:
2179 case AttributeList::AT_ownership_holds:
2180 HandleOwnershipAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002181 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2182 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2183 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002184
2185 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002186 case AttributeList::AT_ns_returns_not_retained:
2187 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002188 case AttributeList::AT_ns_returns_retained:
2189 case AttributeList::AT_cf_returns_retained:
2190 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2191
Nate Begeman6f3d8382009-06-26 06:32:41 +00002192 case AttributeList::AT_reqd_wg_size:
2193 HandleReqdWorkGroupSize(D, Attr, S); break;
2194
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002195 case AttributeList::AT_init_priority:
2196 HandleInitPriorityAttr(D, Attr, S); break;
2197
Sean Hunt7725e672009-11-25 04:20:27 +00002198 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2199 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002200 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2201 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2202 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002203 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002204 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2205 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002206 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002207 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002208 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002209 case AttributeList::AT_transparent_union:
2210 HandleTransparentUnionAttr(D, Attr, S);
2211 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002212 case AttributeList::AT_objc_exception:
2213 HandleObjCExceptionAttr(D, Attr, S);
2214 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002215 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002216 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2217 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2218 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2219 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2220 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2221 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2222 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2223 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2224 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002225 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002226 // Just ignore
2227 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002228 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2229 HandleNoInstrumentFunctionAttr(D, Attr, S);
2230 break;
John McCall04a67a62010-02-05 21:31:56 +00002231 case AttributeList::AT_stdcall:
2232 case AttributeList::AT_cdecl:
2233 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002234 case AttributeList::AT_thiscall:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002235 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002236 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002237 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002238 // Ask target about the attribute.
2239 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2240 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002241 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2242 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002243 break;
2244 }
2245}
2246
2247/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2248/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002249void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002250 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2251 ProcessDeclAttribute(S, D, *l, *this);
2252 }
2253
2254 // GCC accepts
2255 // static int a9 __attribute__((weakref));
2256 // but that looks really pointless. We reject it.
2257 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2258 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002259 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002260 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002261 }
2262}
2263
Ryan Flynne25ff832009-07-30 03:15:39 +00002264/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2265/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002266NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002267 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002268 NamedDecl *NewD = 0;
2269 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2270 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2271 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002272 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002273 if (FD->getQualifier()) {
2274 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2275 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2276 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002277 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2278 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2279 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002280 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002281 VD->getStorageClass(),
2282 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002283 if (VD->getQualifier()) {
2284 VarDecl *NewVD = cast<VarDecl>(NewD);
2285 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2286 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002287 }
2288 return NewD;
2289}
2290
2291/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2292/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002293void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002294 if (W.getUsed()) return; // only do this once
2295 W.setUsed(true);
2296 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2297 IdentifierInfo *NDId = ND->getIdentifier();
2298 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00002299 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002300 NewD->addAttr(::new (Context) WeakAttr());
2301 WeakTopLevelDecl.push_back(NewD);
2302 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2303 // to insert Decl at TU scope, sorry.
2304 DeclContext *SavedContext = CurContext;
2305 CurContext = Context.getTranslationUnitDecl();
2306 PushOnScopeChains(NewD, S);
2307 CurContext = SavedContext;
2308 } else { // just add weak to existing
2309 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002310 }
2311}
2312
Chris Lattner0744e5f2008-06-29 00:23:49 +00002313/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2314/// it, apply them to D. This is a bit tricky because PD can have attributes
2315/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002316void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002317 // Handle #pragma weak
2318 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2319 if (ND->hasLinkage()) {
2320 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2321 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002322 // Identifier referenced by #pragma weak before it was declared
2323 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002324 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2325 }
2326 }
2327 }
2328
Chris Lattner0744e5f2008-06-29 00:23:49 +00002329 // Apply decl attributes from the DeclSpec if present.
2330 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002331 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002332
Chris Lattner0744e5f2008-06-29 00:23:49 +00002333 // Walk the declarator structure, applying decl attributes that were in a type
2334 // position to the decl itself. This handles cases like:
2335 // int *__attr__(x)** D;
2336 // when X is a decl attribute.
2337 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2338 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002339 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002340
Chris Lattner0744e5f2008-06-29 00:23:49 +00002341 // Finally, apply any attributes on the decl itself.
2342 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002343 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002344}
John McCall54abf7d2009-11-04 02:18:39 +00002345
2346/// PushParsingDeclaration - Enter a new "scope" of deprecation
2347/// warnings.
2348///
2349/// The state token we use is the start index of this scope
2350/// on the warning stack.
2351Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2352 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002353 return (ParsingDeclStackState) DelayedDiagnostics.size();
2354}
2355
2356void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2357 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2358 ParsingDeclDepth--;
2359
2360 if (DelayedDiagnostics.empty())
2361 return;
2362
2363 unsigned SavedIndex = (unsigned) S;
2364 assert(SavedIndex <= DelayedDiagnostics.size() &&
2365 "saved index is out of bounds");
2366
John McCall58e6f342010-03-16 05:22:47 +00002367 unsigned E = DelayedDiagnostics.size();
2368
John McCall2f514482010-01-27 03:50:35 +00002369 // We only want to actually emit delayed diagnostics when we
2370 // successfully parsed a decl.
2371 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2372 if (D) {
2373 // We really do want to start with 0 here. We get one push for a
2374 // decl spec and another for each declarator; in a decl group like:
2375 // deprecated_typedef foo, *bar, baz();
2376 // only the declarator pops will be passed decls. This is correct;
2377 // we really do need to consider delayed diagnostics from the decl spec
2378 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002379 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002380 if (DelayedDiagnostics[I].Triggered)
2381 continue;
2382
2383 switch (DelayedDiagnostics[I].Kind) {
2384 case DelayedDiagnostic::Deprecation:
2385 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2386 break;
2387
2388 case DelayedDiagnostic::Access:
2389 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2390 break;
2391 }
2392 }
2393 }
2394
John McCall58e6f342010-03-16 05:22:47 +00002395 // Destroy all the delayed diagnostics we're about to pop off.
2396 for (unsigned I = SavedIndex; I != E; ++I)
2397 DelayedDiagnostics[I].destroy();
2398
John McCall2f514482010-01-27 03:50:35 +00002399 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002400}
2401
2402static bool isDeclDeprecated(Decl *D) {
2403 do {
2404 if (D->hasAttr<DeprecatedAttr>())
2405 return true;
2406 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2407 return false;
2408}
2409
John McCall2f514482010-01-27 03:50:35 +00002410void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2411 Decl *Ctx) {
2412 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002413 return;
2414
John McCall2f514482010-01-27 03:50:35 +00002415 DD.Triggered = true;
2416 Diag(DD.Loc, diag::warn_deprecated)
2417 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002418}
2419
2420void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2421 // Delay if we're currently parsing a declaration.
2422 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002423 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002424 return;
2425 }
2426
2427 // Otherwise, don't warn if our current context is deprecated.
2428 if (isDeclDeprecated(cast<Decl>(CurContext)))
2429 return;
2430
2431 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2432}