blob: a1c07434ecce95cbea80e6e88be42c45ab65e399 [file] [log] [blame]
Chris Lattner2c6fcf52008-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 McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000024using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000026
Chris Lattner58418ff2008-06-29 00:16:31 +000027//===----------------------------------------------------------------------===//
28// Helper functions
29//===----------------------------------------------------------------------===//
30
Ted Kremenek527042b2009-08-14 20:49:40 +000031static const FunctionType *getFunctionType(const Decl *d,
32 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000034 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000036 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000037 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000038 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039 Ty = decl->getUnderlyingType();
40 else
41 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000042
Chris Lattner2c6fcf52008-06-26 18:38:35 +000043 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000044 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000045 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000046 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000047
John McCall9dd450b2009-09-21 23:43:11 +000048 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000049}
50
Daniel Dunbarc136e0c2008-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 Lopes518e3702009-12-20 23:11:08 +000054/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-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 Dunbar70e3eba2008-10-19 02:04:16 +000061/// type (function or function-typed variable) or an Objective-C
62/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000063static bool isFunctionOrMethod(const Decl *d) {
64 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000065}
66
Fariborz Jahanian4447e172009-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 Kremenek527042b2009-08-14 20:49:40 +000070static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-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 Jahanian960910a2009-05-19 17:08:59 +000078 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000079}
80
Daniel Dunbar70e3eba2008-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 Jahanian4447e172009-05-15 23:15:03 +000083/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000084static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000085 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000086 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000087 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000088 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-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 Kremenek527042b2009-08-14 20:49:40 +000096static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000097 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000098 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000099 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
100 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +0000101 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000102}
103
Ted Kremenek527042b2009-08-14 20:49:40 +0000104static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000105 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000106 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000107 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
108 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000109
Chris Lattnera4997152009-02-20 18:43:26 +0000110 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000111}
112
Ted Kremenek527042b2009-08-14 20:49:40 +0000113static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-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 Kremenek527042b2009-08-14 20:49:40 +0000119static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000120 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000121 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000122 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000123 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000124 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000125 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126 return cast<ObjCMethodDecl>(d)->isVariadic();
127 }
128}
129
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000130static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000131 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000132 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000133 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000134
John McCall96fa4842010-05-17 21:00:27 +0000135 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
136 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000137 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
John McCall96fa4842010-05-17 21:00:27 +0000139 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000140
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000141 // FIXME: Should we walk the chain of classes?
142 return ClsName == &Ctx.Idents.get("NSString") ||
143 ClsName == &Ctx.Idents.get("NSMutableString");
144}
145
Daniel Dunbar980c6692008-09-26 03:32:58 +0000146static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000147 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000148 if (!PT)
149 return false;
150
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000151 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000152 if (!RT)
153 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000154
Daniel Dunbar980c6692008-09-26 03:32:58 +0000155 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000156 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000157 return false;
158
159 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
160}
161
Chris Lattner58418ff2008-06-29 00:16:31 +0000162//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000163// Attribute Implementations
164//===----------------------------------------------------------------------===//
165
Daniel Dunbar032db472008-07-31 22:40:48 +0000166// FIXME: All this manual attribute parsing code is gross. At the
167// least add some helper functions to check most argument patterns (#
168// and types of args).
169
Mike Stumpd3bb5572009-07-24 19:02:52 +0000170static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000171 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000172 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
173 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000174 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000175 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000177
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000178 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000179
180 Expr *sizeExpr;
181
182 // Special case where the argument is a template id.
183 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000184 CXXScopeSpec SS;
185 UnqualifiedId id;
186 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
187 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000188 } else {
189 // check the attribute arguments.
190 if (Attr.getNumArgs() != 1) {
191 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
192 return;
193 }
194 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000195 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000196
197 // Instantiate/Install the vector type, and let Sema build the type for us.
198 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000199 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000200 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000201 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000202 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000203
Douglas Gregor758a8692009-06-17 21:51:59 +0000204 // Remember this typedef decl, we will need it later for diagnostics.
205 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000206 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000207}
208
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000209static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000210 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000211 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 return;
214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000215
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000216 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000217 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000218 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
219 // If the alignment is less than or equal to 8 bits, the packed attribute
220 // has no effect.
221 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000222 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000223 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000224 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000225 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000226 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000227 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000228 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000229}
230
Ted Kremenek1f672822010-02-18 03:08:58 +0000231static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000232 // check the attribute arguments.
233 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000234 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000235 return;
236 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000237
Ted Kremenek1f672822010-02-18 03:08:58 +0000238 // The IBAction attributes only apply to instance methods.
239 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
240 if (MD->isInstanceMethod()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000241 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000242 return;
243 }
244
245 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
246}
247
248static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
249 // check the attribute arguments.
250 if (Attr.getNumArgs() > 0) {
251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
252 return;
253 }
254
255 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000256 // Objective-C classes.
257 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000258 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000259 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000260 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000261
262 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000263}
264
Ted Kremenek26bde772010-05-19 17:38:06 +0000265static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
266 Sema &S) {
267
268 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000269 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000270 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
271 return;
272 }
273
274 // The IBOutletCollection attributes only apply to instance variables of
275 // Objective-C classes.
276 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
277 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
278 return;
279 }
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000280 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
281 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
282 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
283 << VD->getType() << 0;
284 return;
285 }
286 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
287 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
288 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
289 << PD->getType() << 1;
290 return;
291 }
292
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000293 IdentifierInfo *II = Attr.getParameterName();
294 if (!II)
295 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000296
John McCallba7bf592010-08-24 05:47:05 +0000297 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000298 S.getScopeForContext(d->getDeclContext()->getParent()));
299 if (!TypeRep) {
300 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
301 return;
302 }
John McCallba7bf592010-08-24 05:47:05 +0000303 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000304 // Diagnose use of non-object type in iboutletcollection attribute.
305 // FIXME. Gnu attribute extension ignores use of builtin types in
306 // attributes. So, __attribute__((iboutletcollection(char))) will be
307 // treated as __attribute__((iboutletcollection())).
308 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
309 !QT->isObjCObjectType()) {
310 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
311 return;
312 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000313 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
314 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000315}
316
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000317static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000318 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
319 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000320 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000321 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000322 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000323 return;
324 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000325
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000326 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000327
328 // The nonnull attribute only applies to pointers.
329 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000330
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000331 for (AttributeList::arg_iterator I=Attr.arg_begin(),
332 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000333
334
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000335 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000336 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000337 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000338 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
339 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000340 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
341 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000342 return;
343 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000344
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000345 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000346
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000347 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000348 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000349 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000350 return;
351 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000352
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000353 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000354
355 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000356 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000357 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000358 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000359 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000360 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000361 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000362 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000363
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000364 NonNullArgs.push_back(x);
365 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000366
367 // If no arguments were specified to __attribute__((nonnull)) then all pointer
368 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000369 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000370 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
371 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000372 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000373 NonNullArgs.push_back(I);
Fariborz Jahanian3567c422010-09-27 22:42:37 +0000374 else if (const RecordType *UT = T->getAsUnionType()) {
375 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
376 RecordDecl *UD = UT->getDecl();
377 for (RecordDecl::field_iterator it = UD->field_begin(),
378 itend = UD->field_end(); it != itend; ++it) {
379 T = it->getType();
380 if (T->isAnyPointerType() || T->isBlockPointerType()) {
381 NonNullArgs.push_back(I);
382 break;
383 }
384 }
385 }
386 }
Ted Kremenek5fa50522008-11-18 06:52:58 +0000387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000388
Ted Kremenek22813f42010-10-21 18:49:36 +0000389 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000390 if (NonNullArgs.empty()) {
391 // Warn the trivial case only if attribute is not coming from a
392 // macro instantiation.
393 if (Attr.getLoc().isFileID())
394 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000395 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000396 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000397 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000398
399 unsigned* start = &NonNullArgs[0];
400 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000401 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000402 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
403 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000404}
405
Ted Kremenekd21139a2010-07-31 01:52:11 +0000406static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
407 // This attribute must be applied to a function declaration.
408 // The first argument to the attribute must be a string,
409 // the name of the resource, for example "malloc".
410 // The following arguments must be argument indexes, the arguments must be
411 // of integer type for Returns, otherwise of pointer type.
412 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000413 // after being held. free() should be __attribute((ownership_takes)), whereas
414 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000415
416 if (!AL.getParameterName()) {
417 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
418 << AL.getName()->getName() << 1;
419 return;
420 }
421 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000422 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000423 switch (AL.getKind()) {
424 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000425 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000426 if (AL.getNumArgs() < 1) {
427 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
428 return;
429 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000430 break;
431 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000432 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000433 if (AL.getNumArgs() < 1) {
434 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
435 return;
436 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000437 break;
438 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000439 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000440 if (AL.getNumArgs() > 1) {
441 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
442 << AL.getNumArgs() + 1;
443 return;
444 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000445 break;
446 default:
447 // This should never happen given how we are called.
448 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000449 }
450
451 if (!isFunction(d) || !hasFunctionProto(d)) {
452 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
453 << 0 /*function*/;
454 return;
455 }
456
457 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
458
459 llvm::StringRef Module = AL.getParameterName()->getName();
460
461 // Normalize the argument, __foo__ becomes foo.
462 if (Module.startswith("__") && Module.endswith("__"))
463 Module = Module.substr(2, Module.size() - 4);
464
465 llvm::SmallVector<unsigned, 10> OwnershipArgs;
466
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000467 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
468 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000469
470 Expr *IdxExpr = static_cast<Expr *>(*I);
471 llvm::APSInt ArgNum(32);
472 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
473 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
474 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
475 << AL.getName()->getName() << IdxExpr->getSourceRange();
476 continue;
477 }
478
479 unsigned x = (unsigned) ArgNum.getZExtValue();
480
481 if (x > NumArgs || x < 1) {
482 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
483 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
484 continue;
485 }
486 --x;
487 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000488 case OwnershipAttr::Takes:
489 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000490 // Is the function argument a pointer type?
491 QualType T = getFunctionOrMethodArgType(d, x);
492 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
493 // FIXME: Should also highlight argument in decl.
494 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000495 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000496 << "pointer"
497 << IdxExpr->getSourceRange();
498 continue;
499 }
500 break;
501 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000502 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000503 if (AL.getNumArgs() > 1) {
504 // Is the function argument an integer type?
505 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
506 llvm::APSInt ArgNum(32);
507 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
508 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
509 S.Diag(AL.getLoc(), diag::err_ownership_type)
510 << "ownership_returns" << "integer"
511 << IdxExpr->getSourceRange();
512 return;
513 }
514 }
515 break;
516 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000517 default:
518 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000519 } // switch
520
521 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000522 for (specific_attr_iterator<OwnershipAttr>
523 i = d->specific_attr_begin<OwnershipAttr>(),
524 e = d->specific_attr_end<OwnershipAttr>();
525 i != e; ++i) {
526 if ((*i)->getOwnKind() != K) {
527 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
528 I!=E; ++I) {
529 if (x == *I) {
530 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
531 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000532 }
533 }
534 }
535 }
536 OwnershipArgs.push_back(x);
537 }
538
539 unsigned* start = OwnershipArgs.data();
540 unsigned size = OwnershipArgs.size();
541 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000542
543 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
544 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
545 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000546 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000547
548 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
549 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000550}
551
Rafael Espindolac18086a2010-02-23 22:00:30 +0000552static bool isStaticVarOrStaticFunciton(Decl *D) {
553 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000554 return VD->getStorageClass() == SC_Static;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000555 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000556 return FD->getStorageClass() == SC_Static;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000557 return false;
558}
559
560static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
561 // Check the attribute arguments.
562 if (Attr.getNumArgs() > 1) {
563 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
564 return;
565 }
566
567 // gcc rejects
568 // class c {
569 // static int a __attribute__((weakref ("v2")));
570 // static int b() __attribute__((weakref ("f3")));
571 // };
572 // and ignores the attributes of
573 // void f(void) {
574 // static int a __attribute__((weakref ("v2")));
575 // }
576 // we reject them
Sebastian Redl50c68252010-08-31 00:36:30 +0000577 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
578 if (!Ctx->isFileContext()) {
579 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
580 dyn_cast<NamedDecl>(d)->getNameAsString();
581 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000582 }
583
584 // The GCC manual says
585 //
586 // At present, a declaration to which `weakref' is attached can only
587 // be `static'.
588 //
589 // It also says
590 //
591 // Without a TARGET,
592 // given as an argument to `weakref' or to `alias', `weakref' is
593 // equivalent to `weak'.
594 //
595 // gcc 4.4.1 will accept
596 // int a7 __attribute__((weakref));
597 // as
598 // int a7 __attribute__((weak));
599 // This looks like a bug in gcc. We reject that for now. We should revisit
600 // it if this behaviour is actually used.
601
602 if (!isStaticVarOrStaticFunciton(d)) {
603 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
604 dyn_cast<NamedDecl>(d)->getNameAsString();
605 return;
606 }
607
608 // GCC rejects
609 // static ((alias ("y"), weakref)).
610 // Should we? How to check that weakref is before or after alias?
611
612 if (Attr.getNumArgs() == 1) {
613 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
614 Arg = Arg->IgnoreParenCasts();
615 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
616
617 if (Str == 0 || Str->isWide()) {
618 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
619 << "weakref" << 1;
620 return;
621 }
622 // GCC will accept anything as the argument of weakref. Should we
623 // check for an existing decl?
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000624 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000625 }
626
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000627 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000628}
629
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000630static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000631 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000632 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000633 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000634 return;
635 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000636
Chris Lattner4a927cb2008-06-28 23:36:30 +0000637 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000638 Arg = Arg->IgnoreParenCasts();
639 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000640
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000641 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000642 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000643 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000644 return;
645 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000646
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000647 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000648
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000649 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000650}
651
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000652static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000653 Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000654 // Check the attribute arguments.
Daniel Dunbar03a38442008-10-28 00:17:57 +0000655 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000657 return;
658 }
Anders Carlsson88097122009-02-19 19:16:48 +0000659
Chris Lattner4225e232009-04-14 17:02:11 +0000660 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000661 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000662 << Attr.getName() << 0 /*function*/;
663 return;
664 }
665
666 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
667}
668
669static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
670 Sema &S) {
671 // Check the attribute arguments.
672 if (Attr.getNumArgs() != 0) {
673 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
674 return;
675 }
676
677 if (!isa<FunctionDecl>(d)) {
678 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
679 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000680 return;
681 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000682
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000683 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000684}
685
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000686static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000687 // Check the attribute arguments.
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000688 if (Attr.getNumArgs() != 0) {
689 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
690 return;
691 }
Mike Stump11289f42009-09-09 15:08:12 +0000692
Ted Kremenek08479ae2009-08-15 00:51:46 +0000693 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000694 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000695 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000696 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000697 return;
698 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000699 }
700
Ted Kremenek08479ae2009-08-15 00:51:46 +0000701 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000702}
703
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000704static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000705 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
706 assert(Attr.isInvalid() == false);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000707 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000708}
709
710static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
711 Sema &S) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000712
713 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
714 // because 'analyzer_noreturn' does not impact the type.
715
716 if (Attr.getNumArgs() != 0) {
717 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
718 return;
719 }
720
721 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
722 ValueDecl *VD = dyn_cast<ValueDecl>(d);
723 if (VD == 0 || (!VD->getType()->isBlockPointerType()
724 && !VD->getType()->isFunctionPointerType())) {
725 S.Diag(Attr.getLoc(),
726 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
727 : diag::warn_attribute_wrong_decl_type)
728 << Attr.getName() << 0 /*function*/;
729 return;
730 }
731 }
732
733 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000734}
735
John Thompsoncdb847ba2010-08-09 21:53:52 +0000736// PS3 PPU-specific.
737static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
738 Sema &S) {
739/*
740 Returning a Vector Class in Registers
741
742 According to the PPU ABI specifications, a class with a single member of vector type is returned in
743 memory when used as the return value of a function. This results in inefficient code when implementing
744 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
745 definition. This attribute is also applicable to struct types.
746
747 Example:
748
749 struct Vector
750 {
751 __vector float xyzw;
752 } __attribute__((vecreturn));
753
754 Vector Add(Vector lhs, Vector rhs)
755 {
756 Vector result;
757 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
758 return result; // This will be returned in a register
759 }
760*/
John Thompson9a587aaa2010-09-18 01:12:07 +0000761 if (!isa<RecordDecl>(d)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000762 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
763 << Attr.getName() << 9 /*class*/;
764 return;
765 }
766
767 if (d->getAttr<VecReturnAttr>()) {
768 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
769 return;
770 }
771
John Thompson9a587aaa2010-09-18 01:12:07 +0000772 RecordDecl *record = cast<RecordDecl>(d);
773 int count = 0;
774
775 if (!isa<CXXRecordDecl>(record)) {
776 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
777 return;
778 }
779
780 if (!cast<CXXRecordDecl>(record)->isPOD()) {
781 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
782 return;
783 }
784
785 for (RecordDecl::field_iterator iter = record->field_begin(); iter != record->field_end(); iter++) {
786 if ((count == 1) || !iter->getType()->isVectorType()) {
787 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
788 return;
789 }
790 count++;
791 }
792
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000793 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000794}
795
Alexis Hunt96d5c762009-11-21 08:43:09 +0000796static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
797 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
798 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000799 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000800 return;
801 }
802 // FIXME: Actually store the attribute on the declaration
803}
804
Ted Kremenek39c59a82008-07-25 04:39:19 +0000805static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
806 // check the attribute arguments.
807 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000809 return;
810 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000811
John McCallcef15822010-03-31 02:47:45 +0000812 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
813 !isa<TypeDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000814 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000815 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000816 return;
817 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000818
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000819 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000820}
821
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000822static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
823 // check the attribute arguments.
824 if (Attr.getNumArgs() != 0) {
825 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
826 return;
827 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000828
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000829 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000830 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000831 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
832 return;
833 }
834 } else if (!isFunctionOrMethod(d)) {
835 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000836 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000837 return;
838 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000839
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000840 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000841}
842
Daniel Dunbar032db472008-07-31 22:40:48 +0000843static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
844 // check the attribute arguments.
845 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
847 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000848 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000849 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000850
851 int priority = 65535; // FIXME: Do not hardcode such constants.
852 if (Attr.getNumArgs() > 0) {
853 Expr *E = static_cast<Expr *>(Attr.getArg(0));
854 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000855 if (E->isTypeDependent() || E->isValueDependent() ||
856 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000857 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000858 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000859 return;
860 }
861 priority = Idx.getZExtValue();
862 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000863
Chris Lattner4225e232009-04-14 17:02:11 +0000864 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000866 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000867 return;
868 }
869
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000870 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000871}
872
873static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
874 // check the attribute arguments.
875 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000876 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
877 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000878 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000879 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000880
881 int priority = 65535; // FIXME: Do not hardcode such constants.
882 if (Attr.getNumArgs() > 0) {
883 Expr *E = static_cast<Expr *>(Attr.getArg(0));
884 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000885 if (E->isTypeDependent() || E->isValueDependent() ||
886 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000887 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000888 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000889 return;
890 }
891 priority = Idx.getZExtValue();
892 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000893
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000894 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000895 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000896 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000897 return;
898 }
899
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000900 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000901}
902
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000903static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000904 // check the attribute arguments.
Fariborz Jahanian551063102010-10-06 21:18:44 +0000905 int noArgs = Attr.getNumArgs();
906 if (noArgs > 1) {
907 S.Diag(Attr.getLoc(),
908 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000909 return;
910 }
Fariborz Jahanian551063102010-10-06 21:18:44 +0000911 // Handle the case where deprecated attribute has a text message.
912 StringLiteral *SE;
913 if (noArgs == 1) {
914 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
915 SE = dyn_cast<StringLiteral>(ArgExpr);
916 if (!SE) {
917 S.Diag(ArgExpr->getLocStart(),
918 diag::err_attribute_not_string) << "deprecated";
919 return;
920 }
921 }
922 else
923 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000924
Fariborz Jahanian551063102010-10-06 21:18:44 +0000925 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
926 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000927}
928
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000929static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
930 // check the attribute arguments.
Fariborz Jahanianc74073c2010-10-06 23:12:32 +0000931 int noArgs = Attr.getNumArgs();
932 if (noArgs > 1) {
933 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000934 return;
935 }
Fariborz Jahanianc74073c2010-10-06 23:12:32 +0000936 // Handle the case where unavailable attribute has a text message.
937 StringLiteral *SE;
938 if (noArgs == 1) {
939 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
940 SE = dyn_cast<StringLiteral>(ArgExpr);
941 if (!SE) {
942 S.Diag(ArgExpr->getLocStart(),
943 diag::err_attribute_not_string) << "unavailable";
944 return;
945 }
946 }
947 else
948 SE = StringLiteral::CreateEmpty(S.Context, 1);
949 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
950 SE->getString()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000951}
952
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000953static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000954 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000955 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000956 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000957 return;
958 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000959
Chris Lattner4a927cb2008-06-28 23:36:30 +0000960 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000961 Arg = Arg->IgnoreParenCasts();
962 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000963
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000964 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000966 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000967 return;
968 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000969
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000970 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000971 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000972
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000973 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000974 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000975 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000976 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000977 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000978 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000979 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000980 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000981 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000982 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000983 return;
984 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000985
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000986 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000987}
988
Chris Lattner677a3582009-02-14 08:09:34 +0000989static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
990 Sema &S) {
991 if (Attr.getNumArgs() != 0) {
992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
993 return;
994 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000995
Chris Lattner677a3582009-02-14 08:09:34 +0000996 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
997 if (OCI == 0) {
998 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
999 return;
1000 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001001
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001002 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001003}
1004
1005static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001006 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001007 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001008 return;
1009 }
Chris Lattner677a3582009-02-14 08:09:34 +00001010 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001011 QualType T = TD->getUnderlyingType();
1012 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001013 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001014 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1015 return;
1016 }
1017 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001018 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001019}
1020
Mike Stumpd3bb5572009-07-24 19:02:52 +00001021static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001022HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1023 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001024 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001025 return;
1026 }
1027
1028 if (!isa<FunctionDecl>(D)) {
1029 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1030 return;
1031 }
1032
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001033 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001034}
1035
Steve Naroff3405a732008-09-18 16:44:58 +00001036static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001037 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001039 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001040 return;
1041 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001042
Steve Naroff3405a732008-09-18 16:44:58 +00001043 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001044 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001045 return;
1046 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001047
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001048 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001049 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001050 type = BlocksAttr::ByRef;
1051 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001052 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001053 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001054 return;
1055 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001056
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001057 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001058}
1059
Anders Carlssonc181b012008-10-05 18:05:59 +00001060static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1061 // check the attribute arguments.
1062 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +00001063 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1064 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +00001065 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001066 }
1067
Anders Carlssonc181b012008-10-05 18:05:59 +00001068 int sentinel = 0;
1069 if (Attr.getNumArgs() > 0) {
1070 Expr *E = static_cast<Expr *>(Attr.getArg(0));
1071 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001072 if (E->isTypeDependent() || E->isValueDependent() ||
1073 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001074 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001075 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001076 return;
1077 }
1078 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001079
Anders Carlssonc181b012008-10-05 18:05:59 +00001080 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001081 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1082 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001083 return;
1084 }
1085 }
1086
1087 int nullPos = 0;
1088 if (Attr.getNumArgs() > 1) {
1089 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1090 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001091 if (E->isTypeDependent() || E->isValueDependent() ||
1092 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001093 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001094 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001095 return;
1096 }
1097 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001098
Anders Carlssonc181b012008-10-05 18:05:59 +00001099 if (nullPos > 1 || nullPos < 0) {
1100 // FIXME: This error message could be improved, it would be nice
1101 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001102 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1103 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001104 return;
1105 }
1106 }
1107
1108 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001109 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001110 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001111
Chris Lattner9363e312009-03-17 23:03:47 +00001112 if (isa<FunctionNoProtoType>(FT)) {
1113 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1114 return;
1115 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001116
Chris Lattner9363e312009-03-17 23:03:47 +00001117 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001118 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001119 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001120 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001121 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1122 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001123 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001124 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001125 }
1126 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001127 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1128 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001129 ;
1130 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001131 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001132 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001133 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +00001134 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001135 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001136 int m = Ty->isFunctionPointerType() ? 0 : 1;
1137 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001138 return;
1139 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001140 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001141 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001142 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001143 return;
1144 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001145 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001146 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001147 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +00001148 return;
1149 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001150 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001151}
1152
Chris Lattner237f2752009-02-14 07:37:35 +00001153static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1154 // check the attribute arguments.
1155 if (Attr.getNumArgs() != 0) {
1156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1157 return;
1158 }
1159
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001160 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001162 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +00001163 return;
1164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001165
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001166 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1167 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1168 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001169 return;
1170 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001171 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1172 if (MD->getResultType()->isVoidType()) {
1173 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1174 << Attr.getName() << 1;
1175 return;
1176 }
1177
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001178 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001179}
1180
1181static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001182 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001183 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001184 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001185 return;
1186 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001187
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001188 /* weak only applies to non-static declarations */
Rafael Espindolac18086a2010-02-23 22:00:30 +00001189 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001190 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1191 dyn_cast<NamedDecl>(D)->getNameAsString();
1192 return;
1193 }
1194
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001195 // TODO: could also be applied to methods?
1196 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1197 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001198 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001199 return;
1200 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001201
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001202 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001203}
1204
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001205static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1206 // check the attribute arguments.
1207 if (Attr.getNumArgs() != 0) {
1208 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1209 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001210 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001211
1212 // weak_import only applies to variable & function declarations.
1213 bool isDef = false;
1214 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1215 isDef = (!VD->hasExternalStorage() || VD->getInit());
1216 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001217 isDef = FD->hasBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +00001218 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1219 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +00001220 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +00001221 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001222 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001223 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001224 !isa<ObjCInterfaceDecl>(D))
1225 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001226 << Attr.getName() << 2 /*variable and function*/;
1227 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001228 }
1229
1230 // Merge should handle any subsequent violations.
1231 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001232 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001233 diag::warn_attribute_weak_import_invalid_on_definition)
1234 << "weak_import" << 2 /*variable and function*/;
1235 return;
1236 }
1237
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001238 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001239}
1240
Nate Begemanf2758702009-06-26 06:32:41 +00001241static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1242 Sema &S) {
1243 // Attribute has 3 arguments.
1244 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001246 return;
1247 }
1248
1249 unsigned WGSize[3];
1250 for (unsigned i = 0; i < 3; ++i) {
1251 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1252 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001253 if (E->isTypeDependent() || E->isValueDependent() ||
1254 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001255 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1256 << "reqd_work_group_size" << E->getSourceRange();
1257 return;
1258 }
1259 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1260 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001261 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1262 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001263 WGSize[2]));
1264}
1265
Chris Lattner237f2752009-02-14 07:37:35 +00001266static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001267 // Attribute has no arguments.
1268 if (Attr.getNumArgs() != 1) {
1269 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1270 return;
1271 }
1272
1273 // Make sure that there is a string literal as the sections's single
1274 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +00001275 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1276 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001277 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001278 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001279 return;
1280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Chris Lattner30ba6742009-08-10 19:03:04 +00001282 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001283 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001284 if (!Error.empty()) {
1285 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1286 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001287 return;
1288 }
Mike Stump11289f42009-09-09 15:08:12 +00001289
Chris Lattner20aee9b2010-01-12 20:58:53 +00001290 // This attribute cannot be applied to local variables.
1291 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1292 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1293 return;
1294 }
1295
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001296 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001297}
1298
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001299
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001300static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001301 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001302 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001304 return;
1305 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001306
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001307 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001308}
1309
Anders Carlssonb8316282008-10-05 23:32:53 +00001310static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1311 // check the attribute arguments.
1312 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001314 return;
1315 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001316
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001317 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001318}
1319
1320static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1321 // check the attribute arguments.
1322 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001323 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001324 return;
1325 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001326
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001327 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001328}
1329
Anders Carlssond277d792009-01-31 01:16:18 +00001330static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001331 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1333 return;
1334 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001335
Anders Carlssond277d792009-01-31 01:16:18 +00001336 if (Attr.getNumArgs() != 0) {
1337 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1338 return;
1339 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001340
Anders Carlssond277d792009-01-31 01:16:18 +00001341 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001342
Anders Carlssond277d792009-01-31 01:16:18 +00001343 if (!VD || !VD->hasLocalStorage()) {
1344 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1345 return;
1346 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001347
Anders Carlssond277d792009-01-31 01:16:18 +00001348 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001349 // FIXME: Lookup probably isn't looking in the right place
1350 // FIXME: The lookup source location should be in the attribute, not the
1351 // start of the attribute.
John McCall9f3059a2009-10-09 21:13:30 +00001352 NamedDecl *CleanupDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001353 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCall9f3059a2009-10-09 21:13:30 +00001354 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001355 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001356 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001357 Attr.getParameterName();
1358 return;
1359 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001360
Anders Carlssond277d792009-01-31 01:16:18 +00001361 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1362 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001363 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001364 Attr.getParameterName();
1365 return;
1366 }
1367
Anders Carlssond277d792009-01-31 01:16:18 +00001368 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001369 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001370 Attr.getParameterName();
1371 return;
1372 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001373
Anders Carlsson723f55d2009-02-07 23:16:50 +00001374 // We're currently more strict than GCC about what function types we accept.
1375 // If this ever proves to be a problem it should be easy to fix.
1376 QualType Ty = S.Context.getPointerType(VD->getType());
1377 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall8cb679e2010-11-15 09:13:47 +00001378 CastKind K;
1379 if (S.CheckAssignmentConstraints(ParamTy, Ty, K) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001380 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001381 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1382 Attr.getParameterName() << ParamTy << Ty;
1383 return;
1384 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001385
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001386 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001387}
1388
Mike Stumpd3bb5572009-07-24 19:02:52 +00001389/// Handle __attribute__((format_arg((idx)))) attribute based on
1390/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1391static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001392 if (Attr.getNumArgs() != 1) {
1393 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1394 return;
1395 }
1396 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1397 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1398 << Attr.getName() << 0 /*function*/;
1399 return;
1400 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001401 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1402 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001403 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1404 unsigned FirstIdx = 1;
1405 // checks for the 2nd argument
1406 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1407 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001408 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1409 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001410 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1411 << "format" << 2 << IdxExpr->getSourceRange();
1412 return;
1413 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001414
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001415 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1416 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1417 << "format" << 2 << IdxExpr->getSourceRange();
1418 return;
1419 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001420
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001421 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001422
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001423 // make sure the format string is really a string
1424 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001425
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001426 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1427 if (not_nsstring_type &&
1428 !isCFStringType(Ty, S.Context) &&
1429 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001430 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001431 // FIXME: Should highlight the actual expression that has the wrong type.
1432 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001433 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001434 << IdxExpr->getSourceRange();
1435 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001436 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001437 Ty = getFunctionOrMethodResultType(d);
1438 if (!isNSStringType(Ty, S.Context) &&
1439 !isCFStringType(Ty, S.Context) &&
1440 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001441 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001442 // FIXME: Should highlight the actual expression that has the wrong type.
1443 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001444 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001445 << IdxExpr->getSourceRange();
1446 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001447 }
1448
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001449 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001450}
1451
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001452enum FormatAttrKind {
1453 CFStringFormat,
1454 NSStringFormat,
1455 StrftimeFormat,
1456 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001457 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001458 InvalidFormat
1459};
1460
1461/// getFormatAttrKind - Map from format attribute names to supported format
1462/// types.
1463static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1464 // Check for formats that get handled specially.
1465 if (Format == "NSString")
1466 return NSStringFormat;
1467 if (Format == "CFString")
1468 return CFStringFormat;
1469 if (Format == "strftime")
1470 return StrftimeFormat;
1471
1472 // Otherwise, check for supported formats.
1473 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1474 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1475 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1476 Format == "zcmn_err")
1477 return SupportedFormat;
1478
Duncan Sandsde4fe352010-03-23 14:44:19 +00001479 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1480 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001481 return IgnoredFormat;
1482
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001483 return InvalidFormat;
1484}
1485
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001486/// Handle __attribute__((init_priority(priority))) attributes based on
1487/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1488static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1489 Sema &S) {
1490 if (!S.getLangOptions().CPlusPlus) {
1491 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1492 return;
1493 }
1494
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001495 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1496 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1497 Attr.setInvalid();
1498 return;
1499 }
1500 QualType T = dyn_cast<VarDecl>(d)->getType();
1501 if (S.Context.getAsArrayType(T))
1502 T = S.Context.getBaseElementType(T);
1503 if (!T->getAs<RecordType>()) {
1504 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1505 Attr.setInvalid();
1506 return;
1507 }
1508
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001509 if (Attr.getNumArgs() != 1) {
1510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1511 Attr.setInvalid();
1512 return;
1513 }
1514 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001515
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001516 llvm::APSInt priority(32);
1517 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1518 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1519 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1520 << "init_priority" << priorityExpr->getSourceRange();
1521 Attr.setInvalid();
1522 return;
1523 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001524 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001525 if (prioritynum < 101 || prioritynum > 65535) {
1526 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1527 << priorityExpr->getSourceRange();
1528 Attr.setInvalid();
1529 return;
1530 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001531 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001532}
1533
Mike Stumpd3bb5572009-07-24 19:02:52 +00001534/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1535/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001536static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001537
Chris Lattner4a927cb2008-06-28 23:36:30 +00001538 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001540 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001541 return;
1542 }
1543
Chris Lattner4a927cb2008-06-28 23:36:30 +00001544 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001546 return;
1547 }
1548
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001549 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001550 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001551 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001552 return;
1553 }
1554
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001555 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001556 unsigned FirstIdx = 1;
1557
Daniel Dunbar07d07852009-10-18 21:17:35 +00001558 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001559
1560 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001561 if (Format.startswith("__") && Format.endswith("__"))
1562 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001563
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001564 // Check for supported formats.
1565 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001566
1567 if (Kind == IgnoredFormat)
1568 return;
1569
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001570 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001571 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001572 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001573 return;
1574 }
1575
1576 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001577 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001578 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001579 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1580 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001581 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001582 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001583 return;
1584 }
1585
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001586 // FIXME: We should handle the implicit 'this' parameter in a more generic
1587 // way that can be used for other arguments.
1588 bool HasImplicitThisParam = false;
1589 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1590 if (MD->isInstance()) {
1591 HasImplicitThisParam = true;
1592 NumArgs++;
1593 }
1594 }
Mike Stump11289f42009-09-09 15:08:12 +00001595
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001596 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001597 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001598 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001599 return;
1600 }
1601
1602 // FIXME: Do we need to bounds check?
1603 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001604
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001605 if (HasImplicitThisParam) {
1606 if (ArgIdx == 0) {
1607 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1608 << "a string type" << IdxExpr->getSourceRange();
1609 return;
1610 }
1611 ArgIdx--;
1612 }
Mike Stump11289f42009-09-09 15:08:12 +00001613
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001614 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001615 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001616
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001617 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001618 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001619 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1620 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001621 return;
1622 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001623 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001624 // FIXME: do we need to check if the type is NSString*? What are the
1625 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001626 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001627 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001628 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1629 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001630 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001631 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001632 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001633 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001634 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001635 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1636 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001637 return;
1638 }
1639
1640 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001641 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001642 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001643 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1644 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001645 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001646 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001647 return;
1648 }
1649
1650 // check if the function is variadic if the 3rd argument non-zero
1651 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001652 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001653 ++NumArgs; // +1 for ...
1654 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001655 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001656 return;
1657 }
1658 }
1659
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001660 // strftime requires FirstArg to be 0 because it doesn't read from any
1661 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001662 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001663 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001664 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1665 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001666 return;
1667 }
1668 // if 0 it disables parameter checking (to use with e.g. va_list)
1669 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001670 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001671 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001672 return;
1673 }
1674
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001675 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1676 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001677 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001678}
1679
Chris Lattnera663a0a2008-06-29 00:28:59 +00001680static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1681 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001682 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001683 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001685 return;
1686 }
1687
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001688 // Try to find the underlying union declaration.
1689 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001690 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001691 if (TD && TD->getUnderlyingType()->isUnionType())
1692 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1693 else
1694 RD = dyn_cast<RecordDecl>(d);
1695
1696 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001697 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001698 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001699 return;
1700 }
1701
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001702 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001703 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001704 diag::warn_transparent_union_attribute_not_definition);
1705 return;
1706 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001707
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001708 RecordDecl::field_iterator Field = RD->field_begin(),
1709 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001710 if (Field == FieldEnd) {
1711 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1712 return;
1713 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001714
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001715 FieldDecl *FirstField = *Field;
1716 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001717 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001718 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001719 diag::warn_transparent_union_attribute_floating)
1720 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001721 return;
1722 }
1723
1724 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1725 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1726 for (; Field != FieldEnd; ++Field) {
1727 QualType FieldType = Field->getType();
1728 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1729 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1730 // Warn if we drop the attribute.
1731 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001732 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001733 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001734 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001735 diag::warn_transparent_union_attribute_field_size_align)
1736 << isSize << Field->getDeclName() << FieldBits;
1737 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001738 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001739 diag::note_transparent_union_first_field_size_align)
1740 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001741 return;
1742 }
1743 }
1744
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001745 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001746}
1747
Chris Lattnera663a0a2008-06-29 00:28:59 +00001748static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001749 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001750 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001752 return;
1753 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001754 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1755 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001756
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001757 // Make sure that there is a string literal as the annotation's single
1758 // argument.
1759 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001760 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001761 return;
1762 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001763 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001764}
1765
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001766static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001767 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001768 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001770 return;
1771 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001772
1773 //FIXME: The C++0x version of this attribute has more limited applicabilty
1774 // than GNU's, and should error out when it is used to specify a
1775 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001776
Chris Lattner4a927cb2008-06-28 23:36:30 +00001777 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001778 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001779 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001780 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001781
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001782 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1783}
1784
1785void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1786 if (E->isTypeDependent() || E->isValueDependent()) {
1787 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001788 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001789 return;
1790 }
1791
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001792 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00001793 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001794 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1795 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1796 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001797 return;
1798 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001799 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001800 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1801 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001802 return;
1803 }
1804
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001805 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1806}
1807
1808void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1809 // FIXME: Cache the number on the Attr object if non-dependent?
1810 // FIXME: Perform checking of type validity
1811 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1812 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001813}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001814
Mike Stumpd3bb5572009-07-24 19:02:52 +00001815/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1816/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001817///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001818/// Despite what would be logical, the mode attribute is a decl attribute, not a
1819/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1820/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001821static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001822 // This attribute isn't documented, but glibc uses it. It changes
1823 // the width of an int or unsigned int to the specified size.
1824
1825 // Check that there aren't any arguments
1826 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001827 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001828 return;
1829 }
1830
1831 IdentifierInfo *Name = Attr.getParameterName();
1832 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001833 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001834 return;
1835 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001836
Daniel Dunbar07d07852009-10-18 21:17:35 +00001837 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001838
1839 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001840 if (Str.startswith("__") && Str.endswith("__"))
1841 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001842
1843 unsigned DestWidth = 0;
1844 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001845 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001846 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001847 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001848 switch (Str[0]) {
1849 case 'Q': DestWidth = 8; break;
1850 case 'H': DestWidth = 16; break;
1851 case 'S': DestWidth = 32; break;
1852 case 'D': DestWidth = 64; break;
1853 case 'X': DestWidth = 96; break;
1854 case 'T': DestWidth = 128; break;
1855 }
1856 if (Str[1] == 'F') {
1857 IntegerMode = false;
1858 } else if (Str[1] == 'C') {
1859 IntegerMode = false;
1860 ComplexMode = true;
1861 } else if (Str[1] != 'I') {
1862 DestWidth = 0;
1863 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001864 break;
1865 case 4:
1866 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1867 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001868 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001869 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001870 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001871 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001872 break;
1873 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001874 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001875 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001876 break;
1877 }
1878
1879 QualType OldTy;
1880 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1881 OldTy = TD->getUnderlyingType();
1882 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1883 OldTy = VD->getType();
1884 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001885 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1886 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001887 return;
1888 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001889
John McCall9dd450b2009-09-21 23:43:11 +00001890 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001891 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1892 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00001893 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001894 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1895 } else if (ComplexMode) {
1896 if (!OldTy->isComplexType())
1897 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1898 } else {
1899 if (!OldTy->isFloatingType())
1900 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1901 }
1902
Mike Stump87c57ac2009-05-16 07:39:55 +00001903 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1904 // and friends, at least with glibc.
1905 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1906 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001907 // FIXME: Make sure floating-point mappings are accurate
1908 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001909 QualType NewTy;
1910 switch (DestWidth) {
1911 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001912 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001913 return;
1914 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001915 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001916 return;
1917 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001918 if (!IntegerMode) {
1919 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1920 return;
1921 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001922 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001923 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001924 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001925 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001926 break;
1927 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001928 if (!IntegerMode) {
1929 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1930 return;
1931 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001932 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001933 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001934 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001935 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001936 break;
1937 case 32:
1938 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001939 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001940 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001941 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001942 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001943 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001944 break;
1945 case 64:
1946 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001947 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001948 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00001949 if (S.Context.Target.getLongWidth() == 64)
1950 NewTy = S.Context.LongTy;
1951 else
1952 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001953 else
Chandler Carruth72343702010-01-26 06:39:24 +00001954 if (S.Context.Target.getLongWidth() == 64)
1955 NewTy = S.Context.UnsignedLongTy;
1956 else
1957 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001958 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001959 case 96:
1960 NewTy = S.Context.LongDoubleTy;
1961 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001962 case 128:
1963 if (!IntegerMode) {
1964 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1965 return;
1966 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00001967 if (OldTy->isSignedIntegerType())
1968 NewTy = S.Context.Int128Ty;
1969 else
1970 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001971 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001972 }
1973
Eli Friedman4735374e2009-03-03 06:41:03 +00001974 if (ComplexMode) {
1975 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001976 }
1977
1978 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001979 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1980 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001981 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001982 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001983 cast<ValueDecl>(D)->setType(NewTy);
1984}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001985
Mike Stump3722f582009-08-26 22:31:08 +00001986static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001987 // check the attribute arguments.
1988 if (Attr.getNumArgs() > 0) {
1989 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1990 return;
1991 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001992
Anders Carlsson88097122009-02-19 19:16:48 +00001993 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001994 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001995 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001996 return;
1997 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001998
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001999 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002000}
2001
Mike Stump3722f582009-08-26 22:31:08 +00002002static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00002003 // check the attribute arguments.
2004 if (Attr.getNumArgs() != 0) {
2005 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2006 return;
2007 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002008
Chris Lattner4225e232009-04-14 17:02:11 +00002009 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002010 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002011 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00002012 return;
2013 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002014
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002015 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002016}
2017
Chris Lattner3c77a352010-06-22 00:03:40 +00002018static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2019 Sema &S) {
2020 // check the attribute arguments.
2021 if (Attr.getNumArgs() != 0) {
2022 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2023 return;
2024 }
2025
2026 if (!isa<FunctionDecl>(d)) {
2027 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2028 << Attr.getName() << 0 /*function*/;
2029 return;
2030 }
2031
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002032 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002033}
2034
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002035static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002036 // check the attribute arguments.
2037 if (Attr.getNumArgs() != 0) {
2038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2039 return;
2040 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002041
Chris Lattner4225e232009-04-14 17:02:11 +00002042 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2043 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002044 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002045 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002046 return;
2047 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002048
Douglas Gregor35b57532009-10-27 21:01:01 +00002049 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002050 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002051 return;
2052 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002053
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002054 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002055}
2056
Abramo Bagnara50099372010-04-30 13:10:51 +00002057static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2058 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2059 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2060 assert(Attr.isInvalid() == false);
2061
2062 switch (Attr.getKind()) {
2063 case AttributeList::AT_fastcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002064 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002065 return;
2066 case AttributeList::AT_stdcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002067 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002068 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002069 case AttributeList::AT_thiscall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002070 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002071 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002072 case AttributeList::AT_cdecl:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002073 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002074 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002075 case AttributeList::AT_pascal:
2076 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2077 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002078 default:
2079 llvm_unreachable("unexpected attribute kind");
2080 return;
2081 }
2082}
2083
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002084static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2085 // check the attribute arguments.
2086 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00002087 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002088 return;
2089 }
Eli Friedman7044b762009-03-27 21:06:47 +00002090
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002091 if (!isFunctionOrMethod(d)) {
2092 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002093 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002094 return;
2095 }
Eli Friedman7044b762009-03-27 21:06:47 +00002096
2097 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2098 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002099 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2100 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman7044b762009-03-27 21:06:47 +00002101 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2102 << "regparm" << NumParamsExpr->getSourceRange();
2103 return;
2104 }
2105
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002106 if (S.Context.Target.getRegParmMax() == 0) {
2107 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002108 << NumParamsExpr->getSourceRange();
2109 return;
2110 }
2111
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00002112 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002113 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2114 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00002115 return;
2116 }
2117
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002118 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2119 NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002120}
2121
Alexis Hunt96d5c762009-11-21 08:43:09 +00002122static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2123 // check the attribute arguments.
2124 if (Attr.getNumArgs() != 0) {
2125 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2126 return;
2127 }
2128
2129 if (!isa<CXXRecordDecl>(d)
2130 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2131 S.Diag(Attr.getLoc(),
2132 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2133 : diag::warn_attribute_wrong_decl_type)
2134 << Attr.getName() << 7 /*virtual method or class*/;
2135 return;
2136 }
Alexis Hunt54a02542009-11-25 04:20:27 +00002137
2138 // FIXME: Conform to C++0x redeclaration rules.
2139
2140 if (d->getAttr<FinalAttr>()) {
2141 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2142 return;
2143 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002144
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002145 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002146}
2147
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002148//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00002149// C++0x member checking attributes
2150//===----------------------------------------------------------------------===//
2151
2152static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2153 if (Attr.getNumArgs() != 0) {
2154 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2155 return;
2156 }
2157
2158 if (!isa<CXXRecordDecl>(d)) {
2159 S.Diag(Attr.getLoc(),
2160 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2161 : diag::warn_attribute_wrong_decl_type)
2162 << Attr.getName() << 9 /*class*/;
2163 return;
2164 }
2165
2166 if (d->getAttr<BaseCheckAttr>()) {
2167 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2168 return;
2169 }
2170
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002171 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002172}
2173
2174static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2175 if (Attr.getNumArgs() != 0) {
2176 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2177 return;
2178 }
2179
2180 if (!isa<RecordDecl>(d->getDeclContext())) {
2181 // FIXME: It's not the type that's the problem
2182 S.Diag(Attr.getLoc(),
2183 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2184 : diag::warn_attribute_wrong_decl_type)
2185 << Attr.getName() << 11 /*member*/;
2186 return;
2187 }
2188
2189 // FIXME: Conform to C++0x redeclaration rules.
2190
2191 if (d->getAttr<HidingAttr>()) {
2192 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2193 return;
2194 }
2195
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002196 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002197}
2198
2199static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2200 if (Attr.getNumArgs() != 0) {
2201 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2202 return;
2203 }
2204
2205 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2206 // FIXME: It's not the type that's the problem
2207 S.Diag(Attr.getLoc(),
2208 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2209 : diag::warn_attribute_wrong_decl_type)
2210 << Attr.getName() << 10 /*virtual method*/;
2211 return;
2212 }
2213
2214 // FIXME: Conform to C++0x redeclaration rules.
2215
2216 if (d->getAttr<OverrideAttr>()) {
2217 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2218 return;
2219 }
2220
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002221 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002222}
2223
2224//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002225// Checker-specific attribute handlers.
2226//===----------------------------------------------------------------------===//
2227
2228static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2229 Sema &S) {
2230
Ted Kremenek3b204e42009-05-13 21:07:32 +00002231 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002232
Ted Kremenek3b204e42009-05-13 21:07:32 +00002233 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2234 RetTy = MD->getResultType();
2235 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2236 RetTy = FD->getResultType();
2237 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002238 SourceLocation L = Attr.getLoc();
2239 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2240 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002241 return;
2242 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002243
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002244 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00002245 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002246 SourceLocation L = Attr.getLoc();
2247 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2248 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002249 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002250 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002251
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002252 switch (Attr.getKind()) {
2253 default:
2254 assert(0 && "invalid ownership attribute");
2255 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002256 case AttributeList::AT_cf_returns_not_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002257 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002258 return;
2259 case AttributeList::AT_ns_returns_not_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002260 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002261 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002262 case AttributeList::AT_cf_returns_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002263 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002264 return;
2265 case AttributeList::AT_ns_returns_retained:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002266 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(), S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002267 return;
2268 };
2269}
2270
Charles Davis163855f2010-02-16 18:27:26 +00002271static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2272 return Attr.getKind() == AttributeList::AT_dllimport ||
2273 Attr.getKind() == AttributeList::AT_dllexport;
2274}
2275
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002276//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002277// Top Level Sema Entry Points
2278//===----------------------------------------------------------------------===//
2279
Sebastian Redlfc24b632008-12-21 19:24:58 +00002280/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002281/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00002282/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2283/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00002284static void ProcessDeclAttribute(Scope *scope, Decl *D,
2285 const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002286 if (Attr.isInvalid())
2287 return;
2288
Charles Davis163855f2010-02-16 18:27:26 +00002289 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2290 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00002291 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002292 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002293 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002294 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2295 case AttributeList::AT_IBOutletCollection:
2296 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002297 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002298 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002299 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002300 case AttributeList::AT_neon_vector_type:
2301 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002302 // Ignore these, these are type attributes, handled by
2303 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002304 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002305 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2306 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002307 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002308 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002309 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002310 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002311 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2312 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002313 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002314 HandleDependencyAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002315 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2316 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2317 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002318 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002319 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002320 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002321 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2322 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2323 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2324 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2325 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2326 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2327 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2328 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002329 case AttributeList::AT_ownership_returns:
2330 case AttributeList::AT_ownership_takes:
2331 case AttributeList::AT_ownership_holds:
2332 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00002333 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002334 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2335 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2336 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002337 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002338
2339 // Checker-specific.
Ted Kremenekd9c66632010-02-18 00:05:45 +00002340 case AttributeList::AT_ns_returns_not_retained:
2341 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002342 case AttributeList::AT_ns_returns_retained:
2343 case AttributeList::AT_cf_returns_retained:
2344 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2345
Nate Begemanf2758702009-06-26 06:32:41 +00002346 case AttributeList::AT_reqd_wg_size:
2347 HandleReqdWorkGroupSize(D, Attr, S); break;
2348
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002349 case AttributeList::AT_init_priority:
2350 HandleInitPriorityAttr(D, Attr, S); break;
2351
Alexis Hunt54a02542009-11-25 04:20:27 +00002352 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2353 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002354 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2355 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2356 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002357 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002358 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2359 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002360 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002361 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002362 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002363 case AttributeList::AT_transparent_union:
2364 HandleTransparentUnionAttr(D, Attr, S);
2365 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002366 case AttributeList::AT_objc_exception:
2367 HandleObjCExceptionAttr(D, Attr, S);
2368 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002369 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002370 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2371 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2372 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2373 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2374 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2375 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2376 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2377 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2378 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002379 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002380 // Just ignore
2381 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002382 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2383 HandleNoInstrumentFunctionAttr(D, Attr, S);
2384 break;
John McCallab26cfa2010-02-05 21:31:56 +00002385 case AttributeList::AT_stdcall:
2386 case AttributeList::AT_cdecl:
2387 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002388 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002389 case AttributeList::AT_pascal:
Abramo Bagnara50099372010-04-30 13:10:51 +00002390 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002391 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002392 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002393 // Ask target about the attribute.
2394 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2395 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002396 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2397 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002398 break;
2399 }
2400}
2401
2402/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2403/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00002404void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002405 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2406 ProcessDeclAttribute(S, D, *l, *this);
2407 }
2408
2409 // GCC accepts
2410 // static int a9 __attribute__((weakref));
2411 // but that looks really pointless. We reject it.
2412 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2413 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002414 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002415 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002416 }
2417}
2418
Ryan Flynn7d470f32009-07-30 03:15:39 +00002419/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2420/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002421NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002422 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002423 NamedDecl *NewD = 0;
2424 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2425 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2426 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002427 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002428 if (FD->getQualifier()) {
2429 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2430 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2431 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002432 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2433 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2434 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002435 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002436 VD->getStorageClass(),
2437 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002438 if (VD->getQualifier()) {
2439 VarDecl *NewVD = cast<VarDecl>(NewD);
2440 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2441 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002442 }
2443 return NewD;
2444}
2445
2446/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2447/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002448void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002449 if (W.getUsed()) return; // only do this once
2450 W.setUsed(true);
2451 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2452 IdentifierInfo *NDId = ND->getIdentifier();
2453 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002454 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2455 NDId->getName()));
2456 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00002457 WeakTopLevelDecl.push_back(NewD);
2458 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2459 // to insert Decl at TU scope, sorry.
2460 DeclContext *SavedContext = CurContext;
2461 CurContext = Context.getTranslationUnitDecl();
2462 PushOnScopeChains(NewD, S);
2463 CurContext = SavedContext;
2464 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002465 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002466 }
2467}
2468
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002469/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2470/// it, apply them to D. This is a bit tricky because PD can have attributes
2471/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002472void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
John McCall6fe02402010-10-27 00:59:00 +00002473 // It's valid to "forward-declare" #pragma weak, in which case we
2474 // have to do this.
2475 if (!WeakUndeclaredIdentifiers.empty()) {
2476 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2477 if (IdentifierInfo *Id = ND->getIdentifier()) {
2478 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2479 = WeakUndeclaredIdentifiers.find(Id);
2480 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2481 WeakInfo W = I->second;
2482 DeclApplyPragmaWeak(S, ND, W);
2483 WeakUndeclaredIdentifiers[Id] = W;
2484 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002485 }
2486 }
2487 }
2488
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002489 // Apply decl attributes from the DeclSpec if present.
2490 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002491 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002492
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002493 // Walk the declarator structure, applying decl attributes that were in a type
2494 // position to the decl itself. This handles cases like:
2495 // int *__attr__(x)** D;
2496 // when X is a decl attribute.
2497 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2498 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002499 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002500
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002501 // Finally, apply any attributes on the decl itself.
2502 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002503 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002504}
John McCall28a6aea2009-11-04 02:18:39 +00002505
2506/// PushParsingDeclaration - Enter a new "scope" of deprecation
2507/// warnings.
2508///
2509/// The state token we use is the start index of this scope
2510/// on the warning stack.
John McCallfaf5fb42010-08-26 23:41:50 +00002511Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall28a6aea2009-11-04 02:18:39 +00002512 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002513 return (ParsingDeclStackState) DelayedDiagnostics.size();
2514}
2515
John McCall48871652010-08-21 09:40:31 +00002516void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall86121512010-01-27 03:50:35 +00002517 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2518 ParsingDeclDepth--;
2519
2520 if (DelayedDiagnostics.empty())
2521 return;
2522
2523 unsigned SavedIndex = (unsigned) S;
2524 assert(SavedIndex <= DelayedDiagnostics.size() &&
2525 "saved index is out of bounds");
2526
John McCall1064d7e2010-03-16 05:22:47 +00002527 unsigned E = DelayedDiagnostics.size();
2528
John McCall86121512010-01-27 03:50:35 +00002529 // We only want to actually emit delayed diagnostics when we
2530 // successfully parsed a decl.
John McCall86121512010-01-27 03:50:35 +00002531 if (D) {
2532 // We really do want to start with 0 here. We get one push for a
2533 // decl spec and another for each declarator; in a decl group like:
2534 // deprecated_typedef foo, *bar, baz();
2535 // only the declarator pops will be passed decls. This is correct;
2536 // we really do need to consider delayed diagnostics from the decl spec
2537 // for each of the different declarations.
John McCall1064d7e2010-03-16 05:22:47 +00002538 for (unsigned I = 0; I != E; ++I) {
John McCall86121512010-01-27 03:50:35 +00002539 if (DelayedDiagnostics[I].Triggered)
2540 continue;
2541
2542 switch (DelayedDiagnostics[I].Kind) {
2543 case DelayedDiagnostic::Deprecation:
2544 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2545 break;
2546
2547 case DelayedDiagnostic::Access:
2548 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2549 break;
2550 }
2551 }
2552 }
2553
John McCall1064d7e2010-03-16 05:22:47 +00002554 // Destroy all the delayed diagnostics we're about to pop off.
2555 for (unsigned I = SavedIndex; I != E; ++I)
2556 DelayedDiagnostics[I].destroy();
2557
John McCall86121512010-01-27 03:50:35 +00002558 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002559}
2560
2561static bool isDeclDeprecated(Decl *D) {
2562 do {
2563 if (D->hasAttr<DeprecatedAttr>())
2564 return true;
2565 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2566 return false;
2567}
2568
John McCallb45a1e72010-08-26 02:13:20 +00002569void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00002570 Decl *Ctx) {
2571 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002572 return;
2573
John McCall86121512010-01-27 03:50:35 +00002574 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002575 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00002576 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002577 << DD.getDeprecationDecl()->getDeclName()
2578 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002579 else
2580 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002581 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002582}
2583
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002584void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian551063102010-10-06 21:18:44 +00002585 SourceLocation Loc) {
John McCall28a6aea2009-11-04 02:18:39 +00002586 // Delay if we're currently parsing a declaration.
2587 if (ParsingDeclDepth) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00002588 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2589 Message));
John McCall28a6aea2009-11-04 02:18:39 +00002590 return;
2591 }
2592
2593 // Otherwise, don't warn if our current context is deprecated.
2594 if (isDeclDeprecated(cast<Decl>(CurContext)))
2595 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002596 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00002597 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2598 << Message;
2599 else
2600 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002601}