blob: d66ae0a554b6bcc483c0ce963f93f38e1e8fba5e [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 }
200 sizeExpr = static_cast<Expr *>(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.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000345 Expr *Ex = static_cast<Expr *>(*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
491 Expr *IdxExpr = static_cast<Expr *>(*I);
492 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?
535 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
536 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) {
643 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
644 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
Chris Lattner545dd342008-06-28 23:36:30 +0000667 Expr *Arg = static_cast<Expr*>(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
741 if (!isa<TypeDecl>(d)) {
742 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
743 << Attr.getName() << 2 /*variable and function*/;
744 return;
745 }
746
747 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
748}
749
Ted Kremenekb7252322009-04-10 00:01:14 +0000750static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000751 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
752 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000753 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000754}
755
756static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
757 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000758
759 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
760 // because 'analyzer_noreturn' does not impact the type.
761
762 if (Attr.getNumArgs() != 0) {
763 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
764 return;
765 }
766
767 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
768 ValueDecl *VD = dyn_cast<ValueDecl>(d);
769 if (VD == 0 || (!VD->getType()->isBlockPointerType()
770 && !VD->getType()->isFunctionPointerType())) {
771 S.Diag(Attr.getLoc(),
772 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
773 : diag::warn_attribute_wrong_decl_type)
774 << Attr.getName() << 0 /*function*/;
775 return;
776 }
777 }
778
779 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000780}
781
John Thompson35cc9622010-08-09 21:53:52 +0000782// PS3 PPU-specific.
783static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
784 Sema &S) {
785/*
786 Returning a Vector Class in Registers
787
788 According to the PPU ABI specifications, a class with a single member of vector type is returned in
789 memory when used as the return value of a function. This results in inefficient code when implementing
790 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
791 definition. This attribute is also applicable to struct types.
792
793 Example:
794
795 struct Vector
796 {
797 __vector float xyzw;
798 } __attribute__((vecreturn));
799
800 Vector Add(Vector lhs, Vector rhs)
801 {
802 Vector result;
803 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
804 return result; // This will be returned in a register
805 }
806*/
John Thompson01add592010-09-18 01:12:07 +0000807 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
809 << Attr.getName() << 9 /*class*/;
810 return;
811 }
812
813 if (d->getAttr<VecReturnAttr>()) {
814 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
815 return;
816 }
817
John Thompson01add592010-09-18 01:12:07 +0000818 RecordDecl *record = cast<RecordDecl>(d);
819 int count = 0;
820
821 if (!isa<CXXRecordDecl>(record)) {
822 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
823 return;
824 }
825
826 if (!cast<CXXRecordDecl>(record)->isPOD()) {
827 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
828 return;
829 }
830
831 for (RecordDecl::field_iterator iter = record->field_begin(); iter != record->field_end(); iter++) {
832 if ((count == 1) || !iter->getType()->isVectorType()) {
833 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
834 return;
835 }
836 count++;
837 }
838
Sean Huntcf807c42010-08-18 23:23:40 +0000839 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000840}
841
Sean Huntbbd37c62009-11-21 08:43:09 +0000842static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
843 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
844 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000845 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000846 return;
847 }
848 // FIXME: Actually store the attribute on the declaration
849}
850
Ted Kremenek73798892008-07-25 04:39:19 +0000851static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
852 // check the attribute arguments.
853 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000854 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000855 return;
856 }
Mike Stumpbf916502009-07-24 19:02:52 +0000857
John McCallaec58602010-03-31 02:47:45 +0000858 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
859 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000860 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000861 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000862 return;
863 }
Mike Stumpbf916502009-07-24 19:02:52 +0000864
Sean Huntcf807c42010-08-18 23:23:40 +0000865 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000866}
867
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000868static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
869 // check the attribute arguments.
870 if (Attr.getNumArgs() != 0) {
871 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
872 return;
873 }
Mike Stumpbf916502009-07-24 19:02:52 +0000874
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000875 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000876 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000877 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
878 return;
879 }
880 } else if (!isFunctionOrMethod(d)) {
881 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000882 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000883 return;
884 }
Mike Stumpbf916502009-07-24 19:02:52 +0000885
Sean Huntcf807c42010-08-18 23:23:40 +0000886 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000887}
888
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000889static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
890 // check the attribute arguments.
891 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
893 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000894 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000895 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000896
897 int priority = 65535; // FIXME: Do not hardcode such constants.
898 if (Attr.getNumArgs() > 0) {
899 Expr *E = static_cast<Expr *>(Attr.getArg(0));
900 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000901 if (E->isTypeDependent() || E->isValueDependent() ||
902 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000903 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000904 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000905 return;
906 }
907 priority = Idx.getZExtValue();
908 }
Mike Stumpbf916502009-07-24 19:02:52 +0000909
Chris Lattnerc5197432009-04-14 17:02:11 +0000910 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000911 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000912 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000913 return;
914 }
915
Sean Huntcf807c42010-08-18 23:23:40 +0000916 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000917}
918
919static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
920 // check the attribute arguments.
921 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000922 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
923 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000924 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000925 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000926
927 int priority = 65535; // FIXME: Do not hardcode such constants.
928 if (Attr.getNumArgs() > 0) {
929 Expr *E = static_cast<Expr *>(Attr.getArg(0));
930 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000931 if (E->isTypeDependent() || E->isValueDependent() ||
932 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000933 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000934 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000935 return;
936 }
937 priority = Idx.getZExtValue();
938 }
Mike Stumpbf916502009-07-24 19:02:52 +0000939
Anders Carlsson6782fc62008-08-22 22:10:48 +0000940 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000941 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000942 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000943 return;
944 }
945
Sean Huntcf807c42010-08-18 23:23:40 +0000946 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000947}
948
Chris Lattner803d0802008-06-29 00:43:07 +0000949static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000950 // check the attribute arguments.
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000951 int noArgs = Attr.getNumArgs();
952 if (noArgs > 1) {
953 S.Diag(Attr.getLoc(),
954 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000955 return;
956 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000957 // Handle the case where deprecated attribute has a text message.
958 StringLiteral *SE;
959 if (noArgs == 1) {
960 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
961 SE = dyn_cast<StringLiteral>(ArgExpr);
962 if (!SE) {
963 S.Diag(ArgExpr->getLocStart(),
964 diag::err_attribute_not_string) << "deprecated";
965 return;
966 }
967 }
968 else
969 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpbf916502009-07-24 19:02:52 +0000970
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000971 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
972 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973}
974
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000975static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
976 // check the attribute arguments.
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000977 int noArgs = Attr.getNumArgs();
978 if (noArgs > 1) {
979 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000980 return;
981 }
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000982 // Handle the case where unavailable attribute has a text message.
983 StringLiteral *SE;
984 if (noArgs == 1) {
985 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
986 SE = dyn_cast<StringLiteral>(ArgExpr);
987 if (!SE) {
988 S.Diag(ArgExpr->getLocStart(),
989 diag::err_attribute_not_string) << "unavailable";
990 return;
991 }
992 }
993 else
994 SE = StringLiteral::CreateEmpty(S.Context, 1);
995 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
996 SE->getString()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000997}
998
Chris Lattner803d0802008-06-29 00:43:07 +0000999static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001000 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001001 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001002 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001003 return;
1004 }
Mike Stumpbf916502009-07-24 19:02:52 +00001005
Chris Lattner545dd342008-06-28 23:36:30 +00001006 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001007 Arg = Arg->IgnoreParenCasts();
1008 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001009
Chris Lattner6b6b5372008-06-26 18:38:35 +00001010 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001011 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001012 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001013 return;
1014 }
Mike Stumpbf916502009-07-24 19:02:52 +00001015
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001016 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001017 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001018
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001019 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001020 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001021 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001022 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001023 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001024 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001025 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001026 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001027 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001028 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001029 return;
1030 }
Mike Stumpbf916502009-07-24 19:02:52 +00001031
Sean Huntcf807c42010-08-18 23:23:40 +00001032 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001033}
1034
Chris Lattner0db29ec2009-02-14 08:09:34 +00001035static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1036 Sema &S) {
1037 if (Attr.getNumArgs() != 0) {
1038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1039 return;
1040 }
Mike Stumpbf916502009-07-24 19:02:52 +00001041
Chris Lattner0db29ec2009-02-14 08:09:34 +00001042 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1043 if (OCI == 0) {
1044 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1045 return;
1046 }
Mike Stumpbf916502009-07-24 19:02:52 +00001047
Sean Huntcf807c42010-08-18 23:23:40 +00001048 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001049}
1050
1051static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001052 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001053 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001054 return;
1055 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001056 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001057 QualType T = TD->getUnderlyingType();
1058 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001059 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001060 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1061 return;
1062 }
1063 }
Sean Huntcf807c42010-08-18 23:23:40 +00001064 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001065}
1066
Mike Stumpbf916502009-07-24 19:02:52 +00001067static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001068HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1069 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001070 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001071 return;
1072 }
1073
1074 if (!isa<FunctionDecl>(D)) {
1075 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1076 return;
1077 }
1078
Sean Huntcf807c42010-08-18 23:23:40 +00001079 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001080}
1081
Steve Naroff9eae5762008-09-18 16:44:58 +00001082static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001083 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001084 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001085 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001086 return;
1087 }
Mike Stumpbf916502009-07-24 19:02:52 +00001088
Steve Naroff9eae5762008-09-18 16:44:58 +00001089 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001091 return;
1092 }
Mike Stumpbf916502009-07-24 19:02:52 +00001093
Sean Huntcf807c42010-08-18 23:23:40 +00001094 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001095 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001096 type = BlocksAttr::ByRef;
1097 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001098 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001099 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001100 return;
1101 }
Mike Stumpbf916502009-07-24 19:02:52 +00001102
Sean Huntcf807c42010-08-18 23:23:40 +00001103 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001104}
1105
Anders Carlsson77091822008-10-05 18:05:59 +00001106static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1107 // check the attribute arguments.
1108 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1110 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001111 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001112 }
1113
Anders Carlsson77091822008-10-05 18:05:59 +00001114 int sentinel = 0;
1115 if (Attr.getNumArgs() > 0) {
1116 Expr *E = static_cast<Expr *>(Attr.getArg(0));
1117 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001118 if (E->isTypeDependent() || E->isValueDependent() ||
1119 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001120 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001121 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001122 return;
1123 }
1124 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001125
Anders Carlsson77091822008-10-05 18:05:59 +00001126 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001127 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1128 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001129 return;
1130 }
1131 }
1132
1133 int nullPos = 0;
1134 if (Attr.getNumArgs() > 1) {
1135 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1136 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001137 if (E->isTypeDependent() || E->isValueDependent() ||
1138 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001140 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001141 return;
1142 }
1143 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001144
Anders Carlsson77091822008-10-05 18:05:59 +00001145 if (nullPos > 1 || nullPos < 0) {
1146 // FIXME: This error message could be improved, it would be nice
1147 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001148 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1149 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001150 return;
1151 }
1152 }
1153
1154 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001155 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001156 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001157
Chris Lattner897cd902009-03-17 23:03:47 +00001158 if (isa<FunctionNoProtoType>(FT)) {
1159 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1160 return;
1161 }
Mike Stumpbf916502009-07-24 19:02:52 +00001162
Chris Lattner897cd902009-03-17 23:03:47 +00001163 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001164 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001165 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001166 }
Anders Carlsson77091822008-10-05 18:05:59 +00001167 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1168 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001169 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001170 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001171 }
1172 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001173 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1174 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001175 ;
1176 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001177 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001178 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001179 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall183700f2009-09-21 23:43:11 +00001180 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001181 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001182 int m = Ty->isFunctionPointerType() ? 0 : 1;
1183 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001184 return;
1185 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001186 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001187 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001188 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001189 return;
1190 }
Anders Carlsson77091822008-10-05 18:05:59 +00001191 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001193 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001194 return;
1195 }
Sean Huntcf807c42010-08-18 23:23:40 +00001196 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001197}
1198
Chris Lattner026dc962009-02-14 07:37:35 +00001199static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1200 // check the attribute arguments.
1201 if (Attr.getNumArgs() != 0) {
1202 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1203 return;
1204 }
1205
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001206 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001207 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001208 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001209 return;
1210 }
Mike Stumpbf916502009-07-24 19:02:52 +00001211
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001212 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1213 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1214 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001215 return;
1216 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001217 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1218 if (MD->getResultType()->isVoidType()) {
1219 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1220 << Attr.getName() << 1;
1221 return;
1222 }
1223
Sean Huntcf807c42010-08-18 23:23:40 +00001224 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001225}
1226
1227static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001228 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001229 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001230 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001231 return;
1232 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001233
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001234 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001235 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001236 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1237 dyn_cast<NamedDecl>(D)->getNameAsString();
1238 return;
1239 }
1240
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001241 // TODO: could also be applied to methods?
1242 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001244 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001245 return;
1246 }
Mike Stumpbf916502009-07-24 19:02:52 +00001247
Sean Huntcf807c42010-08-18 23:23:40 +00001248 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001249}
1250
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001251static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1252 // check the attribute arguments.
1253 if (Attr.getNumArgs() != 0) {
1254 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1255 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001256 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001257
1258 // weak_import only applies to variable & function declarations.
1259 bool isDef = false;
1260 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1261 isDef = (!VD->hasExternalStorage() || VD->getInit());
1262 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001263 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001264 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1265 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001266 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001267 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001268 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001269 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001270 !isa<ObjCInterfaceDecl>(D))
1271 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001272 << Attr.getName() << 2 /*variable and function*/;
1273 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001274 }
1275
1276 // Merge should handle any subsequent violations.
1277 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001278 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001279 diag::warn_attribute_weak_import_invalid_on_definition)
1280 << "weak_import" << 2 /*variable and function*/;
1281 return;
1282 }
1283
Sean Huntcf807c42010-08-18 23:23:40 +00001284 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001285}
1286
Nate Begeman6f3d8382009-06-26 06:32:41 +00001287static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1288 Sema &S) {
1289 // Attribute has 3 arguments.
1290 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001291 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001292 return;
1293 }
1294
1295 unsigned WGSize[3];
1296 for (unsigned i = 0; i < 3; ++i) {
1297 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1298 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001299 if (E->isTypeDependent() || E->isValueDependent() ||
1300 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001301 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1302 << "reqd_work_group_size" << E->getSourceRange();
1303 return;
1304 }
1305 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1306 }
Sean Huntcf807c42010-08-18 23:23:40 +00001307 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1308 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001309 WGSize[2]));
1310}
1311
Chris Lattner026dc962009-02-14 07:37:35 +00001312static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001313 // Attribute has no arguments.
1314 if (Attr.getNumArgs() != 1) {
1315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1316 return;
1317 }
1318
1319 // Make sure that there is a string literal as the sections's single
1320 // argument.
Chris Lattner797c3c42009-08-10 19:03:04 +00001321 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1322 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001323 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001324 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001325 return;
1326 }
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Chris Lattner797c3c42009-08-10 19:03:04 +00001328 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001329 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001330 if (!Error.empty()) {
1331 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1332 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001333 return;
1334 }
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001336 // This attribute cannot be applied to local variables.
1337 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1338 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1339 return;
1340 }
1341
Sean Huntcf807c42010-08-18 23:23:40 +00001342 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001343}
1344
Chris Lattner6b6b5372008-06-26 18:38:35 +00001345
Chris Lattner803d0802008-06-29 00:43:07 +00001346static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001347 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001348 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001349 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 return;
1351 }
Mike Stumpbf916502009-07-24 19:02:52 +00001352
Sean Huntcf807c42010-08-18 23:23:40 +00001353 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001354}
1355
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001356static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1357 // check the attribute arguments.
1358 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001359 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001360 return;
1361 }
Mike Stumpbf916502009-07-24 19:02:52 +00001362
Sean Huntcf807c42010-08-18 23:23:40 +00001363 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001364}
1365
1366static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1367 // check the attribute arguments.
1368 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001370 return;
1371 }
Mike Stumpbf916502009-07-24 19:02:52 +00001372
Sean Huntcf807c42010-08-18 23:23:40 +00001373 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001374}
1375
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001376static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001377 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1379 return;
1380 }
Mike Stumpbf916502009-07-24 19:02:52 +00001381
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001382 if (Attr.getNumArgs() != 0) {
1383 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1384 return;
1385 }
Mike Stumpbf916502009-07-24 19:02:52 +00001386
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001387 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001388
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001389 if (!VD || !VD->hasLocalStorage()) {
1390 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1391 return;
1392 }
Mike Stumpbf916502009-07-24 19:02:52 +00001393
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001394 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001395 // FIXME: Lookup probably isn't looking in the right place
1396 // FIXME: The lookup source location should be in the attribute, not the
1397 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001398 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001399 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001400 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001401 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001402 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001403 Attr.getParameterName();
1404 return;
1405 }
Mike Stumpbf916502009-07-24 19:02:52 +00001406
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001407 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1408 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001409 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001410 Attr.getParameterName();
1411 return;
1412 }
1413
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001414 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001416 Attr.getParameterName();
1417 return;
1418 }
Mike Stumpbf916502009-07-24 19:02:52 +00001419
Anders Carlsson89941c12009-02-07 23:16:50 +00001420 // We're currently more strict than GCC about what function types we accept.
1421 // If this ever proves to be a problem it should be easy to fix.
1422 QualType Ty = S.Context.getPointerType(VD->getType());
1423 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall1c23e912010-11-16 02:32:08 +00001424 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001425 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001426 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1427 Attr.getParameterName() << ParamTy << Ty;
1428 return;
1429 }
Mike Stumpbf916502009-07-24 19:02:52 +00001430
Sean Huntcf807c42010-08-18 23:23:40 +00001431 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001432}
1433
Mike Stumpbf916502009-07-24 19:02:52 +00001434/// Handle __attribute__((format_arg((idx)))) attribute based on
1435/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1436static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001437 if (Attr.getNumArgs() != 1) {
1438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1439 return;
1440 }
1441 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1442 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1443 << Attr.getName() << 0 /*function*/;
1444 return;
1445 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001446
1447 // In C++ the implicit 'this' function parameter also counts, and they are
1448 // counted from one.
1449 bool HasImplicitThisParam = isInstanceMethod(d);
1450 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001451 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001452
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001453 // checks for the 2nd argument
1454 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1455 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001456 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1457 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1459 << "format" << 2 << IdxExpr->getSourceRange();
1460 return;
1461 }
Mike Stumpbf916502009-07-24 19:02:52 +00001462
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001463 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1464 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1465 << "format" << 2 << IdxExpr->getSourceRange();
1466 return;
1467 }
Mike Stumpbf916502009-07-24 19:02:52 +00001468
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001469 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001470
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001471 if (HasImplicitThisParam) {
1472 if (ArgIdx == 0) {
1473 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1474 << "format_arg" << IdxExpr->getSourceRange();
1475 return;
1476 }
1477 ArgIdx--;
1478 }
1479
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001480 // make sure the format string is really a string
1481 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001482
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001483 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1484 if (not_nsstring_type &&
1485 !isCFStringType(Ty, S.Context) &&
1486 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001487 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001488 // FIXME: Should highlight the actual expression that has the wrong type.
1489 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001490 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001491 << IdxExpr->getSourceRange();
1492 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001493 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001494 Ty = getFunctionOrMethodResultType(d);
1495 if (!isNSStringType(Ty, S.Context) &&
1496 !isCFStringType(Ty, S.Context) &&
1497 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001498 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001499 // FIXME: Should highlight the actual expression that has the wrong type.
1500 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001501 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001502 << IdxExpr->getSourceRange();
1503 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001504 }
1505
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001506 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1507 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001508}
1509
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001510enum FormatAttrKind {
1511 CFStringFormat,
1512 NSStringFormat,
1513 StrftimeFormat,
1514 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001515 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001516 InvalidFormat
1517};
1518
1519/// getFormatAttrKind - Map from format attribute names to supported format
1520/// types.
1521static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1522 // Check for formats that get handled specially.
1523 if (Format == "NSString")
1524 return NSStringFormat;
1525 if (Format == "CFString")
1526 return CFStringFormat;
1527 if (Format == "strftime")
1528 return StrftimeFormat;
1529
1530 // Otherwise, check for supported formats.
1531 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1532 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1533 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1534 Format == "zcmn_err")
1535 return SupportedFormat;
1536
Duncan Sandsbc525952010-03-23 14:44:19 +00001537 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1538 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001539 return IgnoredFormat;
1540
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001541 return InvalidFormat;
1542}
1543
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001544/// Handle __attribute__((init_priority(priority))) attributes based on
1545/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1546static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1547 Sema &S) {
1548 if (!S.getLangOptions().CPlusPlus) {
1549 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1550 return;
1551 }
1552
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001553 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1554 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1555 Attr.setInvalid();
1556 return;
1557 }
1558 QualType T = dyn_cast<VarDecl>(d)->getType();
1559 if (S.Context.getAsArrayType(T))
1560 T = S.Context.getBaseElementType(T);
1561 if (!T->getAs<RecordType>()) {
1562 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1563 Attr.setInvalid();
1564 return;
1565 }
1566
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001567 if (Attr.getNumArgs() != 1) {
1568 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1569 Attr.setInvalid();
1570 return;
1571 }
1572 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001573
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001574 llvm::APSInt priority(32);
1575 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1576 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1577 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1578 << "init_priority" << priorityExpr->getSourceRange();
1579 Attr.setInvalid();
1580 return;
1581 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001582 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001583 if (prioritynum < 101 || prioritynum > 65535) {
1584 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1585 << priorityExpr->getSourceRange();
1586 Attr.setInvalid();
1587 return;
1588 }
Sean Huntcf807c42010-08-18 23:23:40 +00001589 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001590}
1591
Mike Stumpbf916502009-07-24 19:02:52 +00001592/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1593/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001594static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001595
Chris Lattner545dd342008-06-28 23:36:30 +00001596 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001597 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001598 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001599 return;
1600 }
1601
Chris Lattner545dd342008-06-28 23:36:30 +00001602 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001603 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001604 return;
1605 }
1606
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001607 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001608 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001609 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001610 return;
1611 }
1612
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001613 // In C++ the implicit 'this' function parameter also counts, and they are
1614 // counted from one.
1615 bool HasImplicitThisParam = isInstanceMethod(d);
1616 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001617 unsigned FirstIdx = 1;
1618
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001619 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001620
1621 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001622 if (Format.startswith("__") && Format.endswith("__"))
1623 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001624
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001625 // Check for supported formats.
1626 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001627
1628 if (Kind == IgnoredFormat)
1629 return;
1630
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001631 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001632 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001633 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001634 return;
1635 }
1636
1637 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001638 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001639 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001640 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1641 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001642 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001643 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001644 return;
1645 }
1646
1647 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001649 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001650 return;
1651 }
1652
1653 // FIXME: Do we need to bounds check?
1654 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001655
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001656 if (HasImplicitThisParam) {
1657 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001658 S.Diag(Attr.getLoc(),
1659 diag::err_format_attribute_implicit_this_format_string)
1660 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001661 return;
1662 }
1663 ArgIdx--;
1664 }
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Chris Lattner6b6b5372008-06-26 18:38:35 +00001666 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001667 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001668
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001669 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001670 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001671 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1672 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001673 return;
1674 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001675 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001676 // FIXME: do we need to check if the type is NSString*? What are the
1677 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001678 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001679 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001680 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1681 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001682 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001683 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001684 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001685 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001686 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001687 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1688 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001689 return;
1690 }
1691
1692 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001693 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001694 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001695 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1696 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001697 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001698 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001699 return;
1700 }
1701
1702 // check if the function is variadic if the 3rd argument non-zero
1703 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001704 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001705 ++NumArgs; // +1 for ...
1706 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001707 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001708 return;
1709 }
1710 }
1711
Chris Lattner3c73c412008-11-19 08:23:25 +00001712 // strftime requires FirstArg to be 0 because it doesn't read from any
1713 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001714 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001715 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001716 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1717 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001718 return;
1719 }
1720 // if 0 it disables parameter checking (to use with e.g. va_list)
1721 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001722 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001723 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001724 return;
1725 }
1726
Sean Huntcf807c42010-08-18 23:23:40 +00001727 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1728 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001729 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001730}
1731
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001732static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1733 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001734 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001735 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001736 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001737 return;
1738 }
1739
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001740 // Try to find the underlying union declaration.
1741 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001742 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001743 if (TD && TD->getUnderlyingType()->isUnionType())
1744 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1745 else
1746 RD = dyn_cast<RecordDecl>(d);
1747
1748 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001749 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001750 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001751 return;
1752 }
1753
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001754 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001755 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001756 diag::warn_transparent_union_attribute_not_definition);
1757 return;
1758 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001759
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001760 RecordDecl::field_iterator Field = RD->field_begin(),
1761 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001762 if (Field == FieldEnd) {
1763 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1764 return;
1765 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001766
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001767 FieldDecl *FirstField = *Field;
1768 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001769 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001770 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001771 diag::warn_transparent_union_attribute_floating)
1772 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001773 return;
1774 }
1775
1776 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1777 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1778 for (; Field != FieldEnd; ++Field) {
1779 QualType FieldType = Field->getType();
1780 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1781 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1782 // Warn if we drop the attribute.
1783 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001784 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001785 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001786 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001787 diag::warn_transparent_union_attribute_field_size_align)
1788 << isSize << Field->getDeclName() << FieldBits;
1789 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001790 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001791 diag::note_transparent_union_first_field_size_align)
1792 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001793 return;
1794 }
1795 }
1796
Sean Huntcf807c42010-08-18 23:23:40 +00001797 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001798}
1799
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001800static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001801 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001802 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001804 return;
1805 }
Chris Lattner797c3c42009-08-10 19:03:04 +00001806 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1807 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001808
Chris Lattner6b6b5372008-06-26 18:38:35 +00001809 // Make sure that there is a string literal as the annotation's single
1810 // argument.
1811 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001812 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001813 return;
1814 }
Sean Huntcf807c42010-08-18 23:23:40 +00001815 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001816}
1817
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001818static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001819 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001820 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001821 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001822 return;
1823 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001824
1825 //FIXME: The C++0x version of this attribute has more limited applicabilty
1826 // than GNU's, and should error out when it is used to specify a
1827 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001828
Chris Lattner545dd342008-06-28 23:36:30 +00001829 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001830 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001831 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001832 }
Mike Stumpbf916502009-07-24 19:02:52 +00001833
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001834 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1835}
1836
1837void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1838 if (E->isTypeDependent() || E->isValueDependent()) {
1839 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001840 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001841 return;
1842 }
1843
Sean Huntcf807c42010-08-18 23:23:40 +00001844 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001845 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001846 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1847 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1848 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001849 return;
1850 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001851 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001852 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1853 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001854 return;
1855 }
1856
Sean Huntcf807c42010-08-18 23:23:40 +00001857 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1858}
1859
1860void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1861 // FIXME: Cache the number on the Attr object if non-dependent?
1862 // FIXME: Perform checking of type validity
1863 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1864 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001865}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001866
Mike Stumpbf916502009-07-24 19:02:52 +00001867/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1868/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001869///
Mike Stumpbf916502009-07-24 19:02:52 +00001870/// Despite what would be logical, the mode attribute is a decl attribute, not a
1871/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1872/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001873static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001874 // This attribute isn't documented, but glibc uses it. It changes
1875 // the width of an int or unsigned int to the specified size.
1876
1877 // Check that there aren't any arguments
1878 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001879 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001880 return;
1881 }
1882
1883 IdentifierInfo *Name = Attr.getParameterName();
1884 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001885 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001886 return;
1887 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001888
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001889 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001890
1891 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001892 if (Str.startswith("__") && Str.endswith("__"))
1893 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001894
1895 unsigned DestWidth = 0;
1896 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001897 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001898 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001899 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001900 switch (Str[0]) {
1901 case 'Q': DestWidth = 8; break;
1902 case 'H': DestWidth = 16; break;
1903 case 'S': DestWidth = 32; break;
1904 case 'D': DestWidth = 64; break;
1905 case 'X': DestWidth = 96; break;
1906 case 'T': DestWidth = 128; break;
1907 }
1908 if (Str[1] == 'F') {
1909 IntegerMode = false;
1910 } else if (Str[1] == 'C') {
1911 IntegerMode = false;
1912 ComplexMode = true;
1913 } else if (Str[1] != 'I') {
1914 DestWidth = 0;
1915 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001916 break;
1917 case 4:
1918 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1919 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001920 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001921 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001922 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001923 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001924 break;
1925 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001926 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001927 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001928 break;
1929 }
1930
1931 QualType OldTy;
1932 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1933 OldTy = TD->getUnderlyingType();
1934 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1935 OldTy = VD->getType();
1936 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001937 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1938 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001939 return;
1940 }
Eli Friedman73397492009-03-03 06:41:03 +00001941
John McCall183700f2009-09-21 23:43:11 +00001942 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001943 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1944 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001945 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001946 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1947 } else if (ComplexMode) {
1948 if (!OldTy->isComplexType())
1949 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1950 } else {
1951 if (!OldTy->isFloatingType())
1952 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1953 }
1954
Mike Stump390b4cc2009-05-16 07:39:55 +00001955 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1956 // and friends, at least with glibc.
1957 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1958 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001959 // FIXME: Make sure floating-point mappings are accurate
1960 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001961 QualType NewTy;
1962 switch (DestWidth) {
1963 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001964 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001965 return;
1966 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001967 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001968 return;
1969 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001970 if (!IntegerMode) {
1971 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1972 return;
1973 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001974 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001975 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001976 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001977 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001978 break;
1979 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001980 if (!IntegerMode) {
1981 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1982 return;
1983 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001984 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001985 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001986 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001987 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001988 break;
1989 case 32:
1990 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001991 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001992 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001993 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001994 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001995 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001996 break;
1997 case 64:
1998 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001999 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002000 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002001 if (S.Context.Target.getLongWidth() == 64)
2002 NewTy = S.Context.LongTy;
2003 else
2004 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002005 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002006 if (S.Context.Target.getLongWidth() == 64)
2007 NewTy = S.Context.UnsignedLongTy;
2008 else
2009 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002010 break;
Eli Friedman73397492009-03-03 06:41:03 +00002011 case 96:
2012 NewTy = S.Context.LongDoubleTy;
2013 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002014 case 128:
2015 if (!IntegerMode) {
2016 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2017 return;
2018 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002019 if (OldTy->isSignedIntegerType())
2020 NewTy = S.Context.Int128Ty;
2021 else
2022 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002023 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002024 }
2025
Eli Friedman73397492009-03-03 06:41:03 +00002026 if (ComplexMode) {
2027 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002028 }
2029
2030 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002031 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2032 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002033 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002034 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002035 cast<ValueDecl>(D)->setType(NewTy);
2036}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002037
Mike Stump1feade82009-08-26 22:31:08 +00002038static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002039 // check the attribute arguments.
2040 if (Attr.getNumArgs() > 0) {
2041 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2042 return;
2043 }
Anders Carlssone896d982009-02-13 08:11:52 +00002044
Anders Carlsson5bab7882009-02-19 19:16:48 +00002045 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002046 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002047 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00002048 return;
2049 }
Mike Stumpbf916502009-07-24 19:02:52 +00002050
Sean Huntcf807c42010-08-18 23:23:40 +00002051 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002052}
2053
Mike Stump1feade82009-08-26 22:31:08 +00002054static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002055 // check the attribute arguments.
2056 if (Attr.getNumArgs() != 0) {
2057 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2058 return;
2059 }
Mike Stumpbf916502009-07-24 19:02:52 +00002060
Chris Lattnerc5197432009-04-14 17:02:11 +00002061 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002062 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002063 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002064 return;
2065 }
Mike Stumpbf916502009-07-24 19:02:52 +00002066
Sean Huntcf807c42010-08-18 23:23:40 +00002067 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002068}
2069
Chris Lattner7255a2d2010-06-22 00:03:40 +00002070static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2071 Sema &S) {
2072 // check the attribute arguments.
2073 if (Attr.getNumArgs() != 0) {
2074 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2075 return;
2076 }
2077
2078 if (!isa<FunctionDecl>(d)) {
2079 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2080 << Attr.getName() << 0 /*function*/;
2081 return;
2082 }
2083
Sean Huntcf807c42010-08-18 23:23:40 +00002084 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002085}
2086
Chris Lattnercf2a7212009-04-20 19:12:28 +00002087static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002088 // check the attribute arguments.
2089 if (Attr.getNumArgs() != 0) {
2090 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2091 return;
2092 }
Mike Stumpbf916502009-07-24 19:02:52 +00002093
Chris Lattnerc5197432009-04-14 17:02:11 +00002094 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2095 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002096 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002097 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002098 return;
2099 }
Mike Stumpbf916502009-07-24 19:02:52 +00002100
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002101 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002102 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002103 return;
2104 }
Mike Stumpbf916502009-07-24 19:02:52 +00002105
Sean Huntcf807c42010-08-18 23:23:40 +00002106 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002107}
2108
Abramo Bagnarae215f722010-04-30 13:10:51 +00002109static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2110 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2111 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2112 assert(Attr.isInvalid() == false);
2113
2114 switch (Attr.getKind()) {
2115 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002116 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002117 return;
2118 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002119 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002120 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002121 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00002122 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002123 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002124 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00002125 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002126 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002127 case AttributeList::AT_pascal:
2128 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2129 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002130 default:
2131 llvm_unreachable("unexpected attribute kind");
2132 return;
2133 }
2134}
2135
Fariborz Jahanianee760332009-03-27 18:38:55 +00002136static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2137 // check the attribute arguments.
2138 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002139 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002140 return;
2141 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002142
Fariborz Jahanianee760332009-03-27 18:38:55 +00002143 if (!isFunctionOrMethod(d)) {
2144 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002145 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002146 return;
2147 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002148
2149 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2150 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002151 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2152 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002153 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2154 << "regparm" << NumParamsExpr->getSourceRange();
2155 return;
2156 }
2157
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002158 if (S.Context.Target.getRegParmMax() == 0) {
2159 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002160 << NumParamsExpr->getSourceRange();
2161 return;
2162 }
2163
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002164 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002165 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2166 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002167 return;
2168 }
2169
Sean Huntcf807c42010-08-18 23:23:40 +00002170 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2171 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002172}
2173
Sean Huntbbd37c62009-11-21 08:43:09 +00002174static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2175 // check the attribute arguments.
2176 if (Attr.getNumArgs() != 0) {
2177 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2178 return;
2179 }
2180
2181 if (!isa<CXXRecordDecl>(d)
2182 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2183 S.Diag(Attr.getLoc(),
2184 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2185 : diag::warn_attribute_wrong_decl_type)
2186 << Attr.getName() << 7 /*virtual method or class*/;
2187 return;
2188 }
Sean Hunt7725e672009-11-25 04:20:27 +00002189
2190 // FIXME: Conform to C++0x redeclaration rules.
2191
2192 if (d->getAttr<FinalAttr>()) {
2193 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2194 return;
2195 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002196
Sean Huntcf807c42010-08-18 23:23:40 +00002197 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002198}
2199
Chris Lattner0744e5f2008-06-29 00:23:49 +00002200//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002201// C++0x member checking attributes
2202//===----------------------------------------------------------------------===//
2203
2204static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2205 if (Attr.getNumArgs() != 0) {
2206 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2207 return;
2208 }
2209
2210 if (!isa<CXXRecordDecl>(d)) {
2211 S.Diag(Attr.getLoc(),
2212 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2213 : diag::warn_attribute_wrong_decl_type)
2214 << Attr.getName() << 9 /*class*/;
2215 return;
2216 }
2217
2218 if (d->getAttr<BaseCheckAttr>()) {
2219 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2220 return;
2221 }
2222
Sean Huntcf807c42010-08-18 23:23:40 +00002223 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002224}
2225
2226static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2227 if (Attr.getNumArgs() != 0) {
2228 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2229 return;
2230 }
2231
2232 if (!isa<RecordDecl>(d->getDeclContext())) {
2233 // FIXME: It's not the type that's the problem
2234 S.Diag(Attr.getLoc(),
2235 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2236 : diag::warn_attribute_wrong_decl_type)
2237 << Attr.getName() << 11 /*member*/;
2238 return;
2239 }
2240
2241 // FIXME: Conform to C++0x redeclaration rules.
2242
2243 if (d->getAttr<HidingAttr>()) {
2244 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2245 return;
2246 }
2247
Sean Huntcf807c42010-08-18 23:23:40 +00002248 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002249}
2250
2251static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2252 if (Attr.getNumArgs() != 0) {
2253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2254 return;
2255 }
2256
2257 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2258 // FIXME: It's not the type that's the problem
2259 S.Diag(Attr.getLoc(),
2260 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2261 : diag::warn_attribute_wrong_decl_type)
2262 << Attr.getName() << 10 /*virtual method*/;
2263 return;
2264 }
2265
2266 // FIXME: Conform to C++0x redeclaration rules.
2267
2268 if (d->getAttr<OverrideAttr>()) {
2269 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2270 return;
2271 }
2272
Sean Huntcf807c42010-08-18 23:23:40 +00002273 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002274}
2275
2276//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002277// Checker-specific attribute handlers.
2278//===----------------------------------------------------------------------===//
2279
2280static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2281 Sema &S) {
2282
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002283 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002284
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002285 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2286 RetTy = MD->getResultType();
2287 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2288 RetTy = FD->getResultType();
2289 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002290 SourceLocation L = Attr.getLoc();
2291 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2292 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002293 return;
2294 }
Mike Stumpbf916502009-07-24 19:02:52 +00002295
Ted Kremenek6217b802009-07-29 21:53:49 +00002296 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002297 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002298 SourceLocation L = Attr.getLoc();
2299 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2300 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002301 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002302 }
Mike Stumpbf916502009-07-24 19:02:52 +00002303
Ted Kremenekb71368d2009-05-09 02:44:38 +00002304 switch (Attr.getKind()) {
2305 default:
2306 assert(0 && "invalid ownership attribute");
2307 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002308 case AttributeList::AT_cf_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002309 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002310 return;
2311 case AttributeList::AT_ns_returns_not_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002312 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002313 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002314 case AttributeList::AT_cf_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002315 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002316 return;
2317 case AttributeList::AT_ns_returns_retained:
Sean Huntcf807c42010-08-18 23:23:40 +00002318 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002319 return;
2320 };
2321}
2322
Charles Davisf0122fe2010-02-16 18:27:26 +00002323static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2324 return Attr.getKind() == AttributeList::AT_dllimport ||
2325 Attr.getKind() == AttributeList::AT_dllexport;
2326}
2327
Ted Kremenekb71368d2009-05-09 02:44:38 +00002328//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002329// Top Level Sema Entry Points
2330//===----------------------------------------------------------------------===//
2331
Sebastian Redla89d82c2008-12-21 19:24:58 +00002332/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002333/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002334/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2335/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002336static void ProcessDeclAttribute(Scope *scope, Decl *D,
2337 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002338 if (Attr.isInvalid())
2339 return;
2340
Charles Davisf0122fe2010-02-16 18:27:26 +00002341 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2342 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002343 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002344 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002345 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002346 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2347 case AttributeList::AT_IBOutletCollection:
2348 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002349 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002350 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002351 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002352 case AttributeList::AT_neon_vector_type:
2353 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002354 // Ignore these, these are type attributes, handled by
2355 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002356 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002357 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2358 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002359 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002360 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002361 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002362 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002363 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2364 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002365 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002366 HandleDependencyAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002367 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2368 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2369 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002370 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002371 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002372 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002373 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2374 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2375 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2376 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2377 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2378 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2379 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002380 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002381 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002382 case AttributeList::AT_ownership_returns:
2383 case AttributeList::AT_ownership_takes:
2384 case AttributeList::AT_ownership_holds:
2385 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002386 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002387 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2388 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2389 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002390 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002391
2392 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002393 case AttributeList::AT_ns_returns_not_retained:
2394 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002395 case AttributeList::AT_ns_returns_retained:
2396 case AttributeList::AT_cf_returns_retained:
2397 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2398
Nate Begeman6f3d8382009-06-26 06:32:41 +00002399 case AttributeList::AT_reqd_wg_size:
2400 HandleReqdWorkGroupSize(D, Attr, S); break;
2401
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002402 case AttributeList::AT_init_priority:
2403 HandleInitPriorityAttr(D, Attr, S); break;
2404
Sean Hunt7725e672009-11-25 04:20:27 +00002405 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2406 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002407 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2408 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2409 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002410 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002411 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2412 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002413 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002414 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002415 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002416 case AttributeList::AT_transparent_union:
2417 HandleTransparentUnionAttr(D, Attr, S);
2418 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002419 case AttributeList::AT_objc_exception:
2420 HandleObjCExceptionAttr(D, Attr, S);
2421 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002422 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002423 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2424 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2425 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2426 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2427 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2428 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2429 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2430 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2431 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002432 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002433 // Just ignore
2434 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002435 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2436 HandleNoInstrumentFunctionAttr(D, Attr, S);
2437 break;
John McCall04a67a62010-02-05 21:31:56 +00002438 case AttributeList::AT_stdcall:
2439 case AttributeList::AT_cdecl:
2440 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002441 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002442 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002443 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002444 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002445 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002446 // Ask target about the attribute.
2447 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2448 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002449 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2450 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002451 break;
2452 }
2453}
2454
2455/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2456/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002457void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002458 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2459 ProcessDeclAttribute(S, D, *l, *this);
2460 }
2461
2462 // GCC accepts
2463 // static int a9 __attribute__((weakref));
2464 // but that looks really pointless. We reject it.
2465 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2466 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002467 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002468 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002469 }
2470}
2471
Ryan Flynne25ff832009-07-30 03:15:39 +00002472/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2473/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002474NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002475 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002476 NamedDecl *NewD = 0;
2477 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2478 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2479 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002480 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002481 if (FD->getQualifier()) {
2482 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2483 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2484 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002485 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2486 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2487 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002488 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002489 VD->getStorageClass(),
2490 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002491 if (VD->getQualifier()) {
2492 VarDecl *NewVD = cast<VarDecl>(NewD);
2493 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2494 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002495 }
2496 return NewD;
2497}
2498
2499/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2500/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002501void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002502 if (W.getUsed()) return; // only do this once
2503 W.setUsed(true);
2504 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2505 IdentifierInfo *NDId = ND->getIdentifier();
2506 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002507 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2508 NDId->getName()));
2509 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002510 WeakTopLevelDecl.push_back(NewD);
2511 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2512 // to insert Decl at TU scope, sorry.
2513 DeclContext *SavedContext = CurContext;
2514 CurContext = Context.getTranslationUnitDecl();
2515 PushOnScopeChains(NewD, S);
2516 CurContext = SavedContext;
2517 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002518 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002519 }
2520}
2521
Chris Lattner0744e5f2008-06-29 00:23:49 +00002522/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2523/// it, apply them to D. This is a bit tricky because PD can have attributes
2524/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002525void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
John McCalld4aff0e2010-10-27 00:59:00 +00002526 // It's valid to "forward-declare" #pragma weak, in which case we
2527 // have to do this.
2528 if (!WeakUndeclaredIdentifiers.empty()) {
2529 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2530 if (IdentifierInfo *Id = ND->getIdentifier()) {
2531 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2532 = WeakUndeclaredIdentifiers.find(Id);
2533 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2534 WeakInfo W = I->second;
2535 DeclApplyPragmaWeak(S, ND, W);
2536 WeakUndeclaredIdentifiers[Id] = W;
2537 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002538 }
2539 }
2540 }
2541
Chris Lattner0744e5f2008-06-29 00:23:49 +00002542 // Apply decl attributes from the DeclSpec if present.
2543 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002544 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002545
Chris Lattner0744e5f2008-06-29 00:23:49 +00002546 // Walk the declarator structure, applying decl attributes that were in a type
2547 // position to the decl itself. This handles cases like:
2548 // int *__attr__(x)** D;
2549 // when X is a decl attribute.
2550 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2551 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002552 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002553
Chris Lattner0744e5f2008-06-29 00:23:49 +00002554 // Finally, apply any attributes on the decl itself.
2555 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002556 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002557}
John McCall54abf7d2009-11-04 02:18:39 +00002558
2559/// PushParsingDeclaration - Enter a new "scope" of deprecation
2560/// warnings.
2561///
2562/// The state token we use is the start index of this scope
2563/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002564Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002565 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002566 return (ParsingDeclStackState) DelayedDiagnostics.size();
2567}
2568
John McCalld226f652010-08-21 09:40:31 +00002569void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002570 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2571 ParsingDeclDepth--;
2572
2573 if (DelayedDiagnostics.empty())
2574 return;
2575
2576 unsigned SavedIndex = (unsigned) S;
2577 assert(SavedIndex <= DelayedDiagnostics.size() &&
2578 "saved index is out of bounds");
2579
John McCall58e6f342010-03-16 05:22:47 +00002580 unsigned E = DelayedDiagnostics.size();
2581
John McCall2f514482010-01-27 03:50:35 +00002582 // We only want to actually emit delayed diagnostics when we
2583 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002584 if (D) {
2585 // We really do want to start with 0 here. We get one push for a
2586 // decl spec and another for each declarator; in a decl group like:
2587 // deprecated_typedef foo, *bar, baz();
2588 // only the declarator pops will be passed decls. This is correct;
2589 // we really do need to consider delayed diagnostics from the decl spec
2590 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002591 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002592 if (DelayedDiagnostics[I].Triggered)
2593 continue;
2594
2595 switch (DelayedDiagnostics[I].Kind) {
2596 case DelayedDiagnostic::Deprecation:
2597 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2598 break;
2599
2600 case DelayedDiagnostic::Access:
2601 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2602 break;
2603 }
2604 }
2605 }
2606
John McCall58e6f342010-03-16 05:22:47 +00002607 // Destroy all the delayed diagnostics we're about to pop off.
2608 for (unsigned I = SavedIndex; I != E; ++I)
2609 DelayedDiagnostics[I].destroy();
2610
John McCall2f514482010-01-27 03:50:35 +00002611 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002612}
2613
2614static bool isDeclDeprecated(Decl *D) {
2615 do {
2616 if (D->hasAttr<DeprecatedAttr>())
2617 return true;
2618 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2619 return false;
2620}
2621
John McCall9c3087b2010-08-26 02:13:20 +00002622void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002623 Decl *Ctx) {
2624 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002625 return;
2626
John McCall2f514482010-01-27 03:50:35 +00002627 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002628 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002629 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002630 << DD.getDeprecationDecl()->getDeclName()
2631 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002632 else
2633 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002634 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002635}
2636
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002637void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002638 SourceLocation Loc) {
John McCall54abf7d2009-11-04 02:18:39 +00002639 // Delay if we're currently parsing a declaration.
2640 if (ParsingDeclDepth) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002641 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2642 Message));
John McCall54abf7d2009-11-04 02:18:39 +00002643 return;
2644 }
2645
2646 // Otherwise, don't warn if our current context is deprecated.
2647 if (isDeclDeprecated(cast<Decl>(CurContext)))
2648 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002649 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002650 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2651 << Message;
2652 else
2653 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002654}