blob: e423c35f52779f3487e53824985d16aece0eb9cb [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
Douglas Gregore737f502010-08-12 20:07:10 +000014#include "clang/Sema/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))
Sean Huntcf807c42010-08-18 23:23:40 +0000214 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
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
Sean Huntcf807c42010-08-18 23:23:40 +0000223 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
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()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000238 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000239 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)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000255 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000256 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.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000266 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000267 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 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000277 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
278 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
279 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
280 << VD->getType() << 0;
281 return;
282 }
283 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
284 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
285 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
286 << PD->getType() << 1;
287 return;
288 }
289
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000290 IdentifierInfo *II = Attr.getParameterName();
291 if (!II)
292 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000293
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000294 Sema::TypeTy *TypeRep = S.getTypeName(*II, Attr.getLoc(),
295 S.getScopeForContext(d->getDeclContext()->getParent()));
296 if (!TypeRep) {
297 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
298 return;
299 }
300 QualType QT(QualType::getFromOpaquePtr(TypeRep));
301 // Diagnose use of non-object type in iboutletcollection attribute.
302 // FIXME. Gnu attribute extension ignores use of builtin types in
303 // attributes. So, __attribute__((iboutletcollection(char))) will be
304 // treated as __attribute__((iboutletcollection())).
305 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
306 !QT->isObjCObjectType()) {
307 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
308 return;
309 }
Sean Huntcf807c42010-08-18 23:23:40 +0000310 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
311 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000312}
313
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000314static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000315 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
316 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000317 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000318 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000319 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000320 return;
321 }
Mike Stumpbf916502009-07-24 19:02:52 +0000322
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000323 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000324
325 // The nonnull attribute only applies to pointers.
326 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000327
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000328 for (AttributeList::arg_iterator I=Attr.arg_begin(),
329 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000330
331
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000332 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000333 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000334 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000335 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
336 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
338 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000339 return;
340 }
Mike Stumpbf916502009-07-24 19:02:52 +0000341
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000342 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000343
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000344 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000345 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000346 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000347 return;
348 }
Mike Stumpbf916502009-07-24 19:02:52 +0000349
Ted Kremenek465172f2008-07-21 22:09:15 +0000350 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351
352 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000353 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000354 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000355 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000356 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000357 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000358 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000359 }
Mike Stumpbf916502009-07-24 19:02:52 +0000360
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361 NonNullArgs.push_back(x);
362 }
Mike Stumpbf916502009-07-24 19:02:52 +0000363
364 // If no arguments were specified to __attribute__((nonnull)) then all pointer
365 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000366 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000367 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
368 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000369 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000370 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000371 }
Mike Stumpbf916502009-07-24 19:02:52 +0000372
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000373 if (NonNullArgs.empty()) {
374 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
375 return;
376 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000377 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000378
379 unsigned* start = &NonNullArgs[0];
380 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000381 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000382 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
383 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000384}
385
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000386static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
387 // This attribute must be applied to a function declaration.
388 // The first argument to the attribute must be a string,
389 // the name of the resource, for example "malloc".
390 // The following arguments must be argument indexes, the arguments must be
391 // of integer type for Returns, otherwise of pointer type.
392 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000393 // after being held. free() should be __attribute((ownership_takes)), whereas
394 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000395
396 if (!AL.getParameterName()) {
397 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
398 << AL.getName()->getName() << 1;
399 return;
400 }
401 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000402 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000403 switch (AL.getKind()) {
404 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000405 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000406 if (AL.getNumArgs() < 1) {
407 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
408 return;
409 }
Jordy Rose2a479922010-08-12 08:54:03 +0000410 break;
411 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000412 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000413 if (AL.getNumArgs() < 1) {
414 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
415 return;
416 }
Jordy Rose2a479922010-08-12 08:54:03 +0000417 break;
418 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000419 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000420 if (AL.getNumArgs() > 1) {
421 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
422 << AL.getNumArgs() + 1;
423 return;
424 }
Jordy Rose2a479922010-08-12 08:54:03 +0000425 break;
426 default:
427 // This should never happen given how we are called.
428 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000429 }
430
431 if (!isFunction(d) || !hasFunctionProto(d)) {
432 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
433 << 0 /*function*/;
434 return;
435 }
436
437 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
438
439 llvm::StringRef Module = AL.getParameterName()->getName();
440
441 // Normalize the argument, __foo__ becomes foo.
442 if (Module.startswith("__") && Module.endswith("__"))
443 Module = Module.substr(2, Module.size() - 4);
444
445 llvm::SmallVector<unsigned, 10> OwnershipArgs;
446
Jordy Rose2a479922010-08-12 08:54:03 +0000447 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
448 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000449
450 Expr *IdxExpr = static_cast<Expr *>(*I);
451 llvm::APSInt ArgNum(32);
452 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
453 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
454 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
455 << AL.getName()->getName() << IdxExpr->getSourceRange();
456 continue;
457 }
458
459 unsigned x = (unsigned) ArgNum.getZExtValue();
460
461 if (x > NumArgs || x < 1) {
462 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
463 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
464 continue;
465 }
466 --x;
467 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000468 case OwnershipAttr::Takes:
469 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000470 // Is the function argument a pointer type?
471 QualType T = getFunctionOrMethodArgType(d, x);
472 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
473 // FIXME: Should also highlight argument in decl.
474 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000475 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000476 << "pointer"
477 << IdxExpr->getSourceRange();
478 continue;
479 }
480 break;
481 }
Sean Huntcf807c42010-08-18 23:23:40 +0000482 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000483 if (AL.getNumArgs() > 1) {
484 // Is the function argument an integer type?
485 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
486 llvm::APSInt ArgNum(32);
487 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
488 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
489 S.Diag(AL.getLoc(), diag::err_ownership_type)
490 << "ownership_returns" << "integer"
491 << IdxExpr->getSourceRange();
492 return;
493 }
494 }
495 break;
496 }
Jordy Rose2a479922010-08-12 08:54:03 +0000497 default:
498 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000499 } // switch
500
501 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000502 for (specific_attr_iterator<OwnershipAttr>
503 i = d->specific_attr_begin<OwnershipAttr>(),
504 e = d->specific_attr_end<OwnershipAttr>();
505 i != e; ++i) {
506 if ((*i)->getOwnKind() != K) {
507 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
508 I!=E; ++I) {
509 if (x == *I) {
510 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
511 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000512 }
513 }
514 }
515 }
516 OwnershipArgs.push_back(x);
517 }
518
519 unsigned* start = OwnershipArgs.data();
520 unsigned size = OwnershipArgs.size();
521 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000522
523 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
524 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
525 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000526 }
Sean Huntcf807c42010-08-18 23:23:40 +0000527
528 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
529 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000530}
531
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000532static bool isStaticVarOrStaticFunciton(Decl *D) {
533 if (VarDecl *VD = dyn_cast<VarDecl>(D))
534 return VD->getStorageClass() == VarDecl::Static;
535 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
536 return FD->getStorageClass() == FunctionDecl::Static;
537 return false;
538}
539
540static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
541 // Check the attribute arguments.
542 if (Attr.getNumArgs() > 1) {
543 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
544 return;
545 }
546
547 // gcc rejects
548 // class c {
549 // static int a __attribute__((weakref ("v2")));
550 // static int b() __attribute__((weakref ("f3")));
551 // };
552 // and ignores the attributes of
553 // void f(void) {
554 // static int a __attribute__((weakref ("v2")));
555 // }
556 // we reject them
557 if (const DeclContext *Ctx = d->getDeclContext()) {
558 Ctx = Ctx->getLookupContext();
559 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
560 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000561 dyn_cast<NamedDecl>(d)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000562 return;
563 }
564 }
565
566 // The GCC manual says
567 //
568 // At present, a declaration to which `weakref' is attached can only
569 // be `static'.
570 //
571 // It also says
572 //
573 // Without a TARGET,
574 // given as an argument to `weakref' or to `alias', `weakref' is
575 // equivalent to `weak'.
576 //
577 // gcc 4.4.1 will accept
578 // int a7 __attribute__((weakref));
579 // as
580 // int a7 __attribute__((weak));
581 // This looks like a bug in gcc. We reject that for now. We should revisit
582 // it if this behaviour is actually used.
583
584 if (!isStaticVarOrStaticFunciton(d)) {
585 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
586 dyn_cast<NamedDecl>(d)->getNameAsString();
587 return;
588 }
589
590 // GCC rejects
591 // static ((alias ("y"), weakref)).
592 // Should we? How to check that weakref is before or after alias?
593
594 if (Attr.getNumArgs() == 1) {
595 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
596 Arg = Arg->IgnoreParenCasts();
597 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
598
599 if (Str == 0 || Str->isWide()) {
600 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
601 << "weakref" << 1;
602 return;
603 }
604 // GCC will accept anything as the argument of weakref. Should we
605 // check for an existing decl?
Sean Huntcf807c42010-08-18 23:23:40 +0000606 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000607 }
608
Sean Huntcf807c42010-08-18 23:23:40 +0000609 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000610}
611
Chris Lattner803d0802008-06-29 00:43:07 +0000612static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000613 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000614 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000615 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000616 return;
617 }
Mike Stumpbf916502009-07-24 19:02:52 +0000618
Chris Lattner545dd342008-06-28 23:36:30 +0000619 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000620 Arg = Arg->IgnoreParenCasts();
621 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000622
Chris Lattner6b6b5372008-06-26 18:38:35 +0000623 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000624 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000625 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000626 return;
627 }
Mike Stumpbf916502009-07-24 19:02:52 +0000628
Chris Lattner6b6b5372008-06-26 18:38:35 +0000629 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000630
Sean Huntcf807c42010-08-18 23:23:40 +0000631 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000632}
633
Mike Stumpbf916502009-07-24 19:02:52 +0000634static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000635 Sema &S) {
636 // check the attribute arguments.
637 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000639 return;
640 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000641
Chris Lattnerc5197432009-04-14 17:02:11 +0000642 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000643 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000644 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000645 return;
646 }
Mike Stumpbf916502009-07-24 19:02:52 +0000647
Sean Huntcf807c42010-08-18 23:23:40 +0000648 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000649}
650
Ryan Flynn76168e22009-08-09 20:07:29 +0000651static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
652 // check the attribute arguments.
653 if (Attr.getNumArgs() != 0) {
654 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
655 return;
656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000658 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000659 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000660 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000661 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000662 return;
663 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000664 }
665
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000666 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000667}
668
Ted Kremenekb7252322009-04-10 00:01:14 +0000669static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000670 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
671 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000672 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000673}
674
675static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
676 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000677
678 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
679 // because 'analyzer_noreturn' does not impact the type.
680
681 if (Attr.getNumArgs() != 0) {
682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
683 return;
684 }
685
686 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
687 ValueDecl *VD = dyn_cast<ValueDecl>(d);
688 if (VD == 0 || (!VD->getType()->isBlockPointerType()
689 && !VD->getType()->isFunctionPointerType())) {
690 S.Diag(Attr.getLoc(),
691 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
692 : diag::warn_attribute_wrong_decl_type)
693 << Attr.getName() << 0 /*function*/;
694 return;
695 }
696 }
697
698 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000699}
700
John Thompson35cc9622010-08-09 21:53:52 +0000701// PS3 PPU-specific.
702static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
703 Sema &S) {
704/*
705 Returning a Vector Class in Registers
706
707 According to the PPU ABI specifications, a class with a single member of vector type is returned in
708 memory when used as the return value of a function. This results in inefficient code when implementing
709 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
710 definition. This attribute is also applicable to struct types.
711
712 Example:
713
714 struct Vector
715 {
716 __vector float xyzw;
717 } __attribute__((vecreturn));
718
719 Vector Add(Vector lhs, Vector rhs)
720 {
721 Vector result;
722 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
723 return result; // This will be returned in a register
724 }
725*/
726 if (!isa<CXXRecordDecl>(d)) {
727 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
728 << Attr.getName() << 9 /*class*/;
729 return;
730 }
731
732 if (d->getAttr<VecReturnAttr>()) {
733 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
734 return;
735 }
736
Sean Huntcf807c42010-08-18 23:23:40 +0000737 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000738}
739
Sean Huntbbd37c62009-11-21 08:43:09 +0000740static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
741 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
742 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000743 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000744 return;
745 }
746 // FIXME: Actually store the attribute on the declaration
747}
748
Ted Kremenek73798892008-07-25 04:39:19 +0000749static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
750 // check the attribute arguments.
751 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000753 return;
754 }
Mike Stumpbf916502009-07-24 19:02:52 +0000755
John McCallaec58602010-03-31 02:47:45 +0000756 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
757 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000759 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000760 return;
761 }
Mike Stumpbf916502009-07-24 19:02:52 +0000762
Sean Huntcf807c42010-08-18 23:23:40 +0000763 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000764}
765
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000766static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
767 // check the attribute arguments.
768 if (Attr.getNumArgs() != 0) {
769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
770 return;
771 }
Mike Stumpbf916502009-07-24 19:02:52 +0000772
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000773 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000774 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000775 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
776 return;
777 }
778 } else if (!isFunctionOrMethod(d)) {
779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000780 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000781 return;
782 }
Mike Stumpbf916502009-07-24 19:02:52 +0000783
Sean Huntcf807c42010-08-18 23:23:40 +0000784 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000785}
786
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000787static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
788 // check the attribute arguments.
789 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
791 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000792 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000793 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000794
795 int priority = 65535; // FIXME: Do not hardcode such constants.
796 if (Attr.getNumArgs() > 0) {
797 Expr *E = static_cast<Expr *>(Attr.getArg(0));
798 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000799 if (E->isTypeDependent() || E->isValueDependent() ||
800 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000801 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000802 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000803 return;
804 }
805 priority = Idx.getZExtValue();
806 }
Mike Stumpbf916502009-07-24 19:02:52 +0000807
Chris Lattnerc5197432009-04-14 17:02:11 +0000808 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000809 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000810 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000811 return;
812 }
813
Sean Huntcf807c42010-08-18 23:23:40 +0000814 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000815}
816
817static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
818 // check the attribute arguments.
819 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000820 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
821 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000822 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000823 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000824
825 int priority = 65535; // FIXME: Do not hardcode such constants.
826 if (Attr.getNumArgs() > 0) {
827 Expr *E = static_cast<Expr *>(Attr.getArg(0));
828 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000829 if (E->isTypeDependent() || E->isValueDependent() ||
830 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000831 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000832 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000833 return;
834 }
835 priority = Idx.getZExtValue();
836 }
Mike Stumpbf916502009-07-24 19:02:52 +0000837
Anders Carlsson6782fc62008-08-22 22:10:48 +0000838 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000839 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000840 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000841 return;
842 }
843
Sean Huntcf807c42010-08-18 23:23:40 +0000844 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000845}
846
Chris Lattner803d0802008-06-29 00:43:07 +0000847static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000848 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000849 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000851 return;
852 }
Mike Stumpbf916502009-07-24 19:02:52 +0000853
Sean Huntcf807c42010-08-18 23:23:40 +0000854 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000855}
856
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000857static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
858 // check the attribute arguments.
859 if (Attr.getNumArgs() != 0) {
860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
861 return;
862 }
Mike Stumpbf916502009-07-24 19:02:52 +0000863
Sean Huntcf807c42010-08-18 23:23:40 +0000864 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000865}
866
Chris Lattner803d0802008-06-29 00:43:07 +0000867static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000868 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000869 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000870 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000871 return;
872 }
Mike Stumpbf916502009-07-24 19:02:52 +0000873
Chris Lattner545dd342008-06-28 23:36:30 +0000874 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000875 Arg = Arg->IgnoreParenCasts();
876 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000877
Chris Lattner6b6b5372008-06-26 18:38:35 +0000878 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000879 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000880 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000881 return;
882 }
Mike Stumpbf916502009-07-24 19:02:52 +0000883
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000884 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +0000885 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +0000886
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000887 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +0000888 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000889 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +0000890 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000891 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +0000892 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +0000893 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +0000894 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000895 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000896 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000897 return;
898 }
Mike Stumpbf916502009-07-24 19:02:52 +0000899
Sean Huntcf807c42010-08-18 23:23:40 +0000900 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000901}
902
Chris Lattner0db29ec2009-02-14 08:09:34 +0000903static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
904 Sema &S) {
905 if (Attr.getNumArgs() != 0) {
906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
907 return;
908 }
Mike Stumpbf916502009-07-24 19:02:52 +0000909
Chris Lattner0db29ec2009-02-14 08:09:34 +0000910 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
911 if (OCI == 0) {
912 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
913 return;
914 }
Mike Stumpbf916502009-07-24 19:02:52 +0000915
Sean Huntcf807c42010-08-18 23:23:40 +0000916 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +0000917}
918
919static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000920 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000922 return;
923 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000924 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000925 QualType T = TD->getUnderlyingType();
926 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +0000927 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000928 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
929 return;
930 }
931 }
Sean Huntcf807c42010-08-18 23:23:40 +0000932 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000933}
934
Mike Stumpbf916502009-07-24 19:02:52 +0000935static void
Douglas Gregorf9201e02009-02-11 23:02:49 +0000936HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
937 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +0000938 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +0000939 return;
940 }
941
942 if (!isa<FunctionDecl>(D)) {
943 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
944 return;
945 }
946
Sean Huntcf807c42010-08-18 23:23:40 +0000947 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +0000948}
949
Steve Naroff9eae5762008-09-18 16:44:58 +0000950static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000951 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000952 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000953 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000954 return;
955 }
Mike Stumpbf916502009-07-24 19:02:52 +0000956
Steve Naroff9eae5762008-09-18 16:44:58 +0000957 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000959 return;
960 }
Mike Stumpbf916502009-07-24 19:02:52 +0000961
Sean Huntcf807c42010-08-18 23:23:40 +0000962 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000963 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000964 type = BlocksAttr::ByRef;
965 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000966 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000967 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000968 return;
969 }
Mike Stumpbf916502009-07-24 19:02:52 +0000970
Sean Huntcf807c42010-08-18 23:23:40 +0000971 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000972}
973
Anders Carlsson77091822008-10-05 18:05:59 +0000974static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
975 // check the attribute arguments.
976 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000977 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
978 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000979 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000980 }
981
Anders Carlsson77091822008-10-05 18:05:59 +0000982 int sentinel = 0;
983 if (Attr.getNumArgs() > 0) {
984 Expr *E = static_cast<Expr *>(Attr.getArg(0));
985 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000986 if (E->isTypeDependent() || E->isValueDependent() ||
987 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000988 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000989 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000990 return;
991 }
992 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000993
Anders Carlsson77091822008-10-05 18:05:59 +0000994 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000995 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
996 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000997 return;
998 }
999 }
1000
1001 int nullPos = 0;
1002 if (Attr.getNumArgs() > 1) {
1003 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1004 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001005 if (E->isTypeDependent() || E->isValueDependent() ||
1006 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001007 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001008 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001009 return;
1010 }
1011 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001012
Anders Carlsson77091822008-10-05 18:05:59 +00001013 if (nullPos > 1 || nullPos < 0) {
1014 // FIXME: This error message could be improved, it would be nice
1015 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001016 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1017 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001018 return;
1019 }
1020 }
1021
1022 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001023 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001024 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001025
Chris Lattner897cd902009-03-17 23:03:47 +00001026 if (isa<FunctionNoProtoType>(FT)) {
1027 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1028 return;
1029 }
Mike Stumpbf916502009-07-24 19:02:52 +00001030
Chris Lattner897cd902009-03-17 23:03:47 +00001031 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001032 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001033 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001034 }
Anders Carlsson77091822008-10-05 18:05:59 +00001035 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1036 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001037 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001038 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001039 }
1040 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001041 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1042 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001043 ;
1044 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001045 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001046 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001047 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001048 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001049 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001050 int m = Ty->isFunctionPointerType() ? 0 : 1;
1051 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001052 return;
1053 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001054 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001055 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001056 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001057 return;
1058 }
Anders Carlsson77091822008-10-05 18:05:59 +00001059 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001060 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001061 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001062 return;
1063 }
Sean Huntcf807c42010-08-18 23:23:40 +00001064 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001065}
1066
Chris Lattner026dc962009-02-14 07:37:35 +00001067static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1068 // check the attribute arguments.
1069 if (Attr.getNumArgs() != 0) {
1070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1071 return;
1072 }
1073
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001074 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001075 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001076 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001077 return;
1078 }
Mike Stumpbf916502009-07-24 19:02:52 +00001079
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001080 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1081 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1082 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001083 return;
1084 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001085 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1086 if (MD->getResultType()->isVoidType()) {
1087 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1088 << Attr.getName() << 1;
1089 return;
1090 }
1091
Sean Huntcf807c42010-08-18 23:23:40 +00001092 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001093}
1094
1095static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001096 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001097 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001098 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001099 return;
1100 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001101
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001102 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001103 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001104 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1105 dyn_cast<NamedDecl>(D)->getNameAsString();
1106 return;
1107 }
1108
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001109 // TODO: could also be applied to methods?
1110 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1111 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001112 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001113 return;
1114 }
Mike Stumpbf916502009-07-24 19:02:52 +00001115
Sean Huntcf807c42010-08-18 23:23:40 +00001116 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001117}
1118
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001119static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1120 // check the attribute arguments.
1121 if (Attr.getNumArgs() != 0) {
1122 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1123 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001124 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001125
1126 // weak_import only applies to variable & function declarations.
1127 bool isDef = false;
1128 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1129 isDef = (!VD->hasExternalStorage() || VD->getInit());
1130 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001131 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001132 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1133 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001134 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001135 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001136 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001137 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001138 !isa<ObjCInterfaceDecl>(D))
1139 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001140 << Attr.getName() << 2 /*variable and function*/;
1141 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001142 }
1143
1144 // Merge should handle any subsequent violations.
1145 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001146 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001147 diag::warn_attribute_weak_import_invalid_on_definition)
1148 << "weak_import" << 2 /*variable and function*/;
1149 return;
1150 }
1151
Sean Huntcf807c42010-08-18 23:23:40 +00001152 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001153}
1154
Nate Begeman6f3d8382009-06-26 06:32:41 +00001155static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1156 Sema &S) {
1157 // Attribute has 3 arguments.
1158 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001159 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001160 return;
1161 }
1162
1163 unsigned WGSize[3];
1164 for (unsigned i = 0; i < 3; ++i) {
1165 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1166 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001167 if (E->isTypeDependent() || E->isValueDependent() ||
1168 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001169 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1170 << "reqd_work_group_size" << E->getSourceRange();
1171 return;
1172 }
1173 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1174 }
Sean Huntcf807c42010-08-18 23:23:40 +00001175 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1176 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001177 WGSize[2]));
1178}
1179
Chris Lattner026dc962009-02-14 07:37:35 +00001180static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001181 // Attribute has no arguments.
1182 if (Attr.getNumArgs() != 1) {
1183 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1184 return;
1185 }
1186
1187 // Make sure that there is a string literal as the sections's single
1188 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001189 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1190 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001191 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001192 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001193 return;
1194 }
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Chris Lattner797c3c42009-08-10 19:03:04 +00001196 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001197 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001198 if (!Error.empty()) {
1199 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1200 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001201 return;
1202 }
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001204 // This attribute cannot be applied to local variables.
1205 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1206 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1207 return;
1208 }
1209
Sean Huntcf807c42010-08-18 23:23:40 +00001210 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001211}
1212
Chris Lattner6b6b5372008-06-26 18:38:35 +00001213
Chris Lattner803d0802008-06-29 00:43:07 +00001214static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001215 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001216 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001217 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001218 return;
1219 }
Mike Stumpbf916502009-07-24 19:02:52 +00001220
Sean Huntcf807c42010-08-18 23:23:40 +00001221 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001222}
1223
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001224static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1225 // check the attribute arguments.
1226 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001228 return;
1229 }
Mike Stumpbf916502009-07-24 19:02:52 +00001230
Sean Huntcf807c42010-08-18 23:23:40 +00001231 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001232}
1233
1234static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1235 // check the attribute arguments.
1236 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001237 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001238 return;
1239 }
Mike Stumpbf916502009-07-24 19:02:52 +00001240
Sean Huntcf807c42010-08-18 23:23:40 +00001241 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001242}
1243
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001244static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001245 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001246 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1247 return;
1248 }
Mike Stumpbf916502009-07-24 19:02:52 +00001249
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001250 if (Attr.getNumArgs() != 0) {
1251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1252 return;
1253 }
Mike Stumpbf916502009-07-24 19:02:52 +00001254
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001255 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001256
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001257 if (!VD || !VD->hasLocalStorage()) {
1258 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1259 return;
1260 }
Mike Stumpbf916502009-07-24 19:02:52 +00001261
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001262 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001263 // FIXME: Lookup probably isn't looking in the right place
1264 // FIXME: The lookup source location should be in the attribute, not the
1265 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001266 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001267 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001268 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001269 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001270 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001271 Attr.getParameterName();
1272 return;
1273 }
Mike Stumpbf916502009-07-24 19:02:52 +00001274
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001275 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1276 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001277 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001278 Attr.getParameterName();
1279 return;
1280 }
1281
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001282 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001283 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001284 Attr.getParameterName();
1285 return;
1286 }
Mike Stumpbf916502009-07-24 19:02:52 +00001287
Anders Carlsson89941c12009-02-07 23:16:50 +00001288 // We're currently more strict than GCC about what function types we accept.
1289 // If this ever proves to be a problem it should be easy to fix.
1290 QualType Ty = S.Context.getPointerType(VD->getType());
1291 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001292 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001293 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001294 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1295 Attr.getParameterName() << ParamTy << Ty;
1296 return;
1297 }
Mike Stumpbf916502009-07-24 19:02:52 +00001298
Sean Huntcf807c42010-08-18 23:23:40 +00001299 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001300}
1301
Mike Stumpbf916502009-07-24 19:02:52 +00001302/// Handle __attribute__((format_arg((idx)))) attribute based on
1303/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1304static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001305 if (Attr.getNumArgs() != 1) {
1306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1307 return;
1308 }
1309 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1310 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1311 << Attr.getName() << 0 /*function*/;
1312 return;
1313 }
Mike Stumpbf916502009-07-24 19:02:52 +00001314 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1315 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001316 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1317 unsigned FirstIdx = 1;
1318 // checks for the 2nd argument
1319 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1320 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001321 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1322 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001323 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1324 << "format" << 2 << IdxExpr->getSourceRange();
1325 return;
1326 }
Mike Stumpbf916502009-07-24 19:02:52 +00001327
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001328 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1329 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1330 << "format" << 2 << IdxExpr->getSourceRange();
1331 return;
1332 }
Mike Stumpbf916502009-07-24 19:02:52 +00001333
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001334 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001335
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001336 // make sure the format string is really a string
1337 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001338
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001339 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1340 if (not_nsstring_type &&
1341 !isCFStringType(Ty, S.Context) &&
1342 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001343 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001344 // FIXME: Should highlight the actual expression that has the wrong type.
1345 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001346 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001347 << IdxExpr->getSourceRange();
1348 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001349 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001350 Ty = getFunctionOrMethodResultType(d);
1351 if (!isNSStringType(Ty, S.Context) &&
1352 !isCFStringType(Ty, S.Context) &&
1353 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001354 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001355 // FIXME: Should highlight the actual expression that has the wrong type.
1356 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001357 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001358 << IdxExpr->getSourceRange();
1359 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001360 }
1361
Sean Huntcf807c42010-08-18 23:23:40 +00001362 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001363}
1364
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001365enum FormatAttrKind {
1366 CFStringFormat,
1367 NSStringFormat,
1368 StrftimeFormat,
1369 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001370 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001371 InvalidFormat
1372};
1373
1374/// getFormatAttrKind - Map from format attribute names to supported format
1375/// types.
1376static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1377 // Check for formats that get handled specially.
1378 if (Format == "NSString")
1379 return NSStringFormat;
1380 if (Format == "CFString")
1381 return CFStringFormat;
1382 if (Format == "strftime")
1383 return StrftimeFormat;
1384
1385 // Otherwise, check for supported formats.
1386 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1387 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1388 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1389 Format == "zcmn_err")
1390 return SupportedFormat;
1391
Duncan Sandsbc525952010-03-23 14:44:19 +00001392 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1393 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001394 return IgnoredFormat;
1395
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001396 return InvalidFormat;
1397}
1398
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001399/// Handle __attribute__((init_priority(priority))) attributes based on
1400/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1401static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1402 Sema &S) {
1403 if (!S.getLangOptions().CPlusPlus) {
1404 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1405 return;
1406 }
1407
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001408 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1409 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1410 Attr.setInvalid();
1411 return;
1412 }
1413 QualType T = dyn_cast<VarDecl>(d)->getType();
1414 if (S.Context.getAsArrayType(T))
1415 T = S.Context.getBaseElementType(T);
1416 if (!T->getAs<RecordType>()) {
1417 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1418 Attr.setInvalid();
1419 return;
1420 }
1421
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001422 if (Attr.getNumArgs() != 1) {
1423 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1424 Attr.setInvalid();
1425 return;
1426 }
1427 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001428
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001429 llvm::APSInt priority(32);
1430 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1431 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1432 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1433 << "init_priority" << priorityExpr->getSourceRange();
1434 Attr.setInvalid();
1435 return;
1436 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001437 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001438 if (prioritynum < 101 || prioritynum > 65535) {
1439 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1440 << priorityExpr->getSourceRange();
1441 Attr.setInvalid();
1442 return;
1443 }
Sean Huntcf807c42010-08-18 23:23:40 +00001444 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001445}
1446
Mike Stumpbf916502009-07-24 19:02:52 +00001447/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1448/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001449static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001450
Chris Lattner545dd342008-06-28 23:36:30 +00001451 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001452 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001453 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001454 return;
1455 }
1456
Chris Lattner545dd342008-06-28 23:36:30 +00001457 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001459 return;
1460 }
1461
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001462 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001463 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001464 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001465 return;
1466 }
1467
Daniel Dunbar35682492008-09-26 04:12:28 +00001468 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001469 unsigned FirstIdx = 1;
1470
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001471 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001472
1473 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001474 if (Format.startswith("__") && Format.endswith("__"))
1475 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001476
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001477 // Check for supported formats.
1478 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001479
1480 if (Kind == IgnoredFormat)
1481 return;
1482
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001483 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001484 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001485 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001486 return;
1487 }
1488
1489 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001490 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001491 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001492 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1493 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001494 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001495 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001496 return;
1497 }
1498
Anders Carlsson4fb77202009-08-25 14:12:34 +00001499 // FIXME: We should handle the implicit 'this' parameter in a more generic
1500 // way that can be used for other arguments.
1501 bool HasImplicitThisParam = false;
1502 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1503 if (MD->isInstance()) {
1504 HasImplicitThisParam = true;
1505 NumArgs++;
1506 }
1507 }
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Chris Lattner6b6b5372008-06-26 18:38:35 +00001509 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001510 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001511 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001512 return;
1513 }
1514
1515 // FIXME: Do we need to bounds check?
1516 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001517
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001518 if (HasImplicitThisParam) {
1519 if (ArgIdx == 0) {
1520 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1521 << "a string type" << IdxExpr->getSourceRange();
1522 return;
1523 }
1524 ArgIdx--;
1525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Chris Lattner6b6b5372008-06-26 18:38:35 +00001527 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001528 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001529
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001530 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001531 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001532 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1533 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001534 return;
1535 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001536 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001537 // FIXME: do we need to check if the type is NSString*? What are the
1538 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001539 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001540 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001541 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1542 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001543 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001544 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001545 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001546 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001547 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001548 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1549 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001550 return;
1551 }
1552
1553 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001554 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001555 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001556 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1557 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001558 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001559 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001560 return;
1561 }
1562
1563 // check if the function is variadic if the 3rd argument non-zero
1564 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001565 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001566 ++NumArgs; // +1 for ...
1567 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001568 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001569 return;
1570 }
1571 }
1572
Chris Lattner3c73c412008-11-19 08:23:25 +00001573 // strftime requires FirstArg to be 0 because it doesn't read from any
1574 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001575 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001576 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001577 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1578 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001579 return;
1580 }
1581 // if 0 it disables parameter checking (to use with e.g. va_list)
1582 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001583 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001584 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001585 return;
1586 }
1587
Sean Huntcf807c42010-08-18 23:23:40 +00001588 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1589 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001590 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001591}
1592
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001593static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1594 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001595 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001596 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001598 return;
1599 }
1600
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001601 // Try to find the underlying union declaration.
1602 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001603 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001604 if (TD && TD->getUnderlyingType()->isUnionType())
1605 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1606 else
1607 RD = dyn_cast<RecordDecl>(d);
1608
1609 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001610 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001611 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001612 return;
1613 }
1614
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001615 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001616 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001617 diag::warn_transparent_union_attribute_not_definition);
1618 return;
1619 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001620
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001621 RecordDecl::field_iterator Field = RD->field_begin(),
1622 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001623 if (Field == FieldEnd) {
1624 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1625 return;
1626 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001627
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001628 FieldDecl *FirstField = *Field;
1629 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001630 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001631 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001632 diag::warn_transparent_union_attribute_floating)
1633 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001634 return;
1635 }
1636
1637 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1638 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1639 for (; Field != FieldEnd; ++Field) {
1640 QualType FieldType = Field->getType();
1641 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1642 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1643 // Warn if we drop the attribute.
1644 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001645 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001646 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001647 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001648 diag::warn_transparent_union_attribute_field_size_align)
1649 << isSize << Field->getDeclName() << FieldBits;
1650 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001651 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001652 diag::note_transparent_union_first_field_size_align)
1653 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001654 return;
1655 }
1656 }
1657
Sean Huntcf807c42010-08-18 23:23:40 +00001658 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001659}
1660
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001661static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001662 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001663 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001664 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001665 return;
1666 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001667 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1668 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001669
Chris Lattner6b6b5372008-06-26 18:38:35 +00001670 // Make sure that there is a string literal as the annotation's single
1671 // argument.
1672 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001673 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001674 return;
1675 }
Sean Huntcf807c42010-08-18 23:23:40 +00001676 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001677}
1678
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001679static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001680 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001681 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001682 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001683 return;
1684 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001685
1686 //FIXME: The C++0x version of this attribute has more limited applicabilty
1687 // than GNU's, and should error out when it is used to specify a
1688 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001689
Chris Lattner545dd342008-06-28 23:36:30 +00001690 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001691 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001692 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001693 }
Mike Stumpbf916502009-07-24 19:02:52 +00001694
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001695 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1696}
1697
1698void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1699 if (E->isTypeDependent() || E->isValueDependent()) {
1700 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001701 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001702 return;
1703 }
1704
Sean Huntcf807c42010-08-18 23:23:40 +00001705 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001706 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001707 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1708 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1709 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001710 return;
1711 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001712 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001713 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1714 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001715 return;
1716 }
1717
Sean Huntcf807c42010-08-18 23:23:40 +00001718 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1719}
1720
1721void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1722 // FIXME: Cache the number on the Attr object if non-dependent?
1723 // FIXME: Perform checking of type validity
1724 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1725 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001726}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001727
Mike Stumpbf916502009-07-24 19:02:52 +00001728/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1729/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001730///
Mike Stumpbf916502009-07-24 19:02:52 +00001731/// Despite what would be logical, the mode attribute is a decl attribute, not a
1732/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1733/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001734static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001735 // This attribute isn't documented, but glibc uses it. It changes
1736 // the width of an int or unsigned int to the specified size.
1737
1738 // Check that there aren't any arguments
1739 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001741 return;
1742 }
1743
1744 IdentifierInfo *Name = Attr.getParameterName();
1745 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001746 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001747 return;
1748 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001749
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001750 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001751
1752 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001753 if (Str.startswith("__") && Str.endswith("__"))
1754 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001755
1756 unsigned DestWidth = 0;
1757 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001758 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001759 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001760 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001761 switch (Str[0]) {
1762 case 'Q': DestWidth = 8; break;
1763 case 'H': DestWidth = 16; break;
1764 case 'S': DestWidth = 32; break;
1765 case 'D': DestWidth = 64; break;
1766 case 'X': DestWidth = 96; break;
1767 case 'T': DestWidth = 128; break;
1768 }
1769 if (Str[1] == 'F') {
1770 IntegerMode = false;
1771 } else if (Str[1] == 'C') {
1772 IntegerMode = false;
1773 ComplexMode = true;
1774 } else if (Str[1] != 'I') {
1775 DestWidth = 0;
1776 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001777 break;
1778 case 4:
1779 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1780 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001781 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001782 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001783 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001784 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001785 break;
1786 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001787 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001788 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001789 break;
1790 }
1791
1792 QualType OldTy;
1793 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1794 OldTy = TD->getUnderlyingType();
1795 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1796 OldTy = VD->getType();
1797 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001798 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1799 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001800 return;
1801 }
Eli Friedman73397492009-03-03 06:41:03 +00001802
John McCall183700f2009-09-21 23:43:11 +00001803 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001804 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1805 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001806 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001807 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1808 } else if (ComplexMode) {
1809 if (!OldTy->isComplexType())
1810 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1811 } else {
1812 if (!OldTy->isFloatingType())
1813 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1814 }
1815
Mike Stump390b4cc2009-05-16 07:39:55 +00001816 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1817 // and friends, at least with glibc.
1818 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1819 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001820 // FIXME: Make sure floating-point mappings are accurate
1821 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001822 QualType NewTy;
1823 switch (DestWidth) {
1824 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001825 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001826 return;
1827 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001828 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001829 return;
1830 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001831 if (!IntegerMode) {
1832 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1833 return;
1834 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001835 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001836 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001837 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001838 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001839 break;
1840 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001841 if (!IntegerMode) {
1842 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1843 return;
1844 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001845 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001846 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001847 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001848 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001849 break;
1850 case 32:
1851 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001852 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001853 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001854 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001855 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001856 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001857 break;
1858 case 64:
1859 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001860 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001861 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001862 if (S.Context.Target.getLongWidth() == 64)
1863 NewTy = S.Context.LongTy;
1864 else
1865 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001866 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001867 if (S.Context.Target.getLongWidth() == 64)
1868 NewTy = S.Context.UnsignedLongTy;
1869 else
1870 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001871 break;
Eli Friedman73397492009-03-03 06:41:03 +00001872 case 96:
1873 NewTy = S.Context.LongDoubleTy;
1874 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001875 case 128:
1876 if (!IntegerMode) {
1877 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1878 return;
1879 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001880 if (OldTy->isSignedIntegerType())
1881 NewTy = S.Context.Int128Ty;
1882 else
1883 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00001884 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001885 }
1886
Eli Friedman73397492009-03-03 06:41:03 +00001887 if (ComplexMode) {
1888 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001889 }
1890
1891 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00001892 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1893 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00001894 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00001895 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00001896 cast<ValueDecl>(D)->setType(NewTy);
1897}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001898
Mike Stump1feade82009-08-26 22:31:08 +00001899static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00001900 // check the attribute arguments.
1901 if (Attr.getNumArgs() > 0) {
1902 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1903 return;
1904 }
Anders Carlssone896d982009-02-13 08:11:52 +00001905
Anders Carlsson5bab7882009-02-19 19:16:48 +00001906 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001908 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001909 return;
1910 }
Mike Stumpbf916502009-07-24 19:02:52 +00001911
Sean Huntcf807c42010-08-18 23:23:40 +00001912 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00001913}
1914
Mike Stump1feade82009-08-26 22:31:08 +00001915static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001916 // check the attribute arguments.
1917 if (Attr.getNumArgs() != 0) {
1918 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1919 return;
1920 }
Mike Stumpbf916502009-07-24 19:02:52 +00001921
Chris Lattnerc5197432009-04-14 17:02:11 +00001922 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001923 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001924 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001925 return;
1926 }
Mike Stumpbf916502009-07-24 19:02:52 +00001927
Sean Huntcf807c42010-08-18 23:23:40 +00001928 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00001929}
1930
Chris Lattner7255a2d2010-06-22 00:03:40 +00001931static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1932 Sema &S) {
1933 // check the attribute arguments.
1934 if (Attr.getNumArgs() != 0) {
1935 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1936 return;
1937 }
1938
1939 if (!isa<FunctionDecl>(d)) {
1940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1941 << Attr.getName() << 0 /*function*/;
1942 return;
1943 }
1944
Sean Huntcf807c42010-08-18 23:23:40 +00001945 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00001946}
1947
Chris Lattnercf2a7212009-04-20 19:12:28 +00001948static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001949 // check the attribute arguments.
1950 if (Attr.getNumArgs() != 0) {
1951 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1952 return;
1953 }
Mike Stumpbf916502009-07-24 19:02:52 +00001954
Chris Lattnerc5197432009-04-14 17:02:11 +00001955 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1956 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001957 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001958 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001959 return;
1960 }
Mike Stumpbf916502009-07-24 19:02:52 +00001961
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001962 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001963 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001964 return;
1965 }
Mike Stumpbf916502009-07-24 19:02:52 +00001966
Sean Huntcf807c42010-08-18 23:23:40 +00001967 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00001968}
1969
Abramo Bagnarae215f722010-04-30 13:10:51 +00001970static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1971 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1972 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1973 assert(Attr.isInvalid() == false);
1974
1975 switch (Attr.getKind()) {
1976 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00001977 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001978 return;
1979 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00001980 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001981 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001982 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00001983 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001984 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00001985 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00001986 return;
1987 default:
1988 llvm_unreachable("unexpected attribute kind");
1989 return;
1990 }
1991}
1992
Fariborz Jahanianee760332009-03-27 18:38:55 +00001993static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1994 // check the attribute arguments.
1995 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001996 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001997 return;
1998 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001999
Fariborz Jahanianee760332009-03-27 18:38:55 +00002000 if (!isFunctionOrMethod(d)) {
2001 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002002 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002003 return;
2004 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002005
2006 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2007 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002008 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2009 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2011 << "regparm" << NumParamsExpr->getSourceRange();
2012 return;
2013 }
2014
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002015 if (S.Context.Target.getRegParmMax() == 0) {
2016 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002017 << NumParamsExpr->getSourceRange();
2018 return;
2019 }
2020
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002021 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002022 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2023 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002024 return;
2025 }
2026
Sean Huntcf807c42010-08-18 23:23:40 +00002027 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2028 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002029}
2030
Sean Huntbbd37c62009-11-21 08:43:09 +00002031static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2032 // check the attribute arguments.
2033 if (Attr.getNumArgs() != 0) {
2034 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2035 return;
2036 }
2037
2038 if (!isa<CXXRecordDecl>(d)
2039 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2040 S.Diag(Attr.getLoc(),
2041 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2042 : diag::warn_attribute_wrong_decl_type)
2043 << Attr.getName() << 7 /*virtual method or class*/;
2044 return;
2045 }
Sean Hunt7725e672009-11-25 04:20:27 +00002046
2047 // FIXME: Conform to C++0x redeclaration rules.
2048
2049 if (d->getAttr<FinalAttr>()) {
2050 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2051 return;
2052 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002053
Sean Huntcf807c42010-08-18 23:23:40 +00002054 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002055}
2056
Chris Lattner0744e5f2008-06-29 00:23:49 +00002057//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002058// C++0x member checking attributes
2059//===----------------------------------------------------------------------===//
2060
2061static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2062 if (Attr.getNumArgs() != 0) {
2063 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2064 return;
2065 }
2066
2067 if (!isa<CXXRecordDecl>(d)) {
2068 S.Diag(Attr.getLoc(),
2069 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2070 : diag::warn_attribute_wrong_decl_type)
2071 << Attr.getName() << 9 /*class*/;
2072 return;
2073 }
2074
2075 if (d->getAttr<BaseCheckAttr>()) {
2076 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2077 return;
2078 }
2079
Sean Huntcf807c42010-08-18 23:23:40 +00002080 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002081}
2082
2083static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2084 if (Attr.getNumArgs() != 0) {
2085 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2086 return;
2087 }
2088
2089 if (!isa<RecordDecl>(d->getDeclContext())) {
2090 // FIXME: It's not the type that's the problem
2091 S.Diag(Attr.getLoc(),
2092 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2093 : diag::warn_attribute_wrong_decl_type)
2094 << Attr.getName() << 11 /*member*/;
2095 return;
2096 }
2097
2098 // FIXME: Conform to C++0x redeclaration rules.
2099
2100 if (d->getAttr<HidingAttr>()) {
2101 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2102 return;
2103 }
2104
Sean Huntcf807c42010-08-18 23:23:40 +00002105 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002106}
2107
2108static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2109 if (Attr.getNumArgs() != 0) {
2110 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2111 return;
2112 }
2113
2114 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2115 // FIXME: It's not the type that's the problem
2116 S.Diag(Attr.getLoc(),
2117 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2118 : diag::warn_attribute_wrong_decl_type)
2119 << Attr.getName() << 10 /*virtual method*/;
2120 return;
2121 }
2122
2123 // FIXME: Conform to C++0x redeclaration rules.
2124
2125 if (d->getAttr<OverrideAttr>()) {
2126 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2127 return;
2128 }
2129
Sean Huntcf807c42010-08-18 23:23:40 +00002130 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002131}
2132
2133//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002134// Checker-specific attribute handlers.
2135//===----------------------------------------------------------------------===//
2136
2137static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2138 Sema &S) {
2139
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002140 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002141
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002142 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2143 RetTy = MD->getResultType();
2144 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2145 RetTy = FD->getResultType();
2146 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002147 SourceLocation L = Attr.getLoc();
2148 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2149 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002150 return;
2151 }
Mike Stumpbf916502009-07-24 19:02:52 +00002152
Ted Kremenek6217b802009-07-29 21:53:49 +00002153 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002154 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002155 SourceLocation L = Attr.getLoc();
2156 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2157 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002158 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002159 }
Mike Stumpbf916502009-07-24 19:02:52 +00002160
Ted Kremenekb71368d2009-05-09 02:44:38 +00002161 switch (Attr.getKind()) {
2162 default:
2163 assert(0 && "invalid ownership attribute");
2164 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002165 case AttributeList::AT_cf_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002166 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002167 return;
2168 case AttributeList::AT_ns_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002169 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002170 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002171 case AttributeList::AT_cf_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002172 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002173 return;
2174 case AttributeList::AT_ns_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002175 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002176 return;
2177 };
2178}
2179
Charles Davisf0122fe2010-02-16 18:27:26 +00002180static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2181 return Attr.getKind() == AttributeList::AT_dllimport ||
2182 Attr.getKind() == AttributeList::AT_dllexport;
2183}
2184
Ted Kremenekb71368d2009-05-09 02:44:38 +00002185//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002186// Top Level Sema Entry Points
2187//===----------------------------------------------------------------------===//
2188
Sebastian Redla89d82c2008-12-21 19:24:58 +00002189/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002190/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002191/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2192/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002193static void ProcessDeclAttribute(Scope *scope, Decl *D,
2194 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002195 if (Attr.isInvalid())
2196 return;
2197
Charles Davisf0122fe2010-02-16 18:27:26 +00002198 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2199 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002200 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002201 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002202 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002203 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2204 case AttributeList::AT_IBOutletCollection:
2205 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002206 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002207 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002208 case AttributeList::AT_vector_size:
Mike Stumpbf916502009-07-24 19:02:52 +00002209 // Ignore these, these are type attributes, handled by
2210 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002211 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002212 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2213 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002214 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002215 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002216 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002217 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002218 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2219 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002220 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002221 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002222 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2223 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2224 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002225 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002226 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002227 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002228 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2229 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2230 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2231 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2232 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2233 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2234 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2235 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002236 case AttributeList::AT_ownership_returns:
2237 case AttributeList::AT_ownership_takes:
2238 case AttributeList::AT_ownership_holds:
2239 HandleOwnershipAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002240 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2241 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2242 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002243 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002244
2245 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002246 case AttributeList::AT_ns_returns_not_retained:
2247 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002248 case AttributeList::AT_ns_returns_retained:
2249 case AttributeList::AT_cf_returns_retained:
2250 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2251
Nate Begeman6f3d8382009-06-26 06:32:41 +00002252 case AttributeList::AT_reqd_wg_size:
2253 HandleReqdWorkGroupSize(D, Attr, S); break;
2254
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002255 case AttributeList::AT_init_priority:
2256 HandleInitPriorityAttr(D, Attr, S); break;
2257
Sean Hunt7725e672009-11-25 04:20:27 +00002258 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2259 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002260 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2261 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2262 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002263 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002264 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2265 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002266 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002267 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002268 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002269 case AttributeList::AT_transparent_union:
2270 HandleTransparentUnionAttr(D, Attr, S);
2271 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002272 case AttributeList::AT_objc_exception:
2273 HandleObjCExceptionAttr(D, Attr, S);
2274 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002275 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002276 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2277 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2278 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2279 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2280 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2281 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2282 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2283 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2284 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002285 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002286 // Just ignore
2287 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002288 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2289 HandleNoInstrumentFunctionAttr(D, Attr, S);
2290 break;
John McCall04a67a62010-02-05 21:31:56 +00002291 case AttributeList::AT_stdcall:
2292 case AttributeList::AT_cdecl:
2293 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002294 case AttributeList::AT_thiscall:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002295 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002296 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002297 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002298 // Ask target about the attribute.
2299 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2300 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002301 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2302 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002303 break;
2304 }
2305}
2306
2307/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2308/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002309void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002310 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2311 ProcessDeclAttribute(S, D, *l, *this);
2312 }
2313
2314 // GCC accepts
2315 // static int a9 __attribute__((weakref));
2316 // but that looks really pointless. We reject it.
2317 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2318 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002319 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002320 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002321 }
2322}
2323
Ryan Flynne25ff832009-07-30 03:15:39 +00002324/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2325/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002326NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002327 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002328 NamedDecl *NewD = 0;
2329 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2330 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2331 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002332 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002333 if (FD->getQualifier()) {
2334 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2335 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2336 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002337 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2338 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2339 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002340 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002341 VD->getStorageClass(),
2342 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002343 if (VD->getQualifier()) {
2344 VarDecl *NewVD = cast<VarDecl>(NewD);
2345 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2346 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002347 }
2348 return NewD;
2349}
2350
2351/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2352/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002353void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002354 if (W.getUsed()) return; // only do this once
2355 W.setUsed(true);
2356 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2357 IdentifierInfo *NDId = ND->getIdentifier();
2358 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002359 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2360 NDId->getName()));
2361 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002362 WeakTopLevelDecl.push_back(NewD);
2363 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2364 // to insert Decl at TU scope, sorry.
2365 DeclContext *SavedContext = CurContext;
2366 CurContext = Context.getTranslationUnitDecl();
2367 PushOnScopeChains(NewD, S);
2368 CurContext = SavedContext;
2369 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002370 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002371 }
2372}
2373
Chris Lattner0744e5f2008-06-29 00:23:49 +00002374/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2375/// it, apply them to D. This is a bit tricky because PD can have attributes
2376/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002377void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynne25ff832009-07-30 03:15:39 +00002378 // Handle #pragma weak
2379 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2380 if (ND->hasLinkage()) {
2381 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2382 if (W != WeakInfo()) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002383 // Identifier referenced by #pragma weak before it was declared
2384 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynne25ff832009-07-30 03:15:39 +00002385 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2386 }
2387 }
2388 }
2389
Chris Lattner0744e5f2008-06-29 00:23:49 +00002390 // Apply decl attributes from the DeclSpec if present.
2391 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002392 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002393
Chris Lattner0744e5f2008-06-29 00:23:49 +00002394 // Walk the declarator structure, applying decl attributes that were in a type
2395 // position to the decl itself. This handles cases like:
2396 // int *__attr__(x)** D;
2397 // when X is a decl attribute.
2398 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2399 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002400 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002401
Chris Lattner0744e5f2008-06-29 00:23:49 +00002402 // Finally, apply any attributes on the decl itself.
2403 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002404 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002405}
John McCall54abf7d2009-11-04 02:18:39 +00002406
2407/// PushParsingDeclaration - Enter a new "scope" of deprecation
2408/// warnings.
2409///
2410/// The state token we use is the start index of this scope
2411/// on the warning stack.
2412Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2413 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002414 return (ParsingDeclStackState) DelayedDiagnostics.size();
2415}
2416
2417void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2418 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2419 ParsingDeclDepth--;
2420
2421 if (DelayedDiagnostics.empty())
2422 return;
2423
2424 unsigned SavedIndex = (unsigned) S;
2425 assert(SavedIndex <= DelayedDiagnostics.size() &&
2426 "saved index is out of bounds");
2427
John McCall58e6f342010-03-16 05:22:47 +00002428 unsigned E = DelayedDiagnostics.size();
2429
John McCall2f514482010-01-27 03:50:35 +00002430 // We only want to actually emit delayed diagnostics when we
2431 // successfully parsed a decl.
2432 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2433 if (D) {
2434 // We really do want to start with 0 here. We get one push for a
2435 // decl spec and another for each declarator; in a decl group like:
2436 // deprecated_typedef foo, *bar, baz();
2437 // only the declarator pops will be passed decls. This is correct;
2438 // we really do need to consider delayed diagnostics from the decl spec
2439 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002440 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002441 if (DelayedDiagnostics[I].Triggered)
2442 continue;
2443
2444 switch (DelayedDiagnostics[I].Kind) {
2445 case DelayedDiagnostic::Deprecation:
2446 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2447 break;
2448
2449 case DelayedDiagnostic::Access:
2450 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2451 break;
2452 }
2453 }
2454 }
2455
John McCall58e6f342010-03-16 05:22:47 +00002456 // Destroy all the delayed diagnostics we're about to pop off.
2457 for (unsigned I = SavedIndex; I != E; ++I)
2458 DelayedDiagnostics[I].destroy();
2459
John McCall2f514482010-01-27 03:50:35 +00002460 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002461}
2462
2463static bool isDeclDeprecated(Decl *D) {
2464 do {
2465 if (D->hasAttr<DeprecatedAttr>())
2466 return true;
2467 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2468 return false;
2469}
2470
John McCall2f514482010-01-27 03:50:35 +00002471void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2472 Decl *Ctx) {
2473 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002474 return;
2475
John McCall2f514482010-01-27 03:50:35 +00002476 DD.Triggered = true;
2477 Diag(DD.Loc, diag::warn_deprecated)
2478 << DD.DeprecationData.Decl->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002479}
2480
2481void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2482 // Delay if we're currently parsing a declaration.
2483 if (ParsingDeclDepth) {
John McCall2f514482010-01-27 03:50:35 +00002484 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall54abf7d2009-11-04 02:18:39 +00002485 return;
2486 }
2487
2488 // Otherwise, don't warn if our current context is deprecated.
2489 if (isDeclDeprecated(cast<Decl>(CurContext)))
2490 return;
2491
2492 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2493}