blob: fac47db7f936de7d50f53552b5c55e967c3da288 [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
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.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"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000024using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000026
Chris Lattnere5c5ee12008-06-29 00:16:31 +000027//===----------------------------------------------------------------------===//
28// Helper functions
29//===----------------------------------------------------------------------===//
30
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031static const FunctionType *getFunctionType(const Decl *d,
32 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000033 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000034 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000035 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000036 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000037 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000038 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000039 Ty = decl->getUnderlyingType();
40 else
41 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000042
Chris Lattner6b6b5372008-06-26 18:38:35 +000043 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000044 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000045 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000046 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000047
John McCall183700f2009-09-21 23:43:11 +000048 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000049}
50
Daniel Dunbar35682492008-09-26 04:12:28 +000051// FIXME: We should provide an abstraction around a method or function
52// to provide the following bits of information.
53
Nuno Lopesd20254f2009-12-20 23:11:08 +000054/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055/// type (function or function-typed variable).
56static bool isFunction(const Decl *d) {
57 return getFunctionType(d, false) != NULL;
58}
59
60/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000061/// type (function or function-typed variable) or an Objective-C
62/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000063static bool isFunctionOrMethod(const Decl *d) {
64 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000065}
66
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000067/// isFunctionOrMethodOrBlock - Return true if the given decl has function
68/// type (function or function-typed variable) or an Objective-C
69/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000070static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000071 if (isFunctionOrMethod(d))
72 return true;
73 // check for block is more involved.
74 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
75 QualType Ty = V->getType();
76 return Ty->isBlockPointerType();
77 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000078 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000079}
80
Daniel Dunbard3f2c102008-10-19 02:04:16 +000081/// hasFunctionProto - Return true if the given decl has a argument
82/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000083/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000084static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000085 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000086 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000087 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000088 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000089 return true;
90 }
91}
92
93/// getFunctionOrMethodNumArgs - Return number of function or method
94/// arguments. It is an error to call this on a K&R function (use
95/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000096static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000097 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000098 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000099 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
100 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +0000101 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000102}
103
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000104static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000105 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000106 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000107 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
108 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000109
Chris Lattner89951a82009-02-20 18:43:26 +0000110 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000111}
112
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000113static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000114 if (const FunctionType *FnTy = getFunctionType(d))
115 return cast<FunctionProtoType>(FnTy)->getResultType();
116 return cast<ObjCMethodDecl>(d)->getResultType();
117}
118
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000119static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000121 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000122 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000123 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000124 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000125 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000126 return cast<ObjCMethodDecl>(d)->isVariadic();
127 }
128}
129
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000130static bool isInstanceMethod(const Decl *d) {
131 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d))
132 return MethodDecl->isInstance();
133 return false;
134}
135
Chris Lattner6b6b5372008-06-26 18:38:35 +0000136static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000137 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000138 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000139 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000140
John McCall506b57e2010-05-17 21:00:27 +0000141 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
142 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000143 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000144
John McCall506b57e2010-05-17 21:00:27 +0000145 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000146
Chris Lattner6b6b5372008-06-26 18:38:35 +0000147 // FIXME: Should we walk the chain of classes?
148 return ClsName == &Ctx.Idents.get("NSString") ||
149 ClsName == &Ctx.Idents.get("NSMutableString");
150}
151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000153 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000154 if (!PT)
155 return false;
156
Ted Kremenek6217b802009-07-29 21:53:49 +0000157 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000158 if (!RT)
159 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000160
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000161 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000162 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000163 return false;
164
165 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
166}
167
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000168//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000169// Attribute Implementations
170//===----------------------------------------------------------------------===//
171
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000172// FIXME: All this manual attribute parsing code is gross. At the
173// least add some helper functions to check most argument patterns (#
174// and types of args).
175
Mike Stumpbf916502009-07-24 19:02:52 +0000176static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000177 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000178 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
179 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000180 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000181 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000182 }
Mike Stumpbf916502009-07-24 19:02:52 +0000183
Chris Lattner6b6b5372008-06-26 18:38:35 +0000184 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185
186 Expr *sizeExpr;
187
188 // Special case where the argument is a template id.
189 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000190 CXXScopeSpec SS;
191 UnqualifiedId id;
192 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
193 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000194 } else {
195 // check the attribute arguments.
196 if (Attr.getNumArgs() != 1) {
197 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
198 return;
199 }
Peter Collingbourne7a730022010-11-23 20:45:58 +0000200 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000201 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000202
203 // Instantiate/Install the vector type, and let Sema build the type for us.
204 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000205 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000206 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000207 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000208 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000209
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000210 // Remember this typedef decl, we will need it later for diagnostics.
211 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000212 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213}
214
Chris Lattner803d0802008-06-29 00:43:07 +0000215static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000216 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000217 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000218 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000219 return;
220 }
Mike Stumpbf916502009-07-24 19:02:52 +0000221
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Sean Huntcf807c42010-08-18 23:23:40 +0000223 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
225 // If the alignment is less than or equal to 8 bits, the packed attribute
226 // has no effect.
227 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000228 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000229 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000230 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000231 else
Sean Huntcf807c42010-08-18 23:23:40 +0000232 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000233 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000234 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000235}
236
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000237static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000238 // check the attribute arguments.
239 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000241 return;
242 }
Mike Stumpbf916502009-07-24 19:02:52 +0000243
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000244 // The IBAction attributes only apply to instance methods.
245 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
246 if (MD->isInstanceMethod()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000247 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000248 return;
249 }
250
251 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
252}
253
254static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
255 // check the attribute arguments.
256 if (Attr.getNumArgs() > 0) {
257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
258 return;
259 }
260
261 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000262 // Objective-C classes.
263 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000264 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000265 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000266 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000267
268 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000269}
270
Ted Kremenek857e9182010-05-19 17:38:06 +0000271static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
272 Sema &S) {
273
274 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000275 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000276 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
277 return;
278 }
279
280 // The IBOutletCollection attributes only apply to instance variables of
281 // Objective-C classes.
282 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
283 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
284 return;
285 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000286 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
287 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
288 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
289 << VD->getType() << 0;
290 return;
291 }
292 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
293 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
294 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
295 << PD->getType() << 1;
296 return;
297 }
298
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000299 IdentifierInfo *II = Attr.getParameterName();
300 if (!II)
301 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000302
John McCallb3d87482010-08-24 05:47:05 +0000303 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000304 S.getScopeForContext(d->getDeclContext()->getParent()));
305 if (!TypeRep) {
306 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
307 return;
308 }
John McCallb3d87482010-08-24 05:47:05 +0000309 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000310 // Diagnose use of non-object type in iboutletcollection attribute.
311 // FIXME. Gnu attribute extension ignores use of builtin types in
312 // attributes. So, __attribute__((iboutletcollection(char))) will be
313 // treated as __attribute__((iboutletcollection())).
314 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
315 !QT->isObjCObjectType()) {
316 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
317 return;
318 }
Sean Huntcf807c42010-08-18 23:23:40 +0000319 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
320 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000321}
322
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000323static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000324 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
325 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000326 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000327 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000328 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329 return;
330 }
Mike Stumpbf916502009-07-24 19:02:52 +0000331
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000332 // In C++ the implicit 'this' function parameter also counts, and they are
333 // counted from one.
334 bool HasImplicitThisParam = isInstanceMethod(d);
335 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336
337 // The nonnull attribute only applies to pointers.
338 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000339
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 for (AttributeList::arg_iterator I=Attr.arg_begin(),
341 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000342
343
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000344 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000345 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000346 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000347 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
348 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000349 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
350 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351 return;
352 }
Mike Stumpbf916502009-07-24 19:02:52 +0000353
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000354 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000355
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000356 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000358 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000359 return;
360 }
Mike Stumpbf916502009-07-24 19:02:52 +0000361
Ted Kremenek465172f2008-07-21 22:09:15 +0000362 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000363 if (HasImplicitThisParam) {
364 if (x == 0) {
365 S.Diag(Attr.getLoc(),
366 diag::err_attribute_invalid_implicit_this_argument)
367 << "nonnull" << Ex->getSourceRange();
368 return;
369 }
370 --x;
371 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000372
373 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000374 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000375 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000376 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000377 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000378 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000379 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000380 }
Mike Stumpbf916502009-07-24 19:02:52 +0000381
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000382 NonNullArgs.push_back(x);
383 }
Mike Stumpbf916502009-07-24 19:02:52 +0000384
385 // If no arguments were specified to __attribute__((nonnull)) then all pointer
386 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000387 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000388 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
389 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000390 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000391 NonNullArgs.push_back(I);
Fariborz Jahanianff3a0782010-09-27 22:42:37 +0000392 else if (const RecordType *UT = T->getAsUnionType()) {
393 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
394 RecordDecl *UD = UT->getDecl();
395 for (RecordDecl::field_iterator it = UD->field_begin(),
396 itend = UD->field_end(); it != itend; ++it) {
397 T = it->getType();
398 if (T->isAnyPointerType() || T->isBlockPointerType()) {
399 NonNullArgs.push_back(I);
400 break;
401 }
402 }
403 }
404 }
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000405 }
Mike Stumpbf916502009-07-24 19:02:52 +0000406
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000407 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000408 if (NonNullArgs.empty()) {
409 // Warn the trivial case only if attribute is not coming from a
410 // macro instantiation.
411 if (Attr.getLoc().isFileID())
412 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000413 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000414 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000415 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000416
417 unsigned* start = &NonNullArgs[0];
418 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000419 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000420 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
421 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000422}
423
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000424static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
425 // This attribute must be applied to a function declaration.
426 // The first argument to the attribute must be a string,
427 // the name of the resource, for example "malloc".
428 // The following arguments must be argument indexes, the arguments must be
429 // of integer type for Returns, otherwise of pointer type.
430 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000431 // after being held. free() should be __attribute((ownership_takes)), whereas
432 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000433
434 if (!AL.getParameterName()) {
435 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
436 << AL.getName()->getName() << 1;
437 return;
438 }
439 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000440 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000441 switch (AL.getKind()) {
442 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000443 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000444 if (AL.getNumArgs() < 1) {
445 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
446 return;
447 }
Jordy Rose2a479922010-08-12 08:54:03 +0000448 break;
449 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000450 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000451 if (AL.getNumArgs() < 1) {
452 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
453 return;
454 }
Jordy Rose2a479922010-08-12 08:54:03 +0000455 break;
456 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000457 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000458 if (AL.getNumArgs() > 1) {
459 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
460 << AL.getNumArgs() + 1;
461 return;
462 }
Jordy Rose2a479922010-08-12 08:54:03 +0000463 break;
464 default:
465 // This should never happen given how we are called.
466 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000467 }
468
469 if (!isFunction(d) || !hasFunctionProto(d)) {
470 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
471 << 0 /*function*/;
472 return;
473 }
474
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000475 // In C++ the implicit 'this' function parameter also counts, and they are
476 // counted from one.
477 bool HasImplicitThisParam = isInstanceMethod(d);
478 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000479
480 llvm::StringRef Module = AL.getParameterName()->getName();
481
482 // Normalize the argument, __foo__ becomes foo.
483 if (Module.startswith("__") && Module.endswith("__"))
484 Module = Module.substr(2, Module.size() - 4);
485
486 llvm::SmallVector<unsigned, 10> OwnershipArgs;
487
Jordy Rose2a479922010-08-12 08:54:03 +0000488 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
489 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000490
Peter Collingbourne7a730022010-11-23 20:45:58 +0000491 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000492 llvm::APSInt ArgNum(32);
493 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
494 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
495 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
496 << AL.getName()->getName() << IdxExpr->getSourceRange();
497 continue;
498 }
499
500 unsigned x = (unsigned) ArgNum.getZExtValue();
501
502 if (x > NumArgs || x < 1) {
503 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
504 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
505 continue;
506 }
507 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000508 if (HasImplicitThisParam) {
509 if (x == 0) {
510 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
511 << "ownership" << IdxExpr->getSourceRange();
512 return;
513 }
514 --x;
515 }
516
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000517 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000518 case OwnershipAttr::Takes:
519 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000520 // Is the function argument a pointer type?
521 QualType T = getFunctionOrMethodArgType(d, x);
522 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
523 // FIXME: Should also highlight argument in decl.
524 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000525 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000526 << "pointer"
527 << IdxExpr->getSourceRange();
528 continue;
529 }
530 break;
531 }
Sean Huntcf807c42010-08-18 23:23:40 +0000532 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000533 if (AL.getNumArgs() > 1) {
534 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000535 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000536 llvm::APSInt ArgNum(32);
537 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
538 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
539 S.Diag(AL.getLoc(), diag::err_ownership_type)
540 << "ownership_returns" << "integer"
541 << IdxExpr->getSourceRange();
542 return;
543 }
544 }
545 break;
546 }
Jordy Rose2a479922010-08-12 08:54:03 +0000547 default:
548 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000549 } // switch
550
551 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000552 for (specific_attr_iterator<OwnershipAttr>
553 i = d->specific_attr_begin<OwnershipAttr>(),
554 e = d->specific_attr_end<OwnershipAttr>();
555 i != e; ++i) {
556 if ((*i)->getOwnKind() != K) {
557 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
558 I!=E; ++I) {
559 if (x == *I) {
560 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
561 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000562 }
563 }
564 }
565 }
566 OwnershipArgs.push_back(x);
567 }
568
569 unsigned* start = OwnershipArgs.data();
570 unsigned size = OwnershipArgs.size();
571 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000572
573 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
574 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
575 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000576 }
Sean Huntcf807c42010-08-18 23:23:40 +0000577
578 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
579 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000580}
581
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000582static bool isStaticVarOrStaticFunciton(Decl *D) {
583 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000584 return VD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000585 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000586 return FD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000587 return false;
588}
589
590static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
591 // Check the attribute arguments.
592 if (Attr.getNumArgs() > 1) {
593 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
594 return;
595 }
596
597 // gcc rejects
598 // class c {
599 // static int a __attribute__((weakref ("v2")));
600 // static int b() __attribute__((weakref ("f3")));
601 // };
602 // and ignores the attributes of
603 // void f(void) {
604 // static int a __attribute__((weakref ("v2")));
605 // }
606 // we reject them
Sebastian Redl7a126a42010-08-31 00:36:30 +0000607 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
608 if (!Ctx->isFileContext()) {
609 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
610 dyn_cast<NamedDecl>(d)->getNameAsString();
611 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000612 }
613
614 // The GCC manual says
615 //
616 // At present, a declaration to which `weakref' is attached can only
617 // be `static'.
618 //
619 // It also says
620 //
621 // Without a TARGET,
622 // given as an argument to `weakref' or to `alias', `weakref' is
623 // equivalent to `weak'.
624 //
625 // gcc 4.4.1 will accept
626 // int a7 __attribute__((weakref));
627 // as
628 // int a7 __attribute__((weak));
629 // This looks like a bug in gcc. We reject that for now. We should revisit
630 // it if this behaviour is actually used.
631
632 if (!isStaticVarOrStaticFunciton(d)) {
633 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
634 dyn_cast<NamedDecl>(d)->getNameAsString();
635 return;
636 }
637
638 // GCC rejects
639 // static ((alias ("y"), weakref)).
640 // Should we? How to check that weakref is before or after alias?
641
642 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000643 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000644 Arg = Arg->IgnoreParenCasts();
645 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
646
647 if (Str == 0 || Str->isWide()) {
648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
649 << "weakref" << 1;
650 return;
651 }
652 // GCC will accept anything as the argument of weakref. Should we
653 // check for an existing decl?
Sean Huntcf807c42010-08-18 23:23:40 +0000654 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000655 }
656
Sean Huntcf807c42010-08-18 23:23:40 +0000657 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000658}
659
Chris Lattner803d0802008-06-29 00:43:07 +0000660static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000661 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000662 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000663 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000664 return;
665 }
Mike Stumpbf916502009-07-24 19:02:52 +0000666
Peter Collingbourne7a730022010-11-23 20:45:58 +0000667 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000668 Arg = Arg->IgnoreParenCasts();
669 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000670
Chris Lattner6b6b5372008-06-26 18:38:35 +0000671 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000672 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000673 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000674 return;
675 }
Mike Stumpbf916502009-07-24 19:02:52 +0000676
Chris Lattner6b6b5372008-06-26 18:38:35 +0000677 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000678
Sean Huntcf807c42010-08-18 23:23:40 +0000679 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000680}
681
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000682static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000683 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000684 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000685 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000686 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000687 return;
688 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000689
Chris Lattnerc5197432009-04-14 17:02:11 +0000690 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000691 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000692 << Attr.getName() << 0 /*function*/;
693 return;
694 }
695
696 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
697}
698
699static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
700 Sema &S) {
701 // Check the attribute arguments.
702 if (Attr.getNumArgs() != 0) {
703 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
704 return;
705 }
706
707 if (!isa<FunctionDecl>(d)) {
708 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
709 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000710 return;
711 }
Mike Stumpbf916502009-07-24 19:02:52 +0000712
Sean Huntcf807c42010-08-18 23:23:40 +0000713 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000714}
715
Ryan Flynn76168e22009-08-09 20:07:29 +0000716static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000717 // Check the attribute arguments.
Ryan Flynn76168e22009-08-09 20:07:29 +0000718 if (Attr.getNumArgs() != 0) {
719 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
720 return;
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000723 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000724 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000725 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000726 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000727 return;
728 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000729 }
730
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000731 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000732}
733
Dan Gohman34c26302010-11-17 00:03:07 +0000734static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
735 // check the attribute arguments.
736 if (Attr.getNumArgs() != 0) {
737 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
738 return;
739 }
740
Dan Gohman34c26302010-11-17 00:03:07 +0000741 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
742}
743
Ted Kremenekb7252322009-04-10 00:01:14 +0000744static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000745 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
746 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000747 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000748}
749
750static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
751 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000752
753 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
754 // because 'analyzer_noreturn' does not impact the type.
755
756 if (Attr.getNumArgs() != 0) {
757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
758 return;
759 }
760
761 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
762 ValueDecl *VD = dyn_cast<ValueDecl>(d);
763 if (VD == 0 || (!VD->getType()->isBlockPointerType()
764 && !VD->getType()->isFunctionPointerType())) {
765 S.Diag(Attr.getLoc(),
766 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
767 : diag::warn_attribute_wrong_decl_type)
768 << Attr.getName() << 0 /*function*/;
769 return;
770 }
771 }
772
773 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000774}
775
John Thompson35cc9622010-08-09 21:53:52 +0000776// PS3 PPU-specific.
777static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
778 Sema &S) {
779/*
780 Returning a Vector Class in Registers
781
782 According to the PPU ABI specifications, a class with a single member of vector type is returned in
783 memory when used as the return value of a function. This results in inefficient code when implementing
784 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
785 definition. This attribute is also applicable to struct types.
786
787 Example:
788
789 struct Vector
790 {
791 __vector float xyzw;
792 } __attribute__((vecreturn));
793
794 Vector Add(Vector lhs, Vector rhs)
795 {
796 Vector result;
797 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
798 return result; // This will be returned in a register
799 }
800*/
John Thompson01add592010-09-18 01:12:07 +0000801 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000802 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
803 << Attr.getName() << 9 /*class*/;
804 return;
805 }
806
807 if (d->getAttr<VecReturnAttr>()) {
808 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
809 return;
810 }
811
John Thompson01add592010-09-18 01:12:07 +0000812 RecordDecl *record = cast<RecordDecl>(d);
813 int count = 0;
814
815 if (!isa<CXXRecordDecl>(record)) {
816 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
817 return;
818 }
819
820 if (!cast<CXXRecordDecl>(record)->isPOD()) {
821 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
822 return;
823 }
824
825 for (RecordDecl::field_iterator iter = record->field_begin(); iter != record->field_end(); iter++) {
826 if ((count == 1) || !iter->getType()->isVectorType()) {
827 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
828 return;
829 }
830 count++;
831 }
832
Sean Huntcf807c42010-08-18 23:23:40 +0000833 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000834}
835
Sean Huntbbd37c62009-11-21 08:43:09 +0000836static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
837 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
838 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000839 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000840 return;
841 }
842 // FIXME: Actually store the attribute on the declaration
843}
844
Ted Kremenek73798892008-07-25 04:39:19 +0000845static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
846 // check the attribute arguments.
847 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000848 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000849 return;
850 }
Mike Stumpbf916502009-07-24 19:02:52 +0000851
John McCallaec58602010-03-31 02:47:45 +0000852 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
853 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000854 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000855 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000856 return;
857 }
Mike Stumpbf916502009-07-24 19:02:52 +0000858
Sean Huntcf807c42010-08-18 23:23:40 +0000859 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000860}
861
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000862static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
863 // check the attribute arguments.
864 if (Attr.getNumArgs() != 0) {
865 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
866 return;
867 }
Mike Stumpbf916502009-07-24 19:02:52 +0000868
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000869 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000870 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000871 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
872 return;
873 }
874 } else if (!isFunctionOrMethod(d)) {
875 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000876 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000877 return;
878 }
Mike Stumpbf916502009-07-24 19:02:52 +0000879
Sean Huntcf807c42010-08-18 23:23:40 +0000880 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000881}
882
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000883static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
884 // check the attribute arguments.
885 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000886 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
887 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000888 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000889 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000890
891 int priority = 65535; // FIXME: Do not hardcode such constants.
892 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000893 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000894 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000895 if (E->isTypeDependent() || E->isValueDependent() ||
896 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000897 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000898 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000899 return;
900 }
901 priority = Idx.getZExtValue();
902 }
Mike Stumpbf916502009-07-24 19:02:52 +0000903
Chris Lattnerc5197432009-04-14 17:02:11 +0000904 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000905 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000906 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000907 return;
908 }
909
Sean Huntcf807c42010-08-18 23:23:40 +0000910 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000911}
912
913static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
914 // check the attribute arguments.
915 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
917 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000918 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000919 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000920
921 int priority = 65535; // FIXME: Do not hardcode such constants.
922 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000923 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000924 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000925 if (E->isTypeDependent() || E->isValueDependent() ||
926 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000927 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000928 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000929 return;
930 }
931 priority = Idx.getZExtValue();
932 }
Mike Stumpbf916502009-07-24 19:02:52 +0000933
Anders Carlsson6782fc62008-08-22 22:10:48 +0000934 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000935 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000936 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000937 return;
938 }
939
Sean Huntcf807c42010-08-18 23:23:40 +0000940 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000941}
942
Chris Lattner803d0802008-06-29 00:43:07 +0000943static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000944 // check the attribute arguments.
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000945 int noArgs = Attr.getNumArgs();
946 if (noArgs > 1) {
947 S.Diag(Attr.getLoc(),
948 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000949 return;
950 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000951 // Handle the case where deprecated attribute has a text message.
952 StringLiteral *SE;
953 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000954 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000955 SE = dyn_cast<StringLiteral>(ArgExpr);
956 if (!SE) {
957 S.Diag(ArgExpr->getLocStart(),
958 diag::err_attribute_not_string) << "deprecated";
959 return;
960 }
961 }
962 else
963 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpbf916502009-07-24 19:02:52 +0000964
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000965 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
966 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000967}
968
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000969static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
970 // check the attribute arguments.
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000971 int noArgs = Attr.getNumArgs();
972 if (noArgs > 1) {
973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000974 return;
975 }
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000976 // Handle the case where unavailable attribute has a text message.
977 StringLiteral *SE;
978 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000979 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000980 SE = dyn_cast<StringLiteral>(ArgExpr);
981 if (!SE) {
982 S.Diag(ArgExpr->getLocStart(),
983 diag::err_attribute_not_string) << "unavailable";
984 return;
985 }
986 }
987 else
988 SE = StringLiteral::CreateEmpty(S.Context, 1);
989 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
990 SE->getString()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000991}
992
Chris Lattner803d0802008-06-29 00:43:07 +0000993static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000994 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000995 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000996 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000997 return;
998 }
Mike Stumpbf916502009-07-24 19:02:52 +0000999
Peter Collingbourne7a730022010-11-23 20:45:58 +00001000 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001001 Arg = Arg->IgnoreParenCasts();
1002 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001003
Chris Lattner6b6b5372008-06-26 18:38:35 +00001004 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001005 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001006 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001007 return;
1008 }
Mike Stumpbf916502009-07-24 19:02:52 +00001009
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001010 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001011 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001012
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001013 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001014 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001015 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001016 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001017 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001018 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001019 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001020 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001021 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001022 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001023 return;
1024 }
Mike Stumpbf916502009-07-24 19:02:52 +00001025
Sean Huntcf807c42010-08-18 23:23:40 +00001026 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027}
1028
Chris Lattner0db29ec2009-02-14 08:09:34 +00001029static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1030 Sema &S) {
1031 if (Attr.getNumArgs() != 0) {
1032 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1033 return;
1034 }
Mike Stumpbf916502009-07-24 19:02:52 +00001035
Chris Lattner0db29ec2009-02-14 08:09:34 +00001036 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1037 if (OCI == 0) {
1038 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1039 return;
1040 }
Mike Stumpbf916502009-07-24 19:02:52 +00001041
Sean Huntcf807c42010-08-18 23:23:40 +00001042 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001043}
1044
1045static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001046 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001047 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001048 return;
1049 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001050 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001051 QualType T = TD->getUnderlyingType();
1052 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001053 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001054 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1055 return;
1056 }
1057 }
Sean Huntcf807c42010-08-18 23:23:40 +00001058 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001059}
1060
Mike Stumpbf916502009-07-24 19:02:52 +00001061static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001062HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1063 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001065 return;
1066 }
1067
1068 if (!isa<FunctionDecl>(D)) {
1069 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1070 return;
1071 }
1072
Sean Huntcf807c42010-08-18 23:23:40 +00001073 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001074}
1075
Steve Naroff9eae5762008-09-18 16:44:58 +00001076static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001077 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001078 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001079 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001080 return;
1081 }
Mike Stumpbf916502009-07-24 19:02:52 +00001082
Steve Naroff9eae5762008-09-18 16:44:58 +00001083 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001084 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001085 return;
1086 }
Mike Stumpbf916502009-07-24 19:02:52 +00001087
Sean Huntcf807c42010-08-18 23:23:40 +00001088 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001089 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001090 type = BlocksAttr::ByRef;
1091 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001092 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001093 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001094 return;
1095 }
Mike Stumpbf916502009-07-24 19:02:52 +00001096
Sean Huntcf807c42010-08-18 23:23:40 +00001097 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001098}
1099
Anders Carlsson77091822008-10-05 18:05:59 +00001100static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1101 // check the attribute arguments.
1102 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001103 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1104 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001105 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001106 }
1107
Anders Carlsson77091822008-10-05 18:05:59 +00001108 int sentinel = 0;
1109 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001110 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001111 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001112 if (E->isTypeDependent() || E->isValueDependent() ||
1113 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001114 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001115 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001116 return;
1117 }
1118 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001119
Anders Carlsson77091822008-10-05 18:05:59 +00001120 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001121 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1122 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001123 return;
1124 }
1125 }
1126
1127 int nullPos = 0;
1128 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001129 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001130 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001131 if (E->isTypeDependent() || E->isValueDependent() ||
1132 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001133 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001134 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001135 return;
1136 }
1137 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001138
Anders Carlsson77091822008-10-05 18:05:59 +00001139 if (nullPos > 1 || nullPos < 0) {
1140 // FIXME: This error message could be improved, it would be nice
1141 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001142 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1143 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001144 return;
1145 }
1146 }
1147
1148 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001149 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001150 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001151
Chris Lattner897cd902009-03-17 23:03:47 +00001152 if (isa<FunctionNoProtoType>(FT)) {
1153 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1154 return;
1155 }
Mike Stumpbf916502009-07-24 19:02:52 +00001156
Chris Lattner897cd902009-03-17 23:03:47 +00001157 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001158 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001159 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001160 }
Anders Carlsson77091822008-10-05 18:05:59 +00001161 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1162 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001163 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001164 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001165 }
1166 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001167 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1168 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001169 ;
1170 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001171 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001172 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001173 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001174 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001175 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001176 int m = Ty->isFunctionPointerType() ? 0 : 1;
1177 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001178 return;
1179 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001180 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001181 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001182 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001183 return;
1184 }
Anders Carlsson77091822008-10-05 18:05:59 +00001185 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001186 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001187 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001188 return;
1189 }
Sean Huntcf807c42010-08-18 23:23:40 +00001190 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001191}
1192
Chris Lattner026dc962009-02-14 07:37:35 +00001193static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1194 // check the attribute arguments.
1195 if (Attr.getNumArgs() != 0) {
1196 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1197 return;
1198 }
1199
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001200 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001202 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001203 return;
1204 }
Mike Stumpbf916502009-07-24 19:02:52 +00001205
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001206 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1207 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1208 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001209 return;
1210 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001211 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1212 if (MD->getResultType()->isVoidType()) {
1213 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1214 << Attr.getName() << 1;
1215 return;
1216 }
1217
Sean Huntcf807c42010-08-18 23:23:40 +00001218 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001219}
1220
1221static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001222 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001223 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001225 return;
1226 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001227
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001228 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001229 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001230 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1231 dyn_cast<NamedDecl>(D)->getNameAsString();
1232 return;
1233 }
1234
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001235 // TODO: could also be applied to methods?
1236 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1237 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001238 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001239 return;
1240 }
Mike Stumpbf916502009-07-24 19:02:52 +00001241
Sean Huntcf807c42010-08-18 23:23:40 +00001242 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001243}
1244
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001245static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1246 // check the attribute arguments.
1247 if (Attr.getNumArgs() != 0) {
1248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1249 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001250 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001251
1252 // weak_import only applies to variable & function declarations.
1253 bool isDef = false;
1254 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1255 isDef = (!VD->hasExternalStorage() || VD->getInit());
1256 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001257 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001258 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1259 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001260 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001261 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001262 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001263 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001264 !isa<ObjCInterfaceDecl>(D))
1265 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001266 << Attr.getName() << 2 /*variable and function*/;
1267 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001268 }
1269
1270 // Merge should handle any subsequent violations.
1271 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001272 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001273 diag::warn_attribute_weak_import_invalid_on_definition)
1274 << "weak_import" << 2 /*variable and function*/;
1275 return;
1276 }
1277
Sean Huntcf807c42010-08-18 23:23:40 +00001278 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001279}
1280
Nate Begeman6f3d8382009-06-26 06:32:41 +00001281static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1282 Sema &S) {
1283 // Attribute has 3 arguments.
1284 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001285 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001286 return;
1287 }
1288
1289 unsigned WGSize[3];
1290 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001291 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001292 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001293 if (E->isTypeDependent() || E->isValueDependent() ||
1294 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001295 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1296 << "reqd_work_group_size" << E->getSourceRange();
1297 return;
1298 }
1299 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1300 }
Sean Huntcf807c42010-08-18 23:23:40 +00001301 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1302 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001303 WGSize[2]));
1304}
1305
Chris Lattner026dc962009-02-14 07:37:35 +00001306static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001307 // Attribute has no arguments.
1308 if (Attr.getNumArgs() != 1) {
1309 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1310 return;
1311 }
1312
1313 // Make sure that there is a string literal as the sections's single
1314 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001315 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001316 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001317 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001318 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001319 return;
1320 }
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Chris Lattner797c3c42009-08-10 19:03:04 +00001322 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001323 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001324 if (!Error.empty()) {
1325 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1326 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001327 return;
1328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001330 // This attribute cannot be applied to local variables.
1331 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1332 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1333 return;
1334 }
1335
Sean Huntcf807c42010-08-18 23:23:40 +00001336 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001337}
1338
Chris Lattner6b6b5372008-06-26 18:38:35 +00001339
Chris Lattner803d0802008-06-29 00:43:07 +00001340static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001341 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001342 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001343 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001344 return;
1345 }
Mike Stumpbf916502009-07-24 19:02:52 +00001346
Sean Huntcf807c42010-08-18 23:23:40 +00001347 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001348}
1349
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001350static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1351 // check the attribute arguments.
1352 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001353 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001354 return;
1355 }
Mike Stumpbf916502009-07-24 19:02:52 +00001356
Sean Huntcf807c42010-08-18 23:23:40 +00001357 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001358}
1359
1360static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1361 // check the attribute arguments.
1362 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001363 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001364 return;
1365 }
Mike Stumpbf916502009-07-24 19:02:52 +00001366
Sean Huntcf807c42010-08-18 23:23:40 +00001367 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001368}
1369
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001370static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001371 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1373 return;
1374 }
Mike Stumpbf916502009-07-24 19:02:52 +00001375
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001376 if (Attr.getNumArgs() != 0) {
1377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1378 return;
1379 }
Mike Stumpbf916502009-07-24 19:02:52 +00001380
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001381 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001382
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001383 if (!VD || !VD->hasLocalStorage()) {
1384 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1385 return;
1386 }
Mike Stumpbf916502009-07-24 19:02:52 +00001387
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001388 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001389 // FIXME: Lookup probably isn't looking in the right place
1390 // FIXME: The lookup source location should be in the attribute, not the
1391 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001392 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001393 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001394 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001395 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001396 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001397 Attr.getParameterName();
1398 return;
1399 }
Mike Stumpbf916502009-07-24 19:02:52 +00001400
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001401 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1402 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001403 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001404 Attr.getParameterName();
1405 return;
1406 }
1407
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001408 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001409 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001410 Attr.getParameterName();
1411 return;
1412 }
Mike Stumpbf916502009-07-24 19:02:52 +00001413
Anders Carlsson89941c12009-02-07 23:16:50 +00001414 // We're currently more strict than GCC about what function types we accept.
1415 // If this ever proves to be a problem it should be easy to fix.
1416 QualType Ty = S.Context.getPointerType(VD->getType());
1417 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall1c23e912010-11-16 02:32:08 +00001418 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001419 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001420 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1421 Attr.getParameterName() << ParamTy << Ty;
1422 return;
1423 }
Mike Stumpbf916502009-07-24 19:02:52 +00001424
Sean Huntcf807c42010-08-18 23:23:40 +00001425 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001426}
1427
Mike Stumpbf916502009-07-24 19:02:52 +00001428/// Handle __attribute__((format_arg((idx)))) attribute based on
1429/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1430static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001431 if (Attr.getNumArgs() != 1) {
1432 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1433 return;
1434 }
1435 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1436 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1437 << Attr.getName() << 0 /*function*/;
1438 return;
1439 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001440
1441 // In C++ the implicit 'this' function parameter also counts, and they are
1442 // counted from one.
1443 bool HasImplicitThisParam = isInstanceMethod(d);
1444 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001445 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001446
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001447 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001448 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001449 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001450 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1451 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001452 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1453 << "format" << 2 << IdxExpr->getSourceRange();
1454 return;
1455 }
Mike Stumpbf916502009-07-24 19:02:52 +00001456
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001457 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1458 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1459 << "format" << 2 << IdxExpr->getSourceRange();
1460 return;
1461 }
Mike Stumpbf916502009-07-24 19:02:52 +00001462
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001463 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001464
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001465 if (HasImplicitThisParam) {
1466 if (ArgIdx == 0) {
1467 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1468 << "format_arg" << IdxExpr->getSourceRange();
1469 return;
1470 }
1471 ArgIdx--;
1472 }
1473
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001474 // make sure the format string is really a string
1475 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001476
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001477 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1478 if (not_nsstring_type &&
1479 !isCFStringType(Ty, S.Context) &&
1480 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001481 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001482 // FIXME: Should highlight the actual expression that has the wrong type.
1483 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001484 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001485 << IdxExpr->getSourceRange();
1486 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001487 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001488 Ty = getFunctionOrMethodResultType(d);
1489 if (!isNSStringType(Ty, S.Context) &&
1490 !isCFStringType(Ty, S.Context) &&
1491 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001492 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001493 // FIXME: Should highlight the actual expression that has the wrong type.
1494 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001495 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001496 << IdxExpr->getSourceRange();
1497 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001498 }
1499
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001500 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1501 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001502}
1503
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001504enum FormatAttrKind {
1505 CFStringFormat,
1506 NSStringFormat,
1507 StrftimeFormat,
1508 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001509 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001510 InvalidFormat
1511};
1512
1513/// getFormatAttrKind - Map from format attribute names to supported format
1514/// types.
1515static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1516 // Check for formats that get handled specially.
1517 if (Format == "NSString")
1518 return NSStringFormat;
1519 if (Format == "CFString")
1520 return CFStringFormat;
1521 if (Format == "strftime")
1522 return StrftimeFormat;
1523
1524 // Otherwise, check for supported formats.
1525 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1526 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1527 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1528 Format == "zcmn_err")
1529 return SupportedFormat;
1530
Duncan Sandsbc525952010-03-23 14:44:19 +00001531 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1532 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001533 return IgnoredFormat;
1534
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001535 return InvalidFormat;
1536}
1537
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001538/// Handle __attribute__((init_priority(priority))) attributes based on
1539/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1540static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1541 Sema &S) {
1542 if (!S.getLangOptions().CPlusPlus) {
1543 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1544 return;
1545 }
1546
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001547 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1548 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1549 Attr.setInvalid();
1550 return;
1551 }
1552 QualType T = dyn_cast<VarDecl>(d)->getType();
1553 if (S.Context.getAsArrayType(T))
1554 T = S.Context.getBaseElementType(T);
1555 if (!T->getAs<RecordType>()) {
1556 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1557 Attr.setInvalid();
1558 return;
1559 }
1560
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001561 if (Attr.getNumArgs() != 1) {
1562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1563 Attr.setInvalid();
1564 return;
1565 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001566 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001567
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001568 llvm::APSInt priority(32);
1569 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1570 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1571 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1572 << "init_priority" << priorityExpr->getSourceRange();
1573 Attr.setInvalid();
1574 return;
1575 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001576 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001577 if (prioritynum < 101 || prioritynum > 65535) {
1578 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1579 << priorityExpr->getSourceRange();
1580 Attr.setInvalid();
1581 return;
1582 }
Sean Huntcf807c42010-08-18 23:23:40 +00001583 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001584}
1585
Mike Stumpbf916502009-07-24 19:02:52 +00001586/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1587/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001588static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001589
Chris Lattner545dd342008-06-28 23:36:30 +00001590 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001591 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001592 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001593 return;
1594 }
1595
Chris Lattner545dd342008-06-28 23:36:30 +00001596 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001597 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001598 return;
1599 }
1600
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001601 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001602 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001603 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001604 return;
1605 }
1606
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001607 // In C++ the implicit 'this' function parameter also counts, and they are
1608 // counted from one.
1609 bool HasImplicitThisParam = isInstanceMethod(d);
1610 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001611 unsigned FirstIdx = 1;
1612
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001613 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001614
1615 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001616 if (Format.startswith("__") && Format.endswith("__"))
1617 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001618
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001619 // Check for supported formats.
1620 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001621
1622 if (Kind == IgnoredFormat)
1623 return;
1624
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001625 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001626 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001627 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001628 return;
1629 }
1630
1631 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001632 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001633 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001634 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1635 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001637 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001638 return;
1639 }
1640
1641 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001642 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001643 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001644 return;
1645 }
1646
1647 // FIXME: Do we need to bounds check?
1648 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001649
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001650 if (HasImplicitThisParam) {
1651 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001652 S.Diag(Attr.getLoc(),
1653 diag::err_format_attribute_implicit_this_format_string)
1654 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001655 return;
1656 }
1657 ArgIdx--;
1658 }
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Chris Lattner6b6b5372008-06-26 18:38:35 +00001660 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001661 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001662
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001663 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001664 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001665 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1666 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001667 return;
1668 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001669 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001670 // FIXME: do we need to check if the type is NSString*? What are the
1671 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001672 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001673 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001674 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1675 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001676 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001677 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001678 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001679 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001680 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001681 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1682 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001683 return;
1684 }
1685
1686 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001687 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001688 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001689 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1690 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001691 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001692 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001693 return;
1694 }
1695
1696 // check if the function is variadic if the 3rd argument non-zero
1697 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001698 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001699 ++NumArgs; // +1 for ...
1700 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001701 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001702 return;
1703 }
1704 }
1705
Chris Lattner3c73c412008-11-19 08:23:25 +00001706 // strftime requires FirstArg to be 0 because it doesn't read from any
1707 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001708 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001709 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001710 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1711 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001712 return;
1713 }
1714 // if 0 it disables parameter checking (to use with e.g. va_list)
1715 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001716 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001717 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001718 return;
1719 }
1720
Sean Huntcf807c42010-08-18 23:23:40 +00001721 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1722 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001723 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001724}
1725
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001726static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1727 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001728 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001729 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001730 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001731 return;
1732 }
1733
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001734 // Try to find the underlying union declaration.
1735 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001736 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001737 if (TD && TD->getUnderlyingType()->isUnionType())
1738 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1739 else
1740 RD = dyn_cast<RecordDecl>(d);
1741
1742 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001743 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001744 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001745 return;
1746 }
1747
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001748 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001749 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001750 diag::warn_transparent_union_attribute_not_definition);
1751 return;
1752 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001753
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001754 RecordDecl::field_iterator Field = RD->field_begin(),
1755 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001756 if (Field == FieldEnd) {
1757 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1758 return;
1759 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001760
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001761 FieldDecl *FirstField = *Field;
1762 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001763 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001764 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001765 diag::warn_transparent_union_attribute_floating)
1766 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001767 return;
1768 }
1769
1770 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1771 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1772 for (; Field != FieldEnd; ++Field) {
1773 QualType FieldType = Field->getType();
1774 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1775 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1776 // Warn if we drop the attribute.
1777 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001778 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001779 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001780 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001781 diag::warn_transparent_union_attribute_field_size_align)
1782 << isSize << Field->getDeclName() << FieldBits;
1783 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001784 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001785 diag::note_transparent_union_first_field_size_align)
1786 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001787 return;
1788 }
1789 }
1790
Sean Huntcf807c42010-08-18 23:23:40 +00001791 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001792}
1793
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001794static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001795 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001796 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001798 return;
1799 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001800 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001801 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001802
Chris Lattner6b6b5372008-06-26 18:38:35 +00001803 // Make sure that there is a string literal as the annotation's single
1804 // argument.
1805 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001806 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001807 return;
1808 }
Sean Huntcf807c42010-08-18 23:23:40 +00001809 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001810}
1811
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001812static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001813 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001814 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001816 return;
1817 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001818
1819 //FIXME: The C++0x version of this attribute has more limited applicabilty
1820 // than GNU's, and should error out when it is used to specify a
1821 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001822
Chris Lattner545dd342008-06-28 23:36:30 +00001823 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001824 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001825 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001826 }
Mike Stumpbf916502009-07-24 19:02:52 +00001827
Peter Collingbourne7a730022010-11-23 20:45:58 +00001828 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001829}
1830
1831void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1832 if (E->isTypeDependent() || E->isValueDependent()) {
1833 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001834 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001835 return;
1836 }
1837
Sean Huntcf807c42010-08-18 23:23:40 +00001838 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001839 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001840 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1841 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1842 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001843 return;
1844 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001845 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001846 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1847 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001848 return;
1849 }
1850
Sean Huntcf807c42010-08-18 23:23:40 +00001851 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1852}
1853
1854void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1855 // FIXME: Cache the number on the Attr object if non-dependent?
1856 // FIXME: Perform checking of type validity
1857 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1858 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001859}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001860
Mike Stumpbf916502009-07-24 19:02:52 +00001861/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1862/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001863///
Mike Stumpbf916502009-07-24 19:02:52 +00001864/// Despite what would be logical, the mode attribute is a decl attribute, not a
1865/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1866/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001867static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001868 // This attribute isn't documented, but glibc uses it. It changes
1869 // the width of an int or unsigned int to the specified size.
1870
1871 // Check that there aren't any arguments
1872 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001873 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001874 return;
1875 }
1876
1877 IdentifierInfo *Name = Attr.getParameterName();
1878 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001879 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001880 return;
1881 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001882
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001883 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001884
1885 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001886 if (Str.startswith("__") && Str.endswith("__"))
1887 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001888
1889 unsigned DestWidth = 0;
1890 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001891 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001892 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001893 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001894 switch (Str[0]) {
1895 case 'Q': DestWidth = 8; break;
1896 case 'H': DestWidth = 16; break;
1897 case 'S': DestWidth = 32; break;
1898 case 'D': DestWidth = 64; break;
1899 case 'X': DestWidth = 96; break;
1900 case 'T': DestWidth = 128; break;
1901 }
1902 if (Str[1] == 'F') {
1903 IntegerMode = false;
1904 } else if (Str[1] == 'C') {
1905 IntegerMode = false;
1906 ComplexMode = true;
1907 } else if (Str[1] != 'I') {
1908 DestWidth = 0;
1909 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001910 break;
1911 case 4:
1912 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1913 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001914 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001915 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001916 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001917 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001918 break;
1919 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001920 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001921 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001922 break;
1923 }
1924
1925 QualType OldTy;
1926 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1927 OldTy = TD->getUnderlyingType();
1928 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1929 OldTy = VD->getType();
1930 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001931 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1932 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001933 return;
1934 }
Eli Friedman73397492009-03-03 06:41:03 +00001935
John McCall183700f2009-09-21 23:43:11 +00001936 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001937 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1938 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001939 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001940 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1941 } else if (ComplexMode) {
1942 if (!OldTy->isComplexType())
1943 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1944 } else {
1945 if (!OldTy->isFloatingType())
1946 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1947 }
1948
Mike Stump390b4cc2009-05-16 07:39:55 +00001949 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1950 // and friends, at least with glibc.
1951 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1952 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001953 // FIXME: Make sure floating-point mappings are accurate
1954 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001955 QualType NewTy;
1956 switch (DestWidth) {
1957 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001958 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001959 return;
1960 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001961 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001962 return;
1963 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001964 if (!IntegerMode) {
1965 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1966 return;
1967 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001968 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001969 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001970 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001971 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001972 break;
1973 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001974 if (!IntegerMode) {
1975 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1976 return;
1977 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001978 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001979 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001980 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001981 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001982 break;
1983 case 32:
1984 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001985 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001986 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001987 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001988 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001989 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001990 break;
1991 case 64:
1992 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001993 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001994 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00001995 if (S.Context.Target.getLongWidth() == 64)
1996 NewTy = S.Context.LongTy;
1997 else
1998 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001999 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002000 if (S.Context.Target.getLongWidth() == 64)
2001 NewTy = S.Context.UnsignedLongTy;
2002 else
2003 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002004 break;
Eli Friedman73397492009-03-03 06:41:03 +00002005 case 96:
2006 NewTy = S.Context.LongDoubleTy;
2007 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002008 case 128:
2009 if (!IntegerMode) {
2010 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2011 return;
2012 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002013 if (OldTy->isSignedIntegerType())
2014 NewTy = S.Context.Int128Ty;
2015 else
2016 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002017 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002018 }
2019
Eli Friedman73397492009-03-03 06:41:03 +00002020 if (ComplexMode) {
2021 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002022 }
2023
2024 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002025 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2026 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002027 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002028 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002029 cast<ValueDecl>(D)->setType(NewTy);
2030}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002031
Mike Stump1feade82009-08-26 22:31:08 +00002032static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002033 // check the attribute arguments.
2034 if (Attr.getNumArgs() > 0) {
2035 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2036 return;
2037 }
Anders Carlssone896d982009-02-13 08:11:52 +00002038
Anders Carlsson5bab7882009-02-19 19:16:48 +00002039 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002040 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002041 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00002042 return;
2043 }
Mike Stumpbf916502009-07-24 19:02:52 +00002044
Sean Huntcf807c42010-08-18 23:23:40 +00002045 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002046}
2047
Mike Stump1feade82009-08-26 22:31:08 +00002048static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002049 // check the attribute arguments.
2050 if (Attr.getNumArgs() != 0) {
2051 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2052 return;
2053 }
Mike Stumpbf916502009-07-24 19:02:52 +00002054
Chris Lattnerc5197432009-04-14 17:02:11 +00002055 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002056 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002057 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002058 return;
2059 }
Mike Stumpbf916502009-07-24 19:02:52 +00002060
Sean Huntcf807c42010-08-18 23:23:40 +00002061 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002062}
2063
Chris Lattner7255a2d2010-06-22 00:03:40 +00002064static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2065 Sema &S) {
2066 // check the attribute arguments.
2067 if (Attr.getNumArgs() != 0) {
2068 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2069 return;
2070 }
2071
2072 if (!isa<FunctionDecl>(d)) {
2073 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2074 << Attr.getName() << 0 /*function*/;
2075 return;
2076 }
2077
Sean Huntcf807c42010-08-18 23:23:40 +00002078 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002079}
2080
Peter Collingbourneced76712010-12-01 03:15:31 +00002081static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2082 if (S.LangOpts.CUDA) {
2083 // check the attribute arguments.
2084 if (Attr.getNumArgs() != 0) {
2085 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2086 return;
2087 }
2088
2089 if (!isa<VarDecl>(d)) {
2090 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2091 << Attr.getName() << 12 /*variable*/;
2092 return;
2093 }
2094
2095 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2096 } else {
2097 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2098 }
2099}
2100
2101static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2102 if (S.LangOpts.CUDA) {
2103 // check the attribute arguments.
2104 if (Attr.getNumArgs() != 0) {
2105 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2106 return;
2107 }
2108
2109 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2110 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2111 << Attr.getName() << 2 /*variable and function*/;
2112 return;
2113 }
2114
2115 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2116 } else {
2117 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2118 }
2119}
2120
2121static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2122 if (S.LangOpts.CUDA) {
2123 // check the attribute arguments.
2124 if (Attr.getNumArgs() != 0) {
2125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2126 return;
2127 }
2128
2129 if (!isa<FunctionDecl>(d)) {
2130 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2131 << Attr.getName() << 0 /*function*/;
2132 return;
2133 }
2134
2135 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2136 } else {
2137 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2138 }
2139}
2140
2141static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2142 if (S.LangOpts.CUDA) {
2143 // check the attribute arguments.
2144 if (Attr.getNumArgs() != 0) {
2145 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2146 return;
2147 }
2148
2149 if (!isa<FunctionDecl>(d)) {
2150 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2151 << Attr.getName() << 0 /*function*/;
2152 return;
2153 }
2154
2155 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2156 } else {
2157 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2158 }
2159}
2160
2161static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2162 if (S.LangOpts.CUDA) {
2163 // check the attribute arguments.
2164 if (Attr.getNumArgs() != 0) {
2165 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2166 return;
2167 }
2168
2169 if (!isa<VarDecl>(d)) {
2170 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2171 << Attr.getName() << 12 /*variable*/;
2172 return;
2173 }
2174
2175 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2176 } else {
2177 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2178 }
2179}
2180
Chris Lattnercf2a7212009-04-20 19:12:28 +00002181static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002182 // check the attribute arguments.
2183 if (Attr.getNumArgs() != 0) {
2184 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2185 return;
2186 }
Mike Stumpbf916502009-07-24 19:02:52 +00002187
Chris Lattnerc5197432009-04-14 17:02:11 +00002188 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2189 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002190 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002191 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002192 return;
2193 }
Mike Stumpbf916502009-07-24 19:02:52 +00002194
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002195 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002196 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002197 return;
2198 }
Mike Stumpbf916502009-07-24 19:02:52 +00002199
Sean Huntcf807c42010-08-18 23:23:40 +00002200 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002201}
2202
Abramo Bagnarae215f722010-04-30 13:10:51 +00002203static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2204 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2205 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2206 assert(Attr.isInvalid() == false);
2207
2208 switch (Attr.getKind()) {
2209 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002210 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002211 return;
2212 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002213 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002214 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002215 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00002216 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002217 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002218 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00002219 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002220 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002221 case AttributeList::AT_pascal:
2222 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2223 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002224 default:
2225 llvm_unreachable("unexpected attribute kind");
2226 return;
2227 }
2228}
2229
Fariborz Jahanianee760332009-03-27 18:38:55 +00002230static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2231 // check the attribute arguments.
2232 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002233 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002234 return;
2235 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002236
Fariborz Jahanianee760332009-03-27 18:38:55 +00002237 if (!isFunctionOrMethod(d)) {
2238 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002239 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002240 return;
2241 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002242
Peter Collingbourne7a730022010-11-23 20:45:58 +00002243 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002244 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002245 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2246 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002247 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2248 << "regparm" << NumParamsExpr->getSourceRange();
2249 return;
2250 }
2251
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002252 if (S.Context.Target.getRegParmMax() == 0) {
2253 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002254 << NumParamsExpr->getSourceRange();
2255 return;
2256 }
2257
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002258 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002259 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2260 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002261 return;
2262 }
2263
Sean Huntcf807c42010-08-18 23:23:40 +00002264 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2265 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002266}
2267
Sean Huntbbd37c62009-11-21 08:43:09 +00002268static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2269 // check the attribute arguments.
2270 if (Attr.getNumArgs() != 0) {
2271 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2272 return;
2273 }
2274
2275 if (!isa<CXXRecordDecl>(d)
2276 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2277 S.Diag(Attr.getLoc(),
2278 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2279 : diag::warn_attribute_wrong_decl_type)
2280 << Attr.getName() << 7 /*virtual method or class*/;
2281 return;
2282 }
Sean Hunt7725e672009-11-25 04:20:27 +00002283
2284 // FIXME: Conform to C++0x redeclaration rules.
2285
2286 if (d->getAttr<FinalAttr>()) {
2287 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2288 return;
2289 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002290
Sean Huntcf807c42010-08-18 23:23:40 +00002291 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002292}
2293
Chris Lattner0744e5f2008-06-29 00:23:49 +00002294//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002295// C++0x member checking attributes
2296//===----------------------------------------------------------------------===//
2297
2298static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2299 if (Attr.getNumArgs() != 0) {
2300 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2301 return;
2302 }
2303
2304 if (!isa<CXXRecordDecl>(d)) {
2305 S.Diag(Attr.getLoc(),
2306 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2307 : diag::warn_attribute_wrong_decl_type)
2308 << Attr.getName() << 9 /*class*/;
2309 return;
2310 }
2311
2312 if (d->getAttr<BaseCheckAttr>()) {
2313 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2314 return;
2315 }
2316
Sean Huntcf807c42010-08-18 23:23:40 +00002317 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002318}
2319
2320static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2321 if (Attr.getNumArgs() != 0) {
2322 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2323 return;
2324 }
2325
2326 if (!isa<RecordDecl>(d->getDeclContext())) {
2327 // FIXME: It's not the type that's the problem
2328 S.Diag(Attr.getLoc(),
2329 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2330 : diag::warn_attribute_wrong_decl_type)
2331 << Attr.getName() << 11 /*member*/;
2332 return;
2333 }
2334
2335 // FIXME: Conform to C++0x redeclaration rules.
2336
2337 if (d->getAttr<HidingAttr>()) {
2338 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2339 return;
2340 }
2341
Sean Huntcf807c42010-08-18 23:23:40 +00002342 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002343}
2344
2345static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2346 if (Attr.getNumArgs() != 0) {
2347 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2348 return;
2349 }
2350
2351 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2352 // FIXME: It's not the type that's the problem
2353 S.Diag(Attr.getLoc(),
2354 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2355 : diag::warn_attribute_wrong_decl_type)
2356 << Attr.getName() << 10 /*virtual method*/;
2357 return;
2358 }
2359
2360 // FIXME: Conform to C++0x redeclaration rules.
2361
2362 if (d->getAttr<OverrideAttr>()) {
2363 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2364 return;
2365 }
2366
Sean Huntcf807c42010-08-18 23:23:40 +00002367 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002368}
2369
2370//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002371// Checker-specific attribute handlers.
2372//===----------------------------------------------------------------------===//
2373
2374static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2375 Sema &S) {
2376
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002377 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002378
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002379 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2380 RetTy = MD->getResultType();
2381 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2382 RetTy = FD->getResultType();
2383 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002384 SourceLocation L = Attr.getLoc();
2385 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2386 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002387 return;
2388 }
Mike Stumpbf916502009-07-24 19:02:52 +00002389
Ted Kremenek6217b802009-07-29 21:53:49 +00002390 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002391 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002392 SourceLocation L = Attr.getLoc();
2393 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2394 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002395 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002396 }
Mike Stumpbf916502009-07-24 19:02:52 +00002397
Ted Kremenekb71368d2009-05-09 02:44:38 +00002398 switch (Attr.getKind()) {
2399 default:
2400 assert(0 && "invalid ownership attribute");
2401 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002402 case AttributeList::AT_cf_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002403 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002404 return;
2405 case AttributeList::AT_ns_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002406 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002407 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002408 case AttributeList::AT_cf_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002409 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002410 return;
2411 case AttributeList::AT_ns_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002412 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002413 return;
2414 };
2415}
2416
Charles Davisf0122fe2010-02-16 18:27:26 +00002417static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2418 return Attr.getKind() == AttributeList::AT_dllimport ||
2419 Attr.getKind() == AttributeList::AT_dllexport;
2420}
2421
Ted Kremenekb71368d2009-05-09 02:44:38 +00002422//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002423// Top Level Sema Entry Points
2424//===----------------------------------------------------------------------===//
2425
Sebastian Redla89d82c2008-12-21 19:24:58 +00002426/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002427/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002428/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2429/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002430static void ProcessDeclAttribute(Scope *scope, Decl *D,
2431 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002432 if (Attr.isInvalid())
2433 return;
2434
Charles Davisf0122fe2010-02-16 18:27:26 +00002435 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2436 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002437 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002438 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002439 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002440 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2441 case AttributeList::AT_IBOutletCollection:
2442 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002443 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002444 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002445 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002446 case AttributeList::AT_neon_vector_type:
2447 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002448 // Ignore these, these are type attributes, handled by
2449 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002450 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002451 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2452 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002453 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002454 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002455 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002456 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002457 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2458 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002459 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002460 HandleDependencyAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002461 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002462 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2463 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2464 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002465 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002466 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002467 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002468 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002469 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2470 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2471 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002472 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002473 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2474 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002475 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002476 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2477 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002478 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002479 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002480 case AttributeList::AT_ownership_returns:
2481 case AttributeList::AT_ownership_takes:
2482 case AttributeList::AT_ownership_holds:
2483 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002484 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002485 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2486 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2487 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002488 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002489 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002490
2491 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002492 case AttributeList::AT_ns_returns_not_retained:
2493 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002494 case AttributeList::AT_ns_returns_retained:
2495 case AttributeList::AT_cf_returns_retained:
2496 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2497
Nate Begeman6f3d8382009-06-26 06:32:41 +00002498 case AttributeList::AT_reqd_wg_size:
2499 HandleReqdWorkGroupSize(D, Attr, S); break;
2500
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002501 case AttributeList::AT_init_priority:
2502 HandleInitPriorityAttr(D, Attr, S); break;
2503
Sean Hunt7725e672009-11-25 04:20:27 +00002504 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2505 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002506 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2507 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2508 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002509 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002510 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2511 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002512 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002513 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002514 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002515 case AttributeList::AT_transparent_union:
2516 HandleTransparentUnionAttr(D, Attr, S);
2517 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002518 case AttributeList::AT_objc_exception:
2519 HandleObjCExceptionAttr(D, Attr, S);
2520 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002521 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002522 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2523 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2524 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2525 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2526 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2527 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2528 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2529 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2530 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002531 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002532 // Just ignore
2533 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002534 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2535 HandleNoInstrumentFunctionAttr(D, Attr, S);
2536 break;
John McCall04a67a62010-02-05 21:31:56 +00002537 case AttributeList::AT_stdcall:
2538 case AttributeList::AT_cdecl:
2539 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002540 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002541 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002542 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002543 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002544 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002545 // Ask target about the attribute.
2546 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2547 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002548 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2549 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002550 break;
2551 }
2552}
2553
2554/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2555/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002556void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002557 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2558 ProcessDeclAttribute(S, D, *l, *this);
2559 }
2560
2561 // GCC accepts
2562 // static int a9 __attribute__((weakref));
2563 // but that looks really pointless. We reject it.
2564 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2565 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002566 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002567 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002568 }
2569}
2570
Ryan Flynne25ff832009-07-30 03:15:39 +00002571/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2572/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002573NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002574 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002575 NamedDecl *NewD = 0;
2576 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2577 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2578 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002579 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002580 if (FD->getQualifier()) {
2581 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2582 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2583 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002584 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2585 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2586 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002587 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002588 VD->getStorageClass(),
2589 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002590 if (VD->getQualifier()) {
2591 VarDecl *NewVD = cast<VarDecl>(NewD);
2592 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2593 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002594 }
2595 return NewD;
2596}
2597
2598/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2599/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002600void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002601 if (W.getUsed()) return; // only do this once
2602 W.setUsed(true);
2603 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2604 IdentifierInfo *NDId = ND->getIdentifier();
2605 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002606 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2607 NDId->getName()));
2608 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002609 WeakTopLevelDecl.push_back(NewD);
2610 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2611 // to insert Decl at TU scope, sorry.
2612 DeclContext *SavedContext = CurContext;
2613 CurContext = Context.getTranslationUnitDecl();
2614 PushOnScopeChains(NewD, S);
2615 CurContext = SavedContext;
2616 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002617 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002618 }
2619}
2620
Chris Lattner0744e5f2008-06-29 00:23:49 +00002621/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2622/// it, apply them to D. This is a bit tricky because PD can have attributes
2623/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002624void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
John McCalld4aff0e2010-10-27 00:59:00 +00002625 // It's valid to "forward-declare" #pragma weak, in which case we
2626 // have to do this.
2627 if (!WeakUndeclaredIdentifiers.empty()) {
2628 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2629 if (IdentifierInfo *Id = ND->getIdentifier()) {
2630 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2631 = WeakUndeclaredIdentifiers.find(Id);
2632 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2633 WeakInfo W = I->second;
2634 DeclApplyPragmaWeak(S, ND, W);
2635 WeakUndeclaredIdentifiers[Id] = W;
2636 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002637 }
2638 }
2639 }
2640
Chris Lattner0744e5f2008-06-29 00:23:49 +00002641 // Apply decl attributes from the DeclSpec if present.
2642 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002643 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002644
Chris Lattner0744e5f2008-06-29 00:23:49 +00002645 // Walk the declarator structure, applying decl attributes that were in a type
2646 // position to the decl itself. This handles cases like:
2647 // int *__attr__(x)** D;
2648 // when X is a decl attribute.
2649 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2650 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002651 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002652
Chris Lattner0744e5f2008-06-29 00:23:49 +00002653 // Finally, apply any attributes on the decl itself.
2654 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002655 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002656}
John McCall54abf7d2009-11-04 02:18:39 +00002657
2658/// PushParsingDeclaration - Enter a new "scope" of deprecation
2659/// warnings.
2660///
2661/// The state token we use is the start index of this scope
2662/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002663Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002664 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002665 return (ParsingDeclStackState) DelayedDiagnostics.size();
2666}
2667
John McCalld226f652010-08-21 09:40:31 +00002668void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002669 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2670 ParsingDeclDepth--;
2671
2672 if (DelayedDiagnostics.empty())
2673 return;
2674
2675 unsigned SavedIndex = (unsigned) S;
2676 assert(SavedIndex <= DelayedDiagnostics.size() &&
2677 "saved index is out of bounds");
2678
John McCall58e6f342010-03-16 05:22:47 +00002679 unsigned E = DelayedDiagnostics.size();
2680
John McCall2f514482010-01-27 03:50:35 +00002681 // We only want to actually emit delayed diagnostics when we
2682 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002683 if (D) {
2684 // We really do want to start with 0 here. We get one push for a
2685 // decl spec and another for each declarator; in a decl group like:
2686 // deprecated_typedef foo, *bar, baz();
2687 // only the declarator pops will be passed decls. This is correct;
2688 // we really do need to consider delayed diagnostics from the decl spec
2689 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002690 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002691 if (DelayedDiagnostics[I].Triggered)
2692 continue;
2693
2694 switch (DelayedDiagnostics[I].Kind) {
2695 case DelayedDiagnostic::Deprecation:
2696 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2697 break;
2698
2699 case DelayedDiagnostic::Access:
2700 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2701 break;
2702 }
2703 }
2704 }
2705
John McCall58e6f342010-03-16 05:22:47 +00002706 // Destroy all the delayed diagnostics we're about to pop off.
2707 for (unsigned I = SavedIndex; I != E; ++I)
2708 DelayedDiagnostics[I].destroy();
2709
John McCall2f514482010-01-27 03:50:35 +00002710 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002711}
2712
2713static bool isDeclDeprecated(Decl *D) {
2714 do {
2715 if (D->hasAttr<DeprecatedAttr>())
2716 return true;
2717 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2718 return false;
2719}
2720
John McCall9c3087b2010-08-26 02:13:20 +00002721void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002722 Decl *Ctx) {
2723 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002724 return;
2725
John McCall2f514482010-01-27 03:50:35 +00002726 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002727 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002728 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002729 << DD.getDeprecationDecl()->getDeclName()
2730 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002731 else
2732 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002733 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002734}
2735
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002736void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002737 SourceLocation Loc) {
John McCall54abf7d2009-11-04 02:18:39 +00002738 // Delay if we're currently parsing a declaration.
2739 if (ParsingDeclDepth) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002740 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2741 Message));
John McCall54abf7d2009-11-04 02:18:39 +00002742 return;
2743 }
2744
2745 // Otherwise, don't warn if our current context is deprecated.
2746 if (isDeclDeprecated(cast<Decl>(CurContext)))
2747 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002748 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002749 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2750 << Message;
2751 else
2752 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002753}