blob: 3b82f58be43edf619c11f2cc4356f33c6c6db0e5 [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();
349 std::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
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000353static bool isStaticVarOrStaticFunciton(Decl *D) {
354 if (VarDecl *VD = dyn_cast<VarDecl>(D))
355 return VD->getStorageClass() == VarDecl::Static;
356 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
357 return FD->getStorageClass() == FunctionDecl::Static;
358 return false;
359}
360
361static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
362 // Check the attribute arguments.
363 if (Attr.getNumArgs() > 1) {
364 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
365 return;
366 }
367
368 // gcc rejects
369 // class c {
370 // static int a __attribute__((weakref ("v2")));
371 // static int b() __attribute__((weakref ("f3")));
372 // };
373 // and ignores the attributes of
374 // void f(void) {
375 // static int a __attribute__((weakref ("v2")));
376 // }
377 // we reject them
378 if (const DeclContext *Ctx = d->getDeclContext()) {
379 Ctx = Ctx->getLookupContext();
380 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
381 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
382 dyn_cast<NamedDecl>(d)->getNameAsString();
383 return;
384 }
385 }
386
387 // The GCC manual says
388 //
389 // At present, a declaration to which `weakref' is attached can only
390 // be `static'.
391 //
392 // It also says
393 //
394 // Without a TARGET,
395 // given as an argument to `weakref' or to `alias', `weakref' is
396 // equivalent to `weak'.
397 //
398 // gcc 4.4.1 will accept
399 // int a7 __attribute__((weakref));
400 // as
401 // int a7 __attribute__((weak));
402 // This looks like a bug in gcc. We reject that for now. We should revisit
403 // it if this behaviour is actually used.
404
405 if (!isStaticVarOrStaticFunciton(d)) {
406 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
407 dyn_cast<NamedDecl>(d)->getNameAsString();
408 return;
409 }
410
411 // GCC rejects
412 // static ((alias ("y"), weakref)).
413 // Should we? How to check that weakref is before or after alias?
414
415 if (Attr.getNumArgs() == 1) {
416 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
417 Arg = Arg->IgnoreParenCasts();
418 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
419
420 if (Str == 0 || Str->isWide()) {
421 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
422 << "weakref" << 1;
423 return;
424 }
425 // GCC will accept anything as the argument of weakref. Should we
426 // check for an existing decl?
427 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
428 }
429
430 d->addAttr(::new (S.Context) WeakRefAttr());
431}
432
Chris Lattner803d0802008-06-29 00:43:07 +0000433static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000434 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000435 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000436 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000437 return;
438 }
Mike Stumpbf916502009-07-24 19:02:52 +0000439
Chris Lattner545dd342008-06-28 23:36:30 +0000440 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000441 Arg = Arg->IgnoreParenCasts();
442 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000443
Chris Lattner6b6b5372008-06-26 18:38:35 +0000444 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000445 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000446 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000447 return;
448 }
Mike Stumpbf916502009-07-24 19:02:52 +0000449
Chris Lattner6b6b5372008-06-26 18:38:35 +0000450 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000451
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000452 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000453}
454
Mike Stumpbf916502009-07-24 19:02:52 +0000455static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000456 Sema &S) {
457 // check the attribute arguments.
458 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000460 return;
461 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000462
Chris Lattnerc5197432009-04-14 17:02:11 +0000463 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000464 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000465 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000466 return;
467 }
Mike Stumpbf916502009-07-24 19:02:52 +0000468
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000469 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000470}
471
Ryan Flynn76168e22009-08-09 20:07:29 +0000472static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
473 // check the attribute arguments.
474 if (Attr.getNumArgs() != 0) {
475 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
476 return;
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000479 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000480 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000481 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
482 d->addAttr(::new (S.Context) MallocAttr());
483 return;
484 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000485 }
486
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000487 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000488}
489
Ted Kremenekb7252322009-04-10 00:01:14 +0000490static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnarae215f722010-04-30 13:10:51 +0000491 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000492 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000493 if (Attr.getNumArgs() != 0) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000494 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000495 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000496 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000497
Mike Stump19c30c02009-04-29 19:03:13 +0000498 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
499 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000500 if (VD == 0 || (!VD->getType()->isBlockPointerType()
501 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000502 S.Diag(Attr.getLoc(),
503 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
504 : diag::warn_attribute_wrong_decl_type)
505 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000506 return false;
507 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000508 }
Mike Stumpbf916502009-07-24 19:02:52 +0000509
Ted Kremenekb7252322009-04-10 00:01:14 +0000510 return true;
511}
512
513static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000514 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
515 assert(Attr.isInvalid() == false);
516 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000517}
518
519static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
520 Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000521 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000522 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000523}
524
Sean Huntbbd37c62009-11-21 08:43:09 +0000525static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
526 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000528 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000529 return;
530 }
531 // FIXME: Actually store the attribute on the declaration
532}
533
Ted Kremenek73798892008-07-25 04:39:19 +0000534static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
535 // check the attribute arguments.
536 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000537 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000538 return;
539 }
Mike Stumpbf916502009-07-24 19:02:52 +0000540
John McCallaec58602010-03-31 02:47:45 +0000541 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
542 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000543 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000544 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000545 return;
546 }
Mike Stumpbf916502009-07-24 19:02:52 +0000547
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000548 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000549}
550
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000551static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
552 // check the attribute arguments.
553 if (Attr.getNumArgs() != 0) {
554 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
555 return;
556 }
Mike Stumpbf916502009-07-24 19:02:52 +0000557
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000558 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000559 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000560 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
561 return;
562 }
563 } else if (!isFunctionOrMethod(d)) {
564 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000565 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000566 return;
567 }
Mike Stumpbf916502009-07-24 19:02:52 +0000568
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000569 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000570}
571
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000572static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
573 // check the attribute arguments.
574 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000575 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
576 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000577 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000578 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000579
580 int priority = 65535; // FIXME: Do not hardcode such constants.
581 if (Attr.getNumArgs() > 0) {
582 Expr *E = static_cast<Expr *>(Attr.getArg(0));
583 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000584 if (E->isTypeDependent() || E->isValueDependent() ||
585 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000588 return;
589 }
590 priority = Idx.getZExtValue();
591 }
Mike Stumpbf916502009-07-24 19:02:52 +0000592
Chris Lattnerc5197432009-04-14 17:02:11 +0000593 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000594 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000595 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000596 return;
597 }
598
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000599 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000600}
601
602static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
603 // check the attribute arguments.
604 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000605 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
606 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000607 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000608 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000609
610 int priority = 65535; // FIXME: Do not hardcode such constants.
611 if (Attr.getNumArgs() > 0) {
612 Expr *E = static_cast<Expr *>(Attr.getArg(0));
613 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000614 if (E->isTypeDependent() || E->isValueDependent() ||
615 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000616 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000617 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000618 return;
619 }
620 priority = Idx.getZExtValue();
621 }
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Anders Carlsson6782fc62008-08-22 22:10:48 +0000623 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000624 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000625 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000626 return;
627 }
628
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000629 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000630}
631
Chris Lattner803d0802008-06-29 00:43:07 +0000632static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000633 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000634 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000635 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000636 return;
637 }
Mike Stumpbf916502009-07-24 19:02:52 +0000638
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000639 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000640}
641
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000642static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
643 // check the attribute arguments.
644 if (Attr.getNumArgs() != 0) {
645 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
646 return;
647 }
Mike Stumpbf916502009-07-24 19:02:52 +0000648
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000649 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000650}
651
Chris Lattner803d0802008-06-29 00:43:07 +0000652static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000653 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000654 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000655 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000656 return;
657 }
Mike Stumpbf916502009-07-24 19:02:52 +0000658
Chris Lattner545dd342008-06-28 23:36:30 +0000659 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000660 Arg = Arg->IgnoreParenCasts();
661 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000662
Chris Lattner6b6b5372008-06-26 18:38:35 +0000663 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000664 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000665 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000666 return;
667 }
Mike Stumpbf916502009-07-24 19:02:52 +0000668
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000669 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000670 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000671
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000672 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000673 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000674 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000675 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000676 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000677 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000678 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000679 type = VisibilityAttr::ProtectedVisibility;
680 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000681 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000682 return;
683 }
Mike Stumpbf916502009-07-24 19:02:52 +0000684
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000685 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000686}
687
Chris Lattner0db29ec2009-02-14 08:09:34 +0000688static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
689 Sema &S) {
690 if (Attr.getNumArgs() != 0) {
691 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
692 return;
693 }
Mike Stumpbf916502009-07-24 19:02:52 +0000694
Chris Lattner0db29ec2009-02-14 08:09:34 +0000695 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
696 if (OCI == 0) {
697 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
698 return;
699 }
Mike Stumpbf916502009-07-24 19:02:52 +0000700
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000701 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000702}
703
704static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000705 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000706 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000707 return;
708 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000709 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000710 QualType T = TD->getUnderlyingType();
711 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000712 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000713 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
714 return;
715 }
716 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000717 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000718}
719
Mike Stumpbf916502009-07-24 19:02:52 +0000720static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000721HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
722 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000723 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000724 return;
725 }
726
727 if (!isa<FunctionDecl>(D)) {
728 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
729 return;
730 }
731
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000732 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000733}
734
Steve Naroff9eae5762008-09-18 16:44:58 +0000735static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000736 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000737 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000738 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000739 return;
740 }
Mike Stumpbf916502009-07-24 19:02:52 +0000741
Steve Naroff9eae5762008-09-18 16:44:58 +0000742 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000743 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000744 return;
745 }
Mike Stumpbf916502009-07-24 19:02:52 +0000746
Steve Naroff9eae5762008-09-18 16:44:58 +0000747 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000748 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000749 type = BlocksAttr::ByRef;
750 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000751 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000752 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000753 return;
754 }
Mike Stumpbf916502009-07-24 19:02:52 +0000755
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000756 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000757}
758
Anders Carlsson77091822008-10-05 18:05:59 +0000759static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
760 // check the attribute arguments.
761 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000762 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
763 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000764 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000765 }
766
Anders Carlsson77091822008-10-05 18:05:59 +0000767 int sentinel = 0;
768 if (Attr.getNumArgs() > 0) {
769 Expr *E = static_cast<Expr *>(Attr.getArg(0));
770 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000771 if (E->isTypeDependent() || E->isValueDependent() ||
772 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000773 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000774 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000775 return;
776 }
777 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000778
Anders Carlsson77091822008-10-05 18:05:59 +0000779 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000780 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
781 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000782 return;
783 }
784 }
785
786 int nullPos = 0;
787 if (Attr.getNumArgs() > 1) {
788 Expr *E = static_cast<Expr *>(Attr.getArg(1));
789 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000790 if (E->isTypeDependent() || E->isValueDependent() ||
791 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000792 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000793 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000794 return;
795 }
796 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000797
Anders Carlsson77091822008-10-05 18:05:59 +0000798 if (nullPos > 1 || nullPos < 0) {
799 // FIXME: This error message could be improved, it would be nice
800 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000801 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
802 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000803 return;
804 }
805 }
806
807 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +0000808 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +0000809 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +0000810
Chris Lattner897cd902009-03-17 23:03:47 +0000811 if (isa<FunctionNoProtoType>(FT)) {
812 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
813 return;
814 }
Mike Stumpbf916502009-07-24 19:02:52 +0000815
Chris Lattner897cd902009-03-17 23:03:47 +0000816 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000817 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000818 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000819 }
Anders Carlsson77091822008-10-05 18:05:59 +0000820 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
821 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000822 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000823 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000824 }
825 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +0000826 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
827 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000828 ;
829 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000830 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000831 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +0000832 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +0000833 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000834 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000835 int m = Ty->isFunctionPointerType() ? 0 : 1;
836 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000837 return;
838 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000839 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000840 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000841 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000842 return;
843 }
Anders Carlsson77091822008-10-05 18:05:59 +0000844 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000845 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000846 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000847 return;
848 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000849 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000850}
851
Chris Lattner026dc962009-02-14 07:37:35 +0000852static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
853 // check the attribute arguments.
854 if (Attr.getNumArgs() != 0) {
855 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
856 return;
857 }
858
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000859 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000860 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000861 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000862 return;
863 }
Mike Stumpbf916502009-07-24 19:02:52 +0000864
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000865 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
866 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
867 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +0000868 return;
869 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000870 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
871 if (MD->getResultType()->isVoidType()) {
872 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
873 << Attr.getName() << 1;
874 return;
875 }
876
Nuno Lopesd20254f2009-12-20 23:11:08 +0000877 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000878}
879
880static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000881 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000882 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000883 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000884 return;
885 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000886
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000887 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000888 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +0000889 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
890 dyn_cast<NamedDecl>(D)->getNameAsString();
891 return;
892 }
893
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000894 // TODO: could also be applied to methods?
895 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
896 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000897 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000898 return;
899 }
Mike Stumpbf916502009-07-24 19:02:52 +0000900
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000901 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000902}
903
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000904static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
905 // check the attribute arguments.
906 if (Attr.getNumArgs() != 0) {
907 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
908 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000909 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000910
911 // weak_import only applies to variable & function declarations.
912 bool isDef = false;
913 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
914 isDef = (!VD->hasExternalStorage() || VD->getInit());
915 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000916 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000917 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
918 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000919 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +0000920 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +0000921 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +0000922 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +0000923 !isa<ObjCInterfaceDecl>(D))
924 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +0000925 << Attr.getName() << 2 /*variable and function*/;
926 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000927 }
928
929 // Merge should handle any subsequent violations.
930 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +0000931 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000932 diag::warn_attribute_weak_import_invalid_on_definition)
933 << "weak_import" << 2 /*variable and function*/;
934 return;
935 }
936
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000937 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000938}
939
Nate Begeman6f3d8382009-06-26 06:32:41 +0000940static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
941 Sema &S) {
942 // Attribute has 3 arguments.
943 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +0000944 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +0000945 return;
946 }
947
948 unsigned WGSize[3];
949 for (unsigned i = 0; i < 3; ++i) {
950 Expr *E = static_cast<Expr *>(Attr.getArg(i));
951 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000952 if (E->isTypeDependent() || E->isValueDependent() ||
953 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +0000954 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
955 << "reqd_work_group_size" << E->getSourceRange();
956 return;
957 }
958 WGSize[i] = (unsigned) ArgNum.getZExtValue();
959 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000960 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +0000961 WGSize[2]));
962}
963
Chris Lattner026dc962009-02-14 07:37:35 +0000964static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000965 // Attribute has no arguments.
966 if (Attr.getNumArgs() != 1) {
967 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
968 return;
969 }
970
971 // Make sure that there is a string literal as the sections's single
972 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +0000973 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
974 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000975 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +0000976 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000977 return;
978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattner797c3c42009-08-10 19:03:04 +0000980 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +0000981 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000982 if (!Error.empty()) {
983 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
984 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +0000985 return;
986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Chris Lattnera1e1dc72010-01-12 20:58:53 +0000988 // This attribute cannot be applied to local variables.
989 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
990 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
991 return;
992 }
993
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000994 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000995}
996
Chris Lattner6b6b5372008-06-26 18:38:35 +0000997
Chris Lattner803d0802008-06-29 00:43:07 +0000998static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000999 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001000 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001001 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001002 return;
1003 }
Mike Stumpbf916502009-07-24 19:02:52 +00001004
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001005 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001006}
1007
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001008static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1009 // check the attribute arguments.
1010 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001011 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001012 return;
1013 }
Mike Stumpbf916502009-07-24 19:02:52 +00001014
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001015 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001016}
1017
1018static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1019 // check the attribute arguments.
1020 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001021 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001022 return;
1023 }
Mike Stumpbf916502009-07-24 19:02:52 +00001024
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001025 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001026}
1027
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001028static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001029 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001030 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1031 return;
1032 }
Mike Stumpbf916502009-07-24 19:02:52 +00001033
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001034 if (Attr.getNumArgs() != 0) {
1035 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1036 return;
1037 }
Mike Stumpbf916502009-07-24 19:02:52 +00001038
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001039 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001040
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001041 if (!VD || !VD->hasLocalStorage()) {
1042 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1043 return;
1044 }
Mike Stumpbf916502009-07-24 19:02:52 +00001045
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001046 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001047 // FIXME: Lookup probably isn't looking in the right place
1048 // FIXME: The lookup source location should be in the attribute, not the
1049 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001050 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001051 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001052 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001053 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001054 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001055 Attr.getParameterName();
1056 return;
1057 }
Mike Stumpbf916502009-07-24 19:02:52 +00001058
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001059 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1060 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001061 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001062 Attr.getParameterName();
1063 return;
1064 }
1065
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001066 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001067 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001068 Attr.getParameterName();
1069 return;
1070 }
Mike Stumpbf916502009-07-24 19:02:52 +00001071
Anders Carlsson89941c12009-02-07 23:16:50 +00001072 // We're currently more strict than GCC about what function types we accept.
1073 // If this ever proves to be a problem it should be easy to fix.
1074 QualType Ty = S.Context.getPointerType(VD->getType());
1075 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001076 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001077 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001078 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1079 Attr.getParameterName() << ParamTy << Ty;
1080 return;
1081 }
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001083 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001084}
1085
Mike Stumpbf916502009-07-24 19:02:52 +00001086/// Handle __attribute__((format_arg((idx)))) attribute based on
1087/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1088static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001089 if (Attr.getNumArgs() != 1) {
1090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1091 return;
1092 }
1093 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1094 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1095 << Attr.getName() << 0 /*function*/;
1096 return;
1097 }
Mike Stumpbf916502009-07-24 19:02:52 +00001098 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1099 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001100 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1101 unsigned FirstIdx = 1;
1102 // checks for the 2nd argument
1103 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1104 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001105 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1106 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001107 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1108 << "format" << 2 << IdxExpr->getSourceRange();
1109 return;
1110 }
Mike Stumpbf916502009-07-24 19:02:52 +00001111
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001112 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1113 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1114 << "format" << 2 << IdxExpr->getSourceRange();
1115 return;
1116 }
Mike Stumpbf916502009-07-24 19:02:52 +00001117
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001118 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001119
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001120 // make sure the format string is really a string
1121 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001122
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001123 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1124 if (not_nsstring_type &&
1125 !isCFStringType(Ty, S.Context) &&
1126 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001127 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001128 // FIXME: Should highlight the actual expression that has the wrong type.
1129 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001130 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001131 << IdxExpr->getSourceRange();
1132 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001133 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001134 Ty = getFunctionOrMethodResultType(d);
1135 if (!isNSStringType(Ty, S.Context) &&
1136 !isCFStringType(Ty, S.Context) &&
1137 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001138 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001139 // FIXME: Should highlight the actual expression that has the wrong type.
1140 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001141 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001142 << IdxExpr->getSourceRange();
1143 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001144 }
1145
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001146 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001147}
1148
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001149enum FormatAttrKind {
1150 CFStringFormat,
1151 NSStringFormat,
1152 StrftimeFormat,
1153 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001154 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001155 InvalidFormat
1156};
1157
1158/// getFormatAttrKind - Map from format attribute names to supported format
1159/// types.
1160static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1161 // Check for formats that get handled specially.
1162 if (Format == "NSString")
1163 return NSStringFormat;
1164 if (Format == "CFString")
1165 return CFStringFormat;
1166 if (Format == "strftime")
1167 return StrftimeFormat;
1168
1169 // Otherwise, check for supported formats.
1170 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1171 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1172 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1173 Format == "zcmn_err")
1174 return SupportedFormat;
1175
Duncan Sandsbc525952010-03-23 14:44:19 +00001176 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1177 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001178 return IgnoredFormat;
1179
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001180 return InvalidFormat;
1181}
1182
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001183/// Handle __attribute__((init_priority(priority))) attributes based on
1184/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1185static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1186 Sema &S) {
1187 if (!S.getLangOptions().CPlusPlus) {
1188 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1189 return;
1190 }
1191
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001192 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1193 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1194 Attr.setInvalid();
1195 return;
1196 }
1197 QualType T = dyn_cast<VarDecl>(d)->getType();
1198 if (S.Context.getAsArrayType(T))
1199 T = S.Context.getBaseElementType(T);
1200 if (!T->getAs<RecordType>()) {
1201 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1202 Attr.setInvalid();
1203 return;
1204 }
1205
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001206 if (Attr.getNumArgs() != 1) {
1207 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1208 Attr.setInvalid();
1209 return;
1210 }
1211 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001212
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001213 llvm::APSInt priority(32);
1214 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1215 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1216 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1217 << "init_priority" << priorityExpr->getSourceRange();
1218 Attr.setInvalid();
1219 return;
1220 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001221 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001222 if (prioritynum < 101 || prioritynum > 65535) {
1223 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1224 << priorityExpr->getSourceRange();
1225 Attr.setInvalid();
1226 return;
1227 }
1228 d->addAttr(::new (S.Context) InitPriorityAttr(prioritynum));
1229}
1230
Mike Stumpbf916502009-07-24 19:02:52 +00001231/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1232/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001233static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001234
Chris Lattner545dd342008-06-28 23:36:30 +00001235 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001236 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001237 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001238 return;
1239 }
1240
Chris Lattner545dd342008-06-28 23:36:30 +00001241 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001242 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001243 return;
1244 }
1245
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001246 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001247 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001248 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001249 return;
1250 }
1251
Daniel Dunbar35682492008-09-26 04:12:28 +00001252 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001253 unsigned FirstIdx = 1;
1254
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001255 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001256
1257 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001258 if (Format.startswith("__") && Format.endswith("__"))
1259 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001261 // Check for supported formats.
1262 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001263
1264 if (Kind == IgnoredFormat)
1265 return;
1266
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001267 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001268 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001269 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001270 return;
1271 }
1272
1273 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001274 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001275 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001276 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1277 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001279 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001280 return;
1281 }
1282
Anders Carlsson4fb77202009-08-25 14:12:34 +00001283 // FIXME: We should handle the implicit 'this' parameter in a more generic
1284 // way that can be used for other arguments.
1285 bool HasImplicitThisParam = false;
1286 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1287 if (MD->isInstance()) {
1288 HasImplicitThisParam = true;
1289 NumArgs++;
1290 }
1291 }
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattner6b6b5372008-06-26 18:38:35 +00001293 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001294 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001295 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001296 return;
1297 }
1298
1299 // FIXME: Do we need to bounds check?
1300 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001301
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001302 if (HasImplicitThisParam) {
1303 if (ArgIdx == 0) {
1304 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1305 << "a string type" << IdxExpr->getSourceRange();
1306 return;
1307 }
1308 ArgIdx--;
1309 }
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Chris Lattner6b6b5372008-06-26 18:38:35 +00001311 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001312 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001313
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001314 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001315 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001316 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1317 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001318 return;
1319 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001320 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001321 // FIXME: do we need to check if the type is NSString*? What are the
1322 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001323 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001324 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001325 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1326 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001327 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001328 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001329 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001330 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001331 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001332 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1333 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001334 return;
1335 }
1336
1337 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001338 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001339 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001340 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1341 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001342 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001343 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001344 return;
1345 }
1346
1347 // check if the function is variadic if the 3rd argument non-zero
1348 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001349 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 ++NumArgs; // +1 for ...
1351 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001352 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001353 return;
1354 }
1355 }
1356
Chris Lattner3c73c412008-11-19 08:23:25 +00001357 // strftime requires FirstArg to be 0 because it doesn't read from any
1358 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001359 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001360 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001361 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1362 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001363 return;
1364 }
1365 // if 0 it disables parameter checking (to use with e.g. va_list)
1366 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001367 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001368 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001369 return;
1370 }
1371
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001372 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001373 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001374}
1375
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001376static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1377 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001378 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001379 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001380 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001381 return;
1382 }
1383
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001384 // Try to find the underlying union declaration.
1385 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001386 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001387 if (TD && TD->getUnderlyingType()->isUnionType())
1388 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1389 else
1390 RD = dyn_cast<RecordDecl>(d);
1391
1392 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001393 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001394 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001395 return;
1396 }
1397
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001398 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001399 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001400 diag::warn_transparent_union_attribute_not_definition);
1401 return;
1402 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001403
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001404 RecordDecl::field_iterator Field = RD->field_begin(),
1405 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001406 if (Field == FieldEnd) {
1407 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1408 return;
1409 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001410
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001411 FieldDecl *FirstField = *Field;
1412 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001413 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001414 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001415 diag::warn_transparent_union_attribute_floating)
1416 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001417 return;
1418 }
1419
1420 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1421 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1422 for (; Field != FieldEnd; ++Field) {
1423 QualType FieldType = Field->getType();
1424 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1425 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1426 // Warn if we drop the attribute.
1427 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001428 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001429 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001430 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001431 diag::warn_transparent_union_attribute_field_size_align)
1432 << isSize << Field->getDeclName() << FieldBits;
1433 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001434 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001435 diag::note_transparent_union_first_field_size_align)
1436 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001437 return;
1438 }
1439 }
1440
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001441 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001442}
1443
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001444static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001445 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001446 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001447 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001448 return;
1449 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001450 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1451 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001452
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453 // Make sure that there is a string literal as the annotation's single
1454 // argument.
1455 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001456 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001457 return;
1458 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001459 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001460}
1461
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001462static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001463 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001464 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001465 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001466 return;
1467 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001468
1469 //FIXME: The C++0x version of this attribute has more limited applicabilty
1470 // than GNU's, and should error out when it is used to specify a
1471 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001472
Chris Lattner545dd342008-06-28 23:36:30 +00001473 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001474 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001475 // (For now we just use 128 bits which is the maximum on X86).
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001476 D->addAttr(::new (S.Context) AlignedAttr(128));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001477 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001478 }
Mike Stumpbf916502009-07-24 19:02:52 +00001479
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001480 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1481}
1482
1483void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1484 if (E->isTypeDependent() || E->isValueDependent()) {
1485 // Save dependent expressions in the AST to be instantiated.
1486 D->addAttr(::new (Context) AlignedAttr(E));
1487 return;
1488 }
1489
Chris Lattner49e2d342008-06-28 23:50:44 +00001490 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001491 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1492 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1493 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001494 return;
1495 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001496 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001497 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1498 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001499 return;
1500 }
1501
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001502 D->addAttr(::new (Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001503}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001504
Mike Stumpbf916502009-07-24 19:02:52 +00001505/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1506/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001507///
Mike Stumpbf916502009-07-24 19:02:52 +00001508/// Despite what would be logical, the mode attribute is a decl attribute, not a
1509/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1510/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001511static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001512 // This attribute isn't documented, but glibc uses it. It changes
1513 // the width of an int or unsigned int to the specified size.
1514
1515 // Check that there aren't any arguments
1516 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001517 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001518 return;
1519 }
1520
1521 IdentifierInfo *Name = Attr.getParameterName();
1522 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001523 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001524 return;
1525 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001526
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001527 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001528
1529 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001530 if (Str.startswith("__") && Str.endswith("__"))
1531 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001532
1533 unsigned DestWidth = 0;
1534 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001535 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001536 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001537 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001538 switch (Str[0]) {
1539 case 'Q': DestWidth = 8; break;
1540 case 'H': DestWidth = 16; break;
1541 case 'S': DestWidth = 32; break;
1542 case 'D': DestWidth = 64; break;
1543 case 'X': DestWidth = 96; break;
1544 case 'T': DestWidth = 128; break;
1545 }
1546 if (Str[1] == 'F') {
1547 IntegerMode = false;
1548 } else if (Str[1] == 'C') {
1549 IntegerMode = false;
1550 ComplexMode = true;
1551 } else if (Str[1] != 'I') {
1552 DestWidth = 0;
1553 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001554 break;
1555 case 4:
1556 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1557 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001558 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001559 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001560 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001561 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001562 break;
1563 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001564 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001565 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001566 break;
1567 }
1568
1569 QualType OldTy;
1570 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1571 OldTy = TD->getUnderlyingType();
1572 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1573 OldTy = VD->getType();
1574 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001575 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1576 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001577 return;
1578 }
Eli Friedman73397492009-03-03 06:41:03 +00001579
John McCall183700f2009-09-21 23:43:11 +00001580 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001581 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1582 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001583 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001584 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1585 } else if (ComplexMode) {
1586 if (!OldTy->isComplexType())
1587 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1588 } else {
1589 if (!OldTy->isFloatingType())
1590 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1591 }
1592
Mike Stump390b4cc2009-05-16 07:39:55 +00001593 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1594 // and friends, at least with glibc.
1595 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1596 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001597 // FIXME: Make sure floating-point mappings are accurate
1598 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001599 QualType NewTy;
1600 switch (DestWidth) {
1601 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001602 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001603 return;
1604 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001605 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001606 return;
1607 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001608 if (!IntegerMode) {
1609 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1610 return;
1611 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001612 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001613 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001614 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001615 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001616 break;
1617 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001618 if (!IntegerMode) {
1619 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1620 return;
1621 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001622 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001623 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001624 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001625 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001626 break;
1627 case 32:
1628 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001629 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001630 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001631 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001632 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001633 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001634 break;
1635 case 64:
1636 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001637 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001638 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001639 if (S.Context.Target.getLongWidth() == 64)
1640 NewTy = S.Context.LongTy;
1641 else
1642 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001643 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001644 if (S.Context.Target.getLongWidth() == 64)
1645 NewTy = S.Context.UnsignedLongTy;
1646 else
1647 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001648 break;
Eli Friedman73397492009-03-03 06:41:03 +00001649 case 96:
1650 NewTy = S.Context.LongDoubleTy;
1651 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001652 case 128:
1653 if (!IntegerMode) {
1654 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1655 return;
1656 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001657 if (OldTy->isSignedIntegerType())
1658 NewTy = S.Context.Int128Ty;
1659 else
1660 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001661 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001662 }
1663
Eli Friedman73397492009-03-03 06:41:03 +00001664 if (ComplexMode) {
1665 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001666 }
1667
1668 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001669 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1670 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001671 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001672 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001673 cast<ValueDecl>(D)->setType(NewTy);
1674}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001675
Mike Stump1feade82009-08-26 22:31:08 +00001676static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001677 // check the attribute arguments.
1678 if (Attr.getNumArgs() > 0) {
1679 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1680 return;
1681 }
Anders Carlssone896d982009-02-13 08:11:52 +00001682
Anders Carlsson5bab7882009-02-19 19:16:48 +00001683 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001684 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001685 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001686 return;
1687 }
Mike Stumpbf916502009-07-24 19:02:52 +00001688
Mike Stump1feade82009-08-26 22:31:08 +00001689 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001690}
1691
Mike Stump1feade82009-08-26 22:31:08 +00001692static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001693 // check the attribute arguments.
1694 if (Attr.getNumArgs() != 0) {
1695 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1696 return;
1697 }
Mike Stumpbf916502009-07-24 19:02:52 +00001698
Chris Lattnerc5197432009-04-14 17:02:11 +00001699 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001700 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001701 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001702 return;
1703 }
Mike Stumpbf916502009-07-24 19:02:52 +00001704
Mike Stump1feade82009-08-26 22:31:08 +00001705 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001706}
1707
Chris Lattner7255a2d2010-06-22 00:03:40 +00001708static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1709 Sema &S) {
1710 // check the attribute arguments.
1711 if (Attr.getNumArgs() != 0) {
1712 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1713 return;
1714 }
1715
1716 if (!isa<FunctionDecl>(d)) {
1717 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1718 << Attr.getName() << 0 /*function*/;
1719 return;
1720 }
1721
1722 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr());
1723}
1724
Chris Lattnercf2a7212009-04-20 19:12:28 +00001725static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001726 // check the attribute arguments.
1727 if (Attr.getNumArgs() != 0) {
1728 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1729 return;
1730 }
Mike Stumpbf916502009-07-24 19:02:52 +00001731
Chris Lattnerc5197432009-04-14 17:02:11 +00001732 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1733 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001735 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001736 return;
1737 }
Mike Stumpbf916502009-07-24 19:02:52 +00001738
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001739 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001740 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001741 return;
1742 }
Mike Stumpbf916502009-07-24 19:02:52 +00001743
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001744 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001745}
1746
Abramo Bagnarae215f722010-04-30 13:10:51 +00001747static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1748 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1749 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1750 assert(Attr.isInvalid() == false);
1751
1752 switch (Attr.getKind()) {
1753 case AttributeList::AT_fastcall:
1754 d->addAttr(::new (S.Context) FastCallAttr());
1755 return;
1756 case AttributeList::AT_stdcall:
1757 d->addAttr(::new (S.Context) StdCallAttr());
1758 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001759 case AttributeList::AT_thiscall:
1760 d->addAttr(::new (S.Context) ThisCallAttr());
Abramo Bagnarae215f722010-04-30 13:10:51 +00001761 case AttributeList::AT_cdecl:
1762 d->addAttr(::new (S.Context) CDeclAttr());
1763 return;
1764 default:
1765 llvm_unreachable("unexpected attribute kind");
1766 return;
1767 }
1768}
1769
Fariborz Jahanianee760332009-03-27 18:38:55 +00001770static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1771 // check the attribute arguments.
1772 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001773 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001774 return;
1775 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001776
Fariborz Jahanianee760332009-03-27 18:38:55 +00001777 if (!isFunctionOrMethod(d)) {
1778 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001779 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001780 return;
1781 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001782
1783 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1784 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001785 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1786 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001787 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1788 << "regparm" << NumParamsExpr->getSourceRange();
1789 return;
1790 }
1791
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001792 if (S.Context.Target.getRegParmMax() == 0) {
1793 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001794 << NumParamsExpr->getSourceRange();
1795 return;
1796 }
1797
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001798 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001799 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1800 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001801 return;
1802 }
1803
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001804 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001805}
1806
Sean Huntbbd37c62009-11-21 08:43:09 +00001807static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1808 // check the attribute arguments.
1809 if (Attr.getNumArgs() != 0) {
1810 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1811 return;
1812 }
1813
1814 if (!isa<CXXRecordDecl>(d)
1815 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1816 S.Diag(Attr.getLoc(),
1817 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1818 : diag::warn_attribute_wrong_decl_type)
1819 << Attr.getName() << 7 /*virtual method or class*/;
1820 return;
1821 }
Sean Hunt7725e672009-11-25 04:20:27 +00001822
1823 // FIXME: Conform to C++0x redeclaration rules.
1824
1825 if (d->getAttr<FinalAttr>()) {
1826 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1827 return;
1828 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001829
1830 d->addAttr(::new (S.Context) FinalAttr());
1831}
1832
Chris Lattner0744e5f2008-06-29 00:23:49 +00001833//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00001834// C++0x member checking attributes
1835//===----------------------------------------------------------------------===//
1836
1837static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1838 if (Attr.getNumArgs() != 0) {
1839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1840 return;
1841 }
1842
1843 if (!isa<CXXRecordDecl>(d)) {
1844 S.Diag(Attr.getLoc(),
1845 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1846 : diag::warn_attribute_wrong_decl_type)
1847 << Attr.getName() << 9 /*class*/;
1848 return;
1849 }
1850
1851 if (d->getAttr<BaseCheckAttr>()) {
1852 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1853 return;
1854 }
1855
1856 d->addAttr(::new (S.Context) BaseCheckAttr());
1857}
1858
1859static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1860 if (Attr.getNumArgs() != 0) {
1861 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1862 return;
1863 }
1864
1865 if (!isa<RecordDecl>(d->getDeclContext())) {
1866 // FIXME: It's not the type that's the problem
1867 S.Diag(Attr.getLoc(),
1868 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1869 : diag::warn_attribute_wrong_decl_type)
1870 << Attr.getName() << 11 /*member*/;
1871 return;
1872 }
1873
1874 // FIXME: Conform to C++0x redeclaration rules.
1875
1876 if (d->getAttr<HidingAttr>()) {
1877 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1878 return;
1879 }
1880
1881 d->addAttr(::new (S.Context) HidingAttr());
1882}
1883
1884static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1885 if (Attr.getNumArgs() != 0) {
1886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1887 return;
1888 }
1889
1890 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1891 // FIXME: It's not the type that's the problem
1892 S.Diag(Attr.getLoc(),
1893 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1894 : diag::warn_attribute_wrong_decl_type)
1895 << Attr.getName() << 10 /*virtual method*/;
1896 return;
1897 }
1898
1899 // FIXME: Conform to C++0x redeclaration rules.
1900
1901 if (d->getAttr<OverrideAttr>()) {
1902 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1903 return;
1904 }
1905
1906 d->addAttr(::new (S.Context) OverrideAttr());
1907}
1908
1909//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001910// Checker-specific attribute handlers.
1911//===----------------------------------------------------------------------===//
1912
1913static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1914 Sema &S) {
1915
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001916 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00001917
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001918 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1919 RetTy = MD->getResultType();
1920 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1921 RetTy = FD->getResultType();
1922 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001923 SourceLocation L = Attr.getLoc();
1924 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1925 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001926 return;
1927 }
Mike Stumpbf916502009-07-24 19:02:52 +00001928
Ted Kremenek6217b802009-07-29 21:53:49 +00001929 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00001930 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00001931 SourceLocation L = Attr.getLoc();
1932 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1933 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00001934 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001935 }
Mike Stumpbf916502009-07-24 19:02:52 +00001936
Ted Kremenekb71368d2009-05-09 02:44:38 +00001937 switch (Attr.getKind()) {
1938 default:
1939 assert(0 && "invalid ownership attribute");
1940 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00001941 case AttributeList::AT_cf_returns_not_retained:
1942 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
1943 return;
1944 case AttributeList::AT_ns_returns_not_retained:
1945 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
1946 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001947 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001948 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001949 return;
1950 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001951 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00001952 return;
1953 };
1954}
1955
Charles Davisf0122fe2010-02-16 18:27:26 +00001956static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
1957 return Attr.getKind() == AttributeList::AT_dllimport ||
1958 Attr.getKind() == AttributeList::AT_dllexport;
1959}
1960
Ted Kremenekb71368d2009-05-09 02:44:38 +00001961//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001962// Top Level Sema Entry Points
1963//===----------------------------------------------------------------------===//
1964
Sebastian Redla89d82c2008-12-21 19:24:58 +00001965/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001966/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00001967/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1968/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00001969static void ProcessDeclAttribute(Scope *scope, Decl *D,
1970 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00001971 if (Attr.isInvalid())
1972 return;
1973
Charles Davisf0122fe2010-02-16 18:27:26 +00001974 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
1975 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00001976 return;
Chris Lattner803d0802008-06-29 00:43:07 +00001977 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00001978 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00001979 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
1980 case AttributeList::AT_IBOutletCollection:
1981 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001982 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001983 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00001984 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00001985 // Ignore these, these are type attributes, handled by
1986 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001987 break;
Sean Hunt7725e672009-11-25 04:20:27 +00001988 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1989 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00001990 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001991 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001992 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00001993 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001994 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1995 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00001996 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00001997 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00001998 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1999 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2000 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002001 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002002 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002003 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002004 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2005 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2006 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2007 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2008 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2009 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2010 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2011 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
2012 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2013 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2014 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002015
2016 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002017 case AttributeList::AT_ns_returns_not_retained:
2018 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002019 case AttributeList::AT_ns_returns_retained:
2020 case AttributeList::AT_cf_returns_retained:
2021 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2022
Nate Begeman6f3d8382009-06-26 06:32:41 +00002023 case AttributeList::AT_reqd_wg_size:
2024 HandleReqdWorkGroupSize(D, Attr, S); break;
2025
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002026 case AttributeList::AT_init_priority:
2027 HandleInitPriorityAttr(D, Attr, S); break;
2028
Sean Hunt7725e672009-11-25 04:20:27 +00002029 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2030 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002031 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2032 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2033 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002034 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002035 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2036 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002037 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002038 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002039 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002040 case AttributeList::AT_transparent_union:
2041 HandleTransparentUnionAttr(D, Attr, S);
2042 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002043 case AttributeList::AT_objc_exception:
2044 HandleObjCExceptionAttr(D, Attr, S);
2045 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002046 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002047 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2048 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2049 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2050 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2051 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2052 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2053 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2054 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2055 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002056 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002057 // Just ignore
2058 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002059 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2060 HandleNoInstrumentFunctionAttr(D, Attr, S);
2061 break;
John McCall04a67a62010-02-05 21:31:56 +00002062 case AttributeList::AT_stdcall:
2063 case AttributeList::AT_cdecl:
2064 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002065 case AttributeList::AT_thiscall:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002066 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002067 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002068 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002069 // Ask target about the attribute.
2070 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2071 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002072 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2073 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002074 break;
2075 }
2076}
2077
2078/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2079/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002080void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002081 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2082 ProcessDeclAttribute(S, D, *l, *this);
2083 }
2084
2085 // GCC accepts
2086 // static int a9 __attribute__((weakref));
2087 // but that looks really pointless. We reject it.
2088 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2089 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
2090 dyn_cast<NamedDecl>(D)->getNameAsString();
2091 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002092 }
2093}
2094
Ryan Flynne25ff832009-07-30 03:15:39 +00002095/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2096/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002097NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002098 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002099 NamedDecl *NewD = 0;
2100 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2101 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2102 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002103 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002104 if (FD->getQualifier()) {
2105 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2106 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2107 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002108 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2109 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2110 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002111 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002112 VD->getStorageClass(),
2113 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002114 if (VD->getQualifier()) {
2115 VarDecl *NewVD = cast<VarDecl>(NewD);
2116 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2117 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002118 }
2119 return NewD;
2120}
2121
2122/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2123/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002124void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002125 if (W.getUsed()) return; // only do this once
2126 W.setUsed(true);
2127 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2128 IdentifierInfo *NDId = ND->getIdentifier();
2129 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00002130 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002131 NewD->addAttr(::new (Context) WeakAttr());
2132 WeakTopLevelDecl.push_back(NewD);
2133 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2134 // to insert Decl at TU scope, sorry.
2135 DeclContext *SavedContext = CurContext;
2136 CurContext = Context.getTranslationUnitDecl();
2137 PushOnScopeChains(NewD, S);
2138 CurContext = SavedContext;
2139 } else { // just add weak to existing
2140 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002141 }
2142}
2143
Chris Lattner0744e5f2008-06-29 00:23:49 +00002144/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2145/// it, apply them to D. This is a bit tricky because PD can have attributes
2146/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002147void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002148 // Handle #pragma weak
2149 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2150 if (ND->hasLinkage()) {
2151 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2152 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002153 // Identifier referenced by #pragma weak before it was declared
2154 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002155 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2156 }
2157 }
2158 }
2159
Chris Lattner0744e5f2008-06-29 00:23:49 +00002160 // Apply decl attributes from the DeclSpec if present.
2161 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002162 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002163
Chris Lattner0744e5f2008-06-29 00:23:49 +00002164 // Walk the declarator structure, applying decl attributes that were in a type
2165 // position to the decl itself. This handles cases like:
2166 // int *__attr__(x)** D;
2167 // when X is a decl attribute.
2168 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2169 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002170 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002171
Chris Lattner0744e5f2008-06-29 00:23:49 +00002172 // Finally, apply any attributes on the decl itself.
2173 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002174 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002175}
John McCall54abf7d2009-11-04 02:18:39 +00002176
2177/// PushParsingDeclaration - Enter a new "scope" of deprecation
2178/// warnings.
2179///
2180/// The state token we use is the start index of this scope
2181/// on the warning stack.
2182Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2183 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002184 return (ParsingDeclStackState) DelayedDiagnostics.size();
2185}
2186
2187void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2188 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2189 ParsingDeclDepth--;
2190
2191 if (DelayedDiagnostics.empty())
2192 return;
2193
2194 unsigned SavedIndex = (unsigned) S;
2195 assert(SavedIndex <= DelayedDiagnostics.size() &&
2196 "saved index is out of bounds");
2197
John McCall58e6f342010-03-16 05:22:47 +00002198 unsigned E = DelayedDiagnostics.size();
2199
John McCall2f514482010-01-27 03:50:35 +00002200 // We only want to actually emit delayed diagnostics when we
2201 // successfully parsed a decl.
2202 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2203 if (D) {
2204 // We really do want to start with 0 here. We get one push for a
2205 // decl spec and another for each declarator; in a decl group like:
2206 // deprecated_typedef foo, *bar, baz();
2207 // only the declarator pops will be passed decls. This is correct;
2208 // we really do need to consider delayed diagnostics from the decl spec
2209 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002210 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002211 if (DelayedDiagnostics[I].Triggered)
2212 continue;
2213
2214 switch (DelayedDiagnostics[I].Kind) {
2215 case DelayedDiagnostic::Deprecation:
2216 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2217 break;
2218
2219 case DelayedDiagnostic::Access:
2220 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2221 break;
2222 }
2223 }
2224 }
2225
John McCall58e6f342010-03-16 05:22:47 +00002226 // Destroy all the delayed diagnostics we're about to pop off.
2227 for (unsigned I = SavedIndex; I != E; ++I)
2228 DelayedDiagnostics[I].destroy();
2229
John McCall2f514482010-01-27 03:50:35 +00002230 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002231}
2232
2233static bool isDeclDeprecated(Decl *D) {
2234 do {
2235 if (D->hasAttr<DeprecatedAttr>())
2236 return true;
2237 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2238 return false;
2239}
2240
John McCall2f514482010-01-27 03:50:35 +00002241void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2242 Decl *Ctx) {
2243 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002244 return;
2245
John McCall2f514482010-01-27 03:50:35 +00002246 DD.Triggered = true;
2247 Diag(DD.Loc, diag::warn_deprecated)
2248 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002249}
2250
2251void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2252 // Delay if we're currently parsing a declaration.
2253 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002254 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002255 return;
2256 }
2257
2258 // Otherwise, don't warn if our current context is deprecated.
2259 if (isDeclDeprecated(cast<Decl>(CurContext)))
2260 return;
2261
2262 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2263}