blob: ee03d1f7f18b9fee2077b1ca81fa82aa888b6668 [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);
748 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
749}
750
751static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
752 assert(Attr.isInvalid() == false);
753 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
754}
755
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000756static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +0000757 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
758 assert(Attr.isInvalid() == false);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000759 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000760}
761
762static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
763 Sema &S) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000764
765 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
766 // because 'analyzer_noreturn' does not impact the type.
767
768 if (Attr.getNumArgs() != 0) {
769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
770 return;
771 }
772
773 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
774 ValueDecl *VD = dyn_cast<ValueDecl>(d);
775 if (VD == 0 || (!VD->getType()->isBlockPointerType()
776 && !VD->getType()->isFunctionPointerType())) {
777 S.Diag(Attr.getLoc(),
778 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
779 : diag::warn_attribute_wrong_decl_type)
780 << Attr.getName() << 0 /*function*/;
781 return;
782 }
783 }
784
785 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000786}
787
John Thompsoncdb847ba2010-08-09 21:53:52 +0000788// PS3 PPU-specific.
789static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
790 Sema &S) {
791/*
792 Returning a Vector Class in Registers
793
Eric Christopherbc638a82010-12-01 22:13:54 +0000794 According to the PPU ABI specifications, a class with a single member of
795 vector type is returned in memory when used as the return value of a function.
796 This results in inefficient code when implementing vector classes. To return
797 the value in a single vector register, add the vecreturn attribute to the
798 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +0000799
800 Example:
801
802 struct Vector
803 {
804 __vector float xyzw;
805 } __attribute__((vecreturn));
806
807 Vector Add(Vector lhs, Vector rhs)
808 {
809 Vector result;
810 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
811 return result; // This will be returned in a register
812 }
813*/
John Thompson9a587aaa2010-09-18 01:12:07 +0000814 if (!isa<RecordDecl>(d)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000815 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
816 << Attr.getName() << 9 /*class*/;
817 return;
818 }
819
820 if (d->getAttr<VecReturnAttr>()) {
821 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
822 return;
823 }
824
John Thompson9a587aaa2010-09-18 01:12:07 +0000825 RecordDecl *record = cast<RecordDecl>(d);
826 int count = 0;
827
828 if (!isa<CXXRecordDecl>(record)) {
829 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
830 return;
831 }
832
833 if (!cast<CXXRecordDecl>(record)->isPOD()) {
834 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
835 return;
836 }
837
Eric Christopherbc638a82010-12-01 22:13:54 +0000838 for (RecordDecl::field_iterator iter = record->field_begin();
839 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +0000840 if ((count == 1) || !iter->getType()->isVectorType()) {
841 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
842 return;
843 }
844 count++;
845 }
846
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000847 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000848}
849
Alexis Hunt96d5c762009-11-21 08:43:09 +0000850static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
851 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
852 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCallab26cfa2010-02-05 21:31:56 +0000853 << Attr.getName() << 8 /*function, method, or parameter*/;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000854 return;
855 }
856 // FIXME: Actually store the attribute on the declaration
857}
858
Ted Kremenek39c59a82008-07-25 04:39:19 +0000859static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
860 // check the attribute arguments.
861 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000862 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000863 return;
864 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000865
John McCallcef15822010-03-31 02:47:45 +0000866 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
867 !isa<TypeDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000868 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000869 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000870 return;
871 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000872
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000873 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000874}
875
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000876static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
877 // check the attribute arguments.
878 if (Attr.getNumArgs() != 0) {
879 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
880 return;
881 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000882
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000883 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000884 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
886 return;
887 }
888 } else if (!isFunctionOrMethod(d)) {
889 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000890 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000891 return;
892 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000893
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000894 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000895}
896
Daniel Dunbar032db472008-07-31 22:40:48 +0000897static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
898 // check the attribute arguments.
899 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000900 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
901 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000902 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000903 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000904
905 int priority = 65535; // FIXME: Do not hardcode such constants.
906 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000907 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000908 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000909 if (E->isTypeDependent() || E->isValueDependent() ||
910 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000911 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000912 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000913 return;
914 }
915 priority = Idx.getZExtValue();
916 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000917
Chris Lattner4225e232009-04-14 17:02:11 +0000918 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000919 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000920 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000921 return;
922 }
923
Eric Christopherbc638a82010-12-01 22:13:54 +0000924 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
925 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000926}
927
928static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
929 // check the attribute arguments.
930 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000931 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
932 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000933 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000934 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000935
936 int priority = 65535; // FIXME: Do not hardcode such constants.
937 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000938 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000939 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000940 if (E->isTypeDependent() || E->isValueDependent() ||
941 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000942 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000943 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000944 return;
945 }
946 priority = Idx.getZExtValue();
947 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000948
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000949 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000950 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000951 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000952 return;
953 }
954
Eric Christopherbc638a82010-12-01 22:13:54 +0000955 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
956 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000957}
958
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000959static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000960 // check the attribute arguments.
Fariborz Jahanian551063102010-10-06 21:18:44 +0000961 int noArgs = Attr.getNumArgs();
962 if (noArgs > 1) {
963 S.Diag(Attr.getLoc(),
964 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000965 return;
966 }
Fariborz Jahanian551063102010-10-06 21:18:44 +0000967 // Handle the case where deprecated attribute has a text message.
968 StringLiteral *SE;
969 if (noArgs == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000970 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanian551063102010-10-06 21:18:44 +0000971 SE = dyn_cast<StringLiteral>(ArgExpr);
972 if (!SE) {
973 S.Diag(ArgExpr->getLocStart(),
974 diag::err_attribute_not_string) << "deprecated";
975 return;
976 }
977 }
978 else
979 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000980
Fariborz Jahanian551063102010-10-06 21:18:44 +0000981 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
982 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000983}
984
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000985static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
986 // check the attribute arguments.
Fariborz Jahanianc74073c2010-10-06 23:12:32 +0000987 int noArgs = Attr.getNumArgs();
988 if (noArgs > 1) {
Eric Christopherbc638a82010-12-01 22:13:54 +0000989 S.Diag(Attr.getLoc(),
990 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000991 return;
992 }
Fariborz Jahanianc74073c2010-10-06 23:12:32 +0000993 // Handle the case where unavailable attribute has a text message.
994 StringLiteral *SE;
995 if (noArgs == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000996 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc74073c2010-10-06 23:12:32 +0000997 SE = dyn_cast<StringLiteral>(ArgExpr);
998 if (!SE) {
999 S.Diag(ArgExpr->getLocStart(),
1000 diag::err_attribute_not_string) << "unavailable";
1001 return;
1002 }
1003 }
1004 else
1005 SE = StringLiteral::CreateEmpty(S.Context, 1);
1006 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
1007 SE->getString()));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001008}
1009
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001010static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001011 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001012 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001013 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001014 return;
1015 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001016
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001017 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001018 Arg = Arg->IgnoreParenCasts();
1019 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001020
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001021 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001022 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001023 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001024 return;
1025 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001026
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001027 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001028 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001029
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001030 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001031 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001032 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001033 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001034 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001035 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001036 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001037 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001038 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001039 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001040 return;
1041 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001042
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001043 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001044}
1045
Chris Lattner677a3582009-02-14 08:09:34 +00001046static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1047 Sema &S) {
1048 if (Attr.getNumArgs() != 0) {
1049 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1050 return;
1051 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001052
Chris Lattner677a3582009-02-14 08:09:34 +00001053 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1054 if (OCI == 0) {
1055 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1056 return;
1057 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001058
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001059 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001060}
1061
1062static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001063 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001064 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001065 return;
1066 }
Chris Lattner677a3582009-02-14 08:09:34 +00001067 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001068 QualType T = TD->getUnderlyingType();
1069 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001070 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001071 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1072 return;
1073 }
1074 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001075 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001076}
1077
Mike Stumpd3bb5572009-07-24 19:02:52 +00001078static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001079HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1080 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001081 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001082 return;
1083 }
1084
1085 if (!isa<FunctionDecl>(D)) {
1086 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1087 return;
1088 }
1089
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001090 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001091}
1092
Steve Naroff3405a732008-09-18 16:44:58 +00001093static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001094 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001095 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001096 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001097 return;
1098 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001099
Steve Naroff3405a732008-09-18 16:44:58 +00001100 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001102 return;
1103 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001104
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001105 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001106 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001107 type = BlocksAttr::ByRef;
1108 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001109 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001110 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001111 return;
1112 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001113
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001114 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001115}
1116
Anders Carlssonc181b012008-10-05 18:05:59 +00001117static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1118 // check the attribute arguments.
1119 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +00001120 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1121 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +00001122 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001123 }
1124
Anders Carlssonc181b012008-10-05 18:05:59 +00001125 int sentinel = 0;
1126 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001127 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001128 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001129 if (E->isTypeDependent() || E->isValueDependent() ||
1130 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001132 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001133 return;
1134 }
1135 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001136
Anders Carlssonc181b012008-10-05 18:05:59 +00001137 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001138 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1139 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001140 return;
1141 }
1142 }
1143
1144 int nullPos = 0;
1145 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001146 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001147 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001148 if (E->isTypeDependent() || E->isValueDependent() ||
1149 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001150 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001151 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001152 return;
1153 }
1154 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155
Anders Carlssonc181b012008-10-05 18:05:59 +00001156 if (nullPos > 1 || nullPos < 0) {
1157 // FIXME: This error message could be improved, it would be nice
1158 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001159 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1160 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001161 return;
1162 }
1163 }
1164
1165 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001166 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001167 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001168
Chris Lattner9363e312009-03-17 23:03:47 +00001169 if (isa<FunctionNoProtoType>(FT)) {
1170 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1171 return;
1172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001173
Chris Lattner9363e312009-03-17 23:03:47 +00001174 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001175 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001176 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001177 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001178 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1179 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001180 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001181 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001182 }
1183 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001184 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1185 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001186 ;
1187 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001188 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001189 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001190 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherbc638a82010-12-01 22:13:54 +00001191 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001192 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001193 int m = Ty->isFunctionPointerType() ? 0 : 1;
1194 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001195 return;
1196 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001197 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001198 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001199 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001200 return;
1201 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001202 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001203 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +00001204 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +00001205 return;
1206 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001207 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1208 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001209}
1210
Chris Lattner237f2752009-02-14 07:37:35 +00001211static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1212 // check the attribute arguments.
1213 if (Attr.getNumArgs() != 0) {
1214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1215 return;
1216 }
1217
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001218 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001219 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001220 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +00001221 return;
1222 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001223
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001224 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1225 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1226 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001227 return;
1228 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001229 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1230 if (MD->getResultType()->isVoidType()) {
1231 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1232 << Attr.getName() << 1;
1233 return;
1234 }
1235
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001236 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001237}
1238
1239static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001240 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001241 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001242 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001243 return;
1244 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001245
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001246 /* weak only applies to non-static declarations */
Rafael Espindolac18086a2010-02-23 22:00:30 +00001247 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001248 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1249 dyn_cast<NamedDecl>(D)->getNameAsString();
1250 return;
1251 }
1252
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001253 // TODO: could also be applied to methods?
1254 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1255 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001256 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001257 return;
1258 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001259
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001260 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001261}
1262
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001263static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1264 // check the attribute arguments.
1265 if (Attr.getNumArgs() != 0) {
1266 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1267 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001268 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001269
1270 // weak_import only applies to variable & function declarations.
1271 bool isDef = false;
1272 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1273 isDef = (!VD->hasExternalStorage() || VD->getInit());
1274 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001275 isDef = FD->hasBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +00001276 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1277 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +00001278 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +00001279 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001280 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001281 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001282 !isa<ObjCInterfaceDecl>(D))
1283 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001284 << Attr.getName() << 2 /*variable and function*/;
1285 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001286 }
1287
1288 // Merge should handle any subsequent violations.
1289 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001290 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001291 diag::warn_attribute_weak_import_invalid_on_definition)
1292 << "weak_import" << 2 /*variable and function*/;
1293 return;
1294 }
1295
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001296 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001297}
1298
Nate Begemanf2758702009-06-26 06:32:41 +00001299static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1300 Sema &S) {
1301 // Attribute has 3 arguments.
1302 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001304 return;
1305 }
1306
1307 unsigned WGSize[3];
1308 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001309 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001310 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001311 if (E->isTypeDependent() || E->isValueDependent() ||
1312 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001313 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1314 << "reqd_work_group_size" << E->getSourceRange();
1315 return;
1316 }
1317 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1318 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001319 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1320 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001321 WGSize[2]));
1322}
1323
Chris Lattner237f2752009-02-14 07:37:35 +00001324static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001325 // Attribute has no arguments.
1326 if (Attr.getNumArgs() != 1) {
1327 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1328 return;
1329 }
1330
1331 // Make sure that there is a string literal as the sections's single
1332 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001333 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001334 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001335 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001336 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001337 return;
1338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
Chris Lattner30ba6742009-08-10 19:03:04 +00001340 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001341 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001342 if (!Error.empty()) {
1343 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1344 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001345 return;
1346 }
Mike Stump11289f42009-09-09 15:08:12 +00001347
Chris Lattner20aee9b2010-01-12 20:58:53 +00001348 // This attribute cannot be applied to local variables.
1349 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1350 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1351 return;
1352 }
1353
Eric Christopherbc638a82010-12-01 22:13:54 +00001354 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1355 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001356}
1357
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001358
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001359static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001360 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001361 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001362 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001363 return;
1364 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001365
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001366 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001367}
1368
Anders Carlssonb8316282008-10-05 23:32:53 +00001369static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1370 // check the attribute arguments.
1371 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001373 return;
1374 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001375
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001376 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001377}
1378
1379static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1380 // check the attribute arguments.
1381 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001382 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001383 return;
1384 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001385
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001386 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001387}
1388
Anders Carlssond277d792009-01-31 01:16:18 +00001389static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001390 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001391 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1392 return;
1393 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001394
Anders Carlssond277d792009-01-31 01:16:18 +00001395 if (Attr.getNumArgs() != 0) {
1396 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1397 return;
1398 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001399
Anders Carlssond277d792009-01-31 01:16:18 +00001400 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001401
Anders Carlssond277d792009-01-31 01:16:18 +00001402 if (!VD || !VD->hasLocalStorage()) {
1403 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1404 return;
1405 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001406
Anders Carlssond277d792009-01-31 01:16:18 +00001407 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001408 // FIXME: Lookup probably isn't looking in the right place
1409 // FIXME: The lookup source location should be in the attribute, not the
1410 // start of the attribute.
John McCall9f3059a2009-10-09 21:13:30 +00001411 NamedDecl *CleanupDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001412 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCall9f3059a2009-10-09 21:13:30 +00001413 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001414 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001415 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001416 Attr.getParameterName();
1417 return;
1418 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001419
Anders Carlssond277d792009-01-31 01:16:18 +00001420 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1421 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001422 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001423 Attr.getParameterName();
1424 return;
1425 }
1426
Anders Carlssond277d792009-01-31 01:16:18 +00001427 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001428 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001429 Attr.getParameterName();
1430 return;
1431 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001432
Anders Carlsson723f55d2009-02-07 23:16:50 +00001433 // We're currently more strict than GCC about what function types we accept.
1434 // If this ever proves to be a problem it should be easy to fix.
1435 QualType Ty = S.Context.getPointerType(VD->getType());
1436 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall29600e12010-11-16 02:32:08 +00001437 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001438 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001439 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1440 Attr.getParameterName() << ParamTy << Ty;
1441 return;
1442 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001443
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001444 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001445}
1446
Mike Stumpd3bb5572009-07-24 19:02:52 +00001447/// Handle __attribute__((format_arg((idx)))) attribute based on
1448/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1449static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001450 if (Attr.getNumArgs() != 1) {
1451 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1452 return;
1453 }
1454 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1455 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1456 << Attr.getName() << 0 /*function*/;
1457 return;
1458 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001459
1460 // In C++ the implicit 'this' function parameter also counts, and they are
1461 // counted from one.
1462 bool HasImplicitThisParam = isInstanceMethod(d);
1463 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001464 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001465
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001466 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001467 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001468 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001469 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1470 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001471 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1472 << "format" << 2 << IdxExpr->getSourceRange();
1473 return;
1474 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001475
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001476 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1477 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1478 << "format" << 2 << IdxExpr->getSourceRange();
1479 return;
1480 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001481
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001482 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001483
Chandler Carruth743682b2010-11-16 08:35:43 +00001484 if (HasImplicitThisParam) {
1485 if (ArgIdx == 0) {
1486 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1487 << "format_arg" << IdxExpr->getSourceRange();
1488 return;
1489 }
1490 ArgIdx--;
1491 }
1492
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001493 // make sure the format string is really a string
1494 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001495
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001496 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1497 if (not_nsstring_type &&
1498 !isCFStringType(Ty, S.Context) &&
1499 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001500 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001501 // FIXME: Should highlight the actual expression that has the wrong type.
1502 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001503 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001504 << IdxExpr->getSourceRange();
1505 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001506 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001507 Ty = getFunctionOrMethodResultType(d);
1508 if (!isNSStringType(Ty, S.Context) &&
1509 !isCFStringType(Ty, S.Context) &&
1510 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001511 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001512 // FIXME: Should highlight the actual expression that has the wrong type.
1513 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001514 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001515 << IdxExpr->getSourceRange();
1516 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001517 }
1518
Chandler Carruth743682b2010-11-16 08:35:43 +00001519 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1520 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001521}
1522
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001523enum FormatAttrKind {
1524 CFStringFormat,
1525 NSStringFormat,
1526 StrftimeFormat,
1527 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001528 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001529 InvalidFormat
1530};
1531
1532/// getFormatAttrKind - Map from format attribute names to supported format
1533/// types.
1534static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1535 // Check for formats that get handled specially.
1536 if (Format == "NSString")
1537 return NSStringFormat;
1538 if (Format == "CFString")
1539 return CFStringFormat;
1540 if (Format == "strftime")
1541 return StrftimeFormat;
1542
1543 // Otherwise, check for supported formats.
1544 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1545 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1546 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1547 Format == "zcmn_err")
1548 return SupportedFormat;
1549
Duncan Sandsde4fe352010-03-23 14:44:19 +00001550 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1551 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001552 return IgnoredFormat;
1553
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001554 return InvalidFormat;
1555}
1556
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001557/// Handle __attribute__((init_priority(priority))) attributes based on
1558/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1559static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1560 Sema &S) {
1561 if (!S.getLangOptions().CPlusPlus) {
1562 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1563 return;
1564 }
1565
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001566 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1567 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1568 Attr.setInvalid();
1569 return;
1570 }
1571 QualType T = dyn_cast<VarDecl>(d)->getType();
1572 if (S.Context.getAsArrayType(T))
1573 T = S.Context.getBaseElementType(T);
1574 if (!T->getAs<RecordType>()) {
1575 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1576 Attr.setInvalid();
1577 return;
1578 }
1579
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001580 if (Attr.getNumArgs() != 1) {
1581 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1582 Attr.setInvalid();
1583 return;
1584 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001585 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001586
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001587 llvm::APSInt priority(32);
1588 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1589 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1591 << "init_priority" << priorityExpr->getSourceRange();
1592 Attr.setInvalid();
1593 return;
1594 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001595 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001596 if (prioritynum < 101 || prioritynum > 65535) {
1597 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1598 << priorityExpr->getSourceRange();
1599 Attr.setInvalid();
1600 return;
1601 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001602 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1603 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001604}
1605
Mike Stumpd3bb5572009-07-24 19:02:52 +00001606/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1607/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001608static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001609
Chris Lattner4a927cb2008-06-28 23:36:30 +00001610 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001611 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001612 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001613 return;
1614 }
1615
Chris Lattner4a927cb2008-06-28 23:36:30 +00001616 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001617 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001618 return;
1619 }
1620
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001621 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001622 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001623 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001624 return;
1625 }
1626
Chandler Carruth743682b2010-11-16 08:35:43 +00001627 // In C++ the implicit 'this' function parameter also counts, and they are
1628 // counted from one.
1629 bool HasImplicitThisParam = isInstanceMethod(d);
1630 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001631 unsigned FirstIdx = 1;
1632
Daniel Dunbar07d07852009-10-18 21:17:35 +00001633 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001634
1635 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001636 if (Format.startswith("__") && Format.endswith("__"))
1637 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001638
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001639 // Check for supported formats.
1640 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001641
1642 if (Kind == IgnoredFormat)
1643 return;
1644
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001645 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001646 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001647 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001648 return;
1649 }
1650
1651 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001652 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001653 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001654 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1655 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001656 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001657 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001658 return;
1659 }
1660
1661 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001662 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001663 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001664 return;
1665 }
1666
1667 // FIXME: Do we need to bounds check?
1668 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001669
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001670 if (HasImplicitThisParam) {
1671 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001672 S.Diag(Attr.getLoc(),
1673 diag::err_format_attribute_implicit_this_format_string)
1674 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001675 return;
1676 }
1677 ArgIdx--;
1678 }
Mike Stump11289f42009-09-09 15:08:12 +00001679
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001680 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001681 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001682
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001683 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001684 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001685 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1686 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001687 return;
1688 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001689 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001690 // FIXME: do we need to check if the type is NSString*? What are the
1691 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001692 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001693 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001694 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1695 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001696 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001697 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001698 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001699 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001700 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001701 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1702 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001703 return;
1704 }
1705
1706 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001707 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001708 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001709 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1710 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001711 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001712 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001713 return;
1714 }
1715
1716 // check if the function is variadic if the 3rd argument non-zero
1717 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001718 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001719 ++NumArgs; // +1 for ...
1720 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001721 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001722 return;
1723 }
1724 }
1725
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001726 // strftime requires FirstArg to be 0 because it doesn't read from any
1727 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001728 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001729 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001730 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1731 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001732 return;
1733 }
1734 // if 0 it disables parameter checking (to use with e.g. va_list)
1735 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001736 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001737 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001738 return;
1739 }
1740
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001741 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1742 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001743 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001744}
1745
Chris Lattnera663a0a2008-06-29 00:28:59 +00001746static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1747 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001748 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001749 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001751 return;
1752 }
1753
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001754 // Try to find the underlying union declaration.
1755 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001756 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001757 if (TD && TD->getUnderlyingType()->isUnionType())
1758 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1759 else
1760 RD = dyn_cast<RecordDecl>(d);
1761
1762 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001763 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001764 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001765 return;
1766 }
1767
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001768 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001769 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001770 diag::warn_transparent_union_attribute_not_definition);
1771 return;
1772 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001773
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001774 RecordDecl::field_iterator Field = RD->field_begin(),
1775 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001776 if (Field == FieldEnd) {
1777 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1778 return;
1779 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001780
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001781 FieldDecl *FirstField = *Field;
1782 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001783 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001784 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001785 diag::warn_transparent_union_attribute_floating)
1786 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001787 return;
1788 }
1789
1790 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1791 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1792 for (; Field != FieldEnd; ++Field) {
1793 QualType FieldType = Field->getType();
1794 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1795 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1796 // Warn if we drop the attribute.
1797 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001798 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001799 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001800 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001801 diag::warn_transparent_union_attribute_field_size_align)
1802 << isSize << Field->getDeclName() << FieldBits;
1803 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001804 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001805 diag::note_transparent_union_first_field_size_align)
1806 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001807 return;
1808 }
1809 }
1810
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001811 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001812}
1813
Chris Lattnera663a0a2008-06-29 00:28:59 +00001814static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001815 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001816 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001818 return;
1819 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001820 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001821 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001822
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001823 // Make sure that there is a string literal as the annotation's single
1824 // argument.
1825 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001826 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001827 return;
1828 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001829 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1830 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001831}
1832
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001833static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001834 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001835 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001836 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001837 return;
1838 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001839
1840 //FIXME: The C++0x version of this attribute has more limited applicabilty
1841 // than GNU's, and should error out when it is used to specify a
1842 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001843
Chris Lattner4a927cb2008-06-28 23:36:30 +00001844 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001845 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001846 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001847 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001848
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001849 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001850}
1851
1852void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1853 if (E->isTypeDependent() || E->isValueDependent()) {
1854 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001855 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001856 return;
1857 }
1858
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001859 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00001860 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001861 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1862 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1863 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001864 return;
1865 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001866 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001867 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1868 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001869 return;
1870 }
1871
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001872 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1873}
1874
1875void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1876 // FIXME: Cache the number on the Attr object if non-dependent?
1877 // FIXME: Perform checking of type validity
1878 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1879 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001880}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001881
Mike Stumpd3bb5572009-07-24 19:02:52 +00001882/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1883/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001884///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001885/// Despite what would be logical, the mode attribute is a decl attribute, not a
1886/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1887/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001888static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001889 // This attribute isn't documented, but glibc uses it. It changes
1890 // the width of an int or unsigned int to the specified size.
1891
1892 // Check that there aren't any arguments
1893 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001894 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001895 return;
1896 }
1897
1898 IdentifierInfo *Name = Attr.getParameterName();
1899 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001900 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001901 return;
1902 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001903
Daniel Dunbar07d07852009-10-18 21:17:35 +00001904 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001905
1906 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001907 if (Str.startswith("__") && Str.endswith("__"))
1908 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001909
1910 unsigned DestWidth = 0;
1911 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001912 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001913 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001914 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001915 switch (Str[0]) {
1916 case 'Q': DestWidth = 8; break;
1917 case 'H': DestWidth = 16; break;
1918 case 'S': DestWidth = 32; break;
1919 case 'D': DestWidth = 64; break;
1920 case 'X': DestWidth = 96; break;
1921 case 'T': DestWidth = 128; break;
1922 }
1923 if (Str[1] == 'F') {
1924 IntegerMode = false;
1925 } else if (Str[1] == 'C') {
1926 IntegerMode = false;
1927 ComplexMode = true;
1928 } else if (Str[1] != 'I') {
1929 DestWidth = 0;
1930 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001931 break;
1932 case 4:
1933 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1934 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001935 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001936 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001937 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001938 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001939 break;
1940 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001941 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001942 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001943 break;
1944 }
1945
1946 QualType OldTy;
1947 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1948 OldTy = TD->getUnderlyingType();
1949 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1950 OldTy = VD->getType();
1951 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001952 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1953 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001954 return;
1955 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001956
John McCall9dd450b2009-09-21 23:43:11 +00001957 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001958 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1959 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00001960 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001961 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1962 } else if (ComplexMode) {
1963 if (!OldTy->isComplexType())
1964 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1965 } else {
1966 if (!OldTy->isFloatingType())
1967 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1968 }
1969
Mike Stump87c57ac2009-05-16 07:39:55 +00001970 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1971 // and friends, at least with glibc.
1972 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1973 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001974 // FIXME: Make sure floating-point mappings are accurate
1975 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001976 QualType NewTy;
1977 switch (DestWidth) {
1978 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001979 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001980 return;
1981 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001982 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001983 return;
1984 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001985 if (!IntegerMode) {
1986 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1987 return;
1988 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001989 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001990 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001991 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001992 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001993 break;
1994 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001995 if (!IntegerMode) {
1996 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1997 return;
1998 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001999 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002000 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002001 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002002 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002003 break;
2004 case 32:
2005 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002006 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002007 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002008 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002009 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002010 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002011 break;
2012 case 64:
2013 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002014 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002015 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002016 if (S.Context.Target.getLongWidth() == 64)
2017 NewTy = S.Context.LongTy;
2018 else
2019 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002020 else
Chandler Carruth72343702010-01-26 06:39:24 +00002021 if (S.Context.Target.getLongWidth() == 64)
2022 NewTy = S.Context.UnsignedLongTy;
2023 else
2024 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002025 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002026 case 96:
2027 NewTy = S.Context.LongDoubleTy;
2028 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002029 case 128:
2030 if (!IntegerMode) {
2031 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2032 return;
2033 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002034 if (OldTy->isSignedIntegerType())
2035 NewTy = S.Context.Int128Ty;
2036 else
2037 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002038 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002039 }
2040
Eli Friedman4735374e2009-03-03 06:41:03 +00002041 if (ComplexMode) {
2042 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002043 }
2044
2045 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00002046 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2047 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002048 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002049 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002050 cast<ValueDecl>(D)->setType(NewTy);
2051}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002052
Mike Stump3722f582009-08-26 22:31:08 +00002053static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002054 // check the attribute arguments.
2055 if (Attr.getNumArgs() > 0) {
2056 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2057 return;
2058 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002059
Anders Carlsson88097122009-02-19 19:16:48 +00002060 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002061 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002062 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00002063 return;
2064 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002065
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002066 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002067}
2068
Mike Stump3722f582009-08-26 22:31:08 +00002069static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00002070 // check the attribute arguments.
2071 if (Attr.getNumArgs() != 0) {
2072 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2073 return;
2074 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002075
Chris Lattner4225e232009-04-14 17:02:11 +00002076 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002077 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002078 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00002079 return;
2080 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002081
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002082 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002083}
2084
Chris Lattner3c77a352010-06-22 00:03:40 +00002085static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2086 Sema &S) {
2087 // check the attribute arguments.
2088 if (Attr.getNumArgs() != 0) {
2089 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2090 return;
2091 }
2092
2093 if (!isa<FunctionDecl>(d)) {
2094 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2095 << Attr.getName() << 0 /*function*/;
2096 return;
2097 }
2098
Eric Christopherbc638a82010-12-01 22:13:54 +00002099 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2100 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002101}
2102
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002103static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2104 if (S.LangOpts.CUDA) {
2105 // check the attribute arguments.
2106 if (Attr.getNumArgs() != 0) {
2107 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2108 return;
2109 }
2110
2111 if (!isa<VarDecl>(d)) {
2112 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2113 << Attr.getName() << 12 /*variable*/;
2114 return;
2115 }
2116
2117 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2118 } else {
2119 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2120 }
2121}
2122
2123static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2124 if (S.LangOpts.CUDA) {
2125 // check the attribute arguments.
2126 if (Attr.getNumArgs() != 0) {
2127 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2128 return;
2129 }
2130
2131 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2132 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2133 << Attr.getName() << 2 /*variable and function*/;
2134 return;
2135 }
2136
2137 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2138 } else {
2139 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2140 }
2141}
2142
2143static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2144 if (S.LangOpts.CUDA) {
2145 // check the attribute arguments.
2146 if (Attr.getNumArgs() != 0) {
2147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2148 return;
2149 }
2150
2151 if (!isa<FunctionDecl>(d)) {
2152 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2153 << Attr.getName() << 0 /*function*/;
2154 return;
2155 }
2156
2157 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2158 } else {
2159 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2160 }
2161}
2162
2163static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2164 if (S.LangOpts.CUDA) {
2165 // check the attribute arguments.
2166 if (Attr.getNumArgs() != 0) {
2167 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2168 return;
2169 }
2170
2171 if (!isa<FunctionDecl>(d)) {
2172 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2173 << Attr.getName() << 0 /*function*/;
2174 return;
2175 }
2176
2177 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2178 } else {
2179 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2180 }
2181}
2182
2183static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2184 if (S.LangOpts.CUDA) {
2185 // check the attribute arguments.
2186 if (Attr.getNumArgs() != 0) {
2187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2188 return;
2189 }
2190
2191 if (!isa<VarDecl>(d)) {
2192 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2193 << Attr.getName() << 12 /*variable*/;
2194 return;
2195 }
2196
2197 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2198 } else {
2199 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2200 }
2201}
2202
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002203static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002204 // check the attribute arguments.
2205 if (Attr.getNumArgs() != 0) {
2206 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2207 return;
2208 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002209
Chris Lattner4225e232009-04-14 17:02:11 +00002210 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2211 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002212 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002213 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002214 return;
2215 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002216
Douglas Gregor35b57532009-10-27 21:01:01 +00002217 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002218 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002219 return;
2220 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002221
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002222 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002223}
2224
Abramo Bagnara50099372010-04-30 13:10:51 +00002225static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2226 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2227 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2228 assert(Attr.isInvalid() == false);
2229
2230 switch (Attr.getKind()) {
2231 case AttributeList::AT_fastcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002232 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002233 return;
2234 case AttributeList::AT_stdcall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002235 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002236 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002237 case AttributeList::AT_thiscall:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002238 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002239 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002240 case AttributeList::AT_cdecl:
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002241 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002242 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002243 case AttributeList::AT_pascal:
2244 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2245 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002246 default:
2247 llvm_unreachable("unexpected attribute kind");
2248 return;
2249 }
2250}
2251
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002252static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2253 // check the attribute arguments.
2254 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00002255 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002256 return;
2257 }
Eli Friedman7044b762009-03-27 21:06:47 +00002258
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002259 if (!isFunctionOrMethod(d)) {
2260 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00002261 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002262 return;
2263 }
Eli Friedman7044b762009-03-27 21:06:47 +00002264
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002265 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002266 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002267 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2268 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman7044b762009-03-27 21:06:47 +00002269 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2270 << "regparm" << NumParamsExpr->getSourceRange();
2271 return;
2272 }
2273
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002274 if (S.Context.Target.getRegParmMax() == 0) {
2275 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002276 << NumParamsExpr->getSourceRange();
2277 return;
2278 }
2279
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00002280 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00002281 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2282 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00002283 return;
2284 }
2285
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002286 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2287 NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002288}
2289
Alexis Hunt96d5c762009-11-21 08:43:09 +00002290static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2291 // check the attribute arguments.
2292 if (Attr.getNumArgs() != 0) {
2293 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2294 return;
2295 }
2296
2297 if (!isa<CXXRecordDecl>(d)
2298 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2299 S.Diag(Attr.getLoc(),
2300 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2301 : diag::warn_attribute_wrong_decl_type)
2302 << Attr.getName() << 7 /*virtual method or class*/;
2303 return;
2304 }
Alexis Hunt54a02542009-11-25 04:20:27 +00002305
2306 // FIXME: Conform to C++0x redeclaration rules.
2307
2308 if (d->getAttr<FinalAttr>()) {
2309 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2310 return;
2311 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002312
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002313 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002314}
2315
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002316//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00002317// C++0x member checking attributes
2318//===----------------------------------------------------------------------===//
2319
2320static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2321 if (Attr.getNumArgs() != 0) {
2322 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2323 return;
2324 }
2325
2326 if (!isa<CXXRecordDecl>(d)) {
2327 S.Diag(Attr.getLoc(),
2328 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2329 : diag::warn_attribute_wrong_decl_type)
2330 << Attr.getName() << 9 /*class*/;
2331 return;
2332 }
2333
2334 if (d->getAttr<BaseCheckAttr>()) {
2335 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2336 return;
2337 }
2338
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002339 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002340}
2341
2342static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2343 if (Attr.getNumArgs() != 0) {
2344 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2345 return;
2346 }
2347
2348 if (!isa<RecordDecl>(d->getDeclContext())) {
2349 // FIXME: It's not the type that's the problem
2350 S.Diag(Attr.getLoc(),
2351 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2352 : diag::warn_attribute_wrong_decl_type)
2353 << Attr.getName() << 11 /*member*/;
2354 return;
2355 }
2356
2357 // FIXME: Conform to C++0x redeclaration rules.
2358
2359 if (d->getAttr<HidingAttr>()) {
2360 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2361 return;
2362 }
2363
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002364 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002365}
2366
2367static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2368 if (Attr.getNumArgs() != 0) {
2369 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2370 return;
2371 }
2372
2373 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2374 // FIXME: It's not the type that's the problem
2375 S.Diag(Attr.getLoc(),
2376 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2377 : diag::warn_attribute_wrong_decl_type)
2378 << Attr.getName() << 10 /*virtual method*/;
2379 return;
2380 }
2381
2382 // FIXME: Conform to C++0x redeclaration rules.
2383
2384 if (d->getAttr<OverrideAttr>()) {
2385 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2386 return;
2387 }
2388
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002389 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Alexis Hunt54a02542009-11-25 04:20:27 +00002390}
2391
2392//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002393// Checker-specific attribute handlers.
2394//===----------------------------------------------------------------------===//
2395
2396static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2397 Sema &S) {
2398
Ted Kremenek3b204e42009-05-13 21:07:32 +00002399 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002400
Ted Kremenek3b204e42009-05-13 21:07:32 +00002401 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2402 RetTy = MD->getResultType();
2403 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2404 RetTy = FD->getResultType();
2405 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002406 SourceLocation L = Attr.getLoc();
2407 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2408 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002409 return;
2410 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002411
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002412 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00002413 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002414 SourceLocation L = Attr.getLoc();
2415 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2416 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00002417 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002418 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002419
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002420 switch (Attr.getKind()) {
2421 default:
2422 assert(0 && "invalid ownership attribute");
2423 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002424 case AttributeList::AT_cf_returns_not_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002425 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
2426 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002427 return;
2428 case AttributeList::AT_ns_returns_not_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002429 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
2430 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002431 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002432 case AttributeList::AT_cf_returns_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002433 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
2434 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002435 return;
2436 case AttributeList::AT_ns_returns_retained:
Eric Christopherbc638a82010-12-01 22:13:54 +00002437 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
2438 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002439 return;
2440 };
2441}
2442
Charles Davis163855f2010-02-16 18:27:26 +00002443static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2444 return Attr.getKind() == AttributeList::AT_dllimport ||
2445 Attr.getKind() == AttributeList::AT_dllexport;
2446}
2447
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002448//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002449// Top Level Sema Entry Points
2450//===----------------------------------------------------------------------===//
2451
Sebastian Redlfc24b632008-12-21 19:24:58 +00002452/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002453/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00002454/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2455/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00002456static void ProcessDeclAttribute(Scope *scope, Decl *D,
2457 const AttributeList &Attr, Sema &S) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002458 if (Attr.isInvalid())
2459 return;
2460
Charles Davis163855f2010-02-16 18:27:26 +00002461 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2462 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman53339e02009-06-08 23:27:34 +00002463 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002464 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002465 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002466 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2467 case AttributeList::AT_IBOutletCollection:
2468 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002469 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002470 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002471 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002472 case AttributeList::AT_neon_vector_type:
2473 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002474 // Ignore these, these are type attributes, handled by
2475 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002476 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002477 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2478 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002479 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002480 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002481 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002482 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002483 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2484 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002485 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002486 HandleDependencyAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002487 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002488 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002489 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2490 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2491 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002492 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002493 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002494 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002495 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002496 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2497 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2498 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002499 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002500 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2501 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002502 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002503 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2504 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00002505 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002506 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002507 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002508 case AttributeList::AT_ownership_returns:
2509 case AttributeList::AT_ownership_takes:
2510 case AttributeList::AT_ownership_holds:
2511 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00002512 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002513 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2514 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2515 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002516 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002517 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002518
2519 // Checker-specific.
Ted Kremenekd9c66632010-02-18 00:05:45 +00002520 case AttributeList::AT_ns_returns_not_retained:
2521 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002522 case AttributeList::AT_ns_returns_retained:
2523 case AttributeList::AT_cf_returns_retained:
2524 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2525
Nate Begemanf2758702009-06-26 06:32:41 +00002526 case AttributeList::AT_reqd_wg_size:
2527 HandleReqdWorkGroupSize(D, Attr, S); break;
2528
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002529 case AttributeList::AT_init_priority:
2530 HandleInitPriorityAttr(D, Attr, S); break;
2531
Alexis Hunt54a02542009-11-25 04:20:27 +00002532 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2533 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002534 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2535 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2536 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002537 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002538 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2539 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002540 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002541 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002542 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002543 case AttributeList::AT_transparent_union:
2544 HandleTransparentUnionAttr(D, Attr, S);
2545 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002546 case AttributeList::AT_objc_exception:
2547 HandleObjCExceptionAttr(D, Attr, S);
2548 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00002549 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002550 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2551 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2552 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2553 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2554 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2555 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2556 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2557 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2558 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002559 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002560 // Just ignore
2561 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002562 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2563 HandleNoInstrumentFunctionAttr(D, Attr, S);
2564 break;
John McCallab26cfa2010-02-05 21:31:56 +00002565 case AttributeList::AT_stdcall:
2566 case AttributeList::AT_cdecl:
2567 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002568 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002569 case AttributeList::AT_pascal:
Abramo Bagnara50099372010-04-30 13:10:51 +00002570 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002571 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002572 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002573 // Ask target about the attribute.
2574 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2575 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002576 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2577 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002578 break;
2579 }
2580}
2581
2582/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2583/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00002584void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
2585 const AttributeList *AttrList) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002586 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2587 ProcessDeclAttribute(S, D, *l, *this);
2588 }
2589
2590 // GCC accepts
2591 // static int a9 __attribute__((weakref));
2592 // but that looks really pointless. We reject it.
2593 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2594 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002595 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002596 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002597 }
2598}
2599
Ryan Flynn7d470f32009-07-30 03:15:39 +00002600/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2601/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002602NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002603 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002604 NamedDecl *NewD = 0;
2605 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2606 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2607 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002608 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002609 if (FD->getQualifier()) {
2610 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2611 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2612 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002613 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2614 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2615 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002616 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002617 VD->getStorageClass(),
2618 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002619 if (VD->getQualifier()) {
2620 VarDecl *NewVD = cast<VarDecl>(NewD);
2621 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2622 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002623 }
2624 return NewD;
2625}
2626
2627/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2628/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002629void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002630 if (W.getUsed()) return; // only do this once
2631 W.setUsed(true);
2632 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2633 IdentifierInfo *NDId = ND->getIdentifier();
2634 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002635 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2636 NDId->getName()));
2637 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00002638 WeakTopLevelDecl.push_back(NewD);
2639 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2640 // to insert Decl at TU scope, sorry.
2641 DeclContext *SavedContext = CurContext;
2642 CurContext = Context.getTranslationUnitDecl();
2643 PushOnScopeChains(NewD, S);
2644 CurContext = SavedContext;
2645 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002646 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002647 }
2648}
2649
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002650/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2651/// it, apply them to D. This is a bit tricky because PD can have attributes
2652/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002653void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
John McCall6fe02402010-10-27 00:59:00 +00002654 // It's valid to "forward-declare" #pragma weak, in which case we
2655 // have to do this.
2656 if (!WeakUndeclaredIdentifiers.empty()) {
2657 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2658 if (IdentifierInfo *Id = ND->getIdentifier()) {
2659 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2660 = WeakUndeclaredIdentifiers.find(Id);
2661 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2662 WeakInfo W = I->second;
2663 DeclApplyPragmaWeak(S, ND, W);
2664 WeakUndeclaredIdentifiers[Id] = W;
2665 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002666 }
2667 }
2668 }
2669
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002670 // Apply decl attributes from the DeclSpec if present.
2671 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002672 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002673
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002674 // Walk the declarator structure, applying decl attributes that were in a type
2675 // position to the decl itself. This handles cases like:
2676 // int *__attr__(x)** D;
2677 // when X is a decl attribute.
2678 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2679 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002680 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002681
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002682 // Finally, apply any attributes on the decl itself.
2683 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002684 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002685}
John McCall28a6aea2009-11-04 02:18:39 +00002686
2687/// PushParsingDeclaration - Enter a new "scope" of deprecation
2688/// warnings.
2689///
2690/// The state token we use is the start index of this scope
2691/// on the warning stack.
John McCallfaf5fb42010-08-26 23:41:50 +00002692Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall28a6aea2009-11-04 02:18:39 +00002693 ParsingDeclDepth++;
John McCall86121512010-01-27 03:50:35 +00002694 return (ParsingDeclStackState) DelayedDiagnostics.size();
2695}
2696
John McCall48871652010-08-21 09:40:31 +00002697void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall86121512010-01-27 03:50:35 +00002698 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2699 ParsingDeclDepth--;
2700
2701 if (DelayedDiagnostics.empty())
2702 return;
2703
2704 unsigned SavedIndex = (unsigned) S;
2705 assert(SavedIndex <= DelayedDiagnostics.size() &&
2706 "saved index is out of bounds");
2707
John McCall1064d7e2010-03-16 05:22:47 +00002708 unsigned E = DelayedDiagnostics.size();
2709
John McCall86121512010-01-27 03:50:35 +00002710 // We only want to actually emit delayed diagnostics when we
2711 // successfully parsed a decl.
John McCall86121512010-01-27 03:50:35 +00002712 if (D) {
2713 // We really do want to start with 0 here. We get one push for a
2714 // decl spec and another for each declarator; in a decl group like:
2715 // deprecated_typedef foo, *bar, baz();
2716 // only the declarator pops will be passed decls. This is correct;
2717 // we really do need to consider delayed diagnostics from the decl spec
2718 // for each of the different declarations.
John McCall1064d7e2010-03-16 05:22:47 +00002719 for (unsigned I = 0; I != E; ++I) {
John McCall86121512010-01-27 03:50:35 +00002720 if (DelayedDiagnostics[I].Triggered)
2721 continue;
2722
2723 switch (DelayedDiagnostics[I].Kind) {
2724 case DelayedDiagnostic::Deprecation:
2725 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2726 break;
2727
2728 case DelayedDiagnostic::Access:
2729 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2730 break;
2731 }
2732 }
2733 }
2734
John McCall1064d7e2010-03-16 05:22:47 +00002735 // Destroy all the delayed diagnostics we're about to pop off.
2736 for (unsigned I = SavedIndex; I != E; ++I)
2737 DelayedDiagnostics[I].destroy();
2738
John McCall86121512010-01-27 03:50:35 +00002739 DelayedDiagnostics.set_size(SavedIndex);
John McCall28a6aea2009-11-04 02:18:39 +00002740}
2741
2742static bool isDeclDeprecated(Decl *D) {
2743 do {
2744 if (D->hasAttr<DeprecatedAttr>())
2745 return true;
2746 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2747 return false;
2748}
2749
John McCallb45a1e72010-08-26 02:13:20 +00002750void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00002751 Decl *Ctx) {
2752 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00002753 return;
2754
John McCall86121512010-01-27 03:50:35 +00002755 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002756 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00002757 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002758 << DD.getDeprecationDecl()->getDeclName()
2759 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00002760 else
2761 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002762 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002763}
2764
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002765void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian551063102010-10-06 21:18:44 +00002766 SourceLocation Loc) {
John McCall28a6aea2009-11-04 02:18:39 +00002767 // Delay if we're currently parsing a declaration.
2768 if (ParsingDeclDepth) {
Fariborz Jahanian551063102010-10-06 21:18:44 +00002769 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2770 Message));
John McCall28a6aea2009-11-04 02:18:39 +00002771 return;
2772 }
2773
2774 // Otherwise, don't warn if our current context is deprecated.
2775 if (isDeclDeprecated(cast<Decl>(CurContext)))
2776 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00002777 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00002778 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2779 << Message;
2780 else
2781 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00002782}