blob: df74e00de0b9a7a475d41674c4699373e6524cd4 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000024using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000026
Chris Lattnere5c5ee12008-06-29 00:16:31 +000027//===----------------------------------------------------------------------===//
28// Helper functions
29//===----------------------------------------------------------------------===//
30
Ted Kremeneka18d7d82009-08-14 20:49:40 +000031static const FunctionType *getFunctionType(const Decl *d,
32 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000033 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000034 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000035 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000036 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000037 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000038 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000039 Ty = decl->getUnderlyingType();
40 else
41 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000042
Chris Lattner6b6b5372008-06-26 18:38:35 +000043 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000044 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000045 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000046 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000047
John McCall183700f2009-09-21 23:43:11 +000048 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000049}
50
Daniel Dunbar35682492008-09-26 04:12:28 +000051// FIXME: We should provide an abstraction around a method or function
52// to provide the following bits of information.
53
Nuno Lopesd20254f2009-12-20 23:11:08 +000054/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-08-14 20:49:40 +000055/// type (function or function-typed variable).
56static bool isFunction(const Decl *d) {
57 return getFunctionType(d, false) != NULL;
58}
59
60/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbard3f2c102008-10-19 02:04:16 +000061/// type (function or function-typed variable) or an Objective-C
62/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000063static bool isFunctionOrMethod(const Decl *d) {
64 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000065}
66
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000067/// isFunctionOrMethodOrBlock - Return true if the given decl has function
68/// type (function or function-typed variable) or an Objective-C
69/// method or a block.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000070static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000071 if (isFunctionOrMethod(d))
72 return true;
73 // check for block is more involved.
74 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
75 QualType Ty = V->getType();
76 return Ty->isBlockPointerType();
77 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000078 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000079}
80
Daniel Dunbard3f2c102008-10-19 02:04:16 +000081/// hasFunctionProto - Return true if the given decl has a argument
82/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000083/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000084static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000085 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000086 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000087 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000088 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000089 return true;
90 }
91}
92
93/// getFunctionOrMethodNumArgs - Return number of function or method
94/// arguments. It is an error to call this on a K&R function (use
95/// hasFunctionProto first).
Ted Kremeneka18d7d82009-08-14 20:49:40 +000096static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000097 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000098 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000099 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
100 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +0000101 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000102}
103
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000104static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000105 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000106 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000107 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
108 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000109
Chris Lattner89951a82009-02-20 18:43:26 +0000110 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000111}
112
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000113static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000114 if (const FunctionType *FnTy = getFunctionType(d))
115 return cast<FunctionProtoType>(FnTy)->getResultType();
116 return cast<ObjCMethodDecl>(d)->getResultType();
117}
118
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000119static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000120 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000121 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000122 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000123 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000124 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000125 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000126 return cast<ObjCMethodDecl>(d)->isVariadic();
127 }
128}
129
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000130static bool isInstanceMethod(const Decl *d) {
131 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d))
132 return MethodDecl->isInstance();
133 return false;
134}
135
Chris Lattner6b6b5372008-06-26 18:38:35 +0000136static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000137 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000138 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000139 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000140
John McCall506b57e2010-05-17 21:00:27 +0000141 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
142 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000143 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000144
John McCall506b57e2010-05-17 21:00:27 +0000145 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000146
Chris Lattner6b6b5372008-06-26 18:38:35 +0000147 // FIXME: Should we walk the chain of classes?
148 return ClsName == &Ctx.Idents.get("NSString") ||
149 ClsName == &Ctx.Idents.get("NSMutableString");
150}
151
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000152static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000153 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000154 if (!PT)
155 return false;
156
Ted Kremenek6217b802009-07-29 21:53:49 +0000157 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000158 if (!RT)
159 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000160
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000161 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000162 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000163 return false;
164
165 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
166}
167
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000168//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000169// Attribute Implementations
170//===----------------------------------------------------------------------===//
171
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000172// FIXME: All this manual attribute parsing code is gross. At the
173// least add some helper functions to check most argument patterns (#
174// and types of args).
175
Mike Stumpbf916502009-07-24 19:02:52 +0000176static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000177 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000178 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
179 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000180 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000181 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000182 }
Mike Stumpbf916502009-07-24 19:02:52 +0000183
Chris Lattner6b6b5372008-06-26 18:38:35 +0000184 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000185
186 Expr *sizeExpr;
187
188 // Special case where the argument is a template id.
189 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000190 CXXScopeSpec SS;
191 UnqualifiedId id;
192 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
193 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000194 } else {
195 // check the attribute arguments.
196 if (Attr.getNumArgs() != 1) {
197 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
198 return;
199 }
Peter Collingbourne7a730022010-11-23 20:45:58 +0000200 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000201 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000202
203 // Instantiate/Install the vector type, and let Sema build the type for us.
204 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000205 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000206 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000207 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000208 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000209
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000210 // Remember this typedef decl, we will need it later for diagnostics.
211 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000212 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000213}
214
Chris Lattner803d0802008-06-29 00:43:07 +0000215static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000216 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000217 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000218 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000219 return;
220 }
Mike Stumpbf916502009-07-24 19:02:52 +0000221
Chris Lattner6b6b5372008-06-26 18:38:35 +0000222 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Sean Huntcf807c42010-08-18 23:23:40 +0000223 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000224 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
225 // If the alignment is less than or equal to 8 bits, the packed attribute
226 // has no effect.
227 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000228 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000229 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000230 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000231 else
Sean Huntcf807c42010-08-18 23:23:40 +0000232 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000233 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000234 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000235}
236
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000237static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000238 // check the attribute arguments.
239 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000240 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000241 return;
242 }
Mike Stumpbf916502009-07-24 19:02:52 +0000243
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000244 // The IBAction attributes only apply to instance methods.
245 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
246 if (MD->isInstanceMethod()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000247 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000248 return;
249 }
250
251 S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName();
252}
253
254static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
255 // check the attribute arguments.
256 if (Attr.getNumArgs() > 0) {
257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
258 return;
259 }
260
261 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000262 // Objective-C classes.
263 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000264 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000265 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000266 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000267
268 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000269}
270
Ted Kremenek857e9182010-05-19 17:38:06 +0000271static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
272 Sema &S) {
273
274 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000275 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000276 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
277 return;
278 }
279
280 // The IBOutletCollection attributes only apply to instance variables of
281 // Objective-C classes.
282 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
283 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName();
284 return;
285 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000286 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
287 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
288 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
289 << VD->getType() << 0;
290 return;
291 }
292 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
293 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
294 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
295 << PD->getType() << 1;
296 return;
297 }
298
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000299 IdentifierInfo *II = Attr.getParameterName();
300 if (!II)
301 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000302
John McCallb3d87482010-08-24 05:47:05 +0000303 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000304 S.getScopeForContext(d->getDeclContext()->getParent()));
305 if (!TypeRep) {
306 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
307 return;
308 }
John McCallb3d87482010-08-24 05:47:05 +0000309 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000310 // Diagnose use of non-object type in iboutletcollection attribute.
311 // FIXME. Gnu attribute extension ignores use of builtin types in
312 // attributes. So, __attribute__((iboutletcollection(char))) will be
313 // treated as __attribute__((iboutletcollection())).
314 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
315 !QT->isObjCObjectType()) {
316 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
317 return;
318 }
Sean Huntcf807c42010-08-18 23:23:40 +0000319 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
320 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000321}
322
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000323static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000324 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
325 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000326 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000327 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000328 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000329 return;
330 }
Mike Stumpbf916502009-07-24 19:02:52 +0000331
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000332 // In C++ the implicit 'this' function parameter also counts, and they are
333 // counted from one.
334 bool HasImplicitThisParam = isInstanceMethod(d);
335 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000336
337 // The nonnull attribute only applies to pointers.
338 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000339
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000340 for (AttributeList::arg_iterator I=Attr.arg_begin(),
341 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000342
343
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000344 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000345 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000346 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000347 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
348 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000349 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
350 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000351 return;
352 }
Mike Stumpbf916502009-07-24 19:02:52 +0000353
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000354 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000355
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000356 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000357 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000358 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000359 return;
360 }
Mike Stumpbf916502009-07-24 19:02:52 +0000361
Ted Kremenek465172f2008-07-21 22:09:15 +0000362 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000363 if (HasImplicitThisParam) {
364 if (x == 0) {
365 S.Diag(Attr.getLoc(),
366 diag::err_attribute_invalid_implicit_this_argument)
367 << "nonnull" << Ex->getSourceRange();
368 return;
369 }
370 --x;
371 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000372
373 // Is the function argument a pointer type?
Mike Stumpbf916502009-07-24 19:02:52 +0000374 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000375 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000376 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000377 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000378 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000379 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000380 }
Mike Stumpbf916502009-07-24 19:02:52 +0000381
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000382 NonNullArgs.push_back(x);
383 }
Mike Stumpbf916502009-07-24 19:02:52 +0000384
385 // If no arguments were specified to __attribute__((nonnull)) then all pointer
386 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000387 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000388 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
389 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000390 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000391 NonNullArgs.push_back(I);
Fariborz Jahanianff3a0782010-09-27 22:42:37 +0000392 else if (const RecordType *UT = T->getAsUnionType()) {
393 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
394 RecordDecl *UD = UT->getDecl();
395 for (RecordDecl::field_iterator it = UD->field_begin(),
396 itend = UD->field_end(); it != itend; ++it) {
397 T = it->getType();
398 if (T->isAnyPointerType() || T->isBlockPointerType()) {
399 NonNullArgs.push_back(I);
400 break;
401 }
402 }
403 }
404 }
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000405 }
Mike Stumpbf916502009-07-24 19:02:52 +0000406
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000407 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000408 if (NonNullArgs.empty()) {
409 // Warn the trivial case only if attribute is not coming from a
410 // macro instantiation.
411 if (Attr.getLoc().isFileID())
412 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000413 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000414 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000415 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000416
417 unsigned* start = &NonNullArgs[0];
418 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000419 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000420 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
421 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000422}
423
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000424static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
425 // This attribute must be applied to a function declaration.
426 // The first argument to the attribute must be a string,
427 // the name of the resource, for example "malloc".
428 // The following arguments must be argument indexes, the arguments must be
429 // of integer type for Returns, otherwise of pointer type.
430 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000431 // after being held. free() should be __attribute((ownership_takes)), whereas
432 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000433
434 if (!AL.getParameterName()) {
435 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
436 << AL.getName()->getName() << 1;
437 return;
438 }
439 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000440 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000441 switch (AL.getKind()) {
442 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000443 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000444 if (AL.getNumArgs() < 1) {
445 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
446 return;
447 }
Jordy Rose2a479922010-08-12 08:54:03 +0000448 break;
449 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000450 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000451 if (AL.getNumArgs() < 1) {
452 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
453 return;
454 }
Jordy Rose2a479922010-08-12 08:54:03 +0000455 break;
456 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000457 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000458 if (AL.getNumArgs() > 1) {
459 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
460 << AL.getNumArgs() + 1;
461 return;
462 }
Jordy Rose2a479922010-08-12 08:54:03 +0000463 break;
464 default:
465 // This should never happen given how we are called.
466 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000467 }
468
469 if (!isFunction(d) || !hasFunctionProto(d)) {
470 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName()
471 << 0 /*function*/;
472 return;
473 }
474
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000475 // In C++ the implicit 'this' function parameter also counts, and they are
476 // counted from one.
477 bool HasImplicitThisParam = isInstanceMethod(d);
478 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000479
480 llvm::StringRef Module = AL.getParameterName()->getName();
481
482 // Normalize the argument, __foo__ becomes foo.
483 if (Module.startswith("__") && Module.endswith("__"))
484 Module = Module.substr(2, Module.size() - 4);
485
486 llvm::SmallVector<unsigned, 10> OwnershipArgs;
487
Jordy Rose2a479922010-08-12 08:54:03 +0000488 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
489 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000490
Peter Collingbourne7a730022010-11-23 20:45:58 +0000491 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-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 Carruth07d7e7a2010-11-16 08:35:43 +0000508 if (HasImplicitThisParam) {
509 if (x == 0) {
510 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
511 << "ownership" << IdxExpr->getSourceRange();
512 return;
513 }
514 --x;
515 }
516
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000517 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000518 case OwnershipAttr::Takes:
519 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000520 // Is the function argument a pointer type?
521 QualType T = getFunctionOrMethodArgType(d, x);
522 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
523 // FIXME: Should also highlight argument in decl.
524 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000525 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000526 << "pointer"
527 << IdxExpr->getSourceRange();
528 continue;
529 }
530 break;
531 }
Sean Huntcf807c42010-08-18 23:23:40 +0000532 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000533 if (AL.getNumArgs() > 1) {
534 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000535 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000547 default:
548 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000549 } // switch
550
551 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000552 for (specific_attr_iterator<OwnershipAttr>
553 i = d->specific_attr_begin<OwnershipAttr>(),
554 e = d->specific_attr_end<OwnershipAttr>();
555 i != e; ++i) {
556 if ((*i)->getOwnKind() != K) {
557 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
558 I!=E; ++I) {
559 if (x == *I) {
560 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
561 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000562 }
563 }
564 }
565 }
566 OwnershipArgs.push_back(x);
567 }
568
569 unsigned* start = OwnershipArgs.data();
570 unsigned size = OwnershipArgs.size();
571 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000572
573 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
574 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
575 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000576 }
Sean Huntcf807c42010-08-18 23:23:40 +0000577
578 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
579 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000580}
581
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000582static bool isStaticVarOrStaticFunciton(Decl *D) {
583 if (VarDecl *VD = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000584 return VD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000585 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000586 return FD->getStorageClass() == SC_Static;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000587 return false;
588}
589
590static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
591 // Check the attribute arguments.
592 if (Attr.getNumArgs() > 1) {
593 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
594 return;
595 }
596
597 // gcc rejects
598 // class c {
599 // static int a __attribute__((weakref ("v2")));
600 // static int b() __attribute__((weakref ("f3")));
601 // };
602 // and ignores the attributes of
603 // void f(void) {
604 // static int a __attribute__((weakref ("v2")));
605 // }
606 // we reject them
Sebastian Redl7a126a42010-08-31 00:36:30 +0000607 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
608 if (!Ctx->isFileContext()) {
609 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
610 dyn_cast<NamedDecl>(d)->getNameAsString();
611 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000612 }
613
614 // The GCC manual says
615 //
616 // At present, a declaration to which `weakref' is attached can only
617 // be `static'.
618 //
619 // It also says
620 //
621 // Without a TARGET,
622 // given as an argument to `weakref' or to `alias', `weakref' is
623 // equivalent to `weak'.
624 //
625 // gcc 4.4.1 will accept
626 // int a7 __attribute__((weakref));
627 // as
628 // int a7 __attribute__((weak));
629 // This looks like a bug in gcc. We reject that for now. We should revisit
630 // it if this behaviour is actually used.
631
632 if (!isStaticVarOrStaticFunciton(d)) {
633 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
634 dyn_cast<NamedDecl>(d)->getNameAsString();
635 return;
636 }
637
638 // GCC rejects
639 // static ((alias ("y"), weakref)).
640 // Should we? How to check that weakref is before or after alias?
641
642 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000643 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-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 Christopherf48f3672010-12-01 22:13:54 +0000654 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
655 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000656 }
657
Sean Huntcf807c42010-08-18 23:23:40 +0000658 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000659}
660
Chris Lattner803d0802008-06-29 00:43:07 +0000661static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000662 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000663 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000664 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000665 return;
666 }
Mike Stumpbf916502009-07-24 19:02:52 +0000667
Peter Collingbourne7a730022010-11-23 20:45:58 +0000668 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000669 Arg = Arg->IgnoreParenCasts();
670 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000671
Chris Lattner6b6b5372008-06-26 18:38:35 +0000672 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000673 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000674 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000675 return;
676 }
Mike Stumpbf916502009-07-24 19:02:52 +0000677
Chris Lattner6b6b5372008-06-26 18:38:35 +0000678 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000679
Eric Christopherf48f3672010-12-01 22:13:54 +0000680 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
681 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000682}
683
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000684static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000685 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000686 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000687 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000688 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000689 return;
690 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000691
Chris Lattnerc5197432009-04-14 17:02:11 +0000692 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000693 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Daniel Dunbardd0cb222010-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 Carlsson5bab7882009-02-19 19:16:48 +0000712 return;
713 }
Mike Stumpbf916502009-07-24 19:02:52 +0000714
Sean Huntcf807c42010-08-18 23:23:40 +0000715 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000716}
717
Ryan Flynn76168e22009-08-09 20:07:29 +0000718static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000719 // Check the attribute arguments.
Ryan Flynn76168e22009-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 Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000725 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000726 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000727 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000728 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000729 return;
730 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000731 }
732
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000733 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000734}
735
Dan Gohman34c26302010-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 Gohman34c26302010-11-17 00:03:07 +0000743 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
744}
745
Ted Kremenekb7252322009-04-10 00:01:14 +0000746static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +0000747 /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */
748 assert(Attr.isInvalid() == false);
Sean Huntcf807c42010-08-18 23:23:40 +0000749 d->addAttr(::new (S.Context) NoReturnAttr(Attr.getLoc(), S.Context));
Ted Kremenekb7252322009-04-10 00:01:14 +0000750}
751
752static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
753 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000754
755 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
756 // because 'analyzer_noreturn' does not impact the type.
757
758 if (Attr.getNumArgs() != 0) {
759 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
760 return;
761 }
762
763 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
764 ValueDecl *VD = dyn_cast<ValueDecl>(d);
765 if (VD == 0 || (!VD->getType()->isBlockPointerType()
766 && !VD->getType()->isFunctionPointerType())) {
767 S.Diag(Attr.getLoc(),
768 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
769 : diag::warn_attribute_wrong_decl_type)
770 << Attr.getName() << 0 /*function*/;
771 return;
772 }
773 }
774
775 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000776}
777
John Thompson35cc9622010-08-09 21:53:52 +0000778// PS3 PPU-specific.
779static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
780 Sema &S) {
781/*
782 Returning a Vector Class in Registers
783
Eric Christopherf48f3672010-12-01 22:13:54 +0000784 According to the PPU ABI specifications, a class with a single member of
785 vector type is returned in memory when used as the return value of a function.
786 This results in inefficient code when implementing vector classes. To return
787 the value in a single vector register, add the vecreturn attribute to the
788 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +0000789
790 Example:
791
792 struct Vector
793 {
794 __vector float xyzw;
795 } __attribute__((vecreturn));
796
797 Vector Add(Vector lhs, Vector rhs)
798 {
799 Vector result;
800 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
801 return result; // This will be returned in a register
802 }
803*/
John Thompson01add592010-09-18 01:12:07 +0000804 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
806 << Attr.getName() << 9 /*class*/;
807 return;
808 }
809
810 if (d->getAttr<VecReturnAttr>()) {
811 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
812 return;
813 }
814
John Thompson01add592010-09-18 01:12:07 +0000815 RecordDecl *record = cast<RecordDecl>(d);
816 int count = 0;
817
818 if (!isa<CXXRecordDecl>(record)) {
819 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
820 return;
821 }
822
823 if (!cast<CXXRecordDecl>(record)->isPOD()) {
824 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
825 return;
826 }
827
Eric Christopherf48f3672010-12-01 22:13:54 +0000828 for (RecordDecl::field_iterator iter = record->field_begin();
829 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +0000830 if ((count == 1) || !iter->getType()->isVectorType()) {
831 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
832 return;
833 }
834 count++;
835 }
836
Sean Huntcf807c42010-08-18 23:23:40 +0000837 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000838}
839
Sean Huntbbd37c62009-11-21 08:43:09 +0000840static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
841 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
842 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall04a67a62010-02-05 21:31:56 +0000843 << Attr.getName() << 8 /*function, method, or parameter*/;
Sean Huntbbd37c62009-11-21 08:43:09 +0000844 return;
845 }
846 // FIXME: Actually store the attribute on the declaration
847}
848
Ted Kremenek73798892008-07-25 04:39:19 +0000849static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
850 // check the attribute arguments.
851 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000852 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000853 return;
854 }
Mike Stumpbf916502009-07-24 19:02:52 +0000855
John McCallaec58602010-03-31 02:47:45 +0000856 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
857 !isa<TypeDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000858 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000859 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000860 return;
861 }
Mike Stumpbf916502009-07-24 19:02:52 +0000862
Sean Huntcf807c42010-08-18 23:23:40 +0000863 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000864}
865
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000866static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
867 // check the attribute arguments.
868 if (Attr.getNumArgs() != 0) {
869 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
870 return;
871 }
Mike Stumpbf916502009-07-24 19:02:52 +0000872
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000873 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000874 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000875 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
876 return;
877 }
878 } else if (!isFunctionOrMethod(d)) {
879 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000880 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000881 return;
882 }
Mike Stumpbf916502009-07-24 19:02:52 +0000883
Sean Huntcf807c42010-08-18 23:23:40 +0000884 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000885}
886
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000887static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
888 // check the attribute arguments.
889 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000890 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
891 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000892 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000893 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000894
895 int priority = 65535; // FIXME: Do not hardcode such constants.
896 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000897 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000898 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000899 if (E->isTypeDependent() || E->isValueDependent() ||
900 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000902 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000903 return;
904 }
905 priority = Idx.getZExtValue();
906 }
Mike Stumpbf916502009-07-24 19:02:52 +0000907
Chris Lattnerc5197432009-04-14 17:02:11 +0000908 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000909 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000910 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000911 return;
912 }
913
Eric Christopherf48f3672010-12-01 22:13:54 +0000914 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
915 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000916}
917
918static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
919 // check the attribute arguments.
920 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000921 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
922 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000923 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000924 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000925
926 int priority = 65535; // FIXME: Do not hardcode such constants.
927 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000928 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000929 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000930 if (E->isTypeDependent() || E->isValueDependent() ||
931 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000932 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000933 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000934 return;
935 }
936 priority = Idx.getZExtValue();
937 }
Mike Stumpbf916502009-07-24 19:02:52 +0000938
Anders Carlsson6782fc62008-08-22 22:10:48 +0000939 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000940 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000941 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000942 return;
943 }
944
Eric Christopherf48f3672010-12-01 22:13:54 +0000945 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
946 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000947}
948
Chris Lattner803d0802008-06-29 00:43:07 +0000949static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000950 // check the attribute arguments.
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000951 int noArgs = Attr.getNumArgs();
952 if (noArgs > 1) {
953 S.Diag(Attr.getLoc(),
954 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Chris Lattner6b6b5372008-06-26 18:38:35 +0000955 return;
956 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000957 // Handle the case where deprecated attribute has a text message.
958 StringLiteral *SE;
959 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000960 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000961 SE = dyn_cast<StringLiteral>(ArgExpr);
962 if (!SE) {
963 S.Diag(ArgExpr->getLocStart(),
964 diag::err_attribute_not_string) << "deprecated";
965 return;
966 }
967 }
968 else
969 SE = StringLiteral::CreateEmpty(S.Context, 1);
Mike Stumpbf916502009-07-24 19:02:52 +0000970
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +0000971 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context,
972 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000973}
974
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000975static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
976 // check the attribute arguments.
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000977 int noArgs = Attr.getNumArgs();
978 if (noArgs > 1) {
Eric Christopherf48f3672010-12-01 22:13:54 +0000979 S.Diag(Attr.getLoc(),
980 diag::err_attribute_wrong_number_arguments) << "0 or 1";
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000981 return;
982 }
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000983 // Handle the case where unavailable attribute has a text message.
984 StringLiteral *SE;
985 if (noArgs == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000986 Expr *ArgExpr = Attr.getArg(0);
Fariborz Jahanianc784dc12010-10-06 23:12:32 +0000987 SE = dyn_cast<StringLiteral>(ArgExpr);
988 if (!SE) {
989 S.Diag(ArgExpr->getLocStart(),
990 diag::err_attribute_not_string) << "unavailable";
991 return;
992 }
993 }
994 else
995 SE = StringLiteral::CreateEmpty(S.Context, 1);
996 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context,
997 SE->getString()));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000998}
999
Chris Lattner803d0802008-06-29 00:43:07 +00001000static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001001 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001002 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001004 return;
1005 }
Mike Stumpbf916502009-07-24 19:02:52 +00001006
Peter Collingbourne7a730022010-11-23 20:45:58 +00001007 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001008 Arg = Arg->IgnoreParenCasts();
1009 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001010
Chris Lattner6b6b5372008-06-26 18:38:35 +00001011 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001012 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001013 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001014 return;
1015 }
Mike Stumpbf916502009-07-24 19:02:52 +00001016
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001017 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001018 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001019
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001020 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001021 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001022 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001023 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001024 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001025 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001026 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001027 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001028 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001029 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001030 return;
1031 }
Mike Stumpbf916502009-07-24 19:02:52 +00001032
Sean Huntcf807c42010-08-18 23:23:40 +00001033 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001034}
1035
Chris Lattner0db29ec2009-02-14 08:09:34 +00001036static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1037 Sema &S) {
1038 if (Attr.getNumArgs() != 0) {
1039 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1040 return;
1041 }
Mike Stumpbf916502009-07-24 19:02:52 +00001042
Chris Lattner0db29ec2009-02-14 08:09:34 +00001043 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1044 if (OCI == 0) {
1045 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1046 return;
1047 }
Mike Stumpbf916502009-07-24 19:02:52 +00001048
Sean Huntcf807c42010-08-18 23:23:40 +00001049 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001050}
1051
1052static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001053 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001054 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001055 return;
1056 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001057 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001058 QualType T = TD->getUnderlyingType();
1059 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001060 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001061 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1062 return;
1063 }
1064 }
Sean Huntcf807c42010-08-18 23:23:40 +00001065 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001066}
1067
Mike Stumpbf916502009-07-24 19:02:52 +00001068static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001069HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1070 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001071 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001072 return;
1073 }
1074
1075 if (!isa<FunctionDecl>(D)) {
1076 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1077 return;
1078 }
1079
Sean Huntcf807c42010-08-18 23:23:40 +00001080 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001081}
1082
Steve Naroff9eae5762008-09-18 16:44:58 +00001083static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001084 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001085 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001086 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001087 return;
1088 }
Mike Stumpbf916502009-07-24 19:02:52 +00001089
Steve Naroff9eae5762008-09-18 16:44:58 +00001090 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001091 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001092 return;
1093 }
Mike Stumpbf916502009-07-24 19:02:52 +00001094
Sean Huntcf807c42010-08-18 23:23:40 +00001095 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001096 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001097 type = BlocksAttr::ByRef;
1098 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001099 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001100 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001101 return;
1102 }
Mike Stumpbf916502009-07-24 19:02:52 +00001103
Sean Huntcf807c42010-08-18 23:23:40 +00001104 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001105}
1106
Anders Carlsson77091822008-10-05 18:05:59 +00001107static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1108 // check the attribute arguments.
1109 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001110 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
1111 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +00001112 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001113 }
1114
Anders Carlsson77091822008-10-05 18:05:59 +00001115 int sentinel = 0;
1116 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001117 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001118 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001119 if (E->isTypeDependent() || E->isValueDependent() ||
1120 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001121 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001122 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001123 return;
1124 }
1125 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001126
Anders Carlsson77091822008-10-05 18:05:59 +00001127 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001128 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1129 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001130 return;
1131 }
1132 }
1133
1134 int nullPos = 0;
1135 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001136 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001137 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001138 if (E->isTypeDependent() || E->isValueDependent() ||
1139 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001141 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001142 return;
1143 }
1144 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001145
Anders Carlsson77091822008-10-05 18:05:59 +00001146 if (nullPos > 1 || nullPos < 0) {
1147 // FIXME: This error message could be improved, it would be nice
1148 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001149 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1150 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001151 return;
1152 }
1153 }
1154
1155 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001156 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001157 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001158
Chris Lattner897cd902009-03-17 23:03:47 +00001159 if (isa<FunctionNoProtoType>(FT)) {
1160 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1161 return;
1162 }
Mike Stumpbf916502009-07-24 19:02:52 +00001163
Chris Lattner897cd902009-03-17 23:03:47 +00001164 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001165 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001166 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001167 }
Anders Carlsson77091822008-10-05 18:05:59 +00001168 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1169 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001170 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001171 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001172 }
1173 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001174 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1175 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001176 ;
1177 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001178 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001179 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001180 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherf48f3672010-12-01 22:13:54 +00001181 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001182 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001183 int m = Ty->isFunctionPointerType() ? 0 : 1;
1184 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001185 return;
1186 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001187 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001188 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001189 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001190 return;
1191 }
Anders Carlsson77091822008-10-05 18:05:59 +00001192 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001193 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +00001194 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +00001195 return;
1196 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001197 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1198 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001199}
1200
Chris Lattner026dc962009-02-14 07:37:35 +00001201static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1202 // check the attribute arguments.
1203 if (Attr.getNumArgs() != 0) {
1204 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1205 return;
1206 }
1207
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001208 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001209 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001210 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +00001211 return;
1212 }
Mike Stumpbf916502009-07-24 19:02:52 +00001213
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001214 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1215 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1216 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001217 return;
1218 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001219 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1220 if (MD->getResultType()->isVoidType()) {
1221 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1222 << Attr.getName() << 1;
1223 return;
1224 }
1225
Sean Huntcf807c42010-08-18 23:23:40 +00001226 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001227}
1228
1229static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001230 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001231 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001232 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001233 return;
1234 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001235
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001236 /* weak only applies to non-static declarations */
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001237 if (isStaticVarOrStaticFunciton(D)) {
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001238 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
1239 dyn_cast<NamedDecl>(D)->getNameAsString();
1240 return;
1241 }
1242
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001243 // TODO: could also be applied to methods?
1244 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
1245 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001246 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001247 return;
1248 }
Mike Stumpbf916502009-07-24 19:02:52 +00001249
Sean Huntcf807c42010-08-18 23:23:40 +00001250 D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001251}
1252
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001253static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1254 // check the attribute arguments.
1255 if (Attr.getNumArgs() != 0) {
1256 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1257 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001258 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001259
1260 // weak_import only applies to variable & function declarations.
1261 bool isDef = false;
1262 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1263 isDef = (!VD->hasExternalStorage() || VD->getInit());
1264 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001265 isDef = FD->hasBody();
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +00001266 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1267 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +00001268 return;
Fariborz Jahanian5f8f8572009-11-17 19:08:08 +00001269 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001270 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001271 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001272 !isa<ObjCInterfaceDecl>(D))
1273 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3be17942010-04-12 16:57:31 +00001274 << Attr.getName() << 2 /*variable and function*/;
1275 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001276 }
1277
1278 // Merge should handle any subsequent violations.
1279 if (isDef) {
Mike Stumpbf916502009-07-24 19:02:52 +00001280 S.Diag(Attr.getLoc(),
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001281 diag::warn_attribute_weak_import_invalid_on_definition)
1282 << "weak_import" << 2 /*variable and function*/;
1283 return;
1284 }
1285
Sean Huntcf807c42010-08-18 23:23:40 +00001286 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001287}
1288
Nate Begeman6f3d8382009-06-26 06:32:41 +00001289static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1290 Sema &S) {
1291 // Attribute has 3 arguments.
1292 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001293 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001294 return;
1295 }
1296
1297 unsigned WGSize[3];
1298 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001299 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001300 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001301 if (E->isTypeDependent() || E->isValueDependent() ||
1302 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1304 << "reqd_work_group_size" << E->getSourceRange();
1305 return;
1306 }
1307 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1308 }
Sean Huntcf807c42010-08-18 23:23:40 +00001309 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1310 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001311 WGSize[2]));
1312}
1313
Chris Lattner026dc962009-02-14 07:37:35 +00001314static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001315 // Attribute has no arguments.
1316 if (Attr.getNumArgs() != 1) {
1317 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1318 return;
1319 }
1320
1321 // Make sure that there is a string literal as the sections's single
1322 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001323 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001324 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001325 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001326 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001327 return;
1328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Chris Lattner797c3c42009-08-10 19:03:04 +00001330 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001331 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001332 if (!Error.empty()) {
1333 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1334 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001335 return;
1336 }
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001338 // This attribute cannot be applied to local variables.
1339 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1340 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1341 return;
1342 }
1343
Eric Christopherf48f3672010-12-01 22:13:54 +00001344 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1345 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001346}
1347
Chris Lattner6b6b5372008-06-26 18:38:35 +00001348
Chris Lattner803d0802008-06-29 00:43:07 +00001349static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001350 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001351 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001352 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001353 return;
1354 }
Mike Stumpbf916502009-07-24 19:02:52 +00001355
Sean Huntcf807c42010-08-18 23:23:40 +00001356 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001357}
1358
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001359static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1360 // check the attribute arguments.
1361 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001362 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001363 return;
1364 }
Mike Stumpbf916502009-07-24 19:02:52 +00001365
Sean Huntcf807c42010-08-18 23:23:40 +00001366 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001367}
1368
1369static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1370 // check the attribute arguments.
1371 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001373 return;
1374 }
Mike Stumpbf916502009-07-24 19:02:52 +00001375
Sean Huntcf807c42010-08-18 23:23:40 +00001376 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001377}
1378
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001379static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001380 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001381 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1382 return;
1383 }
Mike Stumpbf916502009-07-24 19:02:52 +00001384
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001385 if (Attr.getNumArgs() != 0) {
1386 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1387 return;
1388 }
Mike Stumpbf916502009-07-24 19:02:52 +00001389
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001390 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001391
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001392 if (!VD || !VD->hasLocalStorage()) {
1393 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1394 return;
1395 }
Mike Stumpbf916502009-07-24 19:02:52 +00001396
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001397 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001398 // FIXME: Lookup probably isn't looking in the right place
1399 // FIXME: The lookup source location should be in the attribute, not the
1400 // start of the attribute.
John McCallf36e02d2009-10-09 21:13:30 +00001401 NamedDecl *CleanupDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001402 = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(),
John McCallf36e02d2009-10-09 21:13:30 +00001403 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001404 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001405 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001406 Attr.getParameterName();
1407 return;
1408 }
Mike Stumpbf916502009-07-24 19:02:52 +00001409
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001410 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1411 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001412 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001413 Attr.getParameterName();
1414 return;
1415 }
1416
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001417 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001418 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001419 Attr.getParameterName();
1420 return;
1421 }
Mike Stumpbf916502009-07-24 19:02:52 +00001422
Anders Carlsson89941c12009-02-07 23:16:50 +00001423 // We're currently more strict than GCC about what function types we accept.
1424 // If this ever proves to be a problem it should be easy to fix.
1425 QualType Ty = S.Context.getPointerType(VD->getType());
1426 QualType ParamTy = FD->getParamDecl(0)->getType();
John McCall1c23e912010-11-16 02:32:08 +00001427 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpbf916502009-07-24 19:02:52 +00001428 S.Diag(Attr.getLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001429 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1430 Attr.getParameterName() << ParamTy << Ty;
1431 return;
1432 }
Mike Stumpbf916502009-07-24 19:02:52 +00001433
Sean Huntcf807c42010-08-18 23:23:40 +00001434 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001435}
1436
Mike Stumpbf916502009-07-24 19:02:52 +00001437/// Handle __attribute__((format_arg((idx)))) attribute based on
1438/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1439static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001440 if (Attr.getNumArgs() != 1) {
1441 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1442 return;
1443 }
1444 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1445 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1446 << Attr.getName() << 0 /*function*/;
1447 return;
1448 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001449
1450 // In C++ the implicit 'this' function parameter also counts, and they are
1451 // counted from one.
1452 bool HasImplicitThisParam = isInstanceMethod(d);
1453 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001454 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001455
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001456 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001457 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001458 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001459 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1460 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001461 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1462 << "format" << 2 << IdxExpr->getSourceRange();
1463 return;
1464 }
Mike Stumpbf916502009-07-24 19:02:52 +00001465
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001466 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1467 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1468 << "format" << 2 << IdxExpr->getSourceRange();
1469 return;
1470 }
Mike Stumpbf916502009-07-24 19:02:52 +00001471
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001472 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001473
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001474 if (HasImplicitThisParam) {
1475 if (ArgIdx == 0) {
1476 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1477 << "format_arg" << IdxExpr->getSourceRange();
1478 return;
1479 }
1480 ArgIdx--;
1481 }
1482
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001483 // make sure the format string is really a string
1484 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001485
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001486 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1487 if (not_nsstring_type &&
1488 !isCFStringType(Ty, S.Context) &&
1489 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001490 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001491 // FIXME: Should highlight the actual expression that has the wrong type.
1492 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001493 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001494 << IdxExpr->getSourceRange();
1495 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001496 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001497 Ty = getFunctionOrMethodResultType(d);
1498 if (!isNSStringType(Ty, S.Context) &&
1499 !isCFStringType(Ty, S.Context) &&
1500 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001501 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001502 // FIXME: Should highlight the actual expression that has the wrong type.
1503 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001504 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001505 << IdxExpr->getSourceRange();
1506 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001507 }
1508
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001509 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1510 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001511}
1512
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001513enum FormatAttrKind {
1514 CFStringFormat,
1515 NSStringFormat,
1516 StrftimeFormat,
1517 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001518 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001519 InvalidFormat
1520};
1521
1522/// getFormatAttrKind - Map from format attribute names to supported format
1523/// types.
1524static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1525 // Check for formats that get handled specially.
1526 if (Format == "NSString")
1527 return NSStringFormat;
1528 if (Format == "CFString")
1529 return CFStringFormat;
1530 if (Format == "strftime")
1531 return StrftimeFormat;
1532
1533 // Otherwise, check for supported formats.
1534 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1535 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1536 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1537 Format == "zcmn_err")
1538 return SupportedFormat;
1539
Duncan Sandsbc525952010-03-23 14:44:19 +00001540 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1541 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001542 return IgnoredFormat;
1543
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001544 return InvalidFormat;
1545}
1546
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001547/// Handle __attribute__((init_priority(priority))) attributes based on
1548/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1549static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1550 Sema &S) {
1551 if (!S.getLangOptions().CPlusPlus) {
1552 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1553 return;
1554 }
1555
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001556 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1557 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1558 Attr.setInvalid();
1559 return;
1560 }
1561 QualType T = dyn_cast<VarDecl>(d)->getType();
1562 if (S.Context.getAsArrayType(T))
1563 T = S.Context.getBaseElementType(T);
1564 if (!T->getAs<RecordType>()) {
1565 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1566 Attr.setInvalid();
1567 return;
1568 }
1569
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001570 if (Attr.getNumArgs() != 1) {
1571 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1572 Attr.setInvalid();
1573 return;
1574 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001575 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001576
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001577 llvm::APSInt priority(32);
1578 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1579 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1580 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1581 << "init_priority" << priorityExpr->getSourceRange();
1582 Attr.setInvalid();
1583 return;
1584 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001585 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001586 if (prioritynum < 101 || prioritynum > 65535) {
1587 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1588 << priorityExpr->getSourceRange();
1589 Attr.setInvalid();
1590 return;
1591 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001592 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1593 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001594}
1595
Mike Stumpbf916502009-07-24 19:02:52 +00001596/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1597/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001598static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001599
Chris Lattner545dd342008-06-28 23:36:30 +00001600 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001602 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001603 return;
1604 }
1605
Chris Lattner545dd342008-06-28 23:36:30 +00001606 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001607 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001608 return;
1609 }
1610
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001611 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001612 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001613 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001614 return;
1615 }
1616
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001617 // In C++ the implicit 'this' function parameter also counts, and they are
1618 // counted from one.
1619 bool HasImplicitThisParam = isInstanceMethod(d);
1620 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001621 unsigned FirstIdx = 1;
1622
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001623 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001624
1625 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001626 if (Format.startswith("__") && Format.endswith("__"))
1627 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001628
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001629 // Check for supported formats.
1630 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001631
1632 if (Kind == IgnoredFormat)
1633 return;
1634
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001635 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001636 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001637 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001638 return;
1639 }
1640
1641 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001642 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001643 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001644 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1645 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001646 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001647 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001648 return;
1649 }
1650
1651 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001652 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001653 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001654 return;
1655 }
1656
1657 // FIXME: Do we need to bounds check?
1658 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001659
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001660 if (HasImplicitThisParam) {
1661 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001662 S.Diag(Attr.getLoc(),
1663 diag::err_format_attribute_implicit_this_format_string)
1664 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001665 return;
1666 }
1667 ArgIdx--;
1668 }
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Chris Lattner6b6b5372008-06-26 18:38:35 +00001670 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001671 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001672
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001673 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001674 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001675 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1676 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001677 return;
1678 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001679 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001680 // FIXME: do we need to check if the type is NSString*? What are the
1681 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001682 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001683 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001684 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1685 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001686 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001687 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001688 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001689 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001690 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001691 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1692 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001693 return;
1694 }
1695
1696 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001697 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001698 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001699 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1700 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001701 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001702 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001703 return;
1704 }
1705
1706 // check if the function is variadic if the 3rd argument non-zero
1707 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001708 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001709 ++NumArgs; // +1 for ...
1710 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001711 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001712 return;
1713 }
1714 }
1715
Chris Lattner3c73c412008-11-19 08:23:25 +00001716 // strftime requires FirstArg to be 0 because it doesn't read from any
1717 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001718 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001719 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001720 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1721 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001722 return;
1723 }
1724 // if 0 it disables parameter checking (to use with e.g. va_list)
1725 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001726 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001727 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001728 return;
1729 }
1730
Sean Huntcf807c42010-08-18 23:23:40 +00001731 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1732 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001733 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001734}
1735
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001736static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1737 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001738 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001739 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001741 return;
1742 }
1743
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001744 // Try to find the underlying union declaration.
1745 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001746 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001747 if (TD && TD->getUnderlyingType()->isUnionType())
1748 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1749 else
1750 RD = dyn_cast<RecordDecl>(d);
1751
1752 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001753 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001754 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001755 return;
1756 }
1757
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001758 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001759 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001760 diag::warn_transparent_union_attribute_not_definition);
1761 return;
1762 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001763
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001764 RecordDecl::field_iterator Field = RD->field_begin(),
1765 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001766 if (Field == FieldEnd) {
1767 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1768 return;
1769 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001770
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001771 FieldDecl *FirstField = *Field;
1772 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001773 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001774 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001775 diag::warn_transparent_union_attribute_floating)
1776 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001777 return;
1778 }
1779
1780 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1781 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1782 for (; Field != FieldEnd; ++Field) {
1783 QualType FieldType = Field->getType();
1784 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1785 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1786 // Warn if we drop the attribute.
1787 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001788 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001789 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001790 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001791 diag::warn_transparent_union_attribute_field_size_align)
1792 << isSize << Field->getDeclName() << FieldBits;
1793 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001794 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001795 diag::note_transparent_union_first_field_size_align)
1796 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001797 return;
1798 }
1799 }
1800
Sean Huntcf807c42010-08-18 23:23:40 +00001801 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001802}
1803
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001804static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001805 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001806 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001808 return;
1809 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001810 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001811 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001812
Chris Lattner6b6b5372008-06-26 18:38:35 +00001813 // Make sure that there is a string literal as the annotation's single
1814 // argument.
1815 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001816 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001817 return;
1818 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001819 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1820 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001821}
1822
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001823static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001824 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001825 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001826 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001827 return;
1828 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001829
1830 //FIXME: The C++0x version of this attribute has more limited applicabilty
1831 // than GNU's, and should error out when it is used to specify a
1832 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001833
Chris Lattner545dd342008-06-28 23:36:30 +00001834 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00001835 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001836 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001837 }
Mike Stumpbf916502009-07-24 19:02:52 +00001838
Peter Collingbourne7a730022010-11-23 20:45:58 +00001839 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001840}
1841
1842void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1843 if (E->isTypeDependent() || E->isValueDependent()) {
1844 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00001845 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001846 return;
1847 }
1848
Sean Huntcf807c42010-08-18 23:23:40 +00001849 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00001850 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001851 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1852 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1853 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001854 return;
1855 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001856 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001857 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1858 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001859 return;
1860 }
1861
Sean Huntcf807c42010-08-18 23:23:40 +00001862 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1863}
1864
1865void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1866 // FIXME: Cache the number on the Attr object if non-dependent?
1867 // FIXME: Perform checking of type validity
1868 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1869 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001870}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001871
Mike Stumpbf916502009-07-24 19:02:52 +00001872/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1873/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001874///
Mike Stumpbf916502009-07-24 19:02:52 +00001875/// Despite what would be logical, the mode attribute is a decl attribute, not a
1876/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1877/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001878static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001879 // This attribute isn't documented, but glibc uses it. It changes
1880 // the width of an int or unsigned int to the specified size.
1881
1882 // Check that there aren't any arguments
1883 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001884 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001885 return;
1886 }
1887
1888 IdentifierInfo *Name = Attr.getParameterName();
1889 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001890 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001891 return;
1892 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00001893
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001894 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001895
1896 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001897 if (Str.startswith("__") && Str.endswith("__"))
1898 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001899
1900 unsigned DestWidth = 0;
1901 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001902 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00001903 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001904 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001905 switch (Str[0]) {
1906 case 'Q': DestWidth = 8; break;
1907 case 'H': DestWidth = 16; break;
1908 case 'S': DestWidth = 32; break;
1909 case 'D': DestWidth = 64; break;
1910 case 'X': DestWidth = 96; break;
1911 case 'T': DestWidth = 128; break;
1912 }
1913 if (Str[1] == 'F') {
1914 IntegerMode = false;
1915 } else if (Str[1] == 'C') {
1916 IntegerMode = false;
1917 ComplexMode = true;
1918 } else if (Str[1] != 'I') {
1919 DestWidth = 0;
1920 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001921 break;
1922 case 4:
1923 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1924 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00001925 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001926 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00001927 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001928 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001929 break;
1930 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00001931 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001932 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001933 break;
1934 }
1935
1936 QualType OldTy;
1937 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1938 OldTy = TD->getUnderlyingType();
1939 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1940 OldTy = VD->getType();
1941 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001942 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1943 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001944 return;
1945 }
Eli Friedman73397492009-03-03 06:41:03 +00001946
John McCall183700f2009-09-21 23:43:11 +00001947 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00001948 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1949 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001950 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00001951 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1952 } else if (ComplexMode) {
1953 if (!OldTy->isComplexType())
1954 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1955 } else {
1956 if (!OldTy->isFloatingType())
1957 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1958 }
1959
Mike Stump390b4cc2009-05-16 07:39:55 +00001960 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1961 // and friends, at least with glibc.
1962 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1963 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001964 // FIXME: Make sure floating-point mappings are accurate
1965 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001966 QualType NewTy;
1967 switch (DestWidth) {
1968 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001969 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001970 return;
1971 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001972 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001973 return;
1974 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001975 if (!IntegerMode) {
1976 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1977 return;
1978 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001979 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001980 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001981 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001982 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001983 break;
1984 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001985 if (!IntegerMode) {
1986 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1987 return;
1988 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001989 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001990 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001991 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001992 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001993 break;
1994 case 32:
1995 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001996 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001997 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001998 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001999 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002000 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002001 break;
2002 case 64:
2003 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002004 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002005 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002006 if (S.Context.Target.getLongWidth() == 64)
2007 NewTy = S.Context.LongTy;
2008 else
2009 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002010 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002011 if (S.Context.Target.getLongWidth() == 64)
2012 NewTy = S.Context.UnsignedLongTy;
2013 else
2014 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002015 break;
Eli Friedman73397492009-03-03 06:41:03 +00002016 case 96:
2017 NewTy = S.Context.LongDoubleTy;
2018 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002019 case 128:
2020 if (!IntegerMode) {
2021 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2022 return;
2023 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002024 if (OldTy->isSignedIntegerType())
2025 NewTy = S.Context.Int128Ty;
2026 else
2027 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002028 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002029 }
2030
Eli Friedman73397492009-03-03 06:41:03 +00002031 if (ComplexMode) {
2032 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002033 }
2034
2035 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002036 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2037 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002038 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002039 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002040 cast<ValueDecl>(D)->setType(NewTy);
2041}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002042
Mike Stump1feade82009-08-26 22:31:08 +00002043static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002044 // check the attribute arguments.
2045 if (Attr.getNumArgs() > 0) {
2046 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2047 return;
2048 }
Anders Carlssone896d982009-02-13 08:11:52 +00002049
Anders Carlsson5bab7882009-02-19 19:16:48 +00002050 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002051 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002052 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00002053 return;
2054 }
Mike Stumpbf916502009-07-24 19:02:52 +00002055
Sean Huntcf807c42010-08-18 23:23:40 +00002056 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002057}
2058
Mike Stump1feade82009-08-26 22:31:08 +00002059static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002060 // check the attribute arguments.
2061 if (Attr.getNumArgs() != 0) {
2062 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2063 return;
2064 }
Mike Stumpbf916502009-07-24 19:02:52 +00002065
Chris Lattnerc5197432009-04-14 17:02:11 +00002066 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002067 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002068 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002069 return;
2070 }
Mike Stumpbf916502009-07-24 19:02:52 +00002071
Sean Huntcf807c42010-08-18 23:23:40 +00002072 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002073}
2074
Chris Lattner7255a2d2010-06-22 00:03:40 +00002075static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2076 Sema &S) {
2077 // check the attribute arguments.
2078 if (Attr.getNumArgs() != 0) {
2079 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2080 return;
2081 }
2082
2083 if (!isa<FunctionDecl>(d)) {
2084 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2085 << Attr.getName() << 0 /*function*/;
2086 return;
2087 }
2088
Eric Christopherf48f3672010-12-01 22:13:54 +00002089 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2090 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002091}
2092
Peter Collingbourneced76712010-12-01 03:15:31 +00002093static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2094 if (S.LangOpts.CUDA) {
2095 // check the attribute arguments.
2096 if (Attr.getNumArgs() != 0) {
2097 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2098 return;
2099 }
2100
2101 if (!isa<VarDecl>(d)) {
2102 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2103 << Attr.getName() << 12 /*variable*/;
2104 return;
2105 }
2106
2107 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2108 } else {
2109 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2110 }
2111}
2112
2113static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2114 if (S.LangOpts.CUDA) {
2115 // check the attribute arguments.
2116 if (Attr.getNumArgs() != 0) {
2117 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2118 return;
2119 }
2120
2121 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2122 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2123 << Attr.getName() << 2 /*variable and function*/;
2124 return;
2125 }
2126
2127 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2128 } else {
2129 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2130 }
2131}
2132
2133static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2134 if (S.LangOpts.CUDA) {
2135 // check the attribute arguments.
2136 if (Attr.getNumArgs() != 0) {
2137 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2138 return;
2139 }
2140
2141 if (!isa<FunctionDecl>(d)) {
2142 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2143 << Attr.getName() << 0 /*function*/;
2144 return;
2145 }
2146
2147 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2148 } else {
2149 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2150 }
2151}
2152
2153static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2154 if (S.LangOpts.CUDA) {
2155 // check the attribute arguments.
2156 if (Attr.getNumArgs() != 0) {
2157 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2158 return;
2159 }
2160
2161 if (!isa<FunctionDecl>(d)) {
2162 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2163 << Attr.getName() << 0 /*function*/;
2164 return;
2165 }
2166
2167 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2168 } else {
2169 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2170 }
2171}
2172
2173static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2174 if (S.LangOpts.CUDA) {
2175 // check the attribute arguments.
2176 if (Attr.getNumArgs() != 0) {
2177 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2178 return;
2179 }
2180
2181 if (!isa<VarDecl>(d)) {
2182 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2183 << Attr.getName() << 12 /*variable*/;
2184 return;
2185 }
2186
2187 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2188 } else {
2189 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2190 }
2191}
2192
Chris Lattnercf2a7212009-04-20 19:12:28 +00002193static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002194 // check the attribute arguments.
2195 if (Attr.getNumArgs() != 0) {
2196 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2197 return;
2198 }
Mike Stumpbf916502009-07-24 19:02:52 +00002199
Chris Lattnerc5197432009-04-14 17:02:11 +00002200 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2201 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002202 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002203 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00002204 return;
2205 }
Mike Stumpbf916502009-07-24 19:02:52 +00002206
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002207 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002208 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002209 return;
2210 }
Mike Stumpbf916502009-07-24 19:02:52 +00002211
Sean Huntcf807c42010-08-18 23:23:40 +00002212 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002213}
2214
Abramo Bagnarae215f722010-04-30 13:10:51 +00002215static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2216 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
2217 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2218 assert(Attr.isInvalid() == false);
2219
2220 switch (Attr.getKind()) {
2221 case AttributeList::AT_fastcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002222 d->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002223 return;
2224 case AttributeList::AT_stdcall:
Sean Huntcf807c42010-08-18 23:23:40 +00002225 d->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002226 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002227 case AttributeList::AT_thiscall:
Sean Huntcf807c42010-08-18 23:23:40 +00002228 d->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002229 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002230 case AttributeList::AT_cdecl:
Sean Huntcf807c42010-08-18 23:23:40 +00002231 d->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002232 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002233 case AttributeList::AT_pascal:
2234 d->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
2235 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002236 default:
2237 llvm_unreachable("unexpected attribute kind");
2238 return;
2239 }
2240}
2241
Fariborz Jahanianee760332009-03-27 18:38:55 +00002242static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2243 // check the attribute arguments.
2244 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002246 return;
2247 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002248
Fariborz Jahanianee760332009-03-27 18:38:55 +00002249 if (!isFunctionOrMethod(d)) {
2250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002251 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002252 return;
2253 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002254
Peter Collingbourne7a730022010-11-23 20:45:58 +00002255 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002256 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002257 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
2258 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002259 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2260 << "regparm" << NumParamsExpr->getSourceRange();
2261 return;
2262 }
2263
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002264 if (S.Context.Target.getRegParmMax() == 0) {
2265 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002266 << NumParamsExpr->getSourceRange();
2267 return;
2268 }
2269
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00002270 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00002271 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2272 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002273 return;
2274 }
2275
Sean Huntcf807c42010-08-18 23:23:40 +00002276 d->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context,
2277 NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00002278}
2279
Sean Huntbbd37c62009-11-21 08:43:09 +00002280static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2281 // check the attribute arguments.
2282 if (Attr.getNumArgs() != 0) {
2283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2284 return;
2285 }
2286
2287 if (!isa<CXXRecordDecl>(d)
2288 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
2289 S.Diag(Attr.getLoc(),
2290 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2291 : diag::warn_attribute_wrong_decl_type)
2292 << Attr.getName() << 7 /*virtual method or class*/;
2293 return;
2294 }
Sean Hunt7725e672009-11-25 04:20:27 +00002295
2296 // FIXME: Conform to C++0x redeclaration rules.
2297
2298 if (d->getAttr<FinalAttr>()) {
2299 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
2300 return;
2301 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002302
Sean Huntcf807c42010-08-18 23:23:40 +00002303 d->addAttr(::new (S.Context) FinalAttr(Attr.getLoc(), S.Context));
Sean Huntbbd37c62009-11-21 08:43:09 +00002304}
2305
Chris Lattner0744e5f2008-06-29 00:23:49 +00002306//===----------------------------------------------------------------------===//
Sean Hunt7725e672009-11-25 04:20:27 +00002307// C++0x member checking attributes
2308//===----------------------------------------------------------------------===//
2309
2310static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2311 if (Attr.getNumArgs() != 0) {
2312 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2313 return;
2314 }
2315
2316 if (!isa<CXXRecordDecl>(d)) {
2317 S.Diag(Attr.getLoc(),
2318 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2319 : diag::warn_attribute_wrong_decl_type)
2320 << Attr.getName() << 9 /*class*/;
2321 return;
2322 }
2323
2324 if (d->getAttr<BaseCheckAttr>()) {
2325 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
2326 return;
2327 }
2328
Sean Huntcf807c42010-08-18 23:23:40 +00002329 d->addAttr(::new (S.Context) BaseCheckAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002330}
2331
2332static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2333 if (Attr.getNumArgs() != 0) {
2334 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2335 return;
2336 }
2337
2338 if (!isa<RecordDecl>(d->getDeclContext())) {
2339 // FIXME: It's not the type that's the problem
2340 S.Diag(Attr.getLoc(),
2341 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2342 : diag::warn_attribute_wrong_decl_type)
2343 << Attr.getName() << 11 /*member*/;
2344 return;
2345 }
2346
2347 // FIXME: Conform to C++0x redeclaration rules.
2348
2349 if (d->getAttr<HidingAttr>()) {
2350 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
2351 return;
2352 }
2353
Sean Huntcf807c42010-08-18 23:23:40 +00002354 d->addAttr(::new (S.Context) HidingAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002355}
2356
2357static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2358 if (Attr.getNumArgs() != 0) {
2359 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2360 return;
2361 }
2362
2363 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
2364 // FIXME: It's not the type that's the problem
2365 S.Diag(Attr.getLoc(),
2366 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
2367 : diag::warn_attribute_wrong_decl_type)
2368 << Attr.getName() << 10 /*virtual method*/;
2369 return;
2370 }
2371
2372 // FIXME: Conform to C++0x redeclaration rules.
2373
2374 if (d->getAttr<OverrideAttr>()) {
2375 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
2376 return;
2377 }
2378
Sean Huntcf807c42010-08-18 23:23:40 +00002379 d->addAttr(::new (S.Context) OverrideAttr(Attr.getLoc(), S.Context));
Sean Hunt7725e672009-11-25 04:20:27 +00002380}
2381
2382//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002383// Checker-specific attribute handlers.
2384//===----------------------------------------------------------------------===//
2385
2386static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
2387 Sema &S) {
2388
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002389 QualType RetTy;
Mike Stumpbf916502009-07-24 19:02:52 +00002390
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002391 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
2392 RetTy = MD->getResultType();
2393 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
2394 RetTy = FD->getResultType();
2395 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002396 SourceLocation L = Attr.getLoc();
2397 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
2398 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002399 return;
2400 }
Mike Stumpbf916502009-07-24 19:02:52 +00002401
Ted Kremenek6217b802009-07-29 21:53:49 +00002402 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall183700f2009-09-21 23:43:11 +00002403 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002404 SourceLocation L = Attr.getLoc();
2405 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2406 << SourceRange(L, L) << Attr.getName();
Mike Stumpbf916502009-07-24 19:02:52 +00002407 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002408 }
Mike Stumpbf916502009-07-24 19:02:52 +00002409
Ted Kremenekb71368d2009-05-09 02:44:38 +00002410 switch (Attr.getKind()) {
2411 default:
2412 assert(0 && "invalid ownership attribute");
2413 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002414 case AttributeList::AT_cf_returns_not_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002415 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
2416 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002417 return;
2418 case AttributeList::AT_ns_returns_not_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002419 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
2420 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002421 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002422 case AttributeList::AT_cf_returns_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002423 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
2424 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002425 return;
2426 case AttributeList::AT_ns_returns_retained:
Eric Christopherf48f3672010-12-01 22:13:54 +00002427 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
2428 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002429 return;
2430 };
2431}
2432
Charles Davisf0122fe2010-02-16 18:27:26 +00002433static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2434 return Attr.getKind() == AttributeList::AT_dllimport ||
2435 Attr.getKind() == AttributeList::AT_dllexport;
2436}
2437
Ted Kremenekb71368d2009-05-09 02:44:38 +00002438//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002439// Top Level Sema Entry Points
2440//===----------------------------------------------------------------------===//
2441
Sebastian Redla89d82c2008-12-21 19:24:58 +00002442/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00002443/// the attribute applies to decls. If the attribute is a type attribute, just
Sean Huntbbd37c62009-11-21 08:43:09 +00002444/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2445/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpbf916502009-07-24 19:02:52 +00002446static void ProcessDeclAttribute(Scope *scope, Decl *D,
2447 const AttributeList &Attr, Sema &S) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002448 if (Attr.isInvalid())
2449 return;
2450
Charles Davisf0122fe2010-02-16 18:27:26 +00002451 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2452 // FIXME: Try to deal with other __declspec attributes!
Eli Friedman290eeb02009-06-08 23:27:34 +00002453 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002454 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002455 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002456 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2457 case AttributeList::AT_IBOutletCollection:
2458 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002459 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002460 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002461 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002462 case AttributeList::AT_neon_vector_type:
2463 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002464 // Ignore these, these are type attributes, handled by
2465 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002466 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002467 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2468 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002469 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002470 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002471 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002472 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002473 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
2474 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002475 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002476 HandleDependencyAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002477 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002478 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2479 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2480 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002481 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002482 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002483 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002484 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002485 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
2486 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2487 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002488 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002489 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
2490 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002491 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002492 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2493 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002494 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002495 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002496 case AttributeList::AT_ownership_returns:
2497 case AttributeList::AT_ownership_takes:
2498 case AttributeList::AT_ownership_holds:
2499 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002500 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002501 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2502 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
2503 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002504 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002505 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002506
2507 // Checker-specific.
Ted Kremenek31c780d2010-02-18 00:05:45 +00002508 case AttributeList::AT_ns_returns_not_retained:
2509 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002510 case AttributeList::AT_ns_returns_retained:
2511 case AttributeList::AT_cf_returns_retained:
2512 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2513
Nate Begeman6f3d8382009-06-26 06:32:41 +00002514 case AttributeList::AT_reqd_wg_size:
2515 HandleReqdWorkGroupSize(D, Attr, S); break;
2516
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002517 case AttributeList::AT_init_priority:
2518 HandleInitPriorityAttr(D, Attr, S); break;
2519
Sean Hunt7725e672009-11-25 04:20:27 +00002520 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2521 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002522 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2523 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2524 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002525 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002526 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2527 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002528 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002529 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002530 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002531 case AttributeList::AT_transparent_union:
2532 HandleTransparentUnionAttr(D, Attr, S);
2533 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002534 case AttributeList::AT_objc_exception:
2535 HandleObjCExceptionAttr(D, Attr, S);
2536 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00002537 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002538 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2539 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2540 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2541 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2542 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2543 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2544 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2545 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2546 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002547 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002548 // Just ignore
2549 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002550 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2551 HandleNoInstrumentFunctionAttr(D, Attr, S);
2552 break;
John McCall04a67a62010-02-05 21:31:56 +00002553 case AttributeList::AT_stdcall:
2554 case AttributeList::AT_cdecl:
2555 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002556 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002557 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002558 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002559 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002560 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002561 // Ask target about the attribute.
2562 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2563 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002564 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2565 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002566 break;
2567 }
2568}
2569
2570/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2571/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00002572void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
2573 const AttributeList *AttrList) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002574 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
2575 ProcessDeclAttribute(S, D, *l, *this);
2576 }
2577
2578 // GCC accepts
2579 // static int a9 __attribute__((weakref));
2580 // but that looks really pointless. We reject it.
2581 if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
2582 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002583 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002584 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002585 }
2586}
2587
Ryan Flynne25ff832009-07-30 03:15:39 +00002588/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2589/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002590NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002591 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002592 NamedDecl *NewD = 0;
2593 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2594 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
2595 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002596 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002597 if (FD->getQualifier()) {
2598 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
2599 NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange());
2600 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002601 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2602 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
2603 VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002604 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002605 VD->getStorageClass(),
2606 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002607 if (VD->getQualifier()) {
2608 VarDecl *NewVD = cast<VarDecl>(NewD);
2609 NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange());
2610 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002611 }
2612 return NewD;
2613}
2614
2615/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2616/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002617void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002618 if (W.getUsed()) return; // only do this once
2619 W.setUsed(true);
2620 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2621 IdentifierInfo *NDId = ND->getIdentifier();
2622 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002623 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2624 NDId->getName()));
2625 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002626 WeakTopLevelDecl.push_back(NewD);
2627 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2628 // to insert Decl at TU scope, sorry.
2629 DeclContext *SavedContext = CurContext;
2630 CurContext = Context.getTranslationUnitDecl();
2631 PushOnScopeChains(NewD, S);
2632 CurContext = SavedContext;
2633 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002634 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002635 }
2636}
2637
Chris Lattner0744e5f2008-06-29 00:23:49 +00002638/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2639/// it, apply them to D. This is a bit tricky because PD can have attributes
2640/// specified in many different places, and we need to find and apply them all.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002641void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
John McCalld4aff0e2010-10-27 00:59:00 +00002642 // It's valid to "forward-declare" #pragma weak, in which case we
2643 // have to do this.
2644 if (!WeakUndeclaredIdentifiers.empty()) {
2645 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2646 if (IdentifierInfo *Id = ND->getIdentifier()) {
2647 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2648 = WeakUndeclaredIdentifiers.find(Id);
2649 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2650 WeakInfo W = I->second;
2651 DeclApplyPragmaWeak(S, ND, W);
2652 WeakUndeclaredIdentifiers[Id] = W;
2653 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002654 }
2655 }
2656 }
2657
Chris Lattner0744e5f2008-06-29 00:23:49 +00002658 // Apply decl attributes from the DeclSpec if present.
2659 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002660 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002661
Chris Lattner0744e5f2008-06-29 00:23:49 +00002662 // Walk the declarator structure, applying decl attributes that were in a type
2663 // position to the decl itself. This handles cases like:
2664 // int *__attr__(x)** D;
2665 // when X is a decl attribute.
2666 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2667 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002668 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpbf916502009-07-24 19:02:52 +00002669
Chris Lattner0744e5f2008-06-29 00:23:49 +00002670 // Finally, apply any attributes on the decl itself.
2671 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002672 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner0744e5f2008-06-29 00:23:49 +00002673}
John McCall54abf7d2009-11-04 02:18:39 +00002674
2675/// PushParsingDeclaration - Enter a new "scope" of deprecation
2676/// warnings.
2677///
2678/// The state token we use is the start index of this scope
2679/// on the warning stack.
John McCallf312b1e2010-08-26 23:41:50 +00002680Sema::ParsingDeclStackState Sema::PushParsingDeclaration() {
John McCall54abf7d2009-11-04 02:18:39 +00002681 ParsingDeclDepth++;
John McCall2f514482010-01-27 03:50:35 +00002682 return (ParsingDeclStackState) DelayedDiagnostics.size();
2683}
2684
John McCalld226f652010-08-21 09:40:31 +00002685void Sema::PopParsingDeclaration(ParsingDeclStackState S, Decl *D) {
John McCall2f514482010-01-27 03:50:35 +00002686 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2687 ParsingDeclDepth--;
2688
2689 if (DelayedDiagnostics.empty())
2690 return;
2691
2692 unsigned SavedIndex = (unsigned) S;
2693 assert(SavedIndex <= DelayedDiagnostics.size() &&
2694 "saved index is out of bounds");
2695
John McCall58e6f342010-03-16 05:22:47 +00002696 unsigned E = DelayedDiagnostics.size();
2697
John McCall2f514482010-01-27 03:50:35 +00002698 // We only want to actually emit delayed diagnostics when we
2699 // successfully parsed a decl.
John McCall2f514482010-01-27 03:50:35 +00002700 if (D) {
2701 // We really do want to start with 0 here. We get one push for a
2702 // decl spec and another for each declarator; in a decl group like:
2703 // deprecated_typedef foo, *bar, baz();
2704 // only the declarator pops will be passed decls. This is correct;
2705 // we really do need to consider delayed diagnostics from the decl spec
2706 // for each of the different declarations.
John McCall58e6f342010-03-16 05:22:47 +00002707 for (unsigned I = 0; I != E; ++I) {
John McCall2f514482010-01-27 03:50:35 +00002708 if (DelayedDiagnostics[I].Triggered)
2709 continue;
2710
2711 switch (DelayedDiagnostics[I].Kind) {
2712 case DelayedDiagnostic::Deprecation:
2713 HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D);
2714 break;
2715
2716 case DelayedDiagnostic::Access:
2717 HandleDelayedAccessCheck(DelayedDiagnostics[I], D);
2718 break;
2719 }
2720 }
2721 }
2722
John McCall58e6f342010-03-16 05:22:47 +00002723 // Destroy all the delayed diagnostics we're about to pop off.
2724 for (unsigned I = SavedIndex; I != E; ++I)
2725 DelayedDiagnostics[I].destroy();
2726
John McCall2f514482010-01-27 03:50:35 +00002727 DelayedDiagnostics.set_size(SavedIndex);
John McCall54abf7d2009-11-04 02:18:39 +00002728}
2729
2730static bool isDeclDeprecated(Decl *D) {
2731 do {
2732 if (D->hasAttr<DeprecatedAttr>())
2733 return true;
2734 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2735 return false;
2736}
2737
John McCall9c3087b2010-08-26 02:13:20 +00002738void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00002739 Decl *Ctx) {
2740 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00002741 return;
2742
John McCall2f514482010-01-27 03:50:35 +00002743 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002744 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002745 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002746 << DD.getDeprecationDecl()->getDeclName()
2747 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002748 else
2749 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002750 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002751}
2752
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002753void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002754 SourceLocation Loc) {
John McCall54abf7d2009-11-04 02:18:39 +00002755 // Delay if we're currently parsing a declaration.
2756 if (ParsingDeclDepth) {
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002757 DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D,
2758 Message));
John McCall54abf7d2009-11-04 02:18:39 +00002759 return;
2760 }
2761
2762 // Otherwise, don't warn if our current context is deprecated.
2763 if (isDeclDeprecated(cast<Decl>(CurContext)))
2764 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00002765 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00002766 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
2767 << Message;
2768 else
2769 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00002770}