blob: 3c4bd8ffc45a9ce42a73fe5e9b3f17b135ace88b [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000024using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000026
Chris Lattner58418ff2008-06-29 00:16:31 +000027//===----------------------------------------------------------------------===//
28// Helper functions
29//===----------------------------------------------------------------------===//
30
Ted Kremenek527042b2009-08-14 20:49:40 +000031static const FunctionType *getFunctionType(const Decl *d,
32 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000034 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000036 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000037 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000038 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039 Ty = decl->getUnderlyingType();
40 else
41 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000042
Chris Lattner2c6fcf52008-06-26 18:38:35 +000043 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000044 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000045 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000046 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000047
John McCall9dd450b2009-09-21 23:43:11 +000048 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000049}
50
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000051// FIXME: We should provide an abstraction around a method or function
52// to provide the following bits of information.
53
Nuno Lopes518e3702009-12-20 23:11:08 +000054/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000055/// type (function or function-typed variable).
56static bool isFunction(const Decl *d) {
57 return getFunctionType(d, false) != NULL;
58}
59
60/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000061/// type (function or function-typed variable) or an Objective-C
62/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000063static bool isFunctionOrMethod(const Decl *d) {
64 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000065}
66
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067/// isFunctionOrMethodOrBlock - Return true if the given decl has function
68/// type (function or function-typed variable) or an Objective-C
69/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000070static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000071 if (isFunctionOrMethod(d))
72 return true;
73 // check for block is more involved.
74 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
75 QualType Ty = V->getType();
76 return Ty->isBlockPointerType();
77 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000078 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000079}
80
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000081/// hasFunctionProto - Return true if the given decl has a argument
82/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000083/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000084static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000085 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000086 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000087 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000088 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000089 return true;
90 }
91}
92
93/// getFunctionOrMethodNumArgs - Return number of function or method
94/// arguments. It is an error to call this on a K&R function (use
95/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000096static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000097 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000098 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000099 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
100 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +0000101 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000102}
103
Ted Kremenek527042b2009-08-14 20:49:40 +0000104static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000105 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000106 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000107 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
108 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000109
Chris Lattnera4997152009-02-20 18:43:26 +0000110 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000111}
112
Ted Kremenek527042b2009-08-14 20:49:40 +0000113static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000114 if (const FunctionType *FnTy = getFunctionType(d))
115 return cast<FunctionProtoType>(FnTy)->getResultType();
116 return cast<ObjCMethodDecl>(d)->getResultType();
117}
118
Ted Kremenek527042b2009-08-14 20:49:40 +0000119static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000120 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000121 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000122 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000123 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000124 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000125 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000126 return cast<ObjCMethodDecl>(d)->isVariadic();
127 }
128}
129
Chandler Carruth743682b2010-11-16 08:35:43 +0000130static bool isInstanceMethod(const Decl *d) {
131 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d))
132 return MethodDecl->isInstance();
133 return false;
134}
135
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000136static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000137 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000138 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000139 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000140
John McCall96fa4842010-05-17 21:00:27 +0000141 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
142 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000143 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000144
John McCall96fa4842010-05-17 21:00:27 +0000145 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000146
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000147 // FIXME: Should we walk the chain of classes?
148 return ClsName == &Ctx.Idents.get("NSString") ||
149 ClsName == &Ctx.Idents.get("NSMutableString");
150}
151
Daniel Dunbar980c6692008-09-26 03:32:58 +0000152static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000153 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000154 if (!PT)
155 return false;
156
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000157 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000158 if (!RT)
159 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000160
Daniel Dunbar980c6692008-09-26 03:32:58 +0000161 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000162 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000163 return false;
164
165 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
166}
167
Chris Lattner58418ff2008-06-29 00:16:31 +0000168//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000169// Attribute Implementations
170//===----------------------------------------------------------------------===//
171
Daniel Dunbar032db472008-07-31 22:40:48 +0000172// FIXME: All this manual attribute parsing code is gross. At the
173// least add some helper functions to check most argument patterns (#
174// and types of args).
175
Mike Stumpd3bb5572009-07-24 19:02:52 +0000176static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000177 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000178 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
179 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000180 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000181 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000182 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000183
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000184 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000185
186 Expr *sizeExpr;
187
188 // Special case where the argument is a template id.
189 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000190 CXXScopeSpec SS;
191 UnqualifiedId id;
192 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
193 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000194 } else {
195 // check the attribute arguments.
196 if (Attr.getNumArgs() != 1) {
197 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
198 return;
199 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000200 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000201 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000202
203 // Instantiate/Install the vector type, and let Sema build the type for us.
204 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000205 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000206 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000207 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000208 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000209
Douglas Gregor758a8692009-06-17 21:51:59 +0000210 // Remember this typedef decl, we will need it later for diagnostics.
211 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000212 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213}
214
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000215static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000216 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000217 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000218 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000219 return;
220 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000221
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000222 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000223 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000224 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
225 // If the alignment is less than or equal to 8 bits, the packed attribute
226 // has no effect.
227 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000228 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000229 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000230 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000231 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000232 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000233 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000234 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000235}
236
Ted Kremenek1f672822010-02-18 03:08:58 +0000237static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000238 // check the attribute arguments.
239 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000241 return;
242 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000243
Ted Kremenek1f672822010-02-18 03:08:58 +0000244 // The IBAction attributes only apply to instance methods.
245 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
246 if (MD->isInstanceMethod()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000247 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000248 return;
249 }
250
251 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
252}
253
254static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
255 // check the attribute arguments.
256 if (Attr.getNumArgs() > 0) {
257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
258 return;
259 }
260
261 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000262 // Objective-C classes.
263 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000264 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000265 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000266 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000267
268 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000269}
270
Ted Kremenek26bde772010-05-19 17:38:06 +0000271static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
272 Sema &S) {
273
274 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000275 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000276 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
277 return;
278 }
279
280 // The IBOutletCollection attributes only apply to instance variables of
281 // Objective-C classes.
282 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
283 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
284 return;
285 }
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000286 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
287 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
288 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
289 << VD->getType() << 0;
290 return;
291 }
292 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
293 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
294 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
295 << PD->getType() << 1;
296 return;
297 }
298
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000299 IdentifierInfo *II = Attr.getParameterName();
300 if (!II)
301 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000302
John McCallba7bf592010-08-24 05:47:05 +0000303 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000304 S.getScopeForContext(d->getDeclContext()->getParent()));
305 if (!TypeRep) {
306 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
307 return;
308 }
John McCallba7bf592010-08-24 05:47:05 +0000309 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000310 // Diagnose use of non-object type in iboutletcollection attribute.
311 // FIXME. Gnu attribute extension ignores use of builtin types in
312 // attributes. So, __attribute__((iboutletcollection(char))) will be
313 // treated as __attribute__((iboutletcollection())).
314 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
315 !QT->isObjCObjectType()) {
316 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
317 return;
318 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000319 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
320 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000321}
322
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000323static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000324 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
325 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000326 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000327 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000328 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000329 return;
330 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000331
Chandler Carruth743682b2010-11-16 08:35:43 +0000332 // In C++ the implicit 'this' function parameter also counts, and they are
333 // counted from one.
334 bool HasImplicitThisParam = isInstanceMethod(d);
335 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000336
337 // The nonnull attribute only applies to pointers.
338 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000339
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000340 for (AttributeList::arg_iterator I=Attr.arg_begin(),
341 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000342
343
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000344 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000345 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000346 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000347 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
348 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000349 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
350 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000351 return;
352 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000353
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000354 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000355
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000356 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000358 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000359 return;
360 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000361
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000362 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000363 if (HasImplicitThisParam) {
364 if (x == 0) {
365 S.Diag(Attr.getLoc(),
366 diag::err_attribute_invalid_implicit_this_argument)
367 << "nonnull" << Ex->getSourceRange();
368 return;
369 }
370 --x;
371 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000372
373 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000374 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000375 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000376 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000377 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000378 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000379 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000380 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000381
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000382 NonNullArgs.push_back(x);
383 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000384
385 // If no arguments were specified to __attribute__((nonnull)) then all pointer
386 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000387 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000388 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
389 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000390 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000391 NonNullArgs.push_back(I);
Fariborz Jahanian3567c422010-09-27 22:42:37 +0000392 else if (const RecordType *UT = T->getAsUnionType()) {
393 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
394 RecordDecl *UD = UT->getDecl();
395 for (RecordDecl::field_iterator it = UD->field_begin(),
396 itend = UD->field_end(); it != itend; ++it) {
397 T = it->getType();
398 if (T->isAnyPointerType() || T->isBlockPointerType()) {
399 NonNullArgs.push_back(I);
400 break;
401 }
402 }
403 }
404 }
Ted Kremenek5fa50522008-11-18 06:52:58 +0000405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000406
Ted Kremenek22813f42010-10-21 18:49:36 +0000407 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000408 if (NonNullArgs.empty()) {
409 // Warn the trivial case only if attribute is not coming from a
410 // macro instantiation.
411 if (Attr.getLoc().isFileID())
412 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000413 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000414 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000415 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000416
417 unsigned* start = &NonNullArgs[0];
418 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000419 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000420 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
421 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000422}
423
Ted Kremenekd21139a2010-07-31 01:52:11 +0000424static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
425 // This attribute must be applied to a function declaration.
426 // The first argument to the attribute must be a string,
427 // the name of the resource, for example "malloc".
428 // The following arguments must be argument indexes, the arguments must be
429 // of integer type for Returns, otherwise of pointer type.
430 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000431 // after being held. free() should be __attribute((ownership_takes)), whereas
432 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000433
434 if (!AL.getParameterName()) {
435 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
436 << AL.getName()->getName() << 1;
437 return;
438 }
439 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000440 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000441 switch (AL.getKind()) {
442 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000443 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000444 if (AL.getNumArgs() < 1) {
445 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
446 return;
447 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000448 break;
449 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000450 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000451 if (AL.getNumArgs() < 1) {
452 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
453 return;
454 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000455 break;
456 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000457 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000458 if (AL.getNumArgs() > 1) {
459 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
460 << AL.getNumArgs() + 1;
461 return;
462 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000463 break;
464 default:
465 // This should never happen given how we are called.
466 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000467 }
468
469 if (!isFunction(d) || !hasFunctionProto(d)) {
470 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
471 << 0 /*function*/;
472 return;
473 }
474
Chandler Carruth743682b2010-11-16 08:35:43 +0000475 // In C++ the implicit 'this' function parameter also counts, and they are
476 // counted from one.
477 bool HasImplicitThisParam = isInstanceMethod(d);
478 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000479
480 llvm::StringRef Module = AL.getParameterName()->getName();
481
482 // Normalize the argument, __foo__ becomes foo.
483 if (Module.startswith("__") && Module.endswith("__"))
484 Module = Module.substr(2, Module.size() - 4);
485
486 llvm::SmallVector<unsigned, 10> OwnershipArgs;
487
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000488 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
489 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000490
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000491 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000492 llvm::APSInt ArgNum(32);
493 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
494 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
495 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
496 << AL.getName()->getName() << IdxExpr->getSourceRange();
497 continue;
498 }
499
500 unsigned x = (unsigned) ArgNum.getZExtValue();
501
502 if (x > NumArgs || x < 1) {
503 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
504 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
505 continue;
506 }
507 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000508 if (HasImplicitThisParam) {
509 if (x == 0) {
510 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
511 << "ownership" << IdxExpr->getSourceRange();
512 return;
513 }
514 --x;
515 }
516
Ted Kremenekd21139a2010-07-31 01:52:11 +0000517 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000518 case OwnershipAttr::Takes:
519 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000520 // Is the function argument a pointer type?
521 QualType T = getFunctionOrMethodArgType(d, x);
522 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
523 // FIXME: Should also highlight argument in decl.
524 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000525 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000526 << "pointer"
527 << IdxExpr->getSourceRange();
528 continue;
529 }
530 break;
531 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000532 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000533 if (AL.getNumArgs() > 1) {
534 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000535 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000536 llvm::APSInt ArgNum(32);
537 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
538 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
539 S.Diag(AL.getLoc(), diag::err_ownership_type)
540 << "ownership_returns" << "integer"
541 << IdxExpr->getSourceRange();
542 return;
543 }
544 }
545 break;
546 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000547 default:
548 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000549 } // switch
550
551 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000552 for (specific_attr_iterator<OwnershipAttr>
553 i = d->specific_attr_begin<OwnershipAttr>(),
554 e = d->specific_attr_end<OwnershipAttr>();
555 i != e; ++i) {
556 if ((*i)->getOwnKind() != K) {
557 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
558 I!=E; ++I) {
559 if (x == *I) {
560 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
561 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000562 }
563 }
564 }
565 }
566 OwnershipArgs.push_back(x);
567 }
568
569 unsigned* start = OwnershipArgs.data();
570 unsigned size = OwnershipArgs.size();
571 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000572
573 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
574 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
575 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000576 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000577
578 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
579 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000580}
581
Rafael Espindolac18086a2010-02-23 22:00:30 +0000582static bool isStaticVarOrStaticFunciton(Decl *D) {
583 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000584 return VD->getStorageClass() == SC_Static;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000585 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000586 return FD->getStorageClass() == SC_Static;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000587 return false;
588}
589
590static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
591 // Check the attribute arguments.
592 if (Attr.getNumArgs() > 1) {
593 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
594 return;
595 }
596
597 // gcc rejects
598 // class c {
599 // static int a __attribute__((weakref ("v2")));
600 // static int b() __attribute__((weakref ("f3")));
601 // };
602 // and ignores the attributes of
603 // void f(void) {
604 // static int a __attribute__((weakref ("v2")));
605 // }
606 // we reject them
Sebastian Redl50c68252010-08-31 00:36:30 +0000607 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
608 if (!Ctx->isFileContext()) {
609 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
610 dyn_cast<NamedDecl>(d)->getNameAsString();
611 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000612 }
613
614 // The GCC manual says
615 //
616 // At present, a declaration to which `weakref' is attached can only
617 // be `static'.
618 //
619 // It also says
620 //
621 // Without a TARGET,
622 // given as an argument to `weakref' or to `alias', `weakref' is
623 // equivalent to `weak'.
624 //
625 // gcc 4.4.1 will accept
626 // int a7 __attribute__((weakref));
627 // as
628 // int a7 __attribute__((weak));
629 // This looks like a bug in gcc. We reject that for now. We should revisit
630 // it if this behaviour is actually used.
631
632 if (!isStaticVarOrStaticFunciton(d)) {
633 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
634 dyn_cast<NamedDecl>(d)->getNameAsString();
635 return;
636 }
637
638 // GCC rejects
639 // static ((alias ("y"), weakref)).
640 // Should we? How to check that weakref is before or after alias?
641
642 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000643 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000644 Arg = Arg->IgnoreParenCasts();
645 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
646
647 if (Str == 0 || Str->isWide()) {
648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
649 << "weakref" << 1;
650 return;
651 }
652 // GCC will accept anything as the argument of weakref. Should we
653 // check for an existing decl?
Eric Christopherbc638a82010-12-01 22:13:54 +0000654 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
655 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000656 }
657
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000658 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000659}
660
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000661static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000662 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000663 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000664 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000665 return;
666 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000667
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000668 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000669 Arg = Arg->IgnoreParenCasts();
670 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000671
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000672 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000673 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000674 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000675 return;
676 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000677
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000678 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000679
Eric Christopherbc638a82010-12-01 22:13:54 +0000680 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
681 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000682}
683
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000684static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000685 Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000686 // Check the attribute arguments.
Daniel Dunbar03a38442008-10-28 00:17:57 +0000687 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000689 return;
690 }
Anders Carlsson88097122009-02-19 19:16:48 +0000691
Chris Lattner4225e232009-04-14 17:02:11 +0000692 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000693 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000694 << Attr.getName() << 0 /*function*/;
695 return;
696 }
697
698 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
699}
700
701static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
702 Sema &S) {
703 // Check the attribute arguments.
704 if (Attr.getNumArgs() != 0) {
705 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
706 return;
707 }
708
709 if (!isa<FunctionDecl>(d)) {
710 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
711 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000712 return;
713 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000714
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000715 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000716}
717
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000718static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000719 // Check the attribute arguments.
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000720 if (Attr.getNumArgs() != 0) {
721 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
722 return;
723 }
Mike Stump11289f42009-09-09 15:08:12 +0000724
Ted Kremenek08479ae2009-08-15 00:51:46 +0000725 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000726 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000727 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000728 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000729 return;
730 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000731 }
732
Ted Kremenek08479ae2009-08-15 00:51:46 +0000733 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000734}
735
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000736static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
737 // check the attribute arguments.
738 if (Attr.getNumArgs() != 0) {
739 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
740 return;
741 }
742
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000743 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
744}
745
Eric Christopher8a2ee392010-12-02 02:45:55 +0000746static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
747 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000748 if (isa<VarDecl>(d))
749 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
750 else
751 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
752 << Attr.getName() << 12 /* variable */;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000753}
754
755static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
756 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000757 if (isa<VarDecl>(d))
758 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
759 else
760 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
761 << Attr.getName() << 12 /* variable */;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000762}
763
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000764static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000765 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
766 assert(Attr.isInvalid() == false);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000767 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000768}
769
770static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
771 Sema &S) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000772
773 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
774 // because 'analyzer_noreturn' does not impact the type.
775
776 if (Attr.getNumArgs() != 0) {
777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
778 return;
779 }
780
781 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
782 ValueDecl *VD = dyn_cast<ValueDecl>(d);
783 if (VD == 0 || (!VD->getType()->isBlockPointerType()
784 && !VD->getType()->isFunctionPointerType())) {
785 S.Diag(Attr.getLoc(),
786 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
787 : diag::warn_attribute_wrong_decl_type)
788 << Attr.getName() << 0 /*function*/;
789 return;
790 }
791 }
792
793 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000794}
795
John Thompsoncdb847ba2010-08-09 21:53:52 +0000796// PS3 PPU-specific.
797static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
798 Sema &S) {
799/*
800 Returning a Vector Class in Registers
801
Eric Christopherbc638a82010-12-01 22:13:54 +0000802 According to the PPU ABI specifications, a class with a single member of
803 vector type is returned in memory when used as the return value of a function.
804 This results in inefficient code when implementing vector classes. To return
805 the value in a single vector register, add the vecreturn attribute to the
806 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +0000807
808 Example:
809
810 struct Vector
811 {
812 __vector float xyzw;
813 } __attribute__((vecreturn));
814
815 Vector Add(Vector lhs, Vector rhs)
816 {
817 Vector result;
818 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
819 return result; // This will be returned in a register
820 }
821*/
John Thompson9a587aaa2010-09-18 01:12:07 +0000822 if (!isa<RecordDecl>(d)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000823 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
824 << Attr.getName() << 9 /*class*/;
825 return;
826 }
827
828 if (d->getAttr<VecReturnAttr>()) {
829 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
830 return;
831 }
832
John Thompson9a587aaa2010-09-18 01:12:07 +0000833 RecordDecl *record = cast<RecordDecl>(d);
834 int count = 0;
835
836 if (!isa<CXXRecordDecl>(record)) {
837 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
838 return;
839 }
840
841 if (!cast<CXXRecordDecl>(record)->isPOD()) {
842 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
843 return;
844 }
845
Eric Christopherbc638a82010-12-01 22:13:54 +0000846 for (RecordDecl::field_iterator iter = record->field_begin();
847 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +0000848 if ((count == 1) || !iter->getType()->isVectorType()) {
849 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
850 return;
851 }
852 count++;
853 }
854
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000855 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000856}
857
Alexis Hunt96d5c762009-11-21 08:43:09 +0000858static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
859 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000861 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000862 return;
863 }
864 // FIXME: Actually store the attribute on the declaration
865}
866
Ted Kremenek39c59a82008-07-25 04:39:19 +0000867static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
868 // check the attribute arguments.
869 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000870 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000871 return;
872 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000873
John McCallcef15822010-03-31 02:47:45 +0000874 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
875 !isa<TypeDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000876 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000877 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000878 return;
879 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000880
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000881 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000882}
883
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000884static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
885 // check the attribute arguments.
886 if (Attr.getNumArgs() != 0) {
887 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
888 return;
889 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000890
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000891 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000892 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000893 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
894 return;
895 }
896 } else if (!isFunctionOrMethod(d)) {
897 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000898 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000899 return;
900 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000901
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000902 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000903}
904
Daniel Dunbar032db472008-07-31 22:40:48 +0000905static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
906 // check the attribute arguments.
907 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000908 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
909 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000910 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000911 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000912
913 int priority = 65535; // FIXME: Do not hardcode such constants.
914 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000915 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000916 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000917 if (E->isTypeDependent() || E->isValueDependent() ||
918 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000919 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000920 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000921 return;
922 }
923 priority = Idx.getZExtValue();
924 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000925
Chris Lattner4225e232009-04-14 17:02:11 +0000926 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000927 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000928 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000929 return;
930 }
931
Eric Christopherbc638a82010-12-01 22:13:54 +0000932 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
933 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000934}
935
936static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
937 // check the attribute arguments.
938 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000939 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
940 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000941 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000942 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000943
944 int priority = 65535; // FIXME: Do not hardcode such constants.
945 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000946 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000947 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000948 if (E->isTypeDependent() || E->isValueDependent() ||
949 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000950 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000951 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000952 return;
953 }
954 priority = Idx.getZExtValue();
955 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000956
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000957 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000958 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000959 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000960 return;
961 }
962
Eric Christopherbc638a82010-12-01 22:13:54 +0000963 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
964 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000965}
966
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000967static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000968 // check the attribute arguments.
Fariborz Jahanian551063102010-10-06 21:18:44 +0000969 int noArgs = Attr.getNumArgs();
970 if (noArgs > 1) {
971 S.Diag(Attr.getLoc(),
972 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000973 return;
974 }
Fariborz Jahanian551063102010-10-06 21:18:44 +0000975 // Handle the case where deprecated attribute has a text message.
976 StringLiteral *SE;
977 if (noArgs == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000978 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanian551063102010-10-06 21:18:44 +0000979 SE = dyn_cast<StringLiteral>(ArgExpr);
980 if (!SE) {
981 S.Diag(ArgExpr->getLocStart(),
982 diag::err_attribute_not_string) << "deprecated";
983 return;
984 }
985 }
986 else
987 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000988
Fariborz Jahanian551063102010-10-06 21:18:44 +0000989 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
990 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000991}
992
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000993static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
994 // check the attribute arguments.
Fariborz Jahanianc74073c2010-10-06 23:12:32 +0000995 int noArgs = Attr.getNumArgs();
996 if (noArgs > 1) {
Eric Christopherbc638a82010-12-01 22:13:54 +0000997 S.Diag(Attr.getLoc(),
998 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000999 return;
1000 }
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001001 // Handle the case where unavailable attribute has a text message.
1002 StringLiteral *SE;
1003 if (noArgs == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001004 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001005 SE = dyn_cast<StringLiteral>(ArgExpr);
1006 if (!SE) {
1007 S.Diag(ArgExpr->getLocStart(),
1008 diag::err_attribute_not_string) << "unavailable";
1009 return;
1010 }
1011 }
1012 else
1013 SE = StringLiteral::CreateEmpty(S.Context, 1);
1014 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
1015 SE->getString()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001016}
1017
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001018static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001019 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001020 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001021 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001022 return;
1023 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001024
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001025 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001026 Arg = Arg->IgnoreParenCasts();
1027 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001028
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001029 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001030 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001031 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001032 return;
1033 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001034
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001035 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001036 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001037
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001038 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001039 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001040 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001041 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001042 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001043 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001044 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001045 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001046 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001047 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001048 return;
1049 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001050
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001051 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001052}
1053
Chris Lattner677a3582009-02-14 08:09:34 +00001054static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1055 Sema &S) {
1056 if (Attr.getNumArgs() != 0) {
1057 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1058 return;
1059 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001060
Chris Lattner677a3582009-02-14 08:09:34 +00001061 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1062 if (OCI == 0) {
1063 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1064 return;
1065 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001066
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001067 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001068}
1069
1070static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001071 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001072 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001073 return;
1074 }
Chris Lattner677a3582009-02-14 08:09:34 +00001075 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001076 QualType T = TD->getUnderlyingType();
1077 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001078 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001079 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1080 return;
1081 }
1082 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001083 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001084}
1085
Mike Stumpd3bb5572009-07-24 19:02:52 +00001086static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001087HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1088 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001089 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001090 return;
1091 }
1092
1093 if (!isa<FunctionDecl>(D)) {
1094 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1095 return;
1096 }
1097
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001098 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001099}
1100
Steve Naroff3405a732008-09-18 16:44:58 +00001101static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001102 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001103 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001104 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001105 return;
1106 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001107
Steve Naroff3405a732008-09-18 16:44:58 +00001108 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001109 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001110 return;
1111 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001112
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001113 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001114 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001115 type = BlocksAttr::ByRef;
1116 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001117 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001118 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001119 return;
1120 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001121
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001122 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001123}
1124
Anders Carlssonc181b012008-10-05 18:05:59 +00001125static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1126 // check the attribute arguments.
1127 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +00001128 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1129 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +00001130 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001131 }
1132
Anders Carlssonc181b012008-10-05 18:05:59 +00001133 int sentinel = 0;
1134 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001135 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001136 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001137 if (E->isTypeDependent() || E->isValueDependent() ||
1138 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001139 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001140 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001141 return;
1142 }
1143 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001144
Anders Carlssonc181b012008-10-05 18:05:59 +00001145 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001146 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1147 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001148 return;
1149 }
1150 }
1151
1152 int nullPos = 0;
1153 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001154 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001155 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001156 if (E->isTypeDependent() || E->isValueDependent() ||
1157 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001158 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001159 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001160 return;
1161 }
1162 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001163
Anders Carlssonc181b012008-10-05 18:05:59 +00001164 if (nullPos > 1 || nullPos < 0) {
1165 // FIXME: This error message could be improved, it would be nice
1166 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001167 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1168 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001169 return;
1170 }
1171 }
1172
1173 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001174 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001175 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001176
Chris Lattner9363e312009-03-17 23:03:47 +00001177 if (isa<FunctionNoProtoType>(FT)) {
1178 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1179 return;
1180 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001181
Chris Lattner9363e312009-03-17 23:03:47 +00001182 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001183 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001184 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001185 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001186 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1187 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001188 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001189 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001190 }
1191 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001192 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1193 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001194 ;
1195 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001196 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001197 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001198 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherbc638a82010-12-01 22:13:54 +00001199 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001200 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001201 int m = Ty->isFunctionPointerType() ? 0 : 1;
1202 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001203 return;
1204 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001205 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001206 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001207 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001208 return;
1209 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001210 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001211 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001212 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +00001213 return;
1214 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001215 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1216 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001217}
1218
Chris Lattner237f2752009-02-14 07:37:35 +00001219static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1220 // check the attribute arguments.
1221 if (Attr.getNumArgs() != 0) {
1222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1223 return;
1224 }
1225
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001226 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001227 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001228 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +00001229 return;
1230 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001231
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001232 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1233 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1234 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001235 return;
1236 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001237 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1238 if (MD->getResultType()->isVoidType()) {
1239 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1240 << Attr.getName() << 1;
1241 return;
1242 }
1243
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001244 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001245}
1246
1247static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001248 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001249 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001250 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001251 return;
1252 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001253
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001254 /* weak only applies to non-static declarations */
Rafael Espindolac18086a2010-02-23 22:00:30 +00001255 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001256 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1257 dyn_cast<NamedDecl>(D)->getNameAsString();
1258 return;
1259 }
1260
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001261 // TODO: could also be applied to methods?
1262 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1263 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001264 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001265 return;
1266 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001267
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001268 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001269}
1270
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001271static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1272 // check the attribute arguments.
1273 if (Attr.getNumArgs() != 0) {
1274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1275 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001276 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001277
1278 // weak_import only applies to variable & function declarations.
1279 bool isDef = false;
1280 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1281 isDef = (!VD->hasExternalStorage() || VD->getInit());
1282 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001283 isDef = FD->hasBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +00001284 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1285 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +00001286 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +00001287 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001288 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001289 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001290 !isa<ObjCInterfaceDecl>(D))
1291 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001292 << Attr.getName() << 2 /*variable and function*/;
1293 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001294 }
1295
1296 // Merge should handle any subsequent violations.
1297 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001298 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001299 diag::warn_attribute_weak_import_invalid_on_definition)
1300 << "weak_import" << 2 /*variable and function*/;
1301 return;
1302 }
1303
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001304 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001305}
1306
Nate Begemanf2758702009-06-26 06:32:41 +00001307static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1308 Sema &S) {
1309 // Attribute has 3 arguments.
1310 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001311 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001312 return;
1313 }
1314
1315 unsigned WGSize[3];
1316 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001317 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001318 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001319 if (E->isTypeDependent() || E->isValueDependent() ||
1320 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1322 << "reqd_work_group_size" << E->getSourceRange();
1323 return;
1324 }
1325 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1326 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001327 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1328 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001329 WGSize[2]));
1330}
1331
Chris Lattner237f2752009-02-14 07:37:35 +00001332static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001333 // Attribute has no arguments.
1334 if (Attr.getNumArgs() != 1) {
1335 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1336 return;
1337 }
1338
1339 // Make sure that there is a string literal as the sections's single
1340 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001341 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001342 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001343 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001344 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001345 return;
1346 }
Mike Stump11289f42009-09-09 15:08:12 +00001347
Chris Lattner30ba6742009-08-10 19:03:04 +00001348 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001349 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001350 if (!Error.empty()) {
1351 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1352 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001353 return;
1354 }
Mike Stump11289f42009-09-09 15:08:12 +00001355
Chris Lattner20aee9b2010-01-12 20:58:53 +00001356 // This attribute cannot be applied to local variables.
1357 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1358 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1359 return;
1360 }
1361
Eric Christopherbc638a82010-12-01 22:13:54 +00001362 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1363 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001364}
1365
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001366
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001367static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001368 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001369 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001371 return;
1372 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001373
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001374 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001375}
1376
Anders Carlssonb8316282008-10-05 23:32:53 +00001377static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1378 // check the attribute arguments.
1379 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001380 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001381 return;
1382 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001383
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001384 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001385}
1386
1387static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1388 // check the attribute arguments.
1389 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001390 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001391 return;
1392 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001393
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001394 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001395}
1396
Anders Carlssond277d792009-01-31 01:16:18 +00001397static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001398 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001399 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1400 return;
1401 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001402
Anders Carlssond277d792009-01-31 01:16:18 +00001403 if (Attr.getNumArgs() != 0) {
1404 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1405 return;
1406 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001407
Anders Carlssond277d792009-01-31 01:16:18 +00001408 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001409
Anders Carlssond277d792009-01-31 01:16:18 +00001410 if (!VD || !VD->hasLocalStorage()) {
1411 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1412 return;
1413 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001414
Anders Carlssond277d792009-01-31 01:16:18 +00001415 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001416 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001417 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001418 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1419 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001420 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001421 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001422 Attr.getParameterName();
1423 return;
1424 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001425
Anders Carlssond277d792009-01-31 01:16:18 +00001426 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1427 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001428 S.Diag(Attr.getParameterLoc(),
1429 diag::err_attribute_cleanup_arg_not_function)
1430 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001431 return;
1432 }
1433
Anders Carlssond277d792009-01-31 01:16:18 +00001434 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001435 S.Diag(Attr.getParameterLoc(),
1436 diag::err_attribute_cleanup_func_must_take_one_arg)
1437 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001438 return;
1439 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001440
Anders Carlsson723f55d2009-02-07 23:16:50 +00001441 // We're currently more strict than GCC about what function types we accept.
1442 // If this ever proves to be a problem it should be easy to fix.
1443 QualType Ty = S.Context.getPointerType(VD->getType());
1444 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall29600e12010-11-16 02:32:08 +00001445 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001446 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001447 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1448 Attr.getParameterName() << ParamTy << Ty;
1449 return;
1450 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001451
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001452 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001453 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001454}
1455
Mike Stumpd3bb5572009-07-24 19:02:52 +00001456/// Handle __attribute__((format_arg((idx)))) attribute based on
1457/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1458static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001459 if (Attr.getNumArgs() != 1) {
1460 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1461 return;
1462 }
1463 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1464 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1465 << Attr.getName() << 0 /*function*/;
1466 return;
1467 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001468
1469 // In C++ the implicit 'this' function parameter also counts, and they are
1470 // counted from one.
1471 bool HasImplicitThisParam = isInstanceMethod(d);
1472 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001473 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001474
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001475 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001476 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001477 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001478 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1479 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1481 << "format" << 2 << IdxExpr->getSourceRange();
1482 return;
1483 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001484
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001485 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1486 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1487 << "format" << 2 << IdxExpr->getSourceRange();
1488 return;
1489 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001490
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001491 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001492
Chandler Carruth743682b2010-11-16 08:35:43 +00001493 if (HasImplicitThisParam) {
1494 if (ArgIdx == 0) {
1495 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1496 << "format_arg" << IdxExpr->getSourceRange();
1497 return;
1498 }
1499 ArgIdx--;
1500 }
1501
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001502 // make sure the format string is really a string
1503 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001504
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001505 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1506 if (not_nsstring_type &&
1507 !isCFStringType(Ty, S.Context) &&
1508 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001509 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001510 // FIXME: Should highlight the actual expression that has the wrong type.
1511 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001512 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001513 << IdxExpr->getSourceRange();
1514 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001515 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001516 Ty = getFunctionOrMethodResultType(d);
1517 if (!isNSStringType(Ty, S.Context) &&
1518 !isCFStringType(Ty, S.Context) &&
1519 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001520 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001521 // FIXME: Should highlight the actual expression that has the wrong type.
1522 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001523 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001524 << IdxExpr->getSourceRange();
1525 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001526 }
1527
Chandler Carruth743682b2010-11-16 08:35:43 +00001528 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1529 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001530}
1531
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001532enum FormatAttrKind {
1533 CFStringFormat,
1534 NSStringFormat,
1535 StrftimeFormat,
1536 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001537 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001538 InvalidFormat
1539};
1540
1541/// getFormatAttrKind - Map from format attribute names to supported format
1542/// types.
1543static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1544 // Check for formats that get handled specially.
1545 if (Format == "NSString")
1546 return NSStringFormat;
1547 if (Format == "CFString")
1548 return CFStringFormat;
1549 if (Format == "strftime")
1550 return StrftimeFormat;
1551
1552 // Otherwise, check for supported formats.
1553 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1554 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1555 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1556 Format == "zcmn_err")
1557 return SupportedFormat;
1558
Duncan Sandsde4fe352010-03-23 14:44:19 +00001559 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1560 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001561 return IgnoredFormat;
1562
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001563 return InvalidFormat;
1564}
1565
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001566/// Handle __attribute__((init_priority(priority))) attributes based on
1567/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1568static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1569 Sema &S) {
1570 if (!S.getLangOptions().CPlusPlus) {
1571 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1572 return;
1573 }
1574
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001575 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1576 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1577 Attr.setInvalid();
1578 return;
1579 }
1580 QualType T = dyn_cast<VarDecl>(d)->getType();
1581 if (S.Context.getAsArrayType(T))
1582 T = S.Context.getBaseElementType(T);
1583 if (!T->getAs<RecordType>()) {
1584 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1585 Attr.setInvalid();
1586 return;
1587 }
1588
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001589 if (Attr.getNumArgs() != 1) {
1590 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1591 Attr.setInvalid();
1592 return;
1593 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001594 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001595
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001596 llvm::APSInt priority(32);
1597 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1598 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1599 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1600 << "init_priority" << priorityExpr->getSourceRange();
1601 Attr.setInvalid();
1602 return;
1603 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001604 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001605 if (prioritynum < 101 || prioritynum > 65535) {
1606 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1607 << priorityExpr->getSourceRange();
1608 Attr.setInvalid();
1609 return;
1610 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001611 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1612 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001613}
1614
Mike Stumpd3bb5572009-07-24 19:02:52 +00001615/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1616/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001617static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001618
Chris Lattner4a927cb2008-06-28 23:36:30 +00001619 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001620 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001621 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001622 return;
1623 }
1624
Chris Lattner4a927cb2008-06-28 23:36:30 +00001625 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001626 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001627 return;
1628 }
1629
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001630 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001631 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001632 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001633 return;
1634 }
1635
Chandler Carruth743682b2010-11-16 08:35:43 +00001636 // In C++ the implicit 'this' function parameter also counts, and they are
1637 // counted from one.
1638 bool HasImplicitThisParam = isInstanceMethod(d);
1639 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001640 unsigned FirstIdx = 1;
1641
Daniel Dunbar07d07852009-10-18 21:17:35 +00001642 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001643
1644 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001645 if (Format.startswith("__") && Format.endswith("__"))
1646 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001647
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001648 // Check for supported formats.
1649 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001650
1651 if (Kind == IgnoredFormat)
1652 return;
1653
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001654 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001655 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001656 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001657 return;
1658 }
1659
1660 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001661 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001662 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001663 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1664 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001666 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001667 return;
1668 }
1669
1670 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001671 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001672 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001673 return;
1674 }
1675
1676 // FIXME: Do we need to bounds check?
1677 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001678
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001679 if (HasImplicitThisParam) {
1680 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001681 S.Diag(Attr.getLoc(),
1682 diag::err_format_attribute_implicit_this_format_string)
1683 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001684 return;
1685 }
1686 ArgIdx--;
1687 }
Mike Stump11289f42009-09-09 15:08:12 +00001688
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001689 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001690 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001691
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001692 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001693 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001694 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1695 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001696 return;
1697 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001698 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001699 // FIXME: do we need to check if the type is NSString*? What are the
1700 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001701 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001702 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001703 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1704 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001705 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001706 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001707 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001708 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001709 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001710 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1711 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001712 return;
1713 }
1714
1715 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001716 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001717 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001718 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1719 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001720 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001721 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001722 return;
1723 }
1724
1725 // check if the function is variadic if the 3rd argument non-zero
1726 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001727 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001728 ++NumArgs; // +1 for ...
1729 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001730 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001731 return;
1732 }
1733 }
1734
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001735 // strftime requires FirstArg to be 0 because it doesn't read from any
1736 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001737 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001738 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001739 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1740 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001741 return;
1742 }
1743 // if 0 it disables parameter checking (to use with e.g. va_list)
1744 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001745 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001746 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001747 return;
1748 }
1749
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001750 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1751 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001752 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001753}
1754
Chris Lattnera663a0a2008-06-29 00:28:59 +00001755static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1756 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001757 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001758 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001759 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001760 return;
1761 }
1762
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001763 // Try to find the underlying union declaration.
1764 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001765 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001766 if (TD && TD->getUnderlyingType()->isUnionType())
1767 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1768 else
1769 RD = dyn_cast<RecordDecl>(d);
1770
1771 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001772 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001773 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001774 return;
1775 }
1776
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001777 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001778 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001779 diag::warn_transparent_union_attribute_not_definition);
1780 return;
1781 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001782
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001783 RecordDecl::field_iterator Field = RD->field_begin(),
1784 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001785 if (Field == FieldEnd) {
1786 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1787 return;
1788 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001789
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001790 FieldDecl *FirstField = *Field;
1791 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001792 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001793 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001794 diag::warn_transparent_union_attribute_floating)
1795 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001796 return;
1797 }
1798
1799 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1800 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1801 for (; Field != FieldEnd; ++Field) {
1802 QualType FieldType = Field->getType();
1803 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1804 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1805 // Warn if we drop the attribute.
1806 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001807 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001808 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001809 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001810 diag::warn_transparent_union_attribute_field_size_align)
1811 << isSize << Field->getDeclName() << FieldBits;
1812 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001813 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001814 diag::note_transparent_union_first_field_size_align)
1815 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001816 return;
1817 }
1818 }
1819
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001820 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001821}
1822
Chris Lattnera663a0a2008-06-29 00:28:59 +00001823static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001824 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001825 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001827 return;
1828 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001829 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001830 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001831
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001832 // Make sure that there is a string literal as the annotation's single
1833 // argument.
1834 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001835 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001836 return;
1837 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001838 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1839 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001840}
1841
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001842static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001843 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001844 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001846 return;
1847 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001848
1849 //FIXME: The C++0x version of this attribute has more limited applicabilty
1850 // than GNU's, and should error out when it is used to specify a
1851 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001852
Chris Lattner4a927cb2008-06-28 23:36:30 +00001853 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001854 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001855 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001856 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001857
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001858 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001859}
1860
1861void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1862 if (E->isTypeDependent() || E->isValueDependent()) {
1863 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001864 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001865 return;
1866 }
1867
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001868 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00001869 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001870 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1871 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1872 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001873 return;
1874 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001875 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001876 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1877 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001878 return;
1879 }
1880
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001881 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1882}
1883
1884void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1885 // FIXME: Cache the number on the Attr object if non-dependent?
1886 // FIXME: Perform checking of type validity
1887 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1888 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001889}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001890
Mike Stumpd3bb5572009-07-24 19:02:52 +00001891/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1892/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001893///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001894/// Despite what would be logical, the mode attribute is a decl attribute, not a
1895/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1896/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001897static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001898 // This attribute isn't documented, but glibc uses it. It changes
1899 // the width of an int or unsigned int to the specified size.
1900
1901 // Check that there aren't any arguments
1902 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001903 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001904 return;
1905 }
1906
1907 IdentifierInfo *Name = Attr.getParameterName();
1908 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001909 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001910 return;
1911 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001912
Daniel Dunbar07d07852009-10-18 21:17:35 +00001913 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001914
1915 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001916 if (Str.startswith("__") && Str.endswith("__"))
1917 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001918
1919 unsigned DestWidth = 0;
1920 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001921 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001922 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001923 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001924 switch (Str[0]) {
1925 case 'Q': DestWidth = 8; break;
1926 case 'H': DestWidth = 16; break;
1927 case 'S': DestWidth = 32; break;
1928 case 'D': DestWidth = 64; break;
1929 case 'X': DestWidth = 96; break;
1930 case 'T': DestWidth = 128; break;
1931 }
1932 if (Str[1] == 'F') {
1933 IntegerMode = false;
1934 } else if (Str[1] == 'C') {
1935 IntegerMode = false;
1936 ComplexMode = true;
1937 } else if (Str[1] != 'I') {
1938 DestWidth = 0;
1939 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001940 break;
1941 case 4:
1942 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1943 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001944 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001945 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001946 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001947 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001948 break;
1949 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001950 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001951 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001952 break;
1953 }
1954
1955 QualType OldTy;
1956 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1957 OldTy = TD->getUnderlyingType();
1958 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1959 OldTy = VD->getType();
1960 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001961 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1962 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001963 return;
1964 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001965
John McCall9dd450b2009-09-21 23:43:11 +00001966 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001967 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1968 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00001969 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001970 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1971 } else if (ComplexMode) {
1972 if (!OldTy->isComplexType())
1973 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1974 } else {
1975 if (!OldTy->isFloatingType())
1976 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1977 }
1978
Mike Stump87c57ac2009-05-16 07:39:55 +00001979 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1980 // and friends, at least with glibc.
1981 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1982 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001983 // FIXME: Make sure floating-point mappings are accurate
1984 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001985 QualType NewTy;
1986 switch (DestWidth) {
1987 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001988 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001989 return;
1990 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001991 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001992 return;
1993 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001994 if (!IntegerMode) {
1995 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1996 return;
1997 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001998 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001999 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002000 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002001 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002002 break;
2003 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002004 if (!IntegerMode) {
2005 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2006 return;
2007 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002008 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002009 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002010 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002011 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002012 break;
2013 case 32:
2014 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002015 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002016 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002017 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002018 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002019 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002020 break;
2021 case 64:
2022 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002023 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002024 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002025 if (S.Context.Target.getLongWidth() == 64)
2026 NewTy = S.Context.LongTy;
2027 else
2028 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002029 else
Chandler Carruth72343702010-01-26 06:39:24 +00002030 if (S.Context.Target.getLongWidth() == 64)
2031 NewTy = S.Context.UnsignedLongTy;
2032 else
2033 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002034 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002035 case 96:
2036 NewTy = S.Context.LongDoubleTy;
2037 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002038 case 128:
2039 if (!IntegerMode) {
2040 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2041 return;
2042 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002043 if (OldTy->isSignedIntegerType())
2044 NewTy = S.Context.Int128Ty;
2045 else
2046 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002047 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002048 }
2049
Eli Friedman4735374e2009-03-03 06:41:03 +00002050 if (ComplexMode) {
2051 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002052 }
2053
2054 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00002055 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2056 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002057 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002058 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002059 cast<ValueDecl>(D)->setType(NewTy);
2060}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002061
Mike Stump3722f582009-08-26 22:31:08 +00002062static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002063 // check the attribute arguments.
2064 if (Attr.getNumArgs() > 0) {
2065 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2066 return;
2067 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002068
Anders Carlsson88097122009-02-19 19:16:48 +00002069 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002070 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002071 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00002072 return;
2073 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002074
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002075 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002076}
2077
Mike Stump3722f582009-08-26 22:31:08 +00002078static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00002079 // check the attribute arguments.
2080 if (Attr.getNumArgs() != 0) {
2081 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2082 return;
2083 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002084
Chris Lattner4225e232009-04-14 17:02:11 +00002085 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002086 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002087 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00002088 return;
2089 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002090
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002091 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002092}
2093
Chris Lattner3c77a352010-06-22 00:03:40 +00002094static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2095 Sema &S) {
2096 // check the attribute arguments.
2097 if (Attr.getNumArgs() != 0) {
2098 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2099 return;
2100 }
2101
2102 if (!isa<FunctionDecl>(d)) {
2103 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2104 << Attr.getName() << 0 /*function*/;
2105 return;
2106 }
2107
Eric Christopherbc638a82010-12-01 22:13:54 +00002108 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2109 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002110}
2111
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002112static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2113 if (S.LangOpts.CUDA) {
2114 // check the attribute arguments.
2115 if (Attr.getNumArgs() != 0) {
2116 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2117 return;
2118 }
2119
2120 if (!isa<VarDecl>(d)) {
2121 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2122 << Attr.getName() << 12 /*variable*/;
2123 return;
2124 }
2125
2126 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2127 } else {
2128 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2129 }
2130}
2131
2132static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2133 if (S.LangOpts.CUDA) {
2134 // check the attribute arguments.
2135 if (Attr.getNumArgs() != 0) {
2136 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2137 return;
2138 }
2139
2140 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2141 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2142 << Attr.getName() << 2 /*variable and function*/;
2143 return;
2144 }
2145
2146 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2147 } else {
2148 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2149 }
2150}
2151
2152static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2153 if (S.LangOpts.CUDA) {
2154 // check the attribute arguments.
2155 if (Attr.getNumArgs() != 0) {
2156 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2157 return;
2158 }
2159
2160 if (!isa<FunctionDecl>(d)) {
2161 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2162 << Attr.getName() << 0 /*function*/;
2163 return;
2164 }
2165
2166 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2167 } else {
2168 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2169 }
2170}
2171
2172static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2173 if (S.LangOpts.CUDA) {
2174 // check the attribute arguments.
2175 if (Attr.getNumArgs() != 0) {
2176 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2177 return;
2178 }
2179
2180 if (!isa<FunctionDecl>(d)) {
2181 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2182 << Attr.getName() << 0 /*function*/;
2183 return;
2184 }
2185
2186 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2187 } else {
2188 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2189 }
2190}
2191
2192static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2193 if (S.LangOpts.CUDA) {
2194 // check the attribute arguments.
2195 if (Attr.getNumArgs() != 0) {
2196 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2197 return;
2198 }
2199
2200 if (!isa<VarDecl>(d)) {
2201 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2202 << Attr.getName() << 12 /*variable*/;
2203 return;
2204 }
2205
2206 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2207 } else {
2208 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2209 }
2210}
2211
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002212static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002213 // check the attribute arguments.
2214 if (Attr.getNumArgs() != 0) {
2215 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2216 return;
2217 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002218
Chris Lattner4225e232009-04-14 17:02:11 +00002219 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2220 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002221 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002222 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002223 return;
2224 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002225
Douglas Gregor35b57532009-10-27 21:01:01 +00002226 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002227 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002228 return;
2229 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002230
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002231 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002232}
2233
Abramo Bagnara50099372010-04-30 13:10:51 +00002234static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2235 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2236 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2237 assert(Attr.isInvalid() == false);
2238
2239 switch (Attr.getKind()) {
2240 case AttributeList::AT_fastcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002241 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002242 return;
2243 case AttributeList::AT_stdcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002244 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002245 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002246 case AttributeList::AT_thiscall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002247 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002248 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002249 case AttributeList::AT_cdecl:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002250 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002251 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002252 case AttributeList::AT_pascal:
2253 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2254 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002255 default:
2256 llvm_unreachable("unexpected attribute kind");
2257 return;
2258 }
2259}
2260
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002261static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2262 // check the attribute arguments.
2263 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00002264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002265 return;
2266 }
Eli Friedman7044b762009-03-27 21:06:47 +00002267
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002268 if (!isFunctionOrMethod(d)) {
2269 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002270 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002271 return;
2272 }
Eli Friedman7044b762009-03-27 21:06:47 +00002273
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002274 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002275 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002276 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2277 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman7044b762009-03-27 21:06:47 +00002278 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2279 << "regparm" << NumParamsExpr->getSourceRange();
2280 return;
2281 }
2282
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002283 if (S.Context.Target.getRegParmMax() == 0) {
2284 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002285 << NumParamsExpr->getSourceRange();
2286 return;
2287 }
2288
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00002289 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002290 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2291 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00002292 return;
2293 }
2294
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002295 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2296 NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002297}
2298
Alexis Hunt96d5c762009-11-21 08:43:09 +00002299static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2300 // check the attribute arguments.
2301 if (Attr.getNumArgs() != 0) {
2302 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2303 return;
2304 }
2305
2306 if (!isa<CXXRecordDecl>(d)
2307 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2308 S.Diag(Attr.getLoc(),
2309 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2310 : diag::warn_attribute_wrong_decl_type)
2311 << Attr.getName() << 7 /*virtual method or class*/;
2312 return;
2313 }
Alexis Hunt54a02542009-11-25 04:20:27 +00002314
2315 // FIXME: Conform to C++0x redeclaration rules.
2316
2317 if (d->getAttr<FinalAttr>()) {
2318 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2319 return;
2320 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002321
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002322 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002323}
2324
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002325//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00002326// C++0x member checking attributes
2327//===----------------------------------------------------------------------===//
2328
2329static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2330 if (Attr.getNumArgs() != 0) {
2331 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2332 return;
2333 }
2334
2335 if (!isa<CXXRecordDecl>(d)) {
2336 S.Diag(Attr.getLoc(),
2337 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2338 : diag::warn_attribute_wrong_decl_type)
2339 << Attr.getName() << 9 /*class*/;
2340 return;
2341 }
2342
2343 if (d->getAttr<BaseCheckAttr>()) {
2344 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2345 return;
2346 }
2347
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002348 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002349}
2350
2351static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2352 if (Attr.getNumArgs() != 0) {
2353 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2354 return;
2355 }
2356
2357 if (!isa<RecordDecl>(d->getDeclContext())) {
2358 // FIXME: It's not the type that's the problem
2359 S.Diag(Attr.getLoc(),
2360 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2361 : diag::warn_attribute_wrong_decl_type)
2362 << Attr.getName() << 11 /*member*/;
2363 return;
2364 }
2365
2366 // FIXME: Conform to C++0x redeclaration rules.
2367
2368 if (d->getAttr<HidingAttr>()) {
2369 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2370 return;
2371 }
2372
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002373 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002374}
2375
2376static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2377 if (Attr.getNumArgs() != 0) {
2378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2379 return;
2380 }
2381
2382 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2383 // FIXME: It's not the type that's the problem
2384 S.Diag(Attr.getLoc(),
2385 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2386 : diag::warn_attribute_wrong_decl_type)
2387 << Attr.getName() << 10 /*virtual method*/;
2388 return;
2389 }
2390
2391 // FIXME: Conform to C++0x redeclaration rules.
2392
2393 if (d->getAttr<OverrideAttr>()) {
2394 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2395 return;
2396 }
2397
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002398 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002399}
2400
2401//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002402// Checker-specific attribute handlers.
2403//===----------------------------------------------------------------------===//
2404
2405static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2406 Sema &S) {
2407
Ted Kremenek3b204e42009-05-13 21:07:32 +00002408 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002409
Ted Kremenek3b204e42009-05-13 21:07:32 +00002410 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2411 RetTy = MD->getResultType();
2412 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2413 RetTy = FD->getResultType();
2414 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002415 SourceLocation L = Attr.getLoc();
2416 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2417 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002418 return;
2419 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002420
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002421 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00002422 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002423 SourceLocation L = Attr.getLoc();
2424 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2425 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002426 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002427 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002428
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002429 switch (Attr.getKind()) {
2430 default:
2431 assert(0 && "invalid ownership attribute");
2432 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002433 case AttributeList::AT_cf_returns_not_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002434 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
2435 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002436 return;
2437 case AttributeList::AT_ns_returns_not_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002438 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
2439 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002440 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002441 case AttributeList::AT_cf_returns_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002442 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
2443 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002444 return;
2445 case AttributeList::AT_ns_returns_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002446 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
2447 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002448 return;
2449 };
2450}
2451
Charles Davis163855f2010-02-16 18:27:26 +00002452static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2453 return Attr.getKind() == AttributeList::AT_dllimport ||
2454 Attr.getKind() == AttributeList::AT_dllexport;
2455}
2456
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002457//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002458// Top Level Sema Entry Points
2459//===----------------------------------------------------------------------===//
2460
Sebastian Redlfc24b632008-12-21 19:24:58 +00002461/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002462/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00002463/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2464/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00002465static void ProcessDeclAttribute(Scope *scope, Decl *D,
2466 const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002467 if (Attr.isInvalid())
2468 return;
2469
Charles Davis163855f2010-02-16 18:27:26 +00002470 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2471 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00002472 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002473 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002474 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002475 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2476 case AttributeList::AT_IBOutletCollection:
2477 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002478 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002479 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002480 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002481 case AttributeList::AT_neon_vector_type:
2482 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002483 // Ignore these, these are type attributes, handled by
2484 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002485 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002486 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2487 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002488 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002489 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002490 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002491 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002492 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2493 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002494 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002495 HandleDependencyAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002496 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002497 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002498 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2499 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2500 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002501 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002502 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002503 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002504 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002505 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2506 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2507 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002508 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002509 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2510 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002511 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002512 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2513 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00002514 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002515 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002516 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002517 case AttributeList::AT_ownership_returns:
2518 case AttributeList::AT_ownership_takes:
2519 case AttributeList::AT_ownership_holds:
2520 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00002521 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002522 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2523 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2524 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002525 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002526 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002527
2528 // Checker-specific.
Ted Kremenekd9c66632010-02-18 00:05:45 +00002529 case AttributeList::AT_ns_returns_not_retained:
2530 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002531 case AttributeList::AT_ns_returns_retained:
2532 case AttributeList::AT_cf_returns_retained:
2533 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2534
Nate Begemanf2758702009-06-26 06:32:41 +00002535 case AttributeList::AT_reqd_wg_size:
2536 HandleReqdWorkGroupSize(D, Attr, S); break;
2537
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002538 case AttributeList::AT_init_priority:
2539 HandleInitPriorityAttr(D, Attr, S); break;
2540
Alexis Hunt54a02542009-11-25 04:20:27 +00002541 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2542 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002543 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2544 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2545 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002546 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002547 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2548 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002549 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002550 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002551 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002552 case AttributeList::AT_transparent_union:
2553 HandleTransparentUnionAttr(D, Attr, S);
2554 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002555 case AttributeList::AT_objc_exception:
2556 HandleObjCExceptionAttr(D, Attr, S);
2557 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002558 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002559 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2560 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2561 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2562 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2563 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2564 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2565 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2566 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2567 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002568 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002569 // Just ignore
2570 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002571 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2572 HandleNoInstrumentFunctionAttr(D, Attr, S);
2573 break;
John McCallab26cfa2010-02-05 21:31:56 +00002574 case AttributeList::AT_stdcall:
2575 case AttributeList::AT_cdecl:
2576 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002577 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002578 case AttributeList::AT_pascal:
Abramo Bagnara50099372010-04-30 13:10:51 +00002579 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002580 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002581 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002582 // Ask target about the attribute.
2583 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2584 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002585 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2586 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002587 break;
2588 }
2589}
2590
2591/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2592/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00002593void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
2594 const AttributeList *AttrList) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002595 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2596 ProcessDeclAttribute(S, D, *l, *this);
2597 }
2598
2599 // GCC accepts
2600 // static int a9 __attribute__((weakref));
2601 // but that looks really pointless. We reject it.
2602 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2603 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002604 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002605 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002606 }
2607}
2608
Ryan Flynn7d470f32009-07-30 03:15:39 +00002609/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2610/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002611NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002612 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002613 NamedDecl *NewD = 0;
2614 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2615 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2616 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002617 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002618 if (FD->getQualifier()) {
2619 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2620 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2621 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002622 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2623 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2624 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002625 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002626 VD->getStorageClass(),
2627 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002628 if (VD->getQualifier()) {
2629 VarDecl *NewVD = cast<VarDecl>(NewD);
2630 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2631 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002632 }
2633 return NewD;
2634}
2635
2636/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2637/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002638void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002639 if (W.getUsed()) return; // only do this once
2640 W.setUsed(true);
2641 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2642 IdentifierInfo *NDId = ND->getIdentifier();
2643 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002644 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2645 NDId->getName()));
2646 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00002647 WeakTopLevelDecl.push_back(NewD);
2648 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2649 // to insert Decl at TU scope, sorry.
2650 DeclContext *SavedContext = CurContext;
2651 CurContext = Context.getTranslationUnitDecl();
2652 PushOnScopeChains(NewD, S);
2653 CurContext = SavedContext;
2654 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002655 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002656 }
2657}
2658
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002659/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2660/// it, apply them to D. This is a bit tricky because PD can have attributes
2661/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002662void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
John McCall6fe02402010-10-27 00:59:00 +00002663 // It's valid to "forward-declare" #pragma weak, in which case we
2664 // have to do this.
2665 if (!WeakUndeclaredIdentifiers.empty()) {
2666 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2667 if (IdentifierInfo *Id = ND->getIdentifier()) {
2668 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2669 = WeakUndeclaredIdentifiers.find(Id);
2670 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2671 WeakInfo W = I->second;
2672 DeclApplyPragmaWeak(S, ND, W);
2673 WeakUndeclaredIdentifiers[Id] = W;
2674 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002675 }
2676 }
2677 }
2678
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002679 // Apply decl attributes from the DeclSpec if present.
2680 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002681 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002682
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002683 // Walk the declarator structure, applying decl attributes that were in a type
2684 // position to the decl itself. This handles cases like:
2685 // int *__attr__(x)** D;
2686 // when X is a decl attribute.
2687 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2688 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002689 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002690
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002691 // Finally, apply any attributes on the decl itself.
2692 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002693 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002694}
John McCall28a6aea2009-11-04 02:18:39 +00002695
2696/// PushParsingDeclaration - Enter a new "scope" of deprecation
2697/// warnings.
2698///
2699/// The state token we use is the start index of this scope
2700/// on the warning stack.
John McCallfaf5fb42010-08-26 23:41:50 +00002701Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall28a6aea2009-11-04 02:18:39 +00002702 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002703 return (ParsingDeclStackState) DelayedDiagnostics.size();
2704}
2705
John McCall48871652010-08-21 09:40:31 +00002706void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall86121512010-01-27 03:50:35 +00002707 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2708 ParsingDeclDepth--;
2709
2710 if (DelayedDiagnostics.empty())
2711 return;
2712
2713 unsigned SavedIndex = (unsigned) S;
2714 assert(SavedIndex <= DelayedDiagnostics.size() &&
2715 "saved index is out of bounds");
2716
John McCall1064d7e2010-03-16 05:22:47 +00002717 unsigned E = DelayedDiagnostics.size();
2718
John McCall86121512010-01-27 03:50:35 +00002719 // We only want to actually emit delayed diagnostics when we
2720 // successfully parsed a decl.
John McCall86121512010-01-27 03:50:35 +00002721 if (D) {
2722 // We really do want to start with 0 here. We get one push for a
2723 // decl spec and another for each declarator; in a decl group like:
2724 // deprecated_typedef foo, *bar, baz();
2725 // only the declarator pops will be passed decls. This is correct;
2726 // we really do need to consider delayed diagnostics from the decl spec
2727 // for each of the different declarations.
John McCall1064d7e2010-03-16 05:22:47 +00002728 for (unsigned I = 0; I != E; ++I) {
John McCall86121512010-01-27 03:50:35 +00002729 if (DelayedDiagnostics[I].Triggered)
2730 continue;
2731
2732 switch (DelayedDiagnostics[I].Kind) {
2733 case DelayedDiagnostic::Deprecation:
2734 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2735 break;
2736
2737 case DelayedDiagnostic::Access:
2738 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2739 break;
2740 }
2741 }
2742 }
2743
John McCall1064d7e2010-03-16 05:22:47 +00002744 // Destroy all the delayed diagnostics we're about to pop off.
2745 for (unsigned I = SavedIndex; I != E; ++I)
2746 DelayedDiagnostics[I].destroy();
2747
John McCall86121512010-01-27 03:50:35 +00002748 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002749}
2750
2751static bool isDeclDeprecated(Decl *D) {
2752 do {
2753 if (D->hasAttr<DeprecatedAttr>())
2754 return true;
2755 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2756 return false;
2757}
2758
John McCallb45a1e72010-08-26 02:13:20 +00002759void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00002760 Decl *Ctx) {
2761 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002762 return;
2763
John McCall86121512010-01-27 03:50:35 +00002764 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002765 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00002766 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002767 << DD.getDeprecationDecl()->getDeclName()
2768 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002769 else
2770 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002771 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002772}
2773
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002774void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian551063102010-10-06 21:18:44 +00002775 SourceLocation Loc) {
John McCall28a6aea2009-11-04 02:18:39 +00002776 // Delay if we're currently parsing a declaration.
2777 if (ParsingDeclDepth) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00002778 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2779 Message));
John McCall28a6aea2009-11-04 02:18:39 +00002780 return;
2781 }
2782
2783 // Otherwise, don't warn if our current context is deprecated.
2784 if (isDeclDeprecated(cast<Decl>(CurContext)))
2785 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002786 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00002787 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2788 << Message;
2789 else
2790 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002791}