blob: cba5f4755dd070f0906bc1305118b2d57d746e2c [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattnere5c5ee12008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremeneka18d7d82009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000039
Chris Lattner6b6b5372008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000044
John McCall183700f2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000046}
47
Daniel Dunbar35682492008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopesd20254f2009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000062}
63
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076}
77
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000084 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000099}
100
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000106
Chris Lattner89951a82009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000108}
109
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000121 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000122 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner6b6b5372008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000130 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000131
John McCall506b57e2010-05-17 21:00:27 +0000132 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
133 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000134 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000135
John McCall506b57e2010-05-17 21:00:27 +0000136 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000137
Chris Lattner6b6b5372008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000153 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpbf916502009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000172 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000173 }
Mike Stumpbf916502009-07-24 19:02:52 +0000174
Chris Lattner6b6b5372008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000192 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000200
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000203 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000204}
205
Chris Lattner803d0802008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpbf916502009-07-24 19:02:52 +0000212
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlssona860e752009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 else
Anders Carlssona860e752009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226}
227
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpbf916502009-07-24 19:02:52 +0000234
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
238 d->addAttr(::new (S.Context) IBActionAttr());
239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000255 d->addAttr(::new (S.Context) IBOutletAttr());
256 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000257 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000260}
261
Ted Kremenek857e9182010-05-19 17:38:06 +0000262static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
263 Sema &S) {
264
265 // The iboutletcollection attribute can have zero or one arguments.
266 if (Attr.getNumArgs() > 1) {
267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
268 return;
269 }
270
271 // The IBOutletCollection attributes only apply to instance variables of
272 // Objective-C classes.
273 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
274 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
275 return;
276 }
277
278 // FIXME: Eventually accept the type argument.
279 d->addAttr(::new (S.Context) IBOutletCollectionAttr());
280}
281
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000282static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000283 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
284 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000285 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000286 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000287 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000288 return;
289 }
Mike Stumpbf916502009-07-24 19:02:52 +0000290
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000291 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000292
293 // The nonnull attribute only applies to pointers.
294 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000295
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000296 for (AttributeList::arg_iterator I=Attr.arg_begin(),
297 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000298
299
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000300 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000301 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000302 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000303 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
304 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000305 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
306 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000307 return;
308 }
Mike Stumpbf916502009-07-24 19:02:52 +0000309
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000310 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000311
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000312 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000314 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000315 return;
316 }
Mike Stumpbf916502009-07-24 19:02:52 +0000317
Ted Kremenek465172f2008-07-21 22:09:15 +0000318 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000319
320 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000321 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000322 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000323 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000324 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
325 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000326 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000327 }
Mike Stumpbf916502009-07-24 19:02:52 +0000328
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329 NonNullArgs.push_back(x);
330 }
Mike Stumpbf916502009-07-24 19:02:52 +0000331
332 // If no arguments were specified to __attribute__((nonnull)) then all pointer
333 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000334 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000335 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
336 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000337 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000338 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000339 }
Mike Stumpbf916502009-07-24 19:02:52 +0000340
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000341 if (NonNullArgs.empty()) {
342 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
343 return;
344 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000345 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000346
347 unsigned* start = &NonNullArgs[0];
348 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000349 llvm::array_pod_sort(start, start + size);
Ted Kremenek59616112010-02-11 07:31:47 +0000350 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351}
352
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000353static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
354 // This attribute must be applied to a function declaration.
355 // The first argument to the attribute must be a string,
356 // the name of the resource, for example "malloc".
357 // The following arguments must be argument indexes, the arguments must be
358 // of integer type for Returns, otherwise of pointer type.
359 // The difference between Holds and Takes is that a pointer may still be used
360 // after being held. free() should be __attribute((ownership_takes)), whereas a list
361 // append function may well be __attribute((ownership_holds)).
362
363 if (!AL.getParameterName()) {
364 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
365 << AL.getName()->getName() << 1;
366 return;
367 }
368 // Figure out our Kind, and check arguments while we're at it.
369 OwnershipAttr::OwnershipKind K = OwnershipAttr::None;
370 if (AL.getName()->getName().equals("ownership_takes")) {
371 K = OwnershipAttr::Takes;
372 if (AL.getNumArgs() < 1) {
373 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
374 return;
375 }
376 } else if (AL.getName()->getName().equals("ownership_holds")) {
377 K = OwnershipAttr::Holds;
378 if (AL.getNumArgs() < 1) {
379 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
380 return;
381 }
382 } else if (AL.getName()->getName().equals("ownership_returns")) {
383 K = OwnershipAttr::Returns;
384 if (AL.getNumArgs() > 1) {
385 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
386 << AL.getNumArgs() + 1;
387 return;
388 }
389 }
390 // This should never happen given how we are called.
391 if (K == OwnershipAttr::None) {
392 return;
393 }
394
395 if (!isFunction(d) || !hasFunctionProto(d)) {
396 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
397 << 0 /*function*/;
398 return;
399 }
400
401 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
402
403 llvm::StringRef Module = AL.getParameterName()->getName();
404
405 // Normalize the argument, __foo__ becomes foo.
406 if (Module.startswith("__") && Module.endswith("__"))
407 Module = Module.substr(2, Module.size() - 4);
408
409 llvm::SmallVector<unsigned, 10> OwnershipArgs;
410
411 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; ++I) {
412
413 Expr *IdxExpr = static_cast<Expr *>(*I);
414 llvm::APSInt ArgNum(32);
415 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
416 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
417 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
418 << AL.getName()->getName() << IdxExpr->getSourceRange();
419 continue;
420 }
421
422 unsigned x = (unsigned) ArgNum.getZExtValue();
423
424 if (x > NumArgs || x < 1) {
425 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
426 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
427 continue;
428 }
429 --x;
430 switch (K) {
431 case OwnershipAttr::Takes:
432 case OwnershipAttr::Holds: {
433 // Is the function argument a pointer type?
434 QualType T = getFunctionOrMethodArgType(d, x);
435 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
436 // FIXME: Should also highlight argument in decl.
437 S.Diag(AL.getLoc(), diag::err_ownership_type)
438 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
439 << "pointer"
440 << IdxExpr->getSourceRange();
441 continue;
442 }
443 break;
444 }
445 case OwnershipAttr::Returns: {
446 if (AL.getNumArgs() > 1) {
447 // Is the function argument an integer type?
448 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
449 llvm::APSInt ArgNum(32);
450 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
451 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
452 S.Diag(AL.getLoc(), diag::err_ownership_type)
453 << "ownership_returns" << "integer"
454 << IdxExpr->getSourceRange();
455 return;
456 }
457 }
458 break;
459 }
460 // Should never happen, here to silence a warning.
461 default: {
462 return;
463 }
464 } // switch
465
466 // Check we don't have a conflict with another ownership attribute.
467 if (d->hasAttrs()) {
468 for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) {
469 if (const OwnershipAttr* Att = dyn_cast<OwnershipAttr>(attr)) {
470 // Two ownership attributes of the same kind can't conflict,
471 // except returns attributes.
472 if (K == OwnershipAttr::Returns || Att->getKind() != K) {
473 for (const unsigned *I = Att->begin(), *E = Att->end(); I != E; ++I) {
474 if (x == *I) {
475 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
476 << AL.getName()->getName() << "ownership_*";
477 }
478 }
479 }
480 }
481 }
482 }
483 OwnershipArgs.push_back(x);
484 }
485
486 unsigned* start = OwnershipArgs.data();
487 unsigned size = OwnershipArgs.size();
488 llvm::array_pod_sort(start, start + size);
489 switch (K) {
490 case OwnershipAttr::Takes: {
491 if (OwnershipArgs.empty()) {
492 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
493 return;
494 }
495 d->addAttr(::new (S.Context) OwnershipTakesAttr(S.Context, start, size,
496 Module));
497 break;
498 }
499 case OwnershipAttr::Holds: {
500 if (OwnershipArgs.empty()) {
501 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
502 return;
503 }
504 d->addAttr(::new (S.Context) OwnershipHoldsAttr(S.Context, start, size,
505 Module));
506 break;
507 }
508 case OwnershipAttr::Returns: {
509 d->addAttr(::new (S.Context) OwnershipReturnsAttr(S.Context, start, size,
510 Module));
511 break;
512 }
513 default:
514 break;
515 }
516}
517
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000518static bool isStaticVarOrStaticFunciton(Decl *D) {
519 if (VarDecl *VD = dyn_cast<VarDecl>(D))
520 return VD->getStorageClass() == VarDecl::Static;
521 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
522 return FD->getStorageClass() == FunctionDecl::Static;
523 return false;
524}
525
526static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
527 // Check the attribute arguments.
528 if (Attr.getNumArgs() > 1) {
529 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
530 return;
531 }
532
533 // gcc rejects
534 // class c {
535 // static int a __attribute__((weakref ("v2")));
536 // static int b() __attribute__((weakref ("f3")));
537 // };
538 // and ignores the attributes of
539 // void f(void) {
540 // static int a __attribute__((weakref ("v2")));
541 // }
542 // we reject them
543 if (const DeclContext *Ctx = d->getDeclContext()) {
544 Ctx = Ctx->getLookupContext();
545 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
546 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000547 dyn_cast<NamedDecl>(d)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000548 return;
549 }
550 }
551
552 // The GCC manual says
553 //
554 // At present, a declaration to which `weakref' is attached can only
555 // be `static'.
556 //
557 // It also says
558 //
559 // Without a TARGET,
560 // given as an argument to `weakref' or to `alias', `weakref' is
561 // equivalent to `weak'.
562 //
563 // gcc 4.4.1 will accept
564 // int a7 __attribute__((weakref));
565 // as
566 // int a7 __attribute__((weak));
567 // This looks like a bug in gcc. We reject that for now. We should revisit
568 // it if this behaviour is actually used.
569
570 if (!isStaticVarOrStaticFunciton(d)) {
571 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
572 dyn_cast<NamedDecl>(d)->getNameAsString();
573 return;
574 }
575
576 // GCC rejects
577 // static ((alias ("y"), weakref)).
578 // Should we? How to check that weakref is before or after alias?
579
580 if (Attr.getNumArgs() == 1) {
581 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
582 Arg = Arg->IgnoreParenCasts();
583 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
584
585 if (Str == 0 || Str->isWide()) {
586 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
587 << "weakref" << 1;
588 return;
589 }
590 // GCC will accept anything as the argument of weakref. Should we
591 // check for an existing decl?
592 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
593 }
594
595 d->addAttr(::new (S.Context) WeakRefAttr());
596}
597
Chris Lattner803d0802008-06-29 00:43:07 +0000598static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000599 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000600 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000602 return;
603 }
Mike Stumpbf916502009-07-24 19:02:52 +0000604
Chris Lattner545dd342008-06-28 23:36:30 +0000605 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000606 Arg = Arg->IgnoreParenCasts();
607 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000608
Chris Lattner6b6b5372008-06-26 18:38:35 +0000609 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000610 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000611 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000612 return;
613 }
Mike Stumpbf916502009-07-24 19:02:52 +0000614
Chris Lattner6b6b5372008-06-26 18:38:35 +0000615 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000616
Ted Kremenek3d2c43e2010-02-11 05:28:37 +0000617 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000618}
619
Mike Stumpbf916502009-07-24 19:02:52 +0000620static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000621 Sema &S) {
622 // check the attribute arguments.
623 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000624 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000625 return;
626 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000627
Chris Lattnerc5197432009-04-14 17:02:11 +0000628 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000630 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000631 return;
632 }
Mike Stumpbf916502009-07-24 19:02:52 +0000633
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000634 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000635}
636
Ryan Flynn76168e22009-08-09 20:07:29 +0000637static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
638 // check the attribute arguments.
639 if (Attr.getNumArgs() != 0) {
640 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
641 return;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000644 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000645 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000646 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
647 d->addAttr(::new (S.Context) MallocAttr());
648 return;
649 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000650 }
651
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000652 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000653}
654
Ted Kremenekb7252322009-04-10 00:01:14 +0000655static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnarae215f722010-04-30 13:10:51 +0000656 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000657 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000658 if (Attr.getNumArgs() != 0) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000659 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000660 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000661 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000662
Mike Stump19c30c02009-04-29 19:03:13 +0000663 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
664 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stump3ee77642009-12-15 03:11:10 +0000665 if (VD == 0 || (!VD->getType()->isBlockPointerType()
666 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000667 S.Diag(Attr.getLoc(),
668 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
669 : diag::warn_attribute_wrong_decl_type)
670 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000671 return false;
672 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000673 }
Mike Stumpbf916502009-07-24 19:02:52 +0000674
Ted Kremenekb7252322009-04-10 00:01:14 +0000675 return true;
676}
677
678static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000679 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
680 assert(Attr.isInvalid() == false);
681 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenekb7252322009-04-10 00:01:14 +0000682}
683
684static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
685 Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000686 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000687 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000688}
689
John Thompson35cc9622010-08-09 21:53:52 +0000690// PS3 PPU-specific.
691static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
692 Sema &S) {
693/*
694 Returning a Vector Class in Registers
695
696 According to the PPU ABI specifications, a class with a single member of vector type is returned in
697 memory when used as the return value of a function. This results in inefficient code when implementing
698 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
699 definition. This attribute is also applicable to struct types.
700
701 Example:
702
703 struct Vector
704 {
705 __vector float xyzw;
706 } __attribute__((vecreturn));
707
708 Vector Add(Vector lhs, Vector rhs)
709 {
710 Vector result;
711 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
712 return result; // This will be returned in a register
713 }
714*/
715 if (!isa<CXXRecordDecl>(d)) {
716 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
717 << Attr.getName() << 9 /*class*/;
718 return;
719 }
720
721 if (d->getAttr<VecReturnAttr>()) {
722 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
723 return;
724 }
725
726 d->addAttr(::new (S.Context) VecReturnAttr());
727}
728
Sean Huntbbd37c62009-11-21 08:43:09 +0000729static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
730 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
731 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000732 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000733 return;
734 }
735 // FIXME: Actually store the attribute on the declaration
736}
737
Ted Kremenek73798892008-07-25 04:39:19 +0000738static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
739 // check the attribute arguments.
740 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000741 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000742 return;
743 }
Mike Stumpbf916502009-07-24 19:02:52 +0000744
John McCallaec58602010-03-31 02:47:45 +0000745 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
746 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000747 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000748 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000749 return;
750 }
Mike Stumpbf916502009-07-24 19:02:52 +0000751
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000752 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000753}
754
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000755static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
756 // check the attribute arguments.
757 if (Attr.getNumArgs() != 0) {
758 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
759 return;
760 }
Mike Stumpbf916502009-07-24 19:02:52 +0000761
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000762 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000763 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000764 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
765 return;
766 }
767 } else if (!isFunctionOrMethod(d)) {
768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000769 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000770 return;
771 }
Mike Stumpbf916502009-07-24 19:02:52 +0000772
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000773 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000774}
775
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000776static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
777 // check the attribute arguments.
778 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000779 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
780 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000781 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000782 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000783
784 int priority = 65535; // FIXME: Do not hardcode such constants.
785 if (Attr.getNumArgs() > 0) {
786 Expr *E = static_cast<Expr *>(Attr.getArg(0));
787 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000788 if (E->isTypeDependent() || E->isValueDependent() ||
789 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000790 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000791 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000792 return;
793 }
794 priority = Idx.getZExtValue();
795 }
Mike Stumpbf916502009-07-24 19:02:52 +0000796
Chris Lattnerc5197432009-04-14 17:02:11 +0000797 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000798 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000799 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000800 return;
801 }
802
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000803 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000804}
805
806static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
807 // check the attribute arguments.
808 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
810 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000811 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000812 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000813
814 int priority = 65535; // FIXME: Do not hardcode such constants.
815 if (Attr.getNumArgs() > 0) {
816 Expr *E = static_cast<Expr *>(Attr.getArg(0));
817 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000818 if (E->isTypeDependent() || E->isValueDependent() ||
819 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000820 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000821 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000822 return;
823 }
824 priority = Idx.getZExtValue();
825 }
Mike Stumpbf916502009-07-24 19:02:52 +0000826
Anders Carlsson6782fc62008-08-22 22:10:48 +0000827 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000829 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000830 return;
831 }
832
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000833 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000834}
835
Chris Lattner803d0802008-06-29 00:43:07 +0000836static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000837 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000838 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000839 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000840 return;
841 }
Mike Stumpbf916502009-07-24 19:02:52 +0000842
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000843 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000844}
845
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000846static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
847 // check the attribute arguments.
848 if (Attr.getNumArgs() != 0) {
849 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
850 return;
851 }
Mike Stumpbf916502009-07-24 19:02:52 +0000852
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000853 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000854}
855
Chris Lattner803d0802008-06-29 00:43:07 +0000856static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000857 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000858 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000860 return;
861 }
Mike Stumpbf916502009-07-24 19:02:52 +0000862
Chris Lattner545dd342008-06-28 23:36:30 +0000863 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000864 Arg = Arg->IgnoreParenCasts();
865 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000866
Chris Lattner6b6b5372008-06-26 18:38:35 +0000867 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000869 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000870 return;
871 }
Mike Stumpbf916502009-07-24 19:02:52 +0000872
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000873 llvm::StringRef TypeStr = Str->getString();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000874 VisibilityAttr::VisibilityTypes type;
Mike Stumpbf916502009-07-24 19:02:52 +0000875
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000876 if (TypeStr == "default")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000877 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000878 else if (TypeStr == "hidden")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000879 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000880 else if (TypeStr == "internal")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000881 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000882 else if (TypeStr == "protected")
Chris Lattner6b6b5372008-06-26 18:38:35 +0000883 type = VisibilityAttr::ProtectedVisibility;
884 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000885 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000886 return;
887 }
Mike Stumpbf916502009-07-24 19:02:52 +0000888
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000889 d->addAttr(::new (S.Context) VisibilityAttr(type, false));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000890}
891
Chris Lattner0db29ec2009-02-14 08:09:34 +0000892static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
893 Sema &S) {
894 if (Attr.getNumArgs() != 0) {
895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
896 return;
897 }
Mike Stumpbf916502009-07-24 19:02:52 +0000898
Chris Lattner0db29ec2009-02-14 08:09:34 +0000899 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
900 if (OCI == 0) {
901 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
902 return;
903 }
Mike Stumpbf916502009-07-24 19:02:52 +0000904
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000905 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000906}
907
908static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000909 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000910 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000911 return;
912 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000913 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000914 QualType T = TD->getUnderlyingType();
915 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000916 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000917 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
918 return;
919 }
920 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000921 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000922}
923
Mike Stumpbf916502009-07-24 19:02:52 +0000924static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000925HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
926 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000927 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000928 return;
929 }
930
931 if (!isa<FunctionDecl>(D)) {
932 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
933 return;
934 }
935
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000936 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000937}
938
Steve Naroff9eae5762008-09-18 16:44:58 +0000939static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000940 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000941 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000942 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000943 return;
944 }
Mike Stumpbf916502009-07-24 19:02:52 +0000945
Steve Naroff9eae5762008-09-18 16:44:58 +0000946 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000947 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000948 return;
949 }
Mike Stumpbf916502009-07-24 19:02:52 +0000950
Steve Naroff9eae5762008-09-18 16:44:58 +0000951 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000952 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000953 type = BlocksAttr::ByRef;
954 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000955 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000956 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000957 return;
958 }
Mike Stumpbf916502009-07-24 19:02:52 +0000959
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000960 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000961}
962
Anders Carlsson77091822008-10-05 18:05:59 +0000963static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
964 // check the attribute arguments.
965 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000966 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
967 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000968 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000969 }
970
Anders Carlsson77091822008-10-05 18:05:59 +0000971 int sentinel = 0;
972 if (Attr.getNumArgs() > 0) {
973 Expr *E = static_cast<Expr *>(Attr.getArg(0));
974 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000975 if (E->isTypeDependent() || E->isValueDependent() ||
976 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000977 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000978 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000979 return;
980 }
981 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000982
Anders Carlsson77091822008-10-05 18:05:59 +0000983 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000984 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
985 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000986 return;
987 }
988 }
989
990 int nullPos = 0;
991 if (Attr.getNumArgs() > 1) {
992 Expr *E = static_cast<Expr *>(Attr.getArg(1));
993 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000994 if (E->isTypeDependent() || E->isValueDependent() ||
995 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000996 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000997 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000998 return;
999 }
1000 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001001
Anders Carlsson77091822008-10-05 18:05:59 +00001002 if (nullPos > 1 || nullPos < 0) {
1003 // FIXME: This error message could be improved, it would be nice
1004 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001005 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1006 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001007 return;
1008 }
1009 }
1010
1011 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001012 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001013 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001014
Chris Lattner897cd902009-03-17 23:03:47 +00001015 if (isa<FunctionNoProtoType>(FT)) {
1016 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1017 return;
1018 }
Mike Stumpbf916502009-07-24 19:02:52 +00001019
Chris Lattner897cd902009-03-17 23:03:47 +00001020 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001021 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001022 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001023 }
Anders Carlsson77091822008-10-05 18:05:59 +00001024 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1025 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001026 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001027 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001028 }
1029 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001030 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1031 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001032 ;
1033 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001034 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001035 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001036 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001037 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001038 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001039 int m = Ty->isFunctionPointerType() ? 0 : 1;
1040 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001041 return;
1042 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001043 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001044 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001045 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001046 return;
1047 }
Anders Carlsson77091822008-10-05 18:05:59 +00001048 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001049 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001050 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001051 return;
1052 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001053 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001054}
1055
Chris Lattner026dc962009-02-14 07:37:35 +00001056static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1057 // check the attribute arguments.
1058 if (Attr.getNumArgs() != 0) {
1059 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1060 return;
1061 }
1062
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001063 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001064 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001065 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001066 return;
1067 }
Mike Stumpbf916502009-07-24 19:02:52 +00001068
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001069 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1070 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1071 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001072 return;
1073 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001074 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1075 if (MD->getResultType()->isVoidType()) {
1076 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1077 << Attr.getName() << 1;
1078 return;
1079 }
1080
Nuno Lopesd20254f2009-12-20 23:11:08 +00001081 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +00001082}
1083
1084static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001085 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001086 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001087 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001088 return;
1089 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001090
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001091 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001092 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1094 dyn_cast<NamedDecl>(D)->getNameAsString();
1095 return;
1096 }
1097
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001098 // TODO: could also be applied to methods?
1099 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1100 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001101 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001102 return;
1103 }
Mike Stumpbf916502009-07-24 19:02:52 +00001104
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001105 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001106}
1107
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001108static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1109 // check the attribute arguments.
1110 if (Attr.getNumArgs() != 0) {
1111 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1112 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001113 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001114
1115 // weak_import only applies to variable & function declarations.
1116 bool isDef = false;
1117 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1118 isDef = (!VD->hasExternalStorage() || VD->getInit());
1119 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001120 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001121 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1122 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001123 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001124 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001125 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001126 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001127 !isa<ObjCInterfaceDecl>(D))
1128 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001129 << Attr.getName() << 2 /*variable and function*/;
1130 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001131 }
1132
1133 // Merge should handle any subsequent violations.
1134 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001135 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001136 diag::warn_attribute_weak_import_invalid_on_definition)
1137 << "weak_import" << 2 /*variable and function*/;
1138 return;
1139 }
1140
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001141 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001142}
1143
Nate Begeman6f3d8382009-06-26 06:32:41 +00001144static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1145 Sema &S) {
1146 // Attribute has 3 arguments.
1147 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001148 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001149 return;
1150 }
1151
1152 unsigned WGSize[3];
1153 for (unsigned i = 0; i < 3; ++i) {
1154 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1155 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001156 if (E->isTypeDependent() || E->isValueDependent() ||
1157 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001158 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1159 << "reqd_work_group_size" << E->getSourceRange();
1160 return;
1161 }
1162 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1163 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001164 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001165 WGSize[2]));
1166}
1167
Chris Lattner026dc962009-02-14 07:37:35 +00001168static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001169 // Attribute has no arguments.
1170 if (Attr.getNumArgs() != 1) {
1171 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1172 return;
1173 }
1174
1175 // Make sure that there is a string literal as the sections's single
1176 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001177 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1178 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001179 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001180 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001181 return;
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Chris Lattner797c3c42009-08-10 19:03:04 +00001184 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001185 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001186 if (!Error.empty()) {
1187 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1188 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001189 return;
1190 }
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001192 // This attribute cannot be applied to local variables.
1193 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1194 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1195 return;
1196 }
1197
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001198 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001199}
1200
Chris Lattner6b6b5372008-06-26 18:38:35 +00001201
Chris Lattner803d0802008-06-29 00:43:07 +00001202static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001203 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001204 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001205 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001206 return;
1207 }
Mike Stumpbf916502009-07-24 19:02:52 +00001208
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001209 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001210}
1211
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001212static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1213 // check the attribute arguments.
1214 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001215 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001216 return;
1217 }
Mike Stumpbf916502009-07-24 19:02:52 +00001218
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001219 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001220}
1221
1222static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1223 // check the attribute arguments.
1224 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001225 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001226 return;
1227 }
Mike Stumpbf916502009-07-24 19:02:52 +00001228
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001229 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001230}
1231
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001232static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001233 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001234 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1235 return;
1236 }
Mike Stumpbf916502009-07-24 19:02:52 +00001237
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001238 if (Attr.getNumArgs() != 0) {
1239 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1240 return;
1241 }
Mike Stumpbf916502009-07-24 19:02:52 +00001242
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001243 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001244
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001245 if (!VD || !VD->hasLocalStorage()) {
1246 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1247 return;
1248 }
Mike Stumpbf916502009-07-24 19:02:52 +00001249
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001250 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001251 // FIXME: Lookup probably isn't looking in the right place
1252 // FIXME: The lookup source location should be in the attribute, not the
1253 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001254 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001255 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001256 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001257 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001259 Attr.getParameterName();
1260 return;
1261 }
Mike Stumpbf916502009-07-24 19:02:52 +00001262
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001263 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1264 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001265 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001266 Attr.getParameterName();
1267 return;
1268 }
1269
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001270 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001271 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001272 Attr.getParameterName();
1273 return;
1274 }
Mike Stumpbf916502009-07-24 19:02:52 +00001275
Anders Carlsson89941c12009-02-07 23:16:50 +00001276 // We're currently more strict than GCC about what function types we accept.
1277 // If this ever proves to be a problem it should be easy to fix.
1278 QualType Ty = S.Context.getPointerType(VD->getType());
1279 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001280 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001281 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001282 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1283 Attr.getParameterName() << ParamTy << Ty;
1284 return;
1285 }
Mike Stumpbf916502009-07-24 19:02:52 +00001286
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001287 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001288}
1289
Mike Stumpbf916502009-07-24 19:02:52 +00001290/// Handle __attribute__((format_arg((idx)))) attribute based on
1291/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1292static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001293 if (Attr.getNumArgs() != 1) {
1294 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1295 return;
1296 }
1297 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1298 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1299 << Attr.getName() << 0 /*function*/;
1300 return;
1301 }
Mike Stumpbf916502009-07-24 19:02:52 +00001302 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1303 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001304 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1305 unsigned FirstIdx = 1;
1306 // checks for the 2nd argument
1307 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1308 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001309 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1310 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1312 << "format" << 2 << IdxExpr->getSourceRange();
1313 return;
1314 }
Mike Stumpbf916502009-07-24 19:02:52 +00001315
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001316 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1317 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1318 << "format" << 2 << IdxExpr->getSourceRange();
1319 return;
1320 }
Mike Stumpbf916502009-07-24 19:02:52 +00001321
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001322 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001323
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001324 // make sure the format string is really a string
1325 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001326
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001327 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1328 if (not_nsstring_type &&
1329 !isCFStringType(Ty, S.Context) &&
1330 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001331 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001332 // FIXME: Should highlight the actual expression that has the wrong type.
1333 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001334 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001335 << IdxExpr->getSourceRange();
1336 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001337 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001338 Ty = getFunctionOrMethodResultType(d);
1339 if (!isNSStringType(Ty, S.Context) &&
1340 !isCFStringType(Ty, S.Context) &&
1341 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001342 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001343 // FIXME: Should highlight the actual expression that has the wrong type.
1344 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001345 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001346 << IdxExpr->getSourceRange();
1347 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001348 }
1349
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001350 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001351}
1352
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001353enum FormatAttrKind {
1354 CFStringFormat,
1355 NSStringFormat,
1356 StrftimeFormat,
1357 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001358 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001359 InvalidFormat
1360};
1361
1362/// getFormatAttrKind - Map from format attribute names to supported format
1363/// types.
1364static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1365 // Check for formats that get handled specially.
1366 if (Format == "NSString")
1367 return NSStringFormat;
1368 if (Format == "CFString")
1369 return CFStringFormat;
1370 if (Format == "strftime")
1371 return StrftimeFormat;
1372
1373 // Otherwise, check for supported formats.
1374 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1375 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1376 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1377 Format == "zcmn_err")
1378 return SupportedFormat;
1379
Duncan Sandsbc525952010-03-23 14:44:19 +00001380 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1381 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001382 return IgnoredFormat;
1383
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001384 return InvalidFormat;
1385}
1386
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001387/// Handle __attribute__((init_priority(priority))) attributes based on
1388/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1389static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1390 Sema &S) {
1391 if (!S.getLangOptions().CPlusPlus) {
1392 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1393 return;
1394 }
1395
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001396 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1397 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1398 Attr.setInvalid();
1399 return;
1400 }
1401 QualType T = dyn_cast<VarDecl>(d)->getType();
1402 if (S.Context.getAsArrayType(T))
1403 T = S.Context.getBaseElementType(T);
1404 if (!T->getAs<RecordType>()) {
1405 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1406 Attr.setInvalid();
1407 return;
1408 }
1409
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001410 if (Attr.getNumArgs() != 1) {
1411 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1412 Attr.setInvalid();
1413 return;
1414 }
1415 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001416
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001417 llvm::APSInt priority(32);
1418 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1419 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1420 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1421 << "init_priority" << priorityExpr->getSourceRange();
1422 Attr.setInvalid();
1423 return;
1424 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001425 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001426 if (prioritynum < 101 || prioritynum > 65535) {
1427 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1428 << priorityExpr->getSourceRange();
1429 Attr.setInvalid();
1430 return;
1431 }
1432 d->addAttr(::new (S.Context) InitPriorityAttr(prioritynum));
1433}
1434
Mike Stumpbf916502009-07-24 19:02:52 +00001435/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1436/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001437static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001438
Chris Lattner545dd342008-06-28 23:36:30 +00001439 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001440 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001441 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001442 return;
1443 }
1444
Chris Lattner545dd342008-06-28 23:36:30 +00001445 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001446 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001447 return;
1448 }
1449
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001450 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001451 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001452 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001453 return;
1454 }
1455
Daniel Dunbar35682492008-09-26 04:12:28 +00001456 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001457 unsigned FirstIdx = 1;
1458
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001459 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001460
1461 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001462 if (Format.startswith("__") && Format.endswith("__"))
1463 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001464
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001465 // Check for supported formats.
1466 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001467
1468 if (Kind == IgnoredFormat)
1469 return;
1470
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001471 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001472 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001473 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001474 return;
1475 }
1476
1477 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001478 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001479 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001480 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1481 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001482 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001483 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001484 return;
1485 }
1486
Anders Carlsson4fb77202009-08-25 14:12:34 +00001487 // FIXME: We should handle the implicit 'this' parameter in a more generic
1488 // way that can be used for other arguments.
1489 bool HasImplicitThisParam = false;
1490 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1491 if (MD->isInstance()) {
1492 HasImplicitThisParam = true;
1493 NumArgs++;
1494 }
1495 }
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattner6b6b5372008-06-26 18:38:35 +00001497 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001498 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001499 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001500 return;
1501 }
1502
1503 // FIXME: Do we need to bounds check?
1504 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001505
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001506 if (HasImplicitThisParam) {
1507 if (ArgIdx == 0) {
1508 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1509 << "a string type" << IdxExpr->getSourceRange();
1510 return;
1511 }
1512 ArgIdx--;
1513 }
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Chris Lattner6b6b5372008-06-26 18:38:35 +00001515 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001516 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001517
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001518 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001519 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001520 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1521 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001522 return;
1523 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001524 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001525 // FIXME: do we need to check if the type is NSString*? What are the
1526 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001527 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001528 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001529 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1530 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001531 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001532 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001533 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001534 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001535 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001536 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1537 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001538 return;
1539 }
1540
1541 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001542 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001543 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001544 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1545 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001546 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001547 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001548 return;
1549 }
1550
1551 // check if the function is variadic if the 3rd argument non-zero
1552 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001553 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001554 ++NumArgs; // +1 for ...
1555 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001556 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001557 return;
1558 }
1559 }
1560
Chris Lattner3c73c412008-11-19 08:23:25 +00001561 // strftime requires FirstArg to be 0 because it doesn't read from any
1562 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001563 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001564 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001565 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1566 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001567 return;
1568 }
1569 // if 0 it disables parameter checking (to use with e.g. va_list)
1570 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001571 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001572 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001573 return;
1574 }
1575
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001576 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001577 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001578}
1579
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001580static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1581 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001582 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001583 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001584 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001585 return;
1586 }
1587
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001588 // Try to find the underlying union declaration.
1589 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001590 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001591 if (TD && TD->getUnderlyingType()->isUnionType())
1592 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1593 else
1594 RD = dyn_cast<RecordDecl>(d);
1595
1596 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001597 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001598 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001599 return;
1600 }
1601
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001602 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001603 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001604 diag::warn_transparent_union_attribute_not_definition);
1605 return;
1606 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001607
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001608 RecordDecl::field_iterator Field = RD->field_begin(),
1609 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001610 if (Field == FieldEnd) {
1611 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1612 return;
1613 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001614
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001615 FieldDecl *FirstField = *Field;
1616 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001617 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001618 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001619 diag::warn_transparent_union_attribute_floating)
1620 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001621 return;
1622 }
1623
1624 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1625 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1626 for (; Field != FieldEnd; ++Field) {
1627 QualType FieldType = Field->getType();
1628 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1629 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1630 // Warn if we drop the attribute.
1631 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001632 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001633 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001634 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001635 diag::warn_transparent_union_attribute_field_size_align)
1636 << isSize << Field->getDeclName() << FieldBits;
1637 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001638 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001639 diag::note_transparent_union_first_field_size_align)
1640 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001641 return;
1642 }
1643 }
1644
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001645 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001646}
1647
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001648static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001649 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001650 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001652 return;
1653 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001654 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1655 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001656
Chris Lattner6b6b5372008-06-26 18:38:35 +00001657 // Make sure that there is a string literal as the annotation's single
1658 // argument.
1659 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001660 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001661 return;
1662 }
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001663 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001664}
1665
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001666static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001667 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001668 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001669 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001670 return;
1671 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001672
1673 //FIXME: The C++0x version of this attribute has more limited applicabilty
1674 // than GNU's, and should error out when it is used to specify a
1675 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001676
Chris Lattner545dd342008-06-28 23:36:30 +00001677 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001678 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001679 // (For now we just use 128 bits which is the maximum on X86).
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001680 D->addAttr(::new (S.Context) AlignedAttr(128));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001681 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001682 }
Mike Stumpbf916502009-07-24 19:02:52 +00001683
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001684 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1685}
1686
1687void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1688 if (E->isTypeDependent() || E->isValueDependent()) {
1689 // Save dependent expressions in the AST to be instantiated.
1690 D->addAttr(::new (Context) AlignedAttr(E));
1691 return;
1692 }
1693
Chris Lattner49e2d342008-06-28 23:50:44 +00001694 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001695 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1696 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1697 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001698 return;
1699 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001700 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001701 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1702 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001703 return;
1704 }
1705
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001706 D->addAttr(::new (Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001707}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001708
Mike Stumpbf916502009-07-24 19:02:52 +00001709/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1710/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001711///
Mike Stumpbf916502009-07-24 19:02:52 +00001712/// Despite what would be logical, the mode attribute is a decl attribute, not a
1713/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1714/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001715static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001716 // This attribute isn't documented, but glibc uses it. It changes
1717 // the width of an int or unsigned int to the specified size.
1718
1719 // Check that there aren't any arguments
1720 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001721 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001722 return;
1723 }
1724
1725 IdentifierInfo *Name = Attr.getParameterName();
1726 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001727 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001728 return;
1729 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001730
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001731 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001732
1733 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001734 if (Str.startswith("__") && Str.endswith("__"))
1735 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001736
1737 unsigned DestWidth = 0;
1738 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001739 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001740 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001741 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001742 switch (Str[0]) {
1743 case 'Q': DestWidth = 8; break;
1744 case 'H': DestWidth = 16; break;
1745 case 'S': DestWidth = 32; break;
1746 case 'D': DestWidth = 64; break;
1747 case 'X': DestWidth = 96; break;
1748 case 'T': DestWidth = 128; break;
1749 }
1750 if (Str[1] == 'F') {
1751 IntegerMode = false;
1752 } else if (Str[1] == 'C') {
1753 IntegerMode = false;
1754 ComplexMode = true;
1755 } else if (Str[1] != 'I') {
1756 DestWidth = 0;
1757 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001758 break;
1759 case 4:
1760 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1761 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001762 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001763 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001764 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001765 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001766 break;
1767 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001768 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001769 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001770 break;
1771 }
1772
1773 QualType OldTy;
1774 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1775 OldTy = TD->getUnderlyingType();
1776 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1777 OldTy = VD->getType();
1778 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001779 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1780 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001781 return;
1782 }
Eli Friedman73397492009-03-03 06:41:03 +00001783
John McCall183700f2009-09-21 23:43:11 +00001784 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001785 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1786 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001787 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001788 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1789 } else if (ComplexMode) {
1790 if (!OldTy->isComplexType())
1791 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1792 } else {
1793 if (!OldTy->isFloatingType())
1794 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1795 }
1796
Mike Stump390b4cc2009-05-16 07:39:55 +00001797 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1798 // and friends, at least with glibc.
1799 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1800 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001801 // FIXME: Make sure floating-point mappings are accurate
1802 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001803 QualType NewTy;
1804 switch (DestWidth) {
1805 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001806 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001807 return;
1808 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001809 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001810 return;
1811 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001812 if (!IntegerMode) {
1813 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1814 return;
1815 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001816 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001817 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001818 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001819 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001820 break;
1821 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001822 if (!IntegerMode) {
1823 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1824 return;
1825 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001826 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001827 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001828 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001829 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001830 break;
1831 case 32:
1832 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001833 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001834 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001835 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001836 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001837 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001838 break;
1839 case 64:
1840 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001841 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001842 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001843 if (S.Context.Target.getLongWidth() == 64)
1844 NewTy = S.Context.LongTy;
1845 else
1846 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001847 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001848 if (S.Context.Target.getLongWidth() == 64)
1849 NewTy = S.Context.UnsignedLongTy;
1850 else
1851 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001852 break;
Eli Friedman73397492009-03-03 06:41:03 +00001853 case 96:
1854 NewTy = S.Context.LongDoubleTy;
1855 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001856 case 128:
1857 if (!IntegerMode) {
1858 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1859 return;
1860 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001861 if (OldTy->isSignedIntegerType())
1862 NewTy = S.Context.Int128Ty;
1863 else
1864 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001865 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001866 }
1867
Eli Friedman73397492009-03-03 06:41:03 +00001868 if (ComplexMode) {
1869 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001870 }
1871
1872 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001873 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1874 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001875 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001876 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001877 cast<ValueDecl>(D)->setType(NewTy);
1878}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001879
Mike Stump1feade82009-08-26 22:31:08 +00001880static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001881 // check the attribute arguments.
1882 if (Attr.getNumArgs() > 0) {
1883 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1884 return;
1885 }
Anders Carlssone896d982009-02-13 08:11:52 +00001886
Anders Carlsson5bab7882009-02-19 19:16:48 +00001887 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001888 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001889 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001890 return;
1891 }
Mike Stumpbf916502009-07-24 19:02:52 +00001892
Mike Stump1feade82009-08-26 22:31:08 +00001893 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001894}
1895
Mike Stump1feade82009-08-26 22:31:08 +00001896static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001897 // check the attribute arguments.
1898 if (Attr.getNumArgs() != 0) {
1899 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1900 return;
1901 }
Mike Stumpbf916502009-07-24 19:02:52 +00001902
Chris Lattnerc5197432009-04-14 17:02:11 +00001903 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001904 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001905 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001906 return;
1907 }
Mike Stumpbf916502009-07-24 19:02:52 +00001908
Mike Stump1feade82009-08-26 22:31:08 +00001909 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001910}
1911
Chris Lattner7255a2d2010-06-22 00:03:40 +00001912static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1913 Sema &S) {
1914 // check the attribute arguments.
1915 if (Attr.getNumArgs() != 0) {
1916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1917 return;
1918 }
1919
1920 if (!isa<FunctionDecl>(d)) {
1921 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1922 << Attr.getName() << 0 /*function*/;
1923 return;
1924 }
1925
1926 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr());
1927}
1928
Chris Lattnercf2a7212009-04-20 19:12:28 +00001929static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001930 // check the attribute arguments.
1931 if (Attr.getNumArgs() != 0) {
1932 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1933 return;
1934 }
Mike Stumpbf916502009-07-24 19:02:52 +00001935
Chris Lattnerc5197432009-04-14 17:02:11 +00001936 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1937 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001938 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001939 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001940 return;
1941 }
Mike Stumpbf916502009-07-24 19:02:52 +00001942
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001943 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001944 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001945 return;
1946 }
Mike Stumpbf916502009-07-24 19:02:52 +00001947
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001948 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001949}
1950
Abramo Bagnarae215f722010-04-30 13:10:51 +00001951static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1952 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1953 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1954 assert(Attr.isInvalid() == false);
1955
1956 switch (Attr.getKind()) {
1957 case AttributeList::AT_fastcall:
1958 d->addAttr(::new (S.Context) FastCallAttr());
1959 return;
1960 case AttributeList::AT_stdcall:
1961 d->addAttr(::new (S.Context) StdCallAttr());
1962 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001963 case AttributeList::AT_thiscall:
1964 d->addAttr(::new (S.Context) ThisCallAttr());
Abramo Bagnarae215f722010-04-30 13:10:51 +00001965 case AttributeList::AT_cdecl:
1966 d->addAttr(::new (S.Context) CDeclAttr());
1967 return;
1968 default:
1969 llvm_unreachable("unexpected attribute kind");
1970 return;
1971 }
1972}
1973
Fariborz Jahanianee760332009-03-27 18:38:55 +00001974static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1975 // check the attribute arguments.
1976 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001977 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001978 return;
1979 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001980
Fariborz Jahanianee760332009-03-27 18:38:55 +00001981 if (!isFunctionOrMethod(d)) {
1982 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001983 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001984 return;
1985 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001986
1987 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1988 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001989 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1990 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001991 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1992 << "regparm" << NumParamsExpr->getSourceRange();
1993 return;
1994 }
1995
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001996 if (S.Context.Target.getRegParmMax() == 0) {
1997 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001998 << NumParamsExpr->getSourceRange();
1999 return;
2000 }
2001
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002002 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002003 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2004 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002005 return;
2006 }
2007
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002008 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002009}
2010
Sean Huntbbd37c62009-11-21 08:43:09 +00002011static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2012 // check the attribute arguments.
2013 if (Attr.getNumArgs() != 0) {
2014 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2015 return;
2016 }
2017
2018 if (!isa<CXXRecordDecl>(d)
2019 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2020 S.Diag(Attr.getLoc(),
2021 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2022 : diag::warn_attribute_wrong_decl_type)
2023 << Attr.getName() << 7 /*virtual method or class*/;
2024 return;
2025 }
Sean Hunt7725e672009-11-25 04:20:27 +00002026
2027 // FIXME: Conform to C++0x redeclaration rules.
2028
2029 if (d->getAttr<FinalAttr>()) {
2030 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2031 return;
2032 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002033
2034 d->addAttr(::new (S.Context) FinalAttr());
2035}
2036
Chris Lattner0744e5f2008-06-29 00:23:49 +00002037//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002038// C++0x member checking attributes
2039//===----------------------------------------------------------------------===//
2040
2041static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2042 if (Attr.getNumArgs() != 0) {
2043 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2044 return;
2045 }
2046
2047 if (!isa<CXXRecordDecl>(d)) {
2048 S.Diag(Attr.getLoc(),
2049 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2050 : diag::warn_attribute_wrong_decl_type)
2051 << Attr.getName() << 9 /*class*/;
2052 return;
2053 }
2054
2055 if (d->getAttr<BaseCheckAttr>()) {
2056 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2057 return;
2058 }
2059
2060 d->addAttr(::new (S.Context) BaseCheckAttr());
2061}
2062
2063static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2064 if (Attr.getNumArgs() != 0) {
2065 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2066 return;
2067 }
2068
2069 if (!isa<RecordDecl>(d->getDeclContext())) {
2070 // FIXME: It's not the type that's the problem
2071 S.Diag(Attr.getLoc(),
2072 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2073 : diag::warn_attribute_wrong_decl_type)
2074 << Attr.getName() << 11 /*member*/;
2075 return;
2076 }
2077
2078 // FIXME: Conform to C++0x redeclaration rules.
2079
2080 if (d->getAttr<HidingAttr>()) {
2081 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2082 return;
2083 }
2084
2085 d->addAttr(::new (S.Context) HidingAttr());
2086}
2087
2088static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2089 if (Attr.getNumArgs() != 0) {
2090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2091 return;
2092 }
2093
2094 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2095 // FIXME: It's not the type that's the problem
2096 S.Diag(Attr.getLoc(),
2097 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2098 : diag::warn_attribute_wrong_decl_type)
2099 << Attr.getName() << 10 /*virtual method*/;
2100 return;
2101 }
2102
2103 // FIXME: Conform to C++0x redeclaration rules.
2104
2105 if (d->getAttr<OverrideAttr>()) {
2106 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2107 return;
2108 }
2109
2110 d->addAttr(::new (S.Context) OverrideAttr());
2111}
2112
2113//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002114// Checker-specific attribute handlers.
2115//===----------------------------------------------------------------------===//
2116
2117static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2118 Sema &S) {
2119
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002120 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002121
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002122 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2123 RetTy = MD->getResultType();
2124 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2125 RetTy = FD->getResultType();
2126 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002127 SourceLocation L = Attr.getLoc();
2128 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2129 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002130 return;
2131 }
Mike Stumpbf916502009-07-24 19:02:52 +00002132
Ted Kremenek6217b802009-07-29 21:53:49 +00002133 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002134 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002135 SourceLocation L = Attr.getLoc();
2136 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2137 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002138 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002139 }
Mike Stumpbf916502009-07-24 19:02:52 +00002140
Ted Kremenekb71368d2009-05-09 02:44:38 +00002141 switch (Attr.getKind()) {
2142 default:
2143 assert(0 && "invalid ownership attribute");
2144 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002145 case AttributeList::AT_cf_returns_not_retained:
2146 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
2147 return;
2148 case AttributeList::AT_ns_returns_not_retained:
2149 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
2150 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002151 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002152 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002153 return;
2154 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00002155 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenekb71368d2009-05-09 02:44:38 +00002156 return;
2157 };
2158}
2159
Charles Davisf0122fe2010-02-16 18:27:26 +00002160static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2161 return Attr.getKind() == AttributeList::AT_dllimport ||
2162 Attr.getKind() == AttributeList::AT_dllexport;
2163}
2164
Ted Kremenekb71368d2009-05-09 02:44:38 +00002165//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002166// Top Level Sema Entry Points
2167//===----------------------------------------------------------------------===//
2168
Sebastian Redla89d82c2008-12-21 19:24:58 +00002169/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002170/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002171/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2172/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002173static void ProcessDeclAttribute(Scope *scope, Decl *D,
2174 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002175 if (Attr.isInvalid())
2176 return;
2177
Charles Davisf0122fe2010-02-16 18:27:26 +00002178 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2179 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002180 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002181 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002182 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002183 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2184 case AttributeList::AT_IBOutletCollection:
2185 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002186 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002187 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002188 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002189 // Ignore these, these are type attributes, handled by
2190 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002191 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002192 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2193 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002194 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002195 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002196 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002197 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002198 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2199 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002200 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002201 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002202 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2203 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2204 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002205 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002206 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002207 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002208 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2209 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2210 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2211 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2212 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2213 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2214 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2215 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002216 case AttributeList::AT_ownership_returns:
2217 case AttributeList::AT_ownership_takes:
2218 case AttributeList::AT_ownership_holds:
2219 HandleOwnershipAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002220 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2221 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2222 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002223 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002224
2225 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002226 case AttributeList::AT_ns_returns_not_retained:
2227 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002228 case AttributeList::AT_ns_returns_retained:
2229 case AttributeList::AT_cf_returns_retained:
2230 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2231
Nate Begeman6f3d8382009-06-26 06:32:41 +00002232 case AttributeList::AT_reqd_wg_size:
2233 HandleReqdWorkGroupSize(D, Attr, S); break;
2234
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002235 case AttributeList::AT_init_priority:
2236 HandleInitPriorityAttr(D, Attr, S); break;
2237
Sean Hunt7725e672009-11-25 04:20:27 +00002238 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2239 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002240 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2241 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2242 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002243 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002244 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2245 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002246 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002247 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002248 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002249 case AttributeList::AT_transparent_union:
2250 HandleTransparentUnionAttr(D, Attr, S);
2251 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002252 case AttributeList::AT_objc_exception:
2253 HandleObjCExceptionAttr(D, Attr, S);
2254 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002255 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002256 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2257 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2258 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2259 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2260 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2261 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2262 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2263 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2264 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002265 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002266 // Just ignore
2267 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002268 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2269 HandleNoInstrumentFunctionAttr(D, Attr, S);
2270 break;
John McCall04a67a62010-02-05 21:31:56 +00002271 case AttributeList::AT_stdcall:
2272 case AttributeList::AT_cdecl:
2273 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002274 case AttributeList::AT_thiscall:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002275 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002276 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002277 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002278 // Ask target about the attribute.
2279 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2280 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002281 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2282 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002283 break;
2284 }
2285}
2286
2287/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2288/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002289void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002290 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2291 ProcessDeclAttribute(S, D, *l, *this);
2292 }
2293
2294 // GCC accepts
2295 // static int a9 __attribute__((weakref));
2296 // but that looks really pointless. We reject it.
2297 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2298 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002299 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002300 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002301 }
2302}
2303
Ryan Flynne25ff832009-07-30 03:15:39 +00002304/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2305/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002306NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002307 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002308 NamedDecl *NewD = 0;
2309 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2310 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2311 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002312 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002313 if (FD->getQualifier()) {
2314 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2315 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2316 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002317 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2318 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2319 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002320 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002321 VD->getStorageClass(),
2322 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002323 if (VD->getQualifier()) {
2324 VarDecl *NewVD = cast<VarDecl>(NewD);
2325 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2326 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002327 }
2328 return NewD;
2329}
2330
2331/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2332/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002333void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002334 if (W.getUsed()) return; // only do this once
2335 W.setUsed(true);
2336 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2337 IdentifierInfo *NDId = ND->getIdentifier();
2338 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00002339 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002340 NewD->addAttr(::new (Context) WeakAttr());
2341 WeakTopLevelDecl.push_back(NewD);
2342 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2343 // to insert Decl at TU scope, sorry.
2344 DeclContext *SavedContext = CurContext;
2345 CurContext = Context.getTranslationUnitDecl();
2346 PushOnScopeChains(NewD, S);
2347 CurContext = SavedContext;
2348 } else { // just add weak to existing
2349 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynne25ff832009-07-30 03:15:39 +00002350 }
2351}
2352
Chris Lattner0744e5f2008-06-29 00:23:49 +00002353/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2354/// it, apply them to D. This is a bit tricky because PD can have attributes
2355/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002356void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002357 // Handle #pragma weak
2358 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2359 if (ND->hasLinkage()) {
2360 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2361 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002362 // Identifier referenced by #pragma weak before it was declared
2363 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002364 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2365 }
2366 }
2367 }
2368
Chris Lattner0744e5f2008-06-29 00:23:49 +00002369 // Apply decl attributes from the DeclSpec if present.
2370 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002371 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002372
Chris Lattner0744e5f2008-06-29 00:23:49 +00002373 // Walk the declarator structure, applying decl attributes that were in a type
2374 // position to the decl itself. This handles cases like:
2375 // int *__attr__(x)** D;
2376 // when X is a decl attribute.
2377 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2378 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002379 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002380
Chris Lattner0744e5f2008-06-29 00:23:49 +00002381 // Finally, apply any attributes on the decl itself.
2382 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002383 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002384}
John McCall54abf7d2009-11-04 02:18:39 +00002385
2386/// PushParsingDeclaration - Enter a new "scope" of deprecation
2387/// warnings.
2388///
2389/// The state token we use is the start index of this scope
2390/// on the warning stack.
2391Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2392 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002393 return (ParsingDeclStackState) DelayedDiagnostics.size();
2394}
2395
2396void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2397 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2398 ParsingDeclDepth--;
2399
2400 if (DelayedDiagnostics.empty())
2401 return;
2402
2403 unsigned SavedIndex = (unsigned) S;
2404 assert(SavedIndex <= DelayedDiagnostics.size() &&
2405 "saved index is out of bounds");
2406
John McCall58e6f342010-03-16 05:22:47 +00002407 unsigned E = DelayedDiagnostics.size();
2408
John McCall2f514482010-01-27 03:50:35 +00002409 // We only want to actually emit delayed diagnostics when we
2410 // successfully parsed a decl.
2411 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2412 if (D) {
2413 // We really do want to start with 0 here. We get one push for a
2414 // decl spec and another for each declarator; in a decl group like:
2415 // deprecated_typedef foo, *bar, baz();
2416 // only the declarator pops will be passed decls. This is correct;
2417 // we really do need to consider delayed diagnostics from the decl spec
2418 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002419 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002420 if (DelayedDiagnostics[I].Triggered)
2421 continue;
2422
2423 switch (DelayedDiagnostics[I].Kind) {
2424 case DelayedDiagnostic::Deprecation:
2425 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2426 break;
2427
2428 case DelayedDiagnostic::Access:
2429 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2430 break;
2431 }
2432 }
2433 }
2434
John McCall58e6f342010-03-16 05:22:47 +00002435 // Destroy all the delayed diagnostics we're about to pop off.
2436 for (unsigned I = SavedIndex; I != E; ++I)
2437 DelayedDiagnostics[I].destroy();
2438
John McCall2f514482010-01-27 03:50:35 +00002439 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002440}
2441
2442static bool isDeclDeprecated(Decl *D) {
2443 do {
2444 if (D->hasAttr<DeprecatedAttr>())
2445 return true;
2446 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2447 return false;
2448}
2449
John McCall2f514482010-01-27 03:50:35 +00002450void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2451 Decl *Ctx) {
2452 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002453 return;
2454
John McCall2f514482010-01-27 03:50:35 +00002455 DD.Triggered = true;
2456 Diag(DD.Loc, diag::warn_deprecated)
2457 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002458}
2459
2460void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2461 // Delay if we're currently parsing a declaration.
2462 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002463 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002464 return;
2465 }
2466
2467 // Otherwise, don't warn if our current context is deprecated.
2468 if (isDeclDeprecated(cast<Decl>(CurContext)))
2469 return;
2470
2471 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2472}