blob: 7125c36a13005a4ae5fd3bc7b1df3eb7085656e7 [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
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000014#include "clang/Sema/Sema.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"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000022using namespace clang;
23
Chris Lattner58418ff2008-06-29 00:16:31 +000024//===----------------------------------------------------------------------===//
25// Helper functions
26//===----------------------------------------------------------------------===//
27
Ted Kremenek527042b2009-08-14 20:49:40 +000028static const FunctionType *getFunctionType(const Decl *d,
29 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000030 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000031 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000032 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000033 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000034 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000035 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000036 Ty = decl->getUnderlyingType();
37 else
38 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000039
Chris Lattner2c6fcf52008-06-26 18:38:35 +000040 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000041 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000042 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000043 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000044
John McCall9dd450b2009-09-21 23:43:11 +000045 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000046}
47
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000048// FIXME: We should provide an abstraction around a method or function
49// to provide the following bits of information.
50
Nuno Lopes518e3702009-12-20 23:11:08 +000051/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000052/// type (function or function-typed variable).
53static bool isFunction(const Decl *d) {
54 return getFunctionType(d, false) != NULL;
55}
56
57/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000058/// type (function or function-typed variable) or an Objective-C
59/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000060static bool isFunctionOrMethod(const Decl *d) {
61 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000062}
63
Fariborz Jahanian4447e172009-05-15 23:15:03 +000064/// isFunctionOrMethodOrBlock - Return true if the given decl has function
65/// type (function or function-typed variable) or an Objective-C
66/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000067static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000068 if (isFunctionOrMethod(d))
69 return true;
70 // check for block is more involved.
71 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
72 QualType Ty = V->getType();
73 return Ty->isBlockPointerType();
74 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000075 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000076}
77
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000078/// hasFunctionProto - Return true if the given decl has a argument
79/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000080/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000081static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000082 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000083 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000084 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000085 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000086 return true;
87 }
88}
89
90/// getFunctionOrMethodNumArgs - Return number of function or method
91/// arguments. It is an error to call this on a K&R function (use
92/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000093static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +000098 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000099}
100
Ted Kremenek527042b2009-08-14 20:49:40 +0000101static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000102 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000103 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000104 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
105 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000106
Chris Lattnera4997152009-02-20 18:43:26 +0000107 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000108}
109
Ted Kremenek527042b2009-08-14 20:49:40 +0000110static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000111 if (const FunctionType *FnTy = getFunctionType(d))
112 return cast<FunctionProtoType>(FnTy)->getResultType();
113 return cast<ObjCMethodDecl>(d)->getResultType();
114}
115
Ted Kremenek527042b2009-08-14 20:49:40 +0000116static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000117 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000118 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000119 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000120 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000121 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000122 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000123 return cast<ObjCMethodDecl>(d)->isVariadic();
124 }
125}
126
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000127static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000128 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000129 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000130 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000131
John McCall96fa4842010-05-17 21:00:27 +0000132 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
133 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000134 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000135
John McCall96fa4842010-05-17 21:00:27 +0000136 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000137
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000138 // FIXME: Should we walk the chain of classes?
139 return ClsName == &Ctx.Idents.get("NSString") ||
140 ClsName == &Ctx.Idents.get("NSMutableString");
141}
142
Daniel Dunbar980c6692008-09-26 03:32:58 +0000143static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000144 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000145 if (!PT)
146 return false;
147
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000148 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000149 if (!RT)
150 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000151
Daniel Dunbar980c6692008-09-26 03:32:58 +0000152 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000153 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000154 return false;
155
156 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
157}
158
Chris Lattner58418ff2008-06-29 00:16:31 +0000159//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000160// Attribute Implementations
161//===----------------------------------------------------------------------===//
162
Daniel Dunbar032db472008-07-31 22:40:48 +0000163// FIXME: All this manual attribute parsing code is gross. At the
164// least add some helper functions to check most argument patterns (#
165// and types of args).
166
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000168 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000169 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
170 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000171 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000172 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000173 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000174
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000175 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000176
177 Expr *sizeExpr;
178
179 // Special case where the argument is a template id.
180 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000181 CXXScopeSpec SS;
182 UnqualifiedId id;
183 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
184 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000185 } else {
186 // check the attribute arguments.
187 if (Attr.getNumArgs() != 1) {
188 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
189 return;
190 }
191 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000192 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000193
194 // Instantiate/Install the vector type, and let Sema build the type for us.
195 // This will run the reguired checks.
196 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
197 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000198 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000199 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000200
Douglas Gregor758a8692009-06-17 21:51:59 +0000201 // Remember this typedef decl, we will need it later for diagnostics.
202 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000203 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000204}
205
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000206static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000207 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000208 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000210 return;
211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000212
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlsson68e0b682009-08-08 18:23:56 +0000214 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000215 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
216 // If the alignment is less than or equal to 8 bits, the packed attribute
217 // has no effect.
218 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000219 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000220 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000221 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000222 else
Anders Carlsson68e0b682009-08-08 18:23:56 +0000223 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000224 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000225 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000226}
227
Ted Kremenek1f672822010-02-18 03:08:58 +0000228static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000229 // check the attribute arguments.
230 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000232 return;
233 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000234
Ted Kremenek1f672822010-02-18 03:08:58 +0000235 // The IBAction attributes only apply to instance methods.
236 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
237 if (MD->isInstanceMethod()) {
238 d->addAttr(::new (S.Context) IBActionAttr());
239 return;
240 }
241
242 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
243}
244
245static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
246 // check the attribute arguments.
247 if (Attr.getNumArgs() > 0) {
248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
249 return;
250 }
251
252 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000253 // Objective-C classes.
254 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000255 d->addAttr(::new (S.Context) IBOutletAttr());
256 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000257 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000258
259 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000260}
261
Ted Kremenek26bde772010-05-19 17:38:06 +0000262static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
263 Sema &S) {
264
265 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000266 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
268 return;
269 }
270
271 // The IBOutletCollection attributes only apply to instance variables of
272 // Objective-C classes.
273 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
274 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
275 return;
276 }
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000277 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
278 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
279 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
280 << VD->getType() << 0;
281 return;
282 }
283 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
284 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
285 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
286 << PD->getType() << 1;
287 return;
288 }
289
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000290 IdentifierInfo *II = Attr.getParameterName();
291 if (!II)
292 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000293
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000294 Sema::TypeTy *TypeRep = S.getTypeName(*II, Attr.getLoc(),
295 S.getScopeForContext(d->getDeclContext()->getParent()));
296 if (!TypeRep) {
297 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
298 return;
299 }
300 QualType QT(QualType::getFromOpaquePtr(TypeRep));
301 // Diagnose use of non-object type in iboutletcollection attribute.
302 // FIXME. Gnu attribute extension ignores use of builtin types in
303 // attributes. So, __attribute__((iboutletcollection(char))) will be
304 // treated as __attribute__((iboutletcollection())).
305 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
306 !QT->isObjCObjectType()) {
307 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
308 return;
309 }
310 d->addAttr(::new (S.Context) IBOutletCollectionAttr(QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000311}
312
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000313static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000314 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
315 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000316 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000317 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000318 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000319 return;
320 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000321
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000322 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000323
324 // The nonnull attribute only applies to pointers.
325 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000326
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000327 for (AttributeList::arg_iterator I=Attr.arg_begin(),
328 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000329
330
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000331 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000332 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000333 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000334 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
335 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
337 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000338 return;
339 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000340
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000341 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000342
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000343 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000345 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000346 return;
347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000348
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000349 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000350
351 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000352 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000353 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000354 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000355 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000356 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000357 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000358 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000359
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000360 NonNullArgs.push_back(x);
361 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000362
363 // If no arguments were specified to __attribute__((nonnull)) then all pointer
364 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000365 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000366 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
367 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000368 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000369 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000370 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000371
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000372 if (NonNullArgs.empty()) {
373 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
374 return;
375 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000376 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000377
378 unsigned* start = &NonNullArgs[0];
379 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000380 llvm::array_pod_sort(start, start + size);
Ted Kremenek510ee252010-02-11 07:31:47 +0000381 d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000382}
383
Ted Kremenekd21139a2010-07-31 01:52:11 +0000384static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
385 // This attribute must be applied to a function declaration.
386 // The first argument to the attribute must be a string,
387 // the name of the resource, for example "malloc".
388 // The following arguments must be argument indexes, the arguments must be
389 // of integer type for Returns, otherwise of pointer type.
390 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000391 // after being held. free() should be __attribute((ownership_takes)), whereas
392 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000393
394 if (!AL.getParameterName()) {
395 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
396 << AL.getName()->getName() << 1;
397 return;
398 }
399 // Figure out our Kind, and check arguments while we're at it.
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000400 attr::Kind K;
401 switch (AL.getKind()) {
402 case AttributeList::AT_ownership_takes:
403 K = attr::OwnershipTakes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000404 if (AL.getNumArgs() < 1) {
405 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
406 return;
407 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000408 break;
409 case AttributeList::AT_ownership_holds:
410 K = attr::OwnershipHolds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000411 if (AL.getNumArgs() < 1) {
412 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
413 return;
414 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000415 break;
416 case AttributeList::AT_ownership_returns:
417 K = attr::OwnershipReturns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000418 if (AL.getNumArgs() > 1) {
419 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
420 << AL.getNumArgs() + 1;
421 return;
422 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000423 break;
424 default:
425 // This should never happen given how we are called.
426 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000427 }
428
429 if (!isFunction(d) || !hasFunctionProto(d)) {
430 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
431 << 0 /*function*/;
432 return;
433 }
434
435 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
436
437 llvm::StringRef Module = AL.getParameterName()->getName();
438
439 // Normalize the argument, __foo__ becomes foo.
440 if (Module.startswith("__") && Module.endswith("__"))
441 Module = Module.substr(2, Module.size() - 4);
442
443 llvm::SmallVector<unsigned, 10> OwnershipArgs;
444
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000445 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
446 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000447
448 Expr *IdxExpr = static_cast<Expr *>(*I);
449 llvm::APSInt ArgNum(32);
450 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
451 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
452 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
453 << AL.getName()->getName() << IdxExpr->getSourceRange();
454 continue;
455 }
456
457 unsigned x = (unsigned) ArgNum.getZExtValue();
458
459 if (x > NumArgs || x < 1) {
460 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
461 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
462 continue;
463 }
464 --x;
465 switch (K) {
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000466 case attr::OwnershipTakes:
467 case attr::OwnershipHolds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000468 // Is the function argument a pointer type?
469 QualType T = getFunctionOrMethodArgType(d, x);
470 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
471 // FIXME: Should also highlight argument in decl.
472 S.Diag(AL.getLoc(), diag::err_ownership_type)
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000473 << ((K==attr::OwnershipTakes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000474 << "pointer"
475 << IdxExpr->getSourceRange();
476 continue;
477 }
478 break;
479 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000480 case attr::OwnershipReturns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000481 if (AL.getNumArgs() > 1) {
482 // Is the function argument an integer type?
483 Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0));
484 llvm::APSInt ArgNum(32);
485 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
486 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
487 S.Diag(AL.getLoc(), diag::err_ownership_type)
488 << "ownership_returns" << "integer"
489 << IdxExpr->getSourceRange();
490 return;
491 }
492 }
493 break;
494 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000495 default:
496 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000497 } // switch
498
499 // Check we don't have a conflict with another ownership attribute.
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000500 if (K != attr::OwnershipReturns && d->hasAttrs()) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000501 for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) {
502 if (const OwnershipAttr* Att = dyn_cast<OwnershipAttr>(attr)) {
503 // Two ownership attributes of the same kind can't conflict,
504 // except returns attributes.
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000505 if (Att->getKind() != K) {
506 for (const unsigned *I = Att->begin(), *E = Att->end(); I!=E; ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000507 if (x == *I) {
508 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
509 << AL.getName()->getName() << "ownership_*";
510 }
511 }
512 }
513 }
514 }
515 }
516 OwnershipArgs.push_back(x);
517 }
518
519 unsigned* start = OwnershipArgs.data();
520 unsigned size = OwnershipArgs.size();
521 llvm::array_pod_sort(start, start + size);
522 switch (K) {
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000523 case attr::OwnershipTakes: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000524 if (OwnershipArgs.empty()) {
525 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
526 return;
527 }
528 d->addAttr(::new (S.Context) OwnershipTakesAttr(S.Context, start, size,
529 Module));
530 break;
531 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000532 case attr::OwnershipHolds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000533 if (OwnershipArgs.empty()) {
534 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
535 return;
536 }
537 d->addAttr(::new (S.Context) OwnershipHoldsAttr(S.Context, start, size,
538 Module));
539 break;
540 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000541 case attr::OwnershipReturns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000542 d->addAttr(::new (S.Context) OwnershipReturnsAttr(S.Context, start, size,
543 Module));
544 break;
545 }
546 default:
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000547 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000548 }
549}
550
Rafael Espindolac18086a2010-02-23 22:00:30 +0000551static bool isStaticVarOrStaticFunciton(Decl *D) {
552 if (VarDecl *VD = dyn_cast<VarDecl>(D))
553 return VD->getStorageClass() == VarDecl::Static;
554 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
555 return FD->getStorageClass() == FunctionDecl::Static;
556 return false;
557}
558
559static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
560 // Check the attribute arguments.
561 if (Attr.getNumArgs() > 1) {
562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
563 return;
564 }
565
566 // gcc rejects
567 // class c {
568 // static int a __attribute__((weakref ("v2")));
569 // static int b() __attribute__((weakref ("f3")));
570 // };
571 // and ignores the attributes of
572 // void f(void) {
573 // static int a __attribute__((weakref ("v2")));
574 // }
575 // we reject them
576 if (const DeclContext *Ctx = d->getDeclContext()) {
577 Ctx = Ctx->getLookupContext();
578 if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) {
579 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +0000580 dyn_cast<NamedDecl>(d)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +0000581 return;
582 }
583 }
584
585 // The GCC manual says
586 //
587 // At present, a declaration to which `weakref' is attached can only
588 // be `static'.
589 //
590 // It also says
591 //
592 // Without a TARGET,
593 // given as an argument to `weakref' or to `alias', `weakref' is
594 // equivalent to `weak'.
595 //
596 // gcc 4.4.1 will accept
597 // int a7 __attribute__((weakref));
598 // as
599 // int a7 __attribute__((weak));
600 // This looks like a bug in gcc. We reject that for now. We should revisit
601 // it if this behaviour is actually used.
602
603 if (!isStaticVarOrStaticFunciton(d)) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
605 dyn_cast<NamedDecl>(d)->getNameAsString();
606 return;
607 }
608
609 // GCC rejects
610 // static ((alias ("y"), weakref)).
611 // Should we? How to check that weakref is before or after alias?
612
613 if (Attr.getNumArgs() == 1) {
614 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
615 Arg = Arg->IgnoreParenCasts();
616 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
617
618 if (Str == 0 || Str->isWide()) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
620 << "weakref" << 1;
621 return;
622 }
623 // GCC will accept anything as the argument of weakref. Should we
624 // check for an existing decl?
625 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
626 }
627
628 d->addAttr(::new (S.Context) WeakRefAttr());
629}
630
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000631static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000632 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000633 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000635 return;
636 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000637
Chris Lattner4a927cb2008-06-28 23:36:30 +0000638 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000639 Arg = Arg->IgnoreParenCasts();
640 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000641
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000642 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000643 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000644 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000645 return;
646 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000647
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000648 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000649
Ted Kremenek7f4945a2010-02-11 05:28:37 +0000650 d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000651}
652
Mike Stumpd3bb5572009-07-24 19:02:52 +0000653static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000654 Sema &S) {
655 // check the attribute arguments.
656 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000657 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000658 return;
659 }
Anders Carlsson88097122009-02-19 19:16:48 +0000660
Chris Lattner4225e232009-04-14 17:02:11 +0000661 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000662 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000663 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000664 return;
665 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000666
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000667 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000668}
669
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000670static void HandleMallocAttr(Decl *d, const AttributeList &Attr, 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 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremenek08479ae2009-08-15 00:51:46 +0000677 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000678 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000679 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
680 d->addAttr(::new (S.Context) MallocAttr());
681 return;
682 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000683 }
684
Ted Kremenek08479ae2009-08-15 00:51:46 +0000685 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000686}
687
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000688static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Abramo Bagnara50099372010-04-30 13:10:51 +0000689 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000690 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000691 if (Attr.getNumArgs() != 0) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000693 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000694 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000695
Mike Stump88788fe2009-04-29 19:03:13 +0000696 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
697 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stumpfeb19452009-12-15 03:11:10 +0000698 if (VD == 0 || (!VD->getType()->isBlockPointerType()
699 && !VD->getType()->isFunctionPointerType())) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000700 S.Diag(Attr.getLoc(),
701 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
702 : diag::warn_attribute_wrong_decl_type)
703 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000704 return false;
705 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000706 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000707
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000708 return true;
709}
710
711static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000712 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
713 assert(Attr.isInvalid() == false);
714 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000715}
716
717static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
718 Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000719 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000720 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000721}
722
John Thompsoncdb847ba2010-08-09 21:53:52 +0000723// PS3 PPU-specific.
724static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
725 Sema &S) {
726/*
727 Returning a Vector Class in Registers
728
729 According to the PPU ABI specifications, a class with a single member of vector type is returned in
730 memory when used as the return value of a function. This results in inefficient code when implementing
731 vector classes. To return the value in a single vector register, add the vecreturn attribute to the class
732 definition. This attribute is also applicable to struct types.
733
734 Example:
735
736 struct Vector
737 {
738 __vector float xyzw;
739 } __attribute__((vecreturn));
740
741 Vector Add(Vector lhs, Vector rhs)
742 {
743 Vector result;
744 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
745 return result; // This will be returned in a register
746 }
747*/
748 if (!isa<CXXRecordDecl>(d)) {
749 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
750 << Attr.getName() << 9 /*class*/;
751 return;
752 }
753
754 if (d->getAttr<VecReturnAttr>()) {
755 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
756 return;
757 }
758
759 d->addAttr(::new (S.Context) VecReturnAttr());
760}
761
Alexis Hunt96d5c762009-11-21 08:43:09 +0000762static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
763 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
764 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000765 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000766 return;
767 }
768 // FIXME: Actually store the attribute on the declaration
769}
770
Ted Kremenek39c59a82008-07-25 04:39:19 +0000771static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
772 // check the attribute arguments.
773 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000774 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000775 return;
776 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000777
John McCallcef15822010-03-31 02:47:45 +0000778 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
779 !isa<TypeDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000780 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000781 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000782 return;
783 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000784
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000785 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000786}
787
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000788static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
789 // check the attribute arguments.
790 if (Attr.getNumArgs() != 0) {
791 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
792 return;
793 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000794
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000795 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000796 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000797 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
798 return;
799 }
800 } else if (!isFunctionOrMethod(d)) {
801 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000802 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000803 return;
804 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000805
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000806 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000807}
808
Daniel Dunbar032db472008-07-31 22:40:48 +0000809static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
810 // check the attribute arguments.
811 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000812 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
813 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000814 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000815 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000816
817 int priority = 65535; // FIXME: Do not hardcode such constants.
818 if (Attr.getNumArgs() > 0) {
819 Expr *E = static_cast<Expr *>(Attr.getArg(0));
820 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000821 if (E->isTypeDependent() || E->isValueDependent() ||
822 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000823 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000824 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000825 return;
826 }
827 priority = Idx.getZExtValue();
828 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000829
Chris Lattner4225e232009-04-14 17:02:11 +0000830 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000831 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000832 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000833 return;
834 }
835
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000836 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000837}
838
839static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
840 // check the attribute arguments.
841 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000842 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
843 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000844 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000845 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000846
847 int priority = 65535; // FIXME: Do not hardcode such constants.
848 if (Attr.getNumArgs() > 0) {
849 Expr *E = static_cast<Expr *>(Attr.getArg(0));
850 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000851 if (E->isTypeDependent() || E->isValueDependent() ||
852 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000854 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000855 return;
856 }
857 priority = Idx.getZExtValue();
858 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000859
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000860 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000861 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000862 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000863 return;
864 }
865
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000866 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000867}
868
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000869static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000870 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000871 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000872 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000873 return;
874 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000875
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000876 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000877}
878
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000879static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
880 // check the attribute arguments.
881 if (Attr.getNumArgs() != 0) {
882 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
883 return;
884 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000885
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000886 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000887}
888
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000889static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000890 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000891 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000893 return;
894 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000895
Chris Lattner4a927cb2008-06-28 23:36:30 +0000896 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000897 Arg = Arg->IgnoreParenCasts();
898 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000899
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000900 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000902 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000903 return;
904 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000905
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000906 llvm::StringRef TypeStr = Str->getString();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000907 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000908
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000909 if (TypeStr == "default")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000910 type = VisibilityAttr::DefaultVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000911 else if (TypeStr == "hidden")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000912 type = VisibilityAttr::HiddenVisibility;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000913 else if (TypeStr == "internal")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000914 type = VisibilityAttr::HiddenVisibility; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +0000915 else if (TypeStr == "protected")
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000916 type = VisibilityAttr::ProtectedVisibility;
917 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000918 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000919 return;
920 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000921
Eli Friedman570024a2010-08-05 06:57:20 +0000922 d->addAttr(::new (S.Context) VisibilityAttr(type, false));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000923}
924
Chris Lattner677a3582009-02-14 08:09:34 +0000925static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
926 Sema &S) {
927 if (Attr.getNumArgs() != 0) {
928 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
929 return;
930 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000931
Chris Lattner677a3582009-02-14 08:09:34 +0000932 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
933 if (OCI == 0) {
934 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
935 return;
936 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000937
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000938 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000939}
940
941static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000942 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +0000943 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000944 return;
945 }
Chris Lattner677a3582009-02-14 08:09:34 +0000946 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000947 QualType T = TD->getUnderlyingType();
948 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000949 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000950 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
951 return;
952 }
953 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000954 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000955}
956
Mike Stumpd3bb5572009-07-24 19:02:52 +0000957static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000958HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
959 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +0000960 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000961 return;
962 }
963
964 if (!isa<FunctionDecl>(D)) {
965 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
966 return;
967 }
968
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000969 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000970}
971
Steve Naroff3405a732008-09-18 16:44:58 +0000972static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000973 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000974 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000975 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000976 return;
977 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000978
Steve Naroff3405a732008-09-18 16:44:58 +0000979 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000980 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000981 return;
982 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000983
Steve Naroff3405a732008-09-18 16:44:58 +0000984 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000985 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000986 type = BlocksAttr::ByRef;
987 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000988 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000989 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000990 return;
991 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000992
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000993 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000994}
995
Anders Carlssonc181b012008-10-05 18:05:59 +0000996static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
997 // check the attribute arguments.
998 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000999 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1000 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +00001001 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001002 }
1003
Anders Carlssonc181b012008-10-05 18:05:59 +00001004 int sentinel = 0;
1005 if (Attr.getNumArgs() > 0) {
1006 Expr *E = static_cast<Expr *>(Attr.getArg(0));
1007 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001008 if (E->isTypeDependent() || E->isValueDependent() ||
1009 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001010 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001011 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001012 return;
1013 }
1014 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001015
Anders Carlssonc181b012008-10-05 18:05:59 +00001016 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001017 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1018 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001019 return;
1020 }
1021 }
1022
1023 int nullPos = 0;
1024 if (Attr.getNumArgs() > 1) {
1025 Expr *E = static_cast<Expr *>(Attr.getArg(1));
1026 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001027 if (E->isTypeDependent() || E->isValueDependent() ||
1028 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001029 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001030 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001031 return;
1032 }
1033 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001034
Anders Carlssonc181b012008-10-05 18:05:59 +00001035 if (nullPos > 1 || nullPos < 0) {
1036 // FIXME: This error message could be improved, it would be nice
1037 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1039 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001040 return;
1041 }
1042 }
1043
1044 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001045 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001046 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001047
Chris Lattner9363e312009-03-17 23:03:47 +00001048 if (isa<FunctionNoProtoType>(FT)) {
1049 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1050 return;
1051 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001052
Chris Lattner9363e312009-03-17 23:03:47 +00001053 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001054 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001055 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001056 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001057 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1058 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001059 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001060 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001061 }
1062 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001063 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1064 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001065 ;
1066 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001067 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001068 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001069 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +00001070 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001071 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001072 int m = Ty->isFunctionPointerType() ? 0 : 1;
1073 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001074 return;
1075 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001076 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001077 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001078 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001079 return;
1080 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001081 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001082 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001083 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +00001084 return;
1085 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001086 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001087}
1088
Chris Lattner237f2752009-02-14 07:37:35 +00001089static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1090 // check the attribute arguments.
1091 if (Attr.getNumArgs() != 0) {
1092 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1093 return;
1094 }
1095
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001096 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001097 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001098 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +00001099 return;
1100 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001101
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001102 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1103 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1104 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001105 return;
1106 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001107 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1108 if (MD->getResultType()->isVoidType()) {
1109 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1110 << Attr.getName() << 1;
1111 return;
1112 }
1113
Nuno Lopes518e3702009-12-20 23:11:08 +00001114 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +00001115}
1116
1117static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001118 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001119 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001120 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001121 return;
1122 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001123
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001124 /* weak only applies to non-static declarations */
Rafael Espindolac18086a2010-02-23 22:00:30 +00001125 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001126 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1127 dyn_cast<NamedDecl>(D)->getNameAsString();
1128 return;
1129 }
1130
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001131 // TODO: could also be applied to methods?
1132 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1133 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001134 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001135 return;
1136 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001137
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001138 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001139}
1140
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001141static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1142 // check the attribute arguments.
1143 if (Attr.getNumArgs() != 0) {
1144 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1145 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001146 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001147
1148 // weak_import only applies to variable & function declarations.
1149 bool isDef = false;
1150 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1151 isDef = (!VD->hasExternalStorage() || VD->getInit());
1152 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001153 isDef = FD->hasBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +00001154 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1155 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +00001156 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +00001157 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001158 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001159 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001160 !isa<ObjCInterfaceDecl>(D))
1161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001162 << Attr.getName() << 2 /*variable and function*/;
1163 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001164 }
1165
1166 // Merge should handle any subsequent violations.
1167 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001168 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001169 diag::warn_attribute_weak_import_invalid_on_definition)
1170 << "weak_import" << 2 /*variable and function*/;
1171 return;
1172 }
1173
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001174 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001175}
1176
Nate Begemanf2758702009-06-26 06:32:41 +00001177static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1178 Sema &S) {
1179 // Attribute has 3 arguments.
1180 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001181 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001182 return;
1183 }
1184
1185 unsigned WGSize[3];
1186 for (unsigned i = 0; i < 3; ++i) {
1187 Expr *E = static_cast<Expr *>(Attr.getArg(i));
1188 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001189 if (E->isTypeDependent() || E->isValueDependent() ||
1190 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001191 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1192 << "reqd_work_group_size" << E->getSourceRange();
1193 return;
1194 }
1195 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1196 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001197 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001198 WGSize[2]));
1199}
1200
Chris Lattner237f2752009-02-14 07:37:35 +00001201static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001202 // Attribute has no arguments.
1203 if (Attr.getNumArgs() != 1) {
1204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1205 return;
1206 }
1207
1208 // Make sure that there is a string literal as the sections's single
1209 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +00001210 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1211 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001212 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001213 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001214 return;
1215 }
Mike Stump11289f42009-09-09 15:08:12 +00001216
Chris Lattner30ba6742009-08-10 19:03:04 +00001217 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001218 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001219 if (!Error.empty()) {
1220 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1221 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001222 return;
1223 }
Mike Stump11289f42009-09-09 15:08:12 +00001224
Chris Lattner20aee9b2010-01-12 20:58:53 +00001225 // This attribute cannot be applied to local variables.
1226 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1227 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1228 return;
1229 }
1230
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001231 D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001232}
1233
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001234
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001235static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001236 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001237 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001238 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001239 return;
1240 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001241
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001242 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001243}
1244
Anders Carlssonb8316282008-10-05 23:32:53 +00001245static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1246 // check the attribute arguments.
1247 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001249 return;
1250 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001251
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001252 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001253}
1254
1255static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1256 // check the attribute arguments.
1257 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001259 return;
1260 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001261
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001262 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001263}
1264
Anders Carlssond277d792009-01-31 01:16:18 +00001265static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001266 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1268 return;
1269 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001270
Anders Carlssond277d792009-01-31 01:16:18 +00001271 if (Attr.getNumArgs() != 0) {
1272 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1273 return;
1274 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001275
Anders Carlssond277d792009-01-31 01:16:18 +00001276 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001277
Anders Carlssond277d792009-01-31 01:16:18 +00001278 if (!VD || !VD->hasLocalStorage()) {
1279 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1280 return;
1281 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001282
Anders Carlssond277d792009-01-31 01:16:18 +00001283 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001284 // FIXME: Lookup probably isn't looking in the right place
1285 // FIXME: The lookup source location should be in the attribute, not the
1286 // start of the attribute.
John McCall9f3059a2009-10-09 21:13:30 +00001287 NamedDecl *CleanupDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001288 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCall9f3059a2009-10-09 21:13:30 +00001289 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001290 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001291 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001292 Attr.getParameterName();
1293 return;
1294 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001295
Anders Carlssond277d792009-01-31 01:16:18 +00001296 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1297 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001298 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001299 Attr.getParameterName();
1300 return;
1301 }
1302
Anders Carlssond277d792009-01-31 01:16:18 +00001303 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001304 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001305 Attr.getParameterName();
1306 return;
1307 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001308
Anders Carlsson723f55d2009-02-07 23:16:50 +00001309 // We're currently more strict than GCC about what function types we accept.
1310 // If this ever proves to be a problem it should be easy to fix.
1311 QualType Ty = S.Context.getPointerType(VD->getType());
1312 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001313 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001314 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001315 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1316 Attr.getParameterName() << ParamTy << Ty;
1317 return;
1318 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001319
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001320 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001321}
1322
Mike Stumpd3bb5572009-07-24 19:02:52 +00001323/// Handle __attribute__((format_arg((idx)))) attribute based on
1324/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1325static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001326 if (Attr.getNumArgs() != 1) {
1327 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1328 return;
1329 }
1330 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1331 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1332 << Attr.getName() << 0 /*function*/;
1333 return;
1334 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001335 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1336 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001337 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1338 unsigned FirstIdx = 1;
1339 // checks for the 2nd argument
1340 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1341 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001342 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1343 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1345 << "format" << 2 << IdxExpr->getSourceRange();
1346 return;
1347 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001348
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001349 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1350 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1351 << "format" << 2 << IdxExpr->getSourceRange();
1352 return;
1353 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001354
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001355 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001356
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001357 // make sure the format string is really a string
1358 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001359
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001360 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1361 if (not_nsstring_type &&
1362 !isCFStringType(Ty, S.Context) &&
1363 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001364 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001365 // FIXME: Should highlight the actual expression that has the wrong type.
1366 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001367 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001368 << IdxExpr->getSourceRange();
1369 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001370 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001371 Ty = getFunctionOrMethodResultType(d);
1372 if (!isNSStringType(Ty, S.Context) &&
1373 !isCFStringType(Ty, S.Context) &&
1374 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001375 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001376 // FIXME: Should highlight the actual expression that has the wrong type.
1377 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001378 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001379 << IdxExpr->getSourceRange();
1380 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001381 }
1382
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001383 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001384}
1385
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001386enum FormatAttrKind {
1387 CFStringFormat,
1388 NSStringFormat,
1389 StrftimeFormat,
1390 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001391 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001392 InvalidFormat
1393};
1394
1395/// getFormatAttrKind - Map from format attribute names to supported format
1396/// types.
1397static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1398 // Check for formats that get handled specially.
1399 if (Format == "NSString")
1400 return NSStringFormat;
1401 if (Format == "CFString")
1402 return CFStringFormat;
1403 if (Format == "strftime")
1404 return StrftimeFormat;
1405
1406 // Otherwise, check for supported formats.
1407 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1408 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1409 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1410 Format == "zcmn_err")
1411 return SupportedFormat;
1412
Duncan Sandsde4fe352010-03-23 14:44:19 +00001413 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1414 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001415 return IgnoredFormat;
1416
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001417 return InvalidFormat;
1418}
1419
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001420/// Handle __attribute__((init_priority(priority))) attributes based on
1421/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1422static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1423 Sema &S) {
1424 if (!S.getLangOptions().CPlusPlus) {
1425 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1426 return;
1427 }
1428
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001429 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1430 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1431 Attr.setInvalid();
1432 return;
1433 }
1434 QualType T = dyn_cast<VarDecl>(d)->getType();
1435 if (S.Context.getAsArrayType(T))
1436 T = S.Context.getBaseElementType(T);
1437 if (!T->getAs<RecordType>()) {
1438 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1439 Attr.setInvalid();
1440 return;
1441 }
1442
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001443 if (Attr.getNumArgs() != 1) {
1444 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1445 Attr.setInvalid();
1446 return;
1447 }
1448 Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0));
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001449
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001450 llvm::APSInt priority(32);
1451 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1452 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1453 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1454 << "init_priority" << priorityExpr->getSourceRange();
1455 Attr.setInvalid();
1456 return;
1457 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001458 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001459 if (prioritynum < 101 || prioritynum > 65535) {
1460 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1461 << priorityExpr->getSourceRange();
1462 Attr.setInvalid();
1463 return;
1464 }
1465 d->addAttr(::new (S.Context) InitPriorityAttr(prioritynum));
1466}
1467
Mike Stumpd3bb5572009-07-24 19:02:52 +00001468/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1469/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001470static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001471
Chris Lattner4a927cb2008-06-28 23:36:30 +00001472 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001473 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001474 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001475 return;
1476 }
1477
Chris Lattner4a927cb2008-06-28 23:36:30 +00001478 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001479 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001480 return;
1481 }
1482
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001483 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001484 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001485 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001486 return;
1487 }
1488
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001489 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001490 unsigned FirstIdx = 1;
1491
Daniel Dunbar07d07852009-10-18 21:17:35 +00001492 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001493
1494 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001495 if (Format.startswith("__") && Format.endswith("__"))
1496 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001497
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001498 // Check for supported formats.
1499 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001500
1501 if (Kind == IgnoredFormat)
1502 return;
1503
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001504 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001505 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001506 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001507 return;
1508 }
1509
1510 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001511 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001512 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001513 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1514 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001515 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001516 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001517 return;
1518 }
1519
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001520 // FIXME: We should handle the implicit 'this' parameter in a more generic
1521 // way that can be used for other arguments.
1522 bool HasImplicitThisParam = false;
1523 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1524 if (MD->isInstance()) {
1525 HasImplicitThisParam = true;
1526 NumArgs++;
1527 }
1528 }
Mike Stump11289f42009-09-09 15:08:12 +00001529
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001530 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001531 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001532 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001533 return;
1534 }
1535
1536 // FIXME: Do we need to bounds check?
1537 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001538
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001539 if (HasImplicitThisParam) {
1540 if (ArgIdx == 0) {
1541 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1542 << "a string type" << IdxExpr->getSourceRange();
1543 return;
1544 }
1545 ArgIdx--;
1546 }
Mike Stump11289f42009-09-09 15:08:12 +00001547
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001548 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001549 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001550
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001551 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001552 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001553 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1554 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001555 return;
1556 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001557 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001558 // FIXME: do we need to check if the type is NSString*? What are the
1559 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001560 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001561 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001562 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1563 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001564 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001565 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001566 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001567 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001568 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001569 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1570 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001571 return;
1572 }
1573
1574 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001575 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001576 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001577 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1578 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001579 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001580 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001581 return;
1582 }
1583
1584 // check if the function is variadic if the 3rd argument non-zero
1585 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001586 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001587 ++NumArgs; // +1 for ...
1588 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001589 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001590 return;
1591 }
1592 }
1593
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001594 // strftime requires FirstArg to be 0 because it doesn't read from any
1595 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001596 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001597 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001598 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1599 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001600 return;
1601 }
1602 // if 0 it disables parameter checking (to use with e.g. va_list)
1603 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001604 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001605 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001606 return;
1607 }
1608
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001609 d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001610 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001611}
1612
Chris Lattnera663a0a2008-06-29 00:28:59 +00001613static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1614 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001615 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001616 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001617 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001618 return;
1619 }
1620
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001621 // Try to find the underlying union declaration.
1622 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001623 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001624 if (TD && TD->getUnderlyingType()->isUnionType())
1625 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1626 else
1627 RD = dyn_cast<RecordDecl>(d);
1628
1629 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001630 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001631 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001632 return;
1633 }
1634
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001635 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001636 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001637 diag::warn_transparent_union_attribute_not_definition);
1638 return;
1639 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001640
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001641 RecordDecl::field_iterator Field = RD->field_begin(),
1642 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001643 if (Field == FieldEnd) {
1644 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1645 return;
1646 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001647
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001648 FieldDecl *FirstField = *Field;
1649 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001650 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001651 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001652 diag::warn_transparent_union_attribute_floating)
1653 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001654 return;
1655 }
1656
1657 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1658 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1659 for (; Field != FieldEnd; ++Field) {
1660 QualType FieldType = Field->getType();
1661 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1662 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1663 // Warn if we drop the attribute.
1664 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001665 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001666 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001667 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001668 diag::warn_transparent_union_attribute_field_size_align)
1669 << isSize << Field->getDeclName() << FieldBits;
1670 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001671 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001672 diag::note_transparent_union_first_field_size_align)
1673 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001674 return;
1675 }
1676 }
1677
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001678 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001679}
1680
Chris Lattnera663a0a2008-06-29 00:28:59 +00001681static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, 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() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001684 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001685 return;
1686 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001687 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1688 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001689
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001690 // Make sure that there is a string literal as the annotation's single
1691 // argument.
1692 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001693 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001694 return;
1695 }
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001696 d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001697}
1698
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001699static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001700 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001701 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001702 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001703 return;
1704 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001705
1706 //FIXME: The C++0x version of this attribute has more limited applicabilty
1707 // than GNU's, and should error out when it is used to specify a
1708 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001709
Chris Lattner4a927cb2008-06-28 23:36:30 +00001710 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001711 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001712 // (For now we just use 128 bits which is the maximum on X86).
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001713 D->addAttr(::new (S.Context) AlignedAttr(128));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001714 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001715 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001716
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001717 S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0)));
1718}
1719
1720void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1721 if (E->isTypeDependent() || E->isValueDependent()) {
1722 // Save dependent expressions in the AST to be instantiated.
1723 D->addAttr(::new (Context) AlignedAttr(E));
1724 return;
1725 }
1726
Chris Lattner4627b742008-06-28 23:50:44 +00001727 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001728 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1729 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1730 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001731 return;
1732 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001733 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001734 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1735 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001736 return;
1737 }
1738
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001739 D->addAttr(::new (Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001740}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001741
Mike Stumpd3bb5572009-07-24 19:02:52 +00001742/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1743/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001744///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001745/// Despite what would be logical, the mode attribute is a decl attribute, not a
1746/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1747/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001748static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001749 // This attribute isn't documented, but glibc uses it. It changes
1750 // the width of an int or unsigned int to the specified size.
1751
1752 // Check that there aren't any arguments
1753 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001754 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001755 return;
1756 }
1757
1758 IdentifierInfo *Name = Attr.getParameterName();
1759 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001760 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001761 return;
1762 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001763
Daniel Dunbar07d07852009-10-18 21:17:35 +00001764 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001765
1766 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001767 if (Str.startswith("__") && Str.endswith("__"))
1768 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001769
1770 unsigned DestWidth = 0;
1771 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001772 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001773 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001774 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001775 switch (Str[0]) {
1776 case 'Q': DestWidth = 8; break;
1777 case 'H': DestWidth = 16; break;
1778 case 'S': DestWidth = 32; break;
1779 case 'D': DestWidth = 64; break;
1780 case 'X': DestWidth = 96; break;
1781 case 'T': DestWidth = 128; break;
1782 }
1783 if (Str[1] == 'F') {
1784 IntegerMode = false;
1785 } else if (Str[1] == 'C') {
1786 IntegerMode = false;
1787 ComplexMode = true;
1788 } else if (Str[1] != 'I') {
1789 DestWidth = 0;
1790 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001791 break;
1792 case 4:
1793 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1794 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001795 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001796 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001797 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001798 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001799 break;
1800 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001801 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001802 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001803 break;
1804 }
1805
1806 QualType OldTy;
1807 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1808 OldTy = TD->getUnderlyingType();
1809 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1810 OldTy = VD->getType();
1811 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001812 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1813 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001814 return;
1815 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001816
John McCall9dd450b2009-09-21 23:43:11 +00001817 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001818 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1819 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00001820 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001821 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1822 } else if (ComplexMode) {
1823 if (!OldTy->isComplexType())
1824 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1825 } else {
1826 if (!OldTy->isFloatingType())
1827 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1828 }
1829
Mike Stump87c57ac2009-05-16 07:39:55 +00001830 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1831 // and friends, at least with glibc.
1832 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1833 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001834 // FIXME: Make sure floating-point mappings are accurate
1835 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001836 QualType NewTy;
1837 switch (DestWidth) {
1838 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001839 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001840 return;
1841 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001842 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001843 return;
1844 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001845 if (!IntegerMode) {
1846 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1847 return;
1848 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001849 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001850 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001851 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001852 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001853 break;
1854 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001855 if (!IntegerMode) {
1856 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1857 return;
1858 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001859 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001860 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001861 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001862 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001863 break;
1864 case 32:
1865 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001866 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001867 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001868 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001869 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001870 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001871 break;
1872 case 64:
1873 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001874 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001875 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00001876 if (S.Context.Target.getLongWidth() == 64)
1877 NewTy = S.Context.LongTy;
1878 else
1879 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001880 else
Chandler Carruth72343702010-01-26 06:39:24 +00001881 if (S.Context.Target.getLongWidth() == 64)
1882 NewTy = S.Context.UnsignedLongTy;
1883 else
1884 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001885 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001886 case 96:
1887 NewTy = S.Context.LongDoubleTy;
1888 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001889 case 128:
1890 if (!IntegerMode) {
1891 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1892 return;
1893 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00001894 if (OldTy->isSignedIntegerType())
1895 NewTy = S.Context.Int128Ty;
1896 else
1897 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00001898 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001899 }
1900
Eli Friedman4735374e2009-03-03 06:41:03 +00001901 if (ComplexMode) {
1902 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001903 }
1904
1905 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001906 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1907 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001908 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001909 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001910 cast<ValueDecl>(D)->setType(NewTy);
1911}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001912
Mike Stump3722f582009-08-26 22:31:08 +00001913static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001914 // check the attribute arguments.
1915 if (Attr.getNumArgs() > 0) {
1916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1917 return;
1918 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001919
Anders Carlsson88097122009-02-19 19:16:48 +00001920 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001921 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001922 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001923 return;
1924 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001925
Mike Stump3722f582009-08-26 22:31:08 +00001926 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001927}
1928
Mike Stump3722f582009-08-26 22:31:08 +00001929static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001930 // check the attribute arguments.
1931 if (Attr.getNumArgs() != 0) {
1932 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1933 return;
1934 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001935
Chris Lattner4225e232009-04-14 17:02:11 +00001936 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001937 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001938 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001939 return;
1940 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001941
Mike Stump3722f582009-08-26 22:31:08 +00001942 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001943}
1944
Chris Lattner3c77a352010-06-22 00:03:40 +00001945static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
1946 Sema &S) {
1947 // check the attribute arguments.
1948 if (Attr.getNumArgs() != 0) {
1949 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1950 return;
1951 }
1952
1953 if (!isa<FunctionDecl>(d)) {
1954 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1955 << Attr.getName() << 0 /*function*/;
1956 return;
1957 }
1958
1959 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr());
1960}
1961
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001962static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001963 // check the attribute arguments.
1964 if (Attr.getNumArgs() != 0) {
1965 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1966 return;
1967 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001968
Chris Lattner4225e232009-04-14 17:02:11 +00001969 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1970 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001971 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001972 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001973 return;
1974 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001975
Douglas Gregor35b57532009-10-27 21:01:01 +00001976 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001977 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001978 return;
1979 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001980
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001981 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001982}
1983
Abramo Bagnara50099372010-04-30 13:10:51 +00001984static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1985 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
1986 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
1987 assert(Attr.isInvalid() == false);
1988
1989 switch (Attr.getKind()) {
1990 case AttributeList::AT_fastcall:
1991 d->addAttr(::new (S.Context) FastCallAttr());
1992 return;
1993 case AttributeList::AT_stdcall:
1994 d->addAttr(::new (S.Context) StdCallAttr());
1995 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00001996 case AttributeList::AT_thiscall:
1997 d->addAttr(::new (S.Context) ThisCallAttr());
Abramo Bagnara50099372010-04-30 13:10:51 +00001998 case AttributeList::AT_cdecl:
1999 d->addAttr(::new (S.Context) CDeclAttr());
2000 return;
2001 default:
2002 llvm_unreachable("unexpected attribute kind");
2003 return;
2004 }
2005}
2006
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002007static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2008 // check the attribute arguments.
2009 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00002010 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002011 return;
2012 }
Eli Friedman7044b762009-03-27 21:06:47 +00002013
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002014 if (!isFunctionOrMethod(d)) {
2015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002016 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002017 return;
2018 }
Eli Friedman7044b762009-03-27 21:06:47 +00002019
2020 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
2021 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002022 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2023 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman7044b762009-03-27 21:06:47 +00002024 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2025 << "regparm" << NumParamsExpr->getSourceRange();
2026 return;
2027 }
2028
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002029 if (S.Context.Target.getRegParmMax() == 0) {
2030 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002031 << NumParamsExpr->getSourceRange();
2032 return;
2033 }
2034
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00002035 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002036 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2037 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00002038 return;
2039 }
2040
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002041 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002042}
2043
Alexis Hunt96d5c762009-11-21 08:43:09 +00002044static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2045 // check the attribute arguments.
2046 if (Attr.getNumArgs() != 0) {
2047 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2048 return;
2049 }
2050
2051 if (!isa<CXXRecordDecl>(d)
2052 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2053 S.Diag(Attr.getLoc(),
2054 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2055 : diag::warn_attribute_wrong_decl_type)
2056 << Attr.getName() << 7 /*virtual method or class*/;
2057 return;
2058 }
Alexis Hunt54a02542009-11-25 04:20:27 +00002059
2060 // FIXME: Conform to C++0x redeclaration rules.
2061
2062 if (d->getAttr<FinalAttr>()) {
2063 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2064 return;
2065 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002066
2067 d->addAttr(::new (S.Context) FinalAttr());
2068}
2069
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002070//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00002071// C++0x member checking attributes
2072//===----------------------------------------------------------------------===//
2073
2074static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2075 if (Attr.getNumArgs() != 0) {
2076 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2077 return;
2078 }
2079
2080 if (!isa<CXXRecordDecl>(d)) {
2081 S.Diag(Attr.getLoc(),
2082 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2083 : diag::warn_attribute_wrong_decl_type)
2084 << Attr.getName() << 9 /*class*/;
2085 return;
2086 }
2087
2088 if (d->getAttr<BaseCheckAttr>()) {
2089 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2090 return;
2091 }
2092
2093 d->addAttr(::new (S.Context) BaseCheckAttr());
2094}
2095
2096static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2097 if (Attr.getNumArgs() != 0) {
2098 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2099 return;
2100 }
2101
2102 if (!isa<RecordDecl>(d->getDeclContext())) {
2103 // FIXME: It's not the type that's the problem
2104 S.Diag(Attr.getLoc(),
2105 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2106 : diag::warn_attribute_wrong_decl_type)
2107 << Attr.getName() << 11 /*member*/;
2108 return;
2109 }
2110
2111 // FIXME: Conform to C++0x redeclaration rules.
2112
2113 if (d->getAttr<HidingAttr>()) {
2114 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2115 return;
2116 }
2117
2118 d->addAttr(::new (S.Context) HidingAttr());
2119}
2120
2121static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2122 if (Attr.getNumArgs() != 0) {
2123 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2124 return;
2125 }
2126
2127 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2128 // FIXME: It's not the type that's the problem
2129 S.Diag(Attr.getLoc(),
2130 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2131 : diag::warn_attribute_wrong_decl_type)
2132 << Attr.getName() << 10 /*virtual method*/;
2133 return;
2134 }
2135
2136 // FIXME: Conform to C++0x redeclaration rules.
2137
2138 if (d->getAttr<OverrideAttr>()) {
2139 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2140 return;
2141 }
2142
2143 d->addAttr(::new (S.Context) OverrideAttr());
2144}
2145
2146//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002147// Checker-specific attribute handlers.
2148//===----------------------------------------------------------------------===//
2149
2150static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2151 Sema &S) {
2152
Ted Kremenek3b204e42009-05-13 21:07:32 +00002153 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002154
Ted Kremenek3b204e42009-05-13 21:07:32 +00002155 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2156 RetTy = MD->getResultType();
2157 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2158 RetTy = FD->getResultType();
2159 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002160 SourceLocation L = Attr.getLoc();
2161 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2162 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002163 return;
2164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002165
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002166 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00002167 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002168 SourceLocation L = Attr.getLoc();
2169 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2170 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002171 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002173
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002174 switch (Attr.getKind()) {
2175 default:
2176 assert(0 && "invalid ownership attribute");
2177 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002178 case AttributeList::AT_cf_returns_not_retained:
2179 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr());
2180 return;
2181 case AttributeList::AT_ns_returns_not_retained:
2182 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr());
2183 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002184 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002185 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002186 return;
2187 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00002188 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002189 return;
2190 };
2191}
2192
Charles Davis163855f2010-02-16 18:27:26 +00002193static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2194 return Attr.getKind() == AttributeList::AT_dllimport ||
2195 Attr.getKind() == AttributeList::AT_dllexport;
2196}
2197
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002198//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002199// Top Level Sema Entry Points
2200//===----------------------------------------------------------------------===//
2201
Sebastian Redlfc24b632008-12-21 19:24:58 +00002202/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002203/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00002204/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2205/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00002206static void ProcessDeclAttribute(Scope *scope, Decl *D,
2207 const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002208 if (Attr.isInvalid())
2209 return;
2210
Charles Davis163855f2010-02-16 18:27:26 +00002211 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2212 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00002213 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002214 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002215 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002216 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2217 case AttributeList::AT_IBOutletCollection:
2218 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002219 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002220 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002221 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002222 // Ignore these, these are type attributes, handled by
2223 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002224 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002225 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2226 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002227 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002228 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002229 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002230 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002231 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2232 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002233 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002234 HandleDependencyAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002235 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2236 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2237 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002238 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002239 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002240 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002241 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2242 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2243 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
2244 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2245 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
2246 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2247 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
2248 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002249 case AttributeList::AT_ownership_returns:
2250 case AttributeList::AT_ownership_takes:
2251 case AttributeList::AT_ownership_holds:
2252 HandleOwnershipAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002253 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2254 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2255 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002256 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002257
2258 // Checker-specific.
Ted Kremenekd9c66632010-02-18 00:05:45 +00002259 case AttributeList::AT_ns_returns_not_retained:
2260 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002261 case AttributeList::AT_ns_returns_retained:
2262 case AttributeList::AT_cf_returns_retained:
2263 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2264
Nate Begemanf2758702009-06-26 06:32:41 +00002265 case AttributeList::AT_reqd_wg_size:
2266 HandleReqdWorkGroupSize(D, Attr, S); break;
2267
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002268 case AttributeList::AT_init_priority:
2269 HandleInitPriorityAttr(D, Attr, S); break;
2270
Alexis Hunt54a02542009-11-25 04:20:27 +00002271 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2272 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002273 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2274 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2275 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002276 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002277 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2278 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002279 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002280 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002281 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002282 case AttributeList::AT_transparent_union:
2283 HandleTransparentUnionAttr(D, Attr, S);
2284 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002285 case AttributeList::AT_objc_exception:
2286 HandleObjCExceptionAttr(D, Attr, S);
2287 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002288 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002289 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2290 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2291 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2292 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2293 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2294 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2295 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2296 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2297 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002298 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002299 // Just ignore
2300 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002301 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2302 HandleNoInstrumentFunctionAttr(D, Attr, S);
2303 break;
John McCallab26cfa2010-02-05 21:31:56 +00002304 case AttributeList::AT_stdcall:
2305 case AttributeList::AT_cdecl:
2306 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002307 case AttributeList::AT_thiscall:
Abramo Bagnara50099372010-04-30 13:10:51 +00002308 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002309 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002310 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002311 // Ask target about the attribute.
2312 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2313 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002314 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2315 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002316 break;
2317 }
2318}
2319
2320/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2321/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00002322void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002323 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2324 ProcessDeclAttribute(S, D, *l, *this);
2325 }
2326
2327 // GCC accepts
2328 // static int a9 __attribute__((weakref));
2329 // but that looks really pointless. We reject it.
2330 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2331 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002332 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002333 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002334 }
2335}
2336
Ryan Flynn7d470f32009-07-30 03:15:39 +00002337/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2338/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002339NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002340 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002341 NamedDecl *NewD = 0;
2342 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2343 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2344 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002345 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002346 if (FD->getQualifier()) {
2347 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2348 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2349 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002350 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2351 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2352 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002353 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002354 VD->getStorageClass(),
2355 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002356 if (VD->getQualifier()) {
2357 VarDecl *NewVD = cast<VarDecl>(NewD);
2358 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2359 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002360 }
2361 return NewD;
2362}
2363
2364/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2365/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002366void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002367 if (W.getUsed()) return; // only do this once
2368 W.setUsed(true);
2369 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2370 IdentifierInfo *NDId = ND->getIdentifier();
2371 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Ted Kremenek7f4945a2010-02-11 05:28:37 +00002372 NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName()));
Chris Lattnere6eab982009-09-08 18:10:11 +00002373 NewD->addAttr(::new (Context) WeakAttr());
2374 WeakTopLevelDecl.push_back(NewD);
2375 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2376 // to insert Decl at TU scope, sorry.
2377 DeclContext *SavedContext = CurContext;
2378 CurContext = Context.getTranslationUnitDecl();
2379 PushOnScopeChains(NewD, S);
2380 CurContext = SavedContext;
2381 } else { // just add weak to existing
2382 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002383 }
2384}
2385
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002386/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2387/// it, apply them to D. This is a bit tricky because PD can have attributes
2388/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002389void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002390 // Handle #pragma weak
2391 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2392 if (ND->hasLinkage()) {
2393 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2394 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002395 // Identifier referenced by #pragma weak before it was declared
2396 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002397 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2398 }
2399 }
2400 }
2401
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002402 // Apply decl attributes from the DeclSpec if present.
2403 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002404 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002405
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002406 // Walk the declarator structure, applying decl attributes that were in a type
2407 // position to the decl itself. This handles cases like:
2408 // int *__attr__(x)** D;
2409 // when X is a decl attribute.
2410 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2411 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002412 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002413
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002414 // Finally, apply any attributes on the decl itself.
2415 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002416 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002417}
John McCall28a6aea2009-11-04 02:18:39 +00002418
2419/// PushParsingDeclaration - Enter a new "scope" of deprecation
2420/// warnings.
2421///
2422/// The state token we use is the start index of this scope
2423/// on the warning stack.
2424Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2425 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002426 return (ParsingDeclStackState) DelayedDiagnostics.size();
2427}
2428
2429void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2430 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2431 ParsingDeclDepth--;
2432
2433 if (DelayedDiagnostics.empty())
2434 return;
2435
2436 unsigned SavedIndex = (unsigned) S;
2437 assert(SavedIndex <= DelayedDiagnostics.size() &&
2438 "saved index is out of bounds");
2439
John McCall1064d7e2010-03-16 05:22:47 +00002440 unsigned E = DelayedDiagnostics.size();
2441
John McCall86121512010-01-27 03:50:35 +00002442 // We only want to actually emit delayed diagnostics when we
2443 // successfully parsed a decl.
2444 Decl *D = Ctx ? Ctx.getAs<Decl>() : 0;
2445 if (D) {
2446 // We really do want to start with 0 here. We get one push for a
2447 // decl spec and another for each declarator; in a decl group like:
2448 // deprecated_typedef foo, *bar, baz();
2449 // only the declarator pops will be passed decls. This is correct;
2450 // we really do need to consider delayed diagnostics from the decl spec
2451 // for each of the different declarations.
John McCall1064d7e2010-03-16 05:22:47 +00002452 for (unsigned I = 0; I != E; ++I) {
John McCall86121512010-01-27 03:50:35 +00002453 if (DelayedDiagnostics[I].Triggered)
2454 continue;
2455
2456 switch (DelayedDiagnostics[I].Kind) {
2457 case DelayedDiagnostic::Deprecation:
2458 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2459 break;
2460
2461 case DelayedDiagnostic::Access:
2462 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2463 break;
2464 }
2465 }
2466 }
2467
John McCall1064d7e2010-03-16 05:22:47 +00002468 // Destroy all the delayed diagnostics we're about to pop off.
2469 for (unsigned I = SavedIndex; I != E; ++I)
2470 DelayedDiagnostics[I].destroy();
2471
John McCall86121512010-01-27 03:50:35 +00002472 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002473}
2474
2475static bool isDeclDeprecated(Decl *D) {
2476 do {
2477 if (D->hasAttr<DeprecatedAttr>())
2478 return true;
2479 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2480 return false;
2481}
2482
John McCall86121512010-01-27 03:50:35 +00002483void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD,
2484 Decl *Ctx) {
2485 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002486 return;
2487
John McCall86121512010-01-27 03:50:35 +00002488 DD.Triggered = true;
2489 Diag(DD.Loc, diag::warn_deprecated)
2490 << DD.DeprecationData.Decl->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002491}
2492
2493void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2494 // Delay if we're currently parsing a declaration.
2495 if (ParsingDeclDepth) {
John McCall86121512010-01-27 03:50:35 +00002496 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D));
John McCall28a6aea2009-11-04 02:18:39 +00002497 return;
2498 }
2499
2500 // Otherwise, don't warn if our current context is deprecated.
2501 if (isDeclDeprecated(cast<Decl>(CurContext)))
2502 return;
2503
2504 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2505}